Skip to content

Commit 48c306b

Browse files
committed
fix: properly handle failure to start CI
1 parent 332576a commit 48c306b

File tree

3 files changed

+64
-43
lines changed

3 files changed

+64
-43
lines changed

lib/ci/run_ci.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,19 @@ class RunPRJob {
5959

6060
try {
6161
cli.startSpinner('Starting PR CI job');
62-
await this.request.text(CI_PR_URL, {
62+
const response = await this.request.fetch(CI_PR_URL, {
6363
method: 'POST',
6464
headers: {
6565
'Jenkins-Crumb': crumb
6666
},
6767
body: this.payload
6868
});
69+
if (response.status !== 201) {
70+
cli.stopSpinner(
71+
`Failed to start PR CI: ${response.status} ${response.statusText}`,
72+
this.cli.SPINNER_STATUS.FAILED);
73+
return false;
74+
}
6975
cli.stopSpinner('PR CI job successfully started');
7076
} catch (err) {
7177
cli.stopSpinner('Failed to start CI', this.cli.SPINNER_STATUS.FAILED);

lib/request.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,22 @@ class Request {
1717
return fs.readFileSync(filePath, 'utf8');
1818
}
1919

20-
async text(url, options = {}) {
20+
async fetch(url, options) {
2121
options.agent = this.proxyAgent;
2222
if (url.startsWith(`https://${CI_DOMAIN}`)) {
2323
options.headers = options.headers || {};
2424
Object.assign(options.headers, this.getJenkinsHeaders());
2525
}
26-
return fetch(url, options).then(res => res.text());
26+
return fetch(url, options);
27+
}
28+
29+
async text(url, options = {}) {
30+
return this.fetch(url, options).then(res => res.text());
2731
}
2832

2933
async json(url, options = {}) {
30-
options.agent = this.proxyAgent;
3134
options.headers = options.headers || {};
32-
options.headers.Accept = 'application/json';
33-
if (url.startsWith(`https://${CI_DOMAIN}`)) {
34-
Object.assign(options.headers, this.getJenkinsHeaders());
35-
}
36-
return fetch(url, options).then(res => res.json());
35+
return this.fetch(url, options).then(res => res.json());
3736
}
3837

3938
async gql(name, variables, path) {

test/unit/ci_start.js renamed to test/unit/ci_start.test.js

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,42 @@ const {
1313
const TestCLI = require('../fixtures/test_cli');
1414

1515
describe('Jenkins', () => {
16-
it('should fail if starting node-pull-request fails', async() => {
16+
const owner = 'nodejs';
17+
const repo = 'node-auto-test';
18+
const prid = 123456;
19+
const crumb = 'asdf1234';
20+
21+
before(() => {
22+
sinon.stub(FormData.prototype, 'append').callsFake(function(key, value) {
23+
assert.strictEqual(key, 'json');
24+
const { parameter } = JSON.parse(value);
25+
const expectedParameters = {
26+
CERTIFY_SAFE: 'on',
27+
TARGET_GITHUB_ORG: owner,
28+
TARGET_REPO_NAME: repo,
29+
PR_ID: prid,
30+
REBASE_ONTO: '<pr base branch>',
31+
DESCRIPTION_SETTER_DESCRIPTION: ''
32+
};
33+
for (const { name, value } of parameter) {
34+
assert.strictEqual(value, expectedParameters[name]);
35+
delete expectedParameters[name];
36+
}
37+
assert.strictEqual(Object.keys(expectedParameters).length, 0);
38+
39+
this._validated = true;
40+
41+
return FormData.prototype.append.wrappedMethod.bind(this)(key, value);
42+
});
43+
});
44+
45+
it('should fail if starting node-pull-request throws', async() => {
1746
const cli = new TestCLI();
18-
const crumb = 'asdf1234';
1947
const request = {
2048
text: sinon.stub().throws(),
2149
json: sinon.stub().withArgs(CI_CRUMB_URL)
2250
.returns(Promise.resolve({ crumb }))
2351
};
24-
const owner = 'nodejs';
25-
const repo = 'node-auto-test';
26-
const prid = 123456;
2752

2853
const jobRunner = new RunPRJob(cli, request, owner, repo, prid);
2954
assert.strictEqual(await jobRunner.start(), false);
@@ -34,55 +59,46 @@ describe('Jenkins', () => {
3459
const request = {
3560
json: sinon.stub().throws()
3661
};
37-
const owner = 'nodejs';
38-
const repo = 'node-auto-test';
39-
const prid = 123456;
4062

4163
const jobRunner = new RunPRJob(cli, request, owner, repo, prid);
4264
assert.strictEqual(await jobRunner.start(), false);
4365
});
4466

4567
it('should start node-pull-request', async() => {
4668
const cli = new TestCLI();
47-
const crumb = 'asdf1234';
48-
const owner = 'nodejs';
49-
const repo = 'node-auto-test';
50-
const prid = 123456;
51-
52-
sinon.stub(FormData.prototype, 'append').callsFake(function(key, value) {
53-
assert.strictEqual(key, 'json');
54-
const { parameter } = JSON.parse(value);
55-
const expectedParameters = {
56-
CERTIFY_SAFE: 'on',
57-
TARGET_GITHUB_ORG: owner,
58-
TARGET_REPO_NAME: repo,
59-
PR_ID: prid,
60-
REBASE_ONTO: '<pr base branch>',
61-
DESCRIPTION_SETTER_DESCRIPTION: ''
62-
};
63-
for (const { name, value } of parameter) {
64-
assert.strictEqual(value, expectedParameters[name]);
65-
delete expectedParameters[name];
66-
}
67-
assert.strictEqual(Object.keys(expectedParameters).length, 0);
6869

69-
this._validated = true;
70+
const request = {
71+
fetch: sinon.stub()
72+
.callsFake((url, { method, headers, body }) => {
73+
assert.strictEqual(url, CI_PR_URL);
74+
assert.strictEqual(method, 'POST');
75+
assert.deepStrictEqual(headers, { 'Jenkins-Crumb': crumb });
76+
assert.ok(body._validated);
77+
return Promise.resolve({ status: 201 });
78+
}),
79+
json: sinon.stub().withArgs(CI_CRUMB_URL)
80+
.returns(Promise.resolve({ crumb }))
81+
};
82+
const jobRunner = new RunPRJob(cli, request, owner, repo, prid);
83+
assert.ok(await jobRunner.start());
84+
});
7085

71-
return FormData.prototype.append.wrappedMethod.bind(this)(key, value);
72-
});
86+
it('should return false if node-pull-request not started', async() => {
87+
const cli = new TestCLI();
7388

7489
const request = {
75-
text: sinon.stub()
90+
fetch: sinon.stub()
7691
.callsFake((url, { method, headers, body }) => {
7792
assert.strictEqual(url, CI_PR_URL);
7893
assert.strictEqual(method, 'POST');
7994
assert.deepStrictEqual(headers, { 'Jenkins-Crumb': crumb });
8095
assert.ok(body._validated);
96+
return Promise.resolve({ status: 401 });
8197
}),
8298
json: sinon.stub().withArgs(CI_CRUMB_URL)
8399
.returns(Promise.resolve({ crumb }))
84100
};
85101
const jobRunner = new RunPRJob(cli, request, owner, repo, prid);
86-
assert.ok(await jobRunner.start());
102+
assert.strictEqual(await jobRunner.start(), false);
87103
});
88104
});

0 commit comments

Comments
 (0)