Skip to content

Commit acfb36b

Browse files
committed
fixes
1 parent 547c5a8 commit acfb36b

File tree

3 files changed

+143
-3
lines changed

3 files changed

+143
-3
lines changed

lib/ci/run_ci.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from './ci_type_parser.js';
1010

1111
export const CI_CRUMB_URL = `https://${CI_DOMAIN}/crumbIssuer/api/json`;
12-
const CI_PR_NAME = CI_TYPES.get(CI_TYPES_KEYS.PR).jobName;
12+
export const CI_PR_NAME = CI_TYPES.get(CI_TYPES_KEYS.PR).jobName;
1313
export const CI_PR_URL = `https://${CI_DOMAIN}/job/${CI_PR_NAME}/build`;
1414
export const CI_PR_RESUME_URL = `https://${CI_DOMAIN}/job/${CI_PR_NAME}/`;
1515

@@ -78,7 +78,7 @@ export class RunPRJob {
7878
},
7979
body: this.payload
8080
});
81-
if (response.status !== 200) {
81+
if (response.status !== 201) {
8282
cli.stopSpinner(
8383
`Failed to start PR CI: ${response.status} ${response.statusText}`,
8484
this.cli.SPINNER_STATUS.FAILED);
@@ -119,7 +119,7 @@ export class RunPRJob {
119119
'Jenkins-Crumb': crumb
120120
}
121121
});
122-
if (response.status !== 201) {
122+
if (response.status !== 200) {
123123
cli.stopSpinner(
124124
`Failed to resume PR CI: ${response.status} ${response.statusText}`,
125125
this.cli.SPINNER_STATUS.FAILED);

test/unit/ci_resume.test.js

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import assert from 'assert';
2+
3+
import sinon from 'sinon';
4+
import FormData from 'form-data';
5+
6+
import {
7+
RunPRJob,
8+
CI_CRUMB_URL,
9+
CI_PR_NAME,
10+
CI_PR_RESUME_URL
11+
} from '../../lib/ci/run_ci.js';
12+
13+
import { CI_DOMAIN } from '../../lib/ci/ci_type_parser.js';
14+
import TestCLI from '../fixtures/test_cli.js';
15+
import { jobCache } from '../../lib/ci/build-types/job.js';
16+
17+
describe('Jenkins resume', () => {
18+
const owner = 'nodejs';
19+
const repo = 'node-auto-test';
20+
const prid = 123456;
21+
const jobid = 654321;
22+
const crumb = 'asdf1234';
23+
24+
before(() => {
25+
jobCache.disable();
26+
sinon.stub(FormData.prototype, 'append').callsFake(function(key, value) {
27+
assert.strictEqual(key, 'json');
28+
const { parameter } = JSON.parse(value);
29+
const expectedParameters = {
30+
CERTIFY_SAFE: 'on',
31+
TARGET_GITHUB_ORG: owner,
32+
TARGET_REPO_NAME: repo,
33+
PR_ID: prid,
34+
REBASE_ONTO: '<pr base branch>',
35+
DESCRIPTION_SETTER_DESCRIPTION: ''
36+
};
37+
for (const { name, value } of parameter) {
38+
assert.strictEqual(value, expectedParameters[name]);
39+
delete expectedParameters[name];
40+
}
41+
assert.strictEqual(Object.keys(expectedParameters).length, 0);
42+
43+
this._validated = true;
44+
45+
return FormData.prototype.append.wrappedMethod.bind(this)(key, value);
46+
});
47+
});
48+
49+
after(() => {
50+
sinon.restore();
51+
});
52+
53+
it('should return false if crumb fails', async() => {
54+
const cli = new TestCLI();
55+
const request = {
56+
json: sinon.stub().throws()
57+
};
58+
59+
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, jobid);
60+
assert.strictEqual(await jobRunner.resume(), false);
61+
});
62+
63+
it('should return false if run status not FAILURE', async() => {
64+
const cli = new TestCLI();
65+
66+
const request = {
67+
json: sinon.stub()
68+
};
69+
70+
request.json.withArgs(CI_CRUMB_URL)
71+
.returns(Promise.resolve({ crumb }));
72+
request.json.withArgs(`https://${CI_DOMAIN}/job/${CI_PR_NAME}/${jobid}/api/json?tree=result%2Curl%2Cnumber`)
73+
.returns(Promise.resolve({ result: null }));
74+
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, jobid);
75+
assert.strictEqual(await jobRunner.resume(), false);
76+
});
77+
78+
it('should resume node-pull-request job', async() => {
79+
const cli = new TestCLI();
80+
81+
const request = {
82+
fetch: sinon.stub()
83+
.callsFake((url, { method, headers }) => {
84+
assert.strictEqual(url, `${CI_PR_RESUME_URL}${jobid}/resume`);
85+
assert.strictEqual(method, 'POST');
86+
assert.deepStrictEqual(headers, { 'Jenkins-Crumb': crumb });
87+
return Promise.resolve({ status: 200 });
88+
}),
89+
json: sinon.stub()
90+
};
91+
92+
request.json.withArgs(CI_CRUMB_URL)
93+
.returns(Promise.resolve({ crumb }));
94+
request.json.withArgs(`https://${CI_DOMAIN}/job/${CI_PR_NAME}/${jobid}/api/json?tree=result%2Curl%2Cnumber`)
95+
.returns(Promise.resolve({ result: 'FAILURE' }));
96+
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, jobid);
97+
assert.ok(await jobRunner.resume());
98+
});
99+
100+
it('should fail if resuming node-pull-request throws', async() => {
101+
const cli = new TestCLI();
102+
const request = {
103+
fetch: sinon.stub().throws(),
104+
json: sinon.stub()
105+
};
106+
107+
request.json.withArgs(CI_CRUMB_URL)
108+
.returns(Promise.resolve({ crumb }));
109+
request.json.withArgs(`https://${CI_DOMAIN}/job/${CI_PR_NAME}/${jobid}/api/json?tree=result%2Curl%2Cnumber`)
110+
.returns(Promise.resolve({ result: 'FAILURE' }));
111+
112+
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, jobid);
113+
assert.strictEqual(await jobRunner.resume(), false);
114+
});
115+
116+
it('should return false if node-pull-request not resumed', async() => {
117+
const cli = new TestCLI();
118+
119+
const request = {
120+
fetch: sinon.stub()
121+
.callsFake((url, { method, headers }) => {
122+
assert.strictEqual(url, `${CI_PR_RESUME_URL}${jobid}/resume`);
123+
assert.strictEqual(method, 'POST');
124+
assert.deepStrictEqual(headers, { 'Jenkins-Crumb': crumb });
125+
return Promise.resolve({ status: 401 });
126+
}),
127+
json: sinon.stub()
128+
};
129+
130+
request.json.withArgs(CI_CRUMB_URL)
131+
.returns(Promise.resolve({ crumb }));
132+
request.json.withArgs(`https://${CI_DOMAIN}/job/${CI_PR_NAME}/${jobid}/api/json?tree=result%2Curl%2Cnumber`)
133+
.returns(Promise.resolve({ result: 'FAILURE' }));
134+
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, jobid);
135+
assert.strictEqual(await jobRunner.resume(), false);
136+
});
137+
});

test/unit/ci_start.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ describe('Jenkins', () => {
4040
return FormData.prototype.append.wrappedMethod.bind(this)(key, value);
4141
});
4242
});
43+
after(() => {
44+
sinon.restore();
45+
});
4346

4447
it('should fail if starting node-pull-request throws', async() => {
4548
const cli = new TestCLI();

0 commit comments

Comments
 (0)