forked from nodejs/github-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush-jenkins-update.test.js
More file actions
259 lines (209 loc) · 7.97 KB
/
push-jenkins-update.test.js
File metadata and controls
259 lines (209 loc) · 7.97 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
'use strict'
const tap = require('tap')
const url = require('url')
const nock = require('nock')
const supertest = require('supertest')
const app = require('../../app')
const readFixture = require('../read-fixture')
tap.test('Sends POST requests to https://api.github.com/repos/nodejs/node/statuses/<SHA>', (t) => {
const jenkinsPayload = readFixture('success-payload.json')
const prCommitsScope = setupGetCommitsMock('node')
const scope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1')
.reply(201)
t.plan(1)
supertest(app)
.post('/node/jenkins/start')
.send(jenkinsPayload)
.expect(201)
.end((err, res) => {
prCommitsScope.done()
scope.done()
t.equal(err, null)
})
})
tap.test('Allows repository name to be provided with URL parameter when pushing job started', (t) => {
const jenkinsPayload = readFixture('pending-payload.json')
const prCommitsScope = setupGetCommitsMock('citgm')
const scope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/citgm/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1')
.reply(201)
t.plan(1)
supertest(app)
.post('/citgm/jenkins/start')
.send(jenkinsPayload)
.expect(201)
.end((err, res) => {
prCommitsScope.done()
scope.done()
t.equal(err, null)
})
})
tap.test('Allows repository name to be provided with URL parameter when pushing job ended', (t) => {
const jenkinsPayload = readFixture('success-payload.json')
const prCommitsScope = setupGetCommitsMock('citgm')
const scope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/citgm/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1')
.reply(201)
t.plan(1)
supertest(app)
.post('/citgm/jenkins/end')
.send(jenkinsPayload)
.expect(201)
.end((err, res) => {
prCommitsScope.done()
scope.done()
t.equal(err, null)
})
})
tap.test('Forwards payload provided in incoming POST to GitHub status API', (t) => {
const fixture = readFixture('success-payload.json')
const prCommitsScope = setupGetCommitsMock('node')
const scope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1', {
state: 'success',
context: 'test/osx',
description: 'tests passed',
target_url: 'https://ci.nodejs.org/job/node-test-commit-osx/3157/'
})
.reply(201)
t.plan(1)
supertest(app)
.post('/node/jenkins/start')
.send(fixture)
.expect(201)
.end((err, res) => {
prCommitsScope.done()
scope.done()
t.equal(err, null)
})
})
tap.test('Posts a CI comment in the related PR when Jenkins build is named node-test-pull-request', (t) => {
const fixture = readFixture('jenkins-test-pull-request-success-payload.json')
const createCommentScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/node/issues/12345/comments', { body: 'CI: https://ci.nodejs.org/job/node-test-pull-request/21633/' })
.reply(200)
nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/repos/nodejs/node/issues/12345/comments')
.reply(200, [])
// we don't care about asserting the scopes below, just want to stop the requests from actually being sent
setupGetBotUsernameMock()
setupGetCommitsMock('node')
nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1')
.reply(201)
t.plan(1)
supertest(app)
.post('/node/jenkins/start')
.send(fixture)
.expect(201)
.end((err, res) => {
createCommentScope.done()
t.equal(err, null)
})
})
tap.test('Edits existing CI comment when bot has posted a CI comment before', (t) => {
const incomingReqFixture = readFixture('jenkins-test-pull-request-success-payload.json')
const existingCommentsFixture = readFixture('pull-request-comments-with-ci-comment.json')
const editCommentScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.patch('/repos/nodejs/node/issues/comments/476584580', {
body: `CI: https://ci.nodejs.org/job/node-test-pull-request/21904/\nCI: https://ci.nodejs.org/job/node-test-pull-request/21633/`
})
.reply(200)
nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/repos/nodejs/node/issues/12345/comments')
.reply(200, existingCommentsFixture)
// we don't care about asserting the scopes below, just want to stop the requests from actually being sent
setupGetBotUsernameMock()
setupGetCommitsMock('node')
nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/node/statuses/8a5fec2a6bade91e544a30314d7cf21f8a200de1')
.reply(201)
t.plan(1)
supertest(app)
.post('/node/jenkins/start')
.send(incomingReqFixture)
.expect(201)
.end((err, res) => {
editCommentScope.done()
t.equal(err, null)
})
})
tap.test('Responds with 400 / "Bad request" when incoming request has invalid payload', (t) => {
const fixture = readFixture('invalid-payload.json')
// don't care about the results, just want to prevent any HTTP request ever being made
nock('https://api.github.com')
t.plan(1)
supertest(app)
.post('/node/jenkins/start')
.send(fixture)
.expect(400, 'Invalid payload')
.end((err, res) => {
t.equal(err, null)
})
})
tap.test('Responds with 400 / "Bad request" when build started status update is not related to a pull request', (t) => {
const fixture = readFixture('jenkins-staging-failure-payload.json')
// don't care about the results, just want to prevent any HTTP request ever being made
nock('https://api.github.com')
t.plan(1)
supertest(app)
.post('/node/jenkins/start')
.send(fixture)
.expect(400, 'Will only push builds related to pull requests')
.end((err, res) => {
t.equal(err, null)
})
})
tap.test('Responds with 400 / "Bad request" when build ended status update is not related to a pull request', (t) => {
const fixture = readFixture('jenkins-staging-failure-payload.json')
// don't care about the results, just want to prevent any HTTP request ever being made
nock('https://api.github.com')
t.plan(1)
supertest(app)
.post('/node/jenkins/end')
.send(fixture)
.expect(400, 'Will only push builds related to pull requests')
.end((err, res) => {
t.equal(err, null)
})
})
tap.test('Responds with 400 / "Bad request" when incoming providing invalid repository name', (t) => {
const fixture = readFixture('pending-payload.json')
// don't care about the results, just want to prevent any HTTP request ever being made
nock('https://api.github.com')
t.plan(1)
supertest(app)
.post('/not-valid-repo-name/jenkins/start')
.send(fixture)
.expect(400, 'Invalid repository')
.end((err, res) => {
t.equal(err, null)
})
})
function setupGetCommitsMock (repoName) {
const commitsResponse = readFixture('pr-commits.json')
return nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get(`/repos/nodejs/${repoName}/pulls/12345/commits`)
.reply(200, commitsResponse)
}
function setupGetBotUsernameMock () {
nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/user')
.reply(200, readFixture('authenticated-user.json'))
}
function ignoreQueryParams (pathAndQuery) {
return url.parse(pathAndQuery, true).pathname
}