Skip to content

Commit

Permalink
feat: add auto run v8 ci
Browse files Browse the repository at this point in the history
  • Loading branch information
gengjiawen authored and Trott committed Oct 22, 2022
1 parent 4e59a64 commit 046bc0d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock=false
auto-install-peers=true
40 changes: 40 additions & 0 deletions lib/ci/run_ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ 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;
this.request = request;
this.owner = owner;
this.repo = repo;
this.prid = prid;
this.prData = new PRData({ prid, owner, repo }, cli, request);
}

async getCrumb() {
Expand All @@ -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');
Expand Down Expand Up @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions test/unit/ci_start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 046bc0d

Please sign in to comment.