-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathci_start.test.js
More file actions
201 lines (180 loc) · 6.58 KB
/
ci_start.test.js
File metadata and controls
201 lines (180 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* eslint-disable import/no-named-as-default-member */
import { describe, it, before, afterEach } from 'node:test';
import assert from 'assert';
import sinon from 'sinon';
import { FormData } from 'undici';
import {
RunPRJob,
CI_CRUMB_URL,
CI_PR_URL
} from '../../lib/ci/run_ci.js';
import PRChecker from '../../lib/pr_checker.js';
import PRData from '../../lib/pr_data.js';
import TestCLI from '../fixtures/test_cli.js';
describe('Jenkins', () => {
const owner = 'nodejs';
const repo = 'node-auto-test';
const prid = 123456;
const crumb = 'asdf1234';
const dummySHA = '51ce389dc1d539216d30bba0986a8c270801d65f';
before(() => {
const stubbed = sinon.stub(FormData.prototype, 'append').callsFake(function(key, value) {
assert.strictEqual(key, 'json');
const { parameter } = JSON.parse(value);
const expectedParameters = {
CERTIFY_SAFE: 'on',
COMMIT_SHA_CHECK: dummySHA,
TARGET_GITHUB_ORG: owner,
TARGET_REPO_NAME: repo,
PR_ID: prid,
REBASE_ONTO: '<pr base branch>',
DESCRIPTION_SETTER_DESCRIPTION: ''
};
for (const { name, value } of parameter) {
assert.strictEqual(value, expectedParameters[name]);
delete expectedParameters[name];
}
assert.strictEqual(Object.keys(expectedParameters).length, 0);
this._validated = true;
return Reflect.apply(FormData.prototype.append.wrappedMethod, this, arguments);
});
return () => stubbed.restore();
});
it('should fail if starting node-pull-request throws', async() => {
const cli = new TestCLI();
const request = {
fetch: sinon.stub().returns(Promise.resolve({ status: 400 })),
text: sinon.stub().throws(),
json: sinon.stub().withArgs(CI_CRUMB_URL)
.returns(Promise.resolve({ crumb }))
};
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, dummySHA);
assert.strictEqual(await jobRunner.start(), false);
});
it('should return false if crumb fails', async() => {
const cli = new TestCLI();
const request = {
json: sinon.stub().throws()
};
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, dummySHA);
assert.strictEqual(await jobRunner.start(), false);
});
it('should start node-pull-request', async() => {
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);
assert.strictEqual(method, 'POST');
assert.deepStrictEqual(headers, { 'Jenkins-Crumb': crumb });
assert.ok(body._validated);
return Promise.resolve({ status: 201 });
}),
json: sinon.stub().withArgs(CI_CRUMB_URL)
.returns(Promise.resolve({ crumb }))
};
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, dummySHA);
assert.ok(await jobRunner.start());
});
it('should return false if node-pull-request not started', async() => {
const cli = new TestCLI();
const request = {
fetch: sinon.stub()
.callsFake((url, { method, headers, body }) => {
assert.strictEqual(url, CI_PR_URL);
assert.strictEqual(method, 'POST');
assert.deepStrictEqual(headers, { 'Jenkins-Crumb': crumb });
assert.ok(body._validated);
return Promise.resolve({ status: 401 });
}),
json: sinon.stub().withArgs(CI_CRUMB_URL)
.returns(Promise.resolve({ crumb }))
};
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, dummySHA);
assert.strictEqual(await jobRunner.start(), false);
});
describe('without --certify-safe flag', { concurrency: false }, () => {
before(() => {
sinon.replace(PRData.prototype, 'getReviews', function() {});
sinon.replace(PRData.prototype, 'getCommits', function() {});
return () => {
PRData.prototype.getReviews.restore();
PRData.prototype.getCommits.restore();
};
});
afterEach(() => {
PRData.prototype.getCollaborators.restore();
PRData.prototype.getComments.restore();
PRChecker.prototype.getApprovedTipOfHead.restore();
});
for (const { headIsApproved = false, collaborators = [], comments = [], expected } of [{
headIsApproved: true,
expected: true,
}, {
headIsApproved: false,
expected: false,
}, {
collaborators: ['foo'],
comments: [{ login: 'foo' }],
expected: true,
}, {
// Validates that passing full commit URL also works.
collaborators: ['foo'],
comments: [{ login: 'foo', body: `@nodejs-github-bot test https://github.com/nodejs/node/commit/${dummySHA}.\n` }],
expected: true,
}, {
// Validates that non-collaborator commenting should have no effect.
collaborators: ['foo'],
comments: [{ login: 'bar' }],
expected: false,
}]) {
it(`should return ${expected} with ${
JSON.stringify({ headIsApproved, collaborators, comments })}`, async() => {
const cli = new TestCLI();
sinon.stub(PRData.prototype, 'getCollaborators').callsFake(function() {
this.collaborators = collaborators.map(login => ({ login }));
});
sinon.stub(PRData.prototype, 'getComments').callsFake(function() {
this.comments = comments.map(({ body, login }) => ({
body: body ?? `@nodejs-github-bot test ${dummySHA}`,
author: { login }
}));
});
sinon.stub(PRChecker.prototype, 'getApprovedTipOfHead').callsFake(
sinon.fake.returns(headIsApproved && dummySHA));
const request = {
gql: sinon.stub().returns({
repository: {
pullRequest: {
labels: {
nodes: []
}
}
}
}),
fetch: sinon.stub()
.callsFake((url, { method, headers, body }) => {
assert.strictEqual(url, CI_PR_URL);
assert.strictEqual(method, 'POST');
assert.deepStrictEqual(headers, { 'Jenkins-Crumb': crumb });
assert.ok(body._validated);
return Promise.resolve({ status: 201 });
}),
json: sinon.stub().withArgs(CI_CRUMB_URL)
.returns(Promise.resolve({ crumb }))
};
const jobRunner = new RunPRJob(cli, request, owner, repo, prid, false);
assert.strictEqual(await jobRunner.start(), expected);
});
}
});
});