-
-
Notifications
You must be signed in to change notification settings - Fork 511
/
github.js
458 lines (388 loc) Β· 15.8 KB
/
github.js
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import test from 'ava';
import sinon from 'sinon';
import { RequestError } from '@octokit/request-error';
import GitHub from '../lib/plugin/github/GitHub.js';
import { factory, runTasks } from './util/index.js';
import {
interceptAuthentication,
interceptCollaborator,
interceptListReleases,
interceptCreate,
interceptUpdate,
interceptAsset
} from './stub/github.js';
const tokenRef = 'GITHUB_TOKEN';
const pushRepo = 'git://github.com/user/repo';
const host = 'github.com';
const git = { changelog: '' };
const requestErrorOptions = { request: { url: '', headers: {} }, response: { headers: {} } };
test.serial('should check token and perform checks', async t => {
const tokenRef = 'MY_GITHUB_TOKEN';
const options = { github: { release: true, tokenRef, pushRepo } };
const github = factory(GitHub, { options });
process.env[tokenRef] = '123'; // eslint-disable-line require-atomic-updates
interceptAuthentication();
interceptCollaborator();
await t.notThrowsAsync(github.init());
});
test.serial('should check token and warn', async t => {
const tokenRef = 'MY_GITHUB_TOKEN';
const options = { github: { release: true, tokenRef, pushRepo } };
const github = factory(GitHub, { options });
delete process.env[tokenRef];
await t.notThrowsAsync(github.init());
t.is(github.log.warn.args[0][0], 'Environment variable "MY_GITHUB_TOKEN" is required for automated GitHub Releases.');
t.is(github.log.warn.args[1][0], 'Falling back to web-based GitHub Release.');
});
test('should release and upload assets', async t => {
const options = {
git,
github: {
pushRepo,
tokenRef,
release: true,
releaseName: 'Release ${tagName}',
releaseNotes: 'echo Custom notes',
assets: 'test/resources/file-v${version}.txt'
}
};
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git log --pretty=format:"* %s (%h)" ${from}...${to}').resolves('');
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('2.0.1');
interceptAuthentication();
interceptCollaborator();
interceptCreate({ body: { tag_name: '2.0.2', name: 'Release 2.0.2', body: 'Custom notes' } });
interceptAsset({ body: '*' });
await runTasks(github);
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(releaseUrl, 'https://github.com/user/repo/releases/tag/2.0.2');
exec.restore();
});
test('should create a pre-release and draft release notes', async t => {
const options = {
git,
github: {
pushRepo,
tokenRef,
release: true,
releaseName: 'Release ${tagName}',
preRelease: true,
draft: true
}
};
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git log --pretty=format:"* %s (%h)" ${from}...${to}').resolves('');
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('2.0.1');
interceptAuthentication();
interceptCollaborator();
interceptCreate({ body: { tag_name: '2.0.2', name: 'Release 2.0.2', prerelease: true, draft: true } });
await runTasks(github);
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(releaseUrl, 'https://github.com/user/repo/releases/tag/2.0.2');
exec.restore();
});
test('should create auto-generated release notes', async t => {
const options = {
git,
github: {
pushRepo,
tokenRef,
release: true,
releaseName: 'Release ${tagName}',
autoGenerate: true
}
};
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('2.0.1');
interceptAuthentication();
interceptCollaborator();
interceptCreate({ body: { tag_name: '2.0.2', name: 'Release 2.0.2', generate_release_notes: true, body: '' } });
await runTasks(github);
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(releaseUrl, 'https://github.com/user/repo/releases/tag/2.0.2');
exec.restore();
});
test('should update release and upload assets', async t => {
const asset = 'file1';
const options = {
increment: false,
git,
github: {
update: true,
pushRepo,
tokenRef,
release: true,
releaseName: 'Release ${tagName}',
releaseNotes: 'echo Custom notes',
assets: `test/resources/${asset}`
}
};
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git log --pretty=format:"* %s (%h)" ${from}...${to}').resolves('');
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('2.0.1');
exec.withArgs('git rev-list 2.0.1 --tags --max-count=1').resolves('a123456');
exec.withArgs('git describe --tags --abbrev=0 "a123456^"').resolves('2.0.1');
interceptAuthentication();
interceptCollaborator();
interceptListReleases({ tag_name: '2.0.1' });
interceptUpdate({ body: { tag_name: '2.0.1', name: 'Release 2.0.1', body: 'Custom notes' } });
interceptAsset({ body: asset });
await runTasks(github);
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(releaseUrl, 'https://github.com/user/repo/releases/tag/2.0.1');
exec.restore();
});
test('should create new release for unreleased tag', async t => {
const options = {
increment: false,
git,
github: {
update: true,
pushRepo,
tokenRef,
release: true,
releaseName: 'Release ${tagName}',
releaseNotes: 'echo Custom notes'
}
};
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git log --pretty=format:"* %s (%h)" ${from}...${to}').resolves('');
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('2.0.1');
exec.withArgs('git rev-list 2.0.1 --tags --max-count=1').resolves('b123456');
exec.withArgs('git describe --tags --abbrev=0 "b123456^"').resolves('2.0.1');
interceptAuthentication();
interceptCollaborator();
interceptListReleases({ tag_name: '2.0.0' });
interceptCreate({ body: { tag_name: '2.0.1', name: 'Release 2.0.1', body: 'Custom notes' } });
await runTasks(github);
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(releaseUrl, 'https://github.com/user/repo/releases/tag/2.0.1');
exec.restore();
});
test('should release to enterprise host', async t => {
const options = { git, github: { tokenRef, pushRepo: 'git://github.example.org/user/repo' } };
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git remote get-url origin').resolves(`https://github.example.org/user/repo`);
exec.withArgs('git config --get remote.origin.url').resolves(`https://github.example.org/user/repo`);
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves(`1.0.0`);
const remote = { api: 'https://github.example.org/api/v3', host: 'github.example.org' };
interceptAuthentication(remote);
interceptCollaborator(remote);
interceptCreate(Object.assign({ body: { tag_name: '1.0.1' } }, remote));
await runTasks(github);
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(releaseUrl, `https://github.example.org/user/repo/releases/tag/1.0.1`);
exec.restore();
});
test('should release to alternative host and proxy', async t => {
const remote = { api: 'https://custom.example.org/api/v3', host: 'custom.example.org' };
interceptAuthentication(remote);
interceptCollaborator(remote);
interceptCreate(Object.assign({ body: { tag_name: '1.0.1' } }, remote));
const options = {
git,
github: {
tokenRef,
pushRepo: `git://custom.example.org/user/repo`,
host: 'custom.example.org',
proxy: 'http://proxy:8080'
}
};
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git log --pretty=format:"* %s (%h)" ${from}...${to}').resolves('');
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('1.0.0');
await runTasks(github);
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(releaseUrl, `https://custom.example.org/user/repo/releases/tag/1.0.1`);
t.is(github.options.proxy, 'http://proxy:8080');
exec.restore();
});
test('should release to git.pushRepo', async t => {
const remote = { api: 'https://custom.example.org/api/v3', host: 'custom.example.org' };
interceptCreate(Object.assign({ body: { tag_name: '1.0.1' } }, remote));
const options = { git: { pushRepo: 'upstream', changelog: '' }, github: { tokenRef, skipChecks: true } };
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git log --pretty=format:"* %s (%h)" ${from}...${to}').resolves('');
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('1.0.0');
exec.withArgs('git remote get-url upstream').resolves('https://custom.example.org/user/repo');
await runTasks(github);
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(releaseUrl, 'https://custom.example.org/user/repo/releases/tag/1.0.1');
exec.restore();
});
const testSkipOnActions = process.env.GITHUB_ACTIONS ? test.skip : test;
testSkipOnActions('should throw for unauthenticated user', async t => {
const options = { github: { tokenRef, pushRepo, host } };
const github = factory(GitHub, { options });
const stub = sinon.stub(github.client.users, 'getAuthenticated');
stub.throws(new RequestError('Bad credentials', 401, requestErrorOptions));
await t.throwsAsync(runTasks(github), {
message: /^Could not authenticate with GitHub using environment variable "GITHUB_TOKEN"/
});
t.is(stub.callCount, 1);
stub.restore();
});
testSkipOnActions('should throw for non-collaborator', async t => {
interceptAuthentication({ username: 'john' });
const options = { github: { tokenRef, pushRepo, host } };
const github = factory(GitHub, { options });
const stub = sinon.stub(github.client.repos, 'checkCollaborator');
stub.throws(new RequestError('HttpError', 401, requestErrorOptions));
await t.throwsAsync(runTasks(github), { message: /^User john is not a collaborator for user\/repo/ });
stub.restore();
});
test.serial('should skip authentication and collaborator checks when running on GitHub Actions', async t => {
const { GITHUB_ACTIONS, GITHUB_ACTOR } = process.env;
if (!GITHUB_ACTIONS) {
process.env.GITHUB_ACTIONS = 1;
process.env.GITHUB_ACTOR = 'webpro';
}
const options = { github: { tokenRef } };
const github = factory(GitHub, { options });
const authStub = sinon.stub(github, 'isAuthenticated');
const collaboratorStub = sinon.stub(github, 'isCollaborator');
await t.notThrowsAsync(github.init());
t.is(authStub.callCount, 0);
t.is(collaboratorStub.callCount, 0);
t.is(github.getContext('username'), process.env.GITHUB_ACTOR);
authStub.restore();
collaboratorStub.restore();
if (!GITHUB_ACTIONS) {
process.env.GITHUB_ACTIONS = GITHUB_ACTIONS || '';
process.env.GITHUB_ACTOR = GITHUB_ACTOR || '';
}
});
test('should handle octokit client error (without retries)', async t => {
const github = factory(GitHub, { options: { github: { tokenRef, pushRepo, host } } });
const stub = sinon.stub(github.client.repos, 'createRelease');
stub.throws(new RequestError('Not found', 404, requestErrorOptions));
interceptAuthentication();
interceptCollaborator();
await t.throwsAsync(runTasks(github), { message: /^404 \(Not found\)/ });
t.is(stub.callCount, 1);
stub.restore();
});
test('should handle octokit client error (with retries)', async t => {
const options = { github: { tokenRef, pushRepo, host, retryMinTimeout: 0 } };
const github = factory(GitHub, { options });
const stub = sinon.stub(github.client.repos, 'createRelease');
stub.throws(new RequestError('Request failed', 500, requestErrorOptions));
interceptAuthentication();
interceptCollaborator();
await t.throwsAsync(runTasks(github), { message: /^500 \(Request failed\)/ });
t.is(stub.callCount, 3);
stub.restore();
});
test('should not call octokit client in dry run', async t => {
const options = { 'dry-run': true, git, github: { tokenRef, pushRepo, releaseName: 'R ${version}', assets: ['*'] } };
const github = factory(GitHub, { options });
const spy = sinon.spy(github, 'client', ['get']);
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git log --pretty=format:"* %s (%h)" ${from}...${to}').resolves('');
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('v1.0.0');
await runTasks(github);
t.is(spy.get.callCount, 0);
t.is(github.log.exec.args[1][0], 'octokit repos.createRelease "R 1.0.1" (v1.0.1)');
t.is(github.log.exec.lastCall.args[0], 'octokit repos.uploadReleaseAssets');
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(releaseUrl, 'https://github.com/user/repo/releases/tag/v1.0.1');
spy.get.restore();
exec.restore();
});
test('should skip checks', async t => {
const options = { github: { tokenRef, skipChecks: true } };
const github = factory(GitHub, { options });
await t.notThrowsAsync(github.init());
});
test('should generate GitHub web release url', async t => {
const options = {
github: {
pushRepo,
release: true,
web: true,
releaseName: 'Release ${tagName}',
releaseNotes: 'echo Custom notes'
}
};
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git log --pretty=format:"* %s (%h)" ${from}...${to}').resolves('');
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('2.0.1');
await runTasks(github);
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(
releaseUrl,
'https://github.com/user/repo/releases/new?tag=2.0.2&title=Release+2.0.2&body=Custom+notes&prerelease=false'
);
exec.restore();
});
test('should generate GitHub web release url for enterprise host', async t => {
const options = {
git,
github: {
pushRepo: 'git@custom.example.org:user/repo',
release: true,
web: true,
host: 'custom.example.org',
releaseName: 'The Launch',
releaseNotes: 'echo It happened'
}
};
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git log --pretty=format:"* %s (%h)" ${from}...${to}').resolves('');
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('2.0.1');
await runTasks(github);
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(
releaseUrl,
'https://custom.example.org/user/repo/releases/new?tag=2.0.2&title=The+Launch&body=It+happened&prerelease=false'
);
exec.restore();
});
// eslint-disable-next-line ava/no-skip-test
test.skip('should truncate long body', async t => {
const releaseNotes = 'a'.repeat(125001);
const body = 'a'.repeat(124000) + '...';
const options = {
git,
github: {
pushRepo,
tokenRef,
release: true,
releaseName: 'Release ${tagName}',
releaseNotes: 'echo ' + releaseNotes
}
};
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git log --pretty=format:"* %s (%h)" ${from}...${to}').resolves('');
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('2.0.1');
interceptAuthentication();
interceptCollaborator();
interceptCreate({ body: { tag_name: '2.0.2', name: 'Release 2.0.2', body } });
await runTasks(github);
const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(releaseUrl, 'https://github.com/user/repo/releases/tag/2.0.2');
exec.restore();
});