From 5fa57b98cff1ebbf6a884aa0052e961cf053a769 Mon Sep 17 00:00:00 2001 From: gengjiawen Date: Thu, 13 Oct 2022 12:37:53 +0800 Subject: [PATCH] feat: add auto run v8 ci --- .npmrc | 1 + lib/ci/run_ci.js | 40 ++++++++++++++++++++++++++++++++++++++ test/unit/ci_start.test.js | 9 +++++++++ 3 files changed, 50 insertions(+) diff --git a/.npmrc b/.npmrc index 43c97e719..62a81f069 100644 --- a/.npmrc +++ b/.npmrc @@ -1 +1,2 @@ package-lock=false +auto-install-peers=true diff --git a/lib/ci/run_ci.js b/lib/ci/run_ci.js index beead3dcc..743179087 100644 --- a/lib/ci/run_ci.js +++ b/lib/ci/run_ci.js @@ -5,11 +5,16 @@ import { CI_TYPES, CI_TYPES_KEYS } from './ci_type_parser.js'; +import PRData from '../pr_data.js'; +import { debuglog } from '../verbosity.js'; export const CI_CRUMB_URL = `https://${CI_DOMAIN}/crumbIssuer/api/json`; const CI_PR_NAME = CI_TYPES.get(CI_TYPES_KEYS.PR).jobName; export const CI_PR_URL = `https://${CI_DOMAIN}/job/${CI_PR_NAME}/build`; +const CI_V8_NAME = CI_TYPES.get(CI_TYPES_KEYS.V8).jobName; +export const CI_V8_URL = `https://${CI_DOMAIN}/job/${CI_V8_NAME}/build`; + export class RunPRJob { constructor(cli, request, owner, repo, prid) { this.cli = cli; @@ -17,6 +22,7 @@ export class RunPRJob { this.owner = owner; this.repo = repo; this.prid = prid; + this.prData = new PRData({ prid, owner, repo }, cli, request); } async getCrumb() { @@ -43,6 +49,18 @@ export class RunPRJob { return payload; } + get v8Payload() { + const payload = new FormData(); + payload.append('json', JSON.stringify({ + parameter: [ + { name: 'GITHUB_ORG', value: this.owner }, + { name: 'REPO_NAME', value: this.repo }, + { name: 'GIT_REMOTE_REF', value: `refs/pull/${this.prid}/head` } + ] + })); + return payload; + } + async start() { const { cli } = this; cli.startSpinner('Validating Jenkins credentials'); @@ -71,7 +89,29 @@ export class RunPRJob { return false; } cli.stopSpinner('PR CI job successfully started'); + + // check if the job need a v8 build and trigger it + await this.prData.getPR(); + const labels = this.prData.pr.labels; + if (labels.nodes.map(i => i.name).includes('v8 engine')) { + cli.startSpinner('Starting V8 CI job'); + const response = await this.request.fetch(CI_V8_URL, { + method: 'POST', + headers: { + 'Jenkins-Crumb': crumb + }, + body: this.v8Payload + }); + if (response.status !== 201) { + cli.stopSpinner( + `Failed to start V8 CI: ${response.status} ${response.statusText}`, + this.cli.SPINNER_STATUS.FAILED); + return false; + } + cli.stopSpinner('V8 CI job successfully started'); + } } catch (err) { + debuglog(err); cli.stopSpinner('Failed to start CI', this.cli.SPINNER_STATUS.FAILED); return false; } diff --git a/test/unit/ci_start.test.js b/test/unit/ci_start.test.js index cac43f629..deb9d46cc 100644 --- a/test/unit/ci_start.test.js +++ b/test/unit/ci_start.test.js @@ -67,6 +67,15 @@ describe('Jenkins', () => { const cli = new TestCLI(); const request = { + gql: sinon.stub().returns({ + repository: { + pullRequest: { + labels: { + nodes: [] + } + } + } + }), fetch: sinon.stub() .callsFake((url, { method, headers, body }) => { assert.strictEqual(url, CI_PR_URL);