-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.test.ts
More file actions
303 lines (260 loc) 路 7.9 KB
/
Copy pathgithub.test.ts
File metadata and controls
303 lines (260 loc) 路 7.9 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
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
import assert from 'node:assert/strict'
import { describe, test } from 'node:test'
import {
apiPull,
apiTag,
commitUrl,
noPulls,
noTags,
page,
PULLS_URL,
releaseCreated,
RELEASES_URL,
REPO,
stubFetch,
TAGS_URL,
} from './fixtures.ts'
import { createRelease, fetchReleaseInputs, resolveToken } from './github.ts'
describe('pagination', () => {
test('walks `Link` headers to completion before filtering tags', async () => {
const secondPage = `${TAGS_URL}&page=2`
const thirdPage = `${TAGS_URL}&page=3`
const { fetch, calls } = stubFetch({
[TAGS_URL]: page([apiTag('v1.0.0', 'sha-1')], secondPage),
[secondPage]: page([apiTag('v1.1.0', 'sha-2')], thirdPage),
// The highest tag sits on the last page: stopping early would base the
// next version on a stale release.
[thirdPage]: page([apiTag('v2.0.0', 'sha-3')]),
[commitUrl('sha-3')]: page({
commit: { committer: { date: '2026-01-01T00:00:00Z' } },
}),
...noPulls(),
})
const { tags } = await fetchReleaseInputs({
...REPO,
fetch,
token: undefined,
})
assert.deepEqual(tags, [{ name: 'v2.0.0', date: '2026-01-01T00:00:00Z' }])
assert.ok(calls.some(({ url }) => url === thirdPage))
})
test('walks `Link` headers to completion for merged pull requests', async () => {
const secondPage = `${PULLS_URL}&page=2`
const { fetch } = stubFetch({
[PULLS_URL]: page([apiPull(1)], secondPage),
[secondPage]: page([apiPull(2, ['enhancement'])]),
...noTags(),
})
const { pullRequests } = await fetchReleaseInputs({
...REPO,
fetch,
token: undefined,
})
assert.deepEqual(
pullRequests.map(({ number }) => number),
[1, 2],
)
})
test('refuses to walk a `Link` header that cycles', async () => {
const { fetch } = stubFetch({
[PULLS_URL]: page([apiPull(1)], PULLS_URL),
...noTags(),
})
await assert.rejects(
fetchReleaseInputs({ ...REPO, fetch, token: undefined }),
/looped back/,
)
})
})
describe('the pull requests it returns', () => {
test('are merged ones only, shaped for the decision core', async () => {
const { fetch } = stubFetch({
[PULLS_URL]: page([
apiPull(1, ['enhancement', 'safe to test']),
apiPull(2, ['bug'], null),
]),
...noTags(),
})
const { pullRequests } = await fetchReleaseInputs({
...REPO,
fetch,
token: undefined,
})
assert.deepEqual(pullRequests, [
{
number: 1,
title: 'PR 1',
labels: [{ name: 'enhancement' }, { name: 'safe to test' }],
merged_at: '2026-02-01T00:00:00Z',
},
])
})
})
describe('the tags it returns', () => {
test('are empty when the repo has no release tag yet', async () => {
const { fetch, calls } = stubFetch({
[TAGS_URL]: page([
apiTag('nightly', 'sha-1'),
apiTag('v2.0.0-beta.1', 'sha-2'),
]),
...noPulls(),
})
const { tags } = await fetchReleaseInputs({
...REPO,
fetch,
token: undefined,
})
assert.deepEqual(tags, [])
// No release tag means no commit worth dating.
assert.deepEqual(
calls.filter(({ url }) => url.includes('/commits/')),
[],
)
})
test('date only the highest release tag, whatever order tags arrive in', async () => {
const { fetch, calls } = stubFetch({
[TAGS_URL]: page([
apiTag('v1.9.0', 'sha-1'),
apiTag('v1.10.0', 'sha-2'),
apiTag('v1.2.3', 'sha-3'),
]),
[commitUrl('sha-2')]: page({
commit: { committer: { date: '2026-01-01T00:00:00Z' } },
}),
...noPulls(),
})
const { tags } = await fetchReleaseInputs({
...REPO,
fetch,
token: undefined,
})
assert.deepEqual(tags, [{ name: 'v1.10.0', date: '2026-01-01T00:00:00Z' }])
assert.equal(calls.filter(({ url }) => url.includes('/commits/')).length, 1)
})
})
describe('requests', () => {
test('carry the GitHub API headers and a bearer token when there is one', async () => {
const { fetch, calls } = stubFetch({ ...noTags(), ...noPulls() })
await fetchReleaseInputs({ ...REPO, fetch, token: 'ghs_secret' })
for (const { headers } of calls) {
assert.equal(headers['authorization'], 'Bearer ghs_secret')
assert.equal(headers['accept'], 'application/vnd.github+json')
assert.equal(headers['x-github-api-version'], '2022-11-28')
assert.ok(headers['user-agent'])
}
})
test('are unauthenticated when there is no token', async () => {
const { fetch, calls } = stubFetch({ ...noTags(), ...noPulls() })
await fetchReleaseInputs({ ...REPO, fetch, token: undefined })
for (const { headers } of calls) {
assert.equal(headers['authorization'], undefined)
}
})
test('fail loudly, naming the request and the status', async () => {
const { fetch } = stubFetch({
[TAGS_URL]: new Response('{"message":"Bad credentials"}', {
status: 401,
statusText: 'Unauthorized',
}),
...noPulls(),
})
await assert.rejects(
fetchReleaseInputs({ ...REPO, fetch, token: undefined }),
/\/tags.*401.*Bad credentials/s,
)
})
})
describe('the release it creates', () => {
test('asks GitHub to generate the notes for the tag', async () => {
const { fetch, calls } = stubFetch(releaseCreated())
await createRelease({
...REPO,
tag: 'v1.2.3',
fetch,
token: 'ghs_secret',
})
assert.equal(calls.length, 1)
const [call] = calls
assert.equal(call?.url, RELEASES_URL)
assert.equal(call?.method, 'POST')
assert.equal(call?.headers['authorization'], 'Bearer ghs_secret')
assert.equal(call?.headers['content-type'], 'application/json')
assert.deepEqual(JSON.parse(String(call?.body)), {
tag_name: 'v1.2.3',
name: 'v1.2.3',
generate_release_notes: true,
})
})
test('names the commit to tag when there is no tag yet', async () => {
const { fetch, calls } = stubFetch(releaseCreated())
await createRelease({
...REPO,
tag: 'v1.2.3',
commitish: 'sha-of-the-run',
fetch,
token: undefined,
})
assert.deepEqual(JSON.parse(String(calls[0]?.body)), {
tag_name: 'v1.2.3',
name: 'v1.2.3',
generate_release_notes: true,
target_commitish: 'sha-of-the-run',
})
})
test('fails loudly, naming the request and the status', async () => {
const { fetch } = stubFetch({
[RELEASES_URL]: new Response(
'{"message":"Validation Failed: already_exists"}',
{ status: 422, statusText: 'Unprocessable Entity' },
),
})
await assert.rejects(
createRelease({ ...REPO, tag: 'v1.2.3', fetch, token: undefined }),
/POST.*\/releases.*422.*already_exists/s,
)
})
})
describe('auth precedence', () => {
const failIfCalled = () => {
assert.fail('`gh auth token` should not have been consulted')
}
test('prefers `GH_TOKEN`', () => {
assert.equal(
resolveToken({
env: { GH_TOKEN: 'from-gh-token', GITHUB_TOKEN: 'from-github-token' },
ghAuthToken: failIfCalled,
}),
'from-gh-token',
)
})
test('falls back to `GITHUB_TOKEN`', () => {
assert.equal(
resolveToken({
env: { GITHUB_TOKEN: 'from-github-token' },
ghAuthToken: failIfCalled,
}),
'from-github-token',
)
})
test('treats a blank env var as unset', () => {
assert.equal(
resolveToken({
env: { GH_TOKEN: ' ', GITHUB_TOKEN: '' },
ghAuthToken: () => 'from-gh-cli',
}),
'from-gh-cli',
)
})
test('falls back to `gh auth token`', () => {
assert.equal(
resolveToken({ env: {}, ghAuthToken: () => 'from-gh-cli' }),
'from-gh-cli',
)
})
test('falls through to unauthenticated when nothing yields a token', () => {
assert.equal(
resolveToken({ env: {}, ghAuthToken: () => undefined }),
undefined,
)
})
})