-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub-commit.test.mts
More file actions
170 lines (151 loc) · 4.9 KB
/
Copy pathgithub-commit.test.mts
File metadata and controls
170 lines (151 loc) · 4.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
/**
* @file Unit tests for `createSignedCommit` — the blob -> tree -> commit ->
* ref PATCH helper that lands a web-flow-verified commit from CI. HTTP is
* mocked with nock (node:http intercept) under `disableNetConnect()`.
*/
import nock from 'nock'
import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest'
import { createSignedCommit } from '../../src/github/commit.mjs'
const API = 'https://api.github.com'
beforeAll(() => {
nock.disableNetConnect()
})
afterAll(() => {
nock.enableNetConnect()
})
afterEach(() => {
nock.cleanAll()
})
describe('createSignedCommit', () => {
it('chains blob -> tree -> commit -> ref and returns the new commit SHA', async () => {
const scope = nock(API)
.post('/repos/owner/repo/git/blobs')
.reply(201, { sha: 'blobA' })
.post('/repos/owner/repo/git/blobs')
.reply(201, { sha: 'blobB' })
.post('/repos/owner/repo/git/trees')
.reply(201, { sha: 'treeX' })
.post('/repos/owner/repo/git/commits')
.reply(201, { sha: 'commitZ' })
.patch('/repos/owner/repo/git/refs/heads/main')
.reply(200, {})
const sha = await createSignedCommit({
baseTreeSha: 'basetree1',
branch: 'main',
files: [
{ content: '{"version":"1.1.0"}', path: 'package.json' },
{ content: '# Changelog', path: 'CHANGELOG.md' },
],
message: 'chore: bump version to 1.1.0',
parentSha: 'parent1',
repo: 'owner/repo',
token: 'tok',
})
expect(sha).toBe('commitZ')
scope.done()
})
it('sends base64 blob content, a base_tree, parent, and message', async () => {
const scope = nock(API)
.post('/repos/owner/repo/git/blobs', body => {
expect(body.encoding).toBe('base64')
expect(Buffer.from(body.content, 'base64').toString('utf8')).toBe(
'{"v":1}',
)
return true
})
.reply(201, { sha: 'b1' })
.post('/repos/owner/repo/git/trees', body => {
expect(body.base_tree).toBe('basetree1')
expect(body.tree[0].path).toBe('package.json')
expect(body.tree[0].mode).toBe('100644')
expect(body.tree[0].sha).toBe('b1')
return true
})
.reply(201, { sha: 't1' })
.post('/repos/owner/repo/git/commits', body => {
expect(body.message).toBe('chore: bump version to 1.1.0')
expect(body.parents).toEqual(['parent1'])
expect(body.tree).toBe('t1')
return true
})
.reply(201, { sha: 'c1' })
.patch('/repos/owner/repo/git/refs/heads/main', body => {
expect(body.sha).toBe('c1')
return true
})
.reply(200, {})
const sha = await createSignedCommit({
baseTreeSha: 'basetree1',
branch: 'main',
files: [{ content: '{"v":1}', path: 'package.json' }],
message: 'chore: bump version to 1.1.0',
parentSha: 'parent1',
repo: 'owner/repo',
token: 'tok',
})
expect(sha).toBe('c1')
scope.done()
})
it('uses the apiUrl override when provided', async () => {
const custom = 'https://github.example.com/api/v3'
const scope = nock(custom)
.post('/repos/owner/name/git/blobs')
.reply(201, { sha: 'blob1' })
.post('/repos/owner/name/git/trees')
.reply(201, { sha: 'tree1' })
.post('/repos/owner/name/git/commits')
.reply(201, { sha: 'commit1' })
.patch('/repos/owner/name/git/refs/heads/main')
.reply(200, {})
const sha = await createSignedCommit({
apiUrl: custom,
baseTreeSha: 'base1',
branch: 'main',
files: [{ content: 'hello', path: 'hello.txt' }],
message: 'test commit',
parentSha: 'parent1',
repo: 'owner/name',
token: 'tok',
})
expect(sha).toBe('commit1')
scope.done()
})
it('throws when a blob POST fails', async () => {
nock(API)
.post('/repos/owner/repo/git/blobs')
.reply(500, 'Internal Server Error')
await expect(
createSignedCommit({
baseTreeSha: 'base1',
branch: 'main',
files: [{ content: 'x', path: 'example.txt' }],
message: 'test',
parentSha: 'parent1',
repo: 'owner/repo',
token: 'tok',
}),
).rejects.toThrow(/GitHub API POST.*failed/)
})
it('throws when the ref PATCH fails', async () => {
nock(API)
.post('/repos/owner/repo/git/blobs')
.reply(201, { sha: 'b1' })
.post('/repos/owner/repo/git/trees')
.reply(201, { sha: 't1' })
.post('/repos/owner/repo/git/commits')
.reply(201, { sha: 'c1' })
.patch('/repos/owner/repo/git/refs/heads/main')
.reply(422, 'Unprocessable Entity')
await expect(
createSignedCommit({
baseTreeSha: 'base1',
branch: 'main',
files: [{ content: 'x', path: 'example.txt' }],
message: 'test',
parentSha: 'parent1',
repo: 'owner/repo',
token: 'tok',
}),
).rejects.toThrow(/GitHub API PATCH.*failed/)
})
})