Repository navigation
Expand file tree
/
Copy pathnode-labels-webhook.test.js
More file actions
173 lines (141 loc) · 6.12 KB
/
Copy pathnode-labels-webhook.test.js
File metadata and controls
173 lines (141 loc) · 6.12 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
'use strict'
const tap = require('tap')
const url = require('url')
const nock = require('nock')
const supertest = require('supertest')
const proxyquire = require('proxyquire')
const lolex = require('lolex')
const testStubs = {
'./github-secret': {
isValid: () => true,
// necessary to make makes proxyquire return this stub
// whenever *any* module tries to require('./github-secret')
'@global': true
}
}
const app = proxyquire('../../app', testStubs)
const readFixture = require('../read-fixture')
setupNoRequestMatchHandler()
tap.test('Sends POST request to https://api.github.com/repos/nodejs/node/issues/<PR-NUMBER>/labels', (t) => {
const clock = lolex.install()
const expectedLabels = ['timers']
const webhookPayload = readFixture('pull-request-opened.json')
const filesScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/repos/nodejs/node/pulls/19/files')
.reply(200, readFixture('pull-request-files.json'))
const existingRepoLabelsScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/repos/nodejs/node/labels')
.reply(200, readFixture('repo-labels.json'))
const newLabelsScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/node/issues/19/labels', expectedLabels)
.reply(200)
t.plan(1)
t.tearDown(() => filesScope.done() && existingRepoLabelsScope.done() && newLabelsScope.done() && clock.uninstall())
supertest(app)
.post('/hooks/github')
.set('x-github-event', 'pull_request')
.send(webhookPayload)
.expect(200)
.end((err, res) => {
clock.runAll()
t.equal(err, null)
})
})
tap.test('Adds v6.x label when PR is targeting the v6.x-staging branch', (t) => {
const clock = lolex.install()
const expectedLabels = ['timers', 'v6.x']
const webhookPayload = readFixture('pull-request-opened-v6.x.json')
const filesScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/repos/nodejs/node/pulls/19/files')
.reply(200, readFixture('pull-request-files.json'))
const existingRepoLabelsScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/repos/nodejs/node/labels')
.reply(200, readFixture('repo-labels.json'))
const newLabelsScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/node/issues/19/labels', expectedLabels)
.reply(200)
t.plan(1)
t.tearDown(() => filesScope.done() && existingRepoLabelsScope.done() && newLabelsScope.done() && clock.uninstall())
supertest(app)
.post('/hooks/github')
.set('x-github-event', 'pull_request')
.send(webhookPayload)
.expect(200)
.end((err, res) => {
clock.runAll()
t.equal(err, null)
})
})
// reported bug: https://github.com/nodejs/github-bot/issues/58
tap.test('Does not create labels which does not already exist', (t) => {
const clock = lolex.install()
const webhookPayload = readFixture('pull-request-opened-mapproxy.json')
const filesScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/repos/nodejs/node/pulls/7972/files')
.reply(200, readFixture('pull-request-files-mapproxy.json'))
const existingRepoLabelsScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/repos/nodejs/node/labels')
.reply(200, readFixture('repo-labels.json'))
t.plan(1)
t.tearDown(() => filesScope.done() && existingRepoLabelsScope.done() && clock.uninstall())
supertest(app)
.post('/hooks/github')
.set('x-github-event', 'pull_request')
.send(webhookPayload)
.expect(200)
.end((err, res) => {
clock.runAll()
t.equal(err, null)
})
})
// reported bug: https://github.com/nodejs/github-bot/issues/92
tap.test('Adds V8 Engine label when PR has deps/v8 file changes', (t) => {
const clock = lolex.install()
const expectedLabels = ['V8 Engine']
const webhookPayload = readFixture('pull-request-opened-v8.json')
const filesScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/repos/nodejs/node/pulls/9422/files')
.reply(200, readFixture('pull-request-files-v8.json'))
const existingRepoLabelsScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get('/repos/nodejs/node/labels')
.reply(200, readFixture('repo-labels.json'))
const newLabelsScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.post('/repos/nodejs/node/issues/9422/labels', expectedLabels)
.reply(200)
t.plan(1)
t.tearDown(() => filesScope.done() && existingRepoLabelsScope.done() && newLabelsScope.done() && clock.uninstall())
supertest(app)
.post('/hooks/github')
.set('x-github-event', 'pull_request')
.send(webhookPayload)
.expect(200)
.end((err, res) => {
clock.runAll()
t.equal(err, null)
})
})
function ignoreQueryParams (pathAndQuery) {
return url.parse(pathAndQuery, true).pathname
}
// nock doesn't make the tests explode if an unexpected external request is made,
// we therefore have to attach an explicit "no match" handler too make tests fail
// if there's made outgoing request we didn't expect
function setupNoRequestMatchHandler () {
nock.emitter.on('no match', (req) => {
// requests against the app is expected and we shouldn't need to tell nock about it
if (req.hostname === '127.0.0.1') return
const reqUrl = `${req._headers.host}${req.path}`
throw new Error(`Unexpected request was sent to ${reqUrl}`)
})
}