forked from nodejs/github-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-repo.test.js
More file actions
152 lines (120 loc) · 4.43 KB
/
Copy pathnode-repo.test.js
File metadata and controls
152 lines (120 loc) · 4.43 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
'use strict'
const tap = require('tap')
const lolex = require('lolex')
const nock = require('nock')
const nodeRepo = require('../../lib/node-repo')
const logger = require('../../lib/logger')
const readFixture = require('../read-fixture')
const { ignoreQueryParams } = require('../common')
tap.test('fetchExistingLabels(): caches existing repository labels', async (t) => {
const owner = 'nodejs'
const repo = 'node1'
// Test passes if nock is only called once, no other checks to run
const scope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get(`/repos/${owner}/${repo}/labels`)
.once() // should only be called once
.reply(200, [])
await nodeRepo._fetchExistingLabels({ owner, repo, logger })
await nodeRepo._fetchExistingLabels({ owner, repo, logger })
scope.done()
})
tap.test('fetchExistingLabels(): cache expires after one hour', async (t) => {
const owner = 'nodejs'
const repo = 'node2'
const clock = lolex.install()
// Test passes if nock is only called once, no other checks to run
const scope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get(`/repos/${owner}/${repo}/labels`)
.twice() // should be called twice
.reply(200, [])
t.tearDown(() => {
clock.uninstall()
})
await nodeRepo._fetchExistingLabels({ owner, repo, logger })
// fetch labels again after 1 hour and 1 minute
clock.tick(1000 * 60 * 61)
await nodeRepo._fetchExistingLabels({ owner, repo, logger })
scope.done()
})
tap.test('fetchExistingLabels(): yields an array of existing label names', async (t) => {
const labelsFixture = readFixture('repo-labels.json')
const owner = 'nodejs'
const repo = 'node3'
const scope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get(`/repos/${owner}/${repo}/labels`)
.reply(200, labelsFixture.data)
t.plan(1)
const existingLabels = await nodeRepo._fetchExistingLabels({ owner, repo, logger })
t.ok(existingLabels.includes('cluster'))
scope.done()
})
tap.test('fetchExistingLabels(): can retrieve more than 100 labels', async (t) => {
const labelsFixturePage1 = readFixture('repo-labels.json')
const labelsFixturePage2 = readFixture('repo-labels-page-2.json')
const owner = 'nodejs'
const repo = 'node4'
const headers = {
'Link': `<https://api.github.com/repos/${owner}/${repo}/labels?page=2>; rel="next"`
}
const firstPageScope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get(`/repos/${owner}/${repo}/labels`)
.reply(200, labelsFixturePage1.data, headers)
const secondPageScope = nock('https://api.github.com')
.get(`/repos/${owner}/${repo}/labels`)
.query({ page: 2, per_page: 100 })
.reply(200, labelsFixturePage2.data)
t.plan(2)
const existingLabels = await nodeRepo._fetchExistingLabels({ owner, repo, logger })
t.ok(existingLabels.includes('cluster'))
t.ok(existingLabels.includes('windows'))
firstPageScope.done()
secondPageScope.done()
})
tap.test('getBotPrLabels(): returns labels added by nodejs-github-bot', (t) => {
const events = readFixture('pull-request-events.json')
const owner = 'nodejs'
const repo = 'node5'
const prId = '1'
const scope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get(`/repos/${owner}/${repo}/issues/${prId}/events`)
.reply(200, events.data)
t.plan(1)
nodeRepo.getBotPrLabels({ owner, repo, prId }, (_, labels) => {
t.same(labels, ['testlabel'])
scope.done()
})
})
tap.test('getBotPrLabels(): returns net labels added/removed by nodejs-github-bot', (t) => {
const events = readFixture('pull-request-events-2.json')
const owner = 'nodejs'
const repo = 'node6'
const prId = '2'
const scope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.get(`/repos/${owner}/${repo}/issues/${prId}/events`)
.reply(200, events.data)
t.plan(1)
nodeRepo.getBotPrLabels({ owner, repo, prId }, (_, labels) => {
t.same(labels, [])
scope.done()
})
})
tap.test('removeLabelFromPR(): should remove label', async (t) => {
const owner = 'nodejs'
const repo = 'node7'
const prId = '3'
const label = '3'
const scope = nock('https://api.github.com')
.filteringPath(ignoreQueryParams)
.delete(`/repos/${owner}/${repo}/issues/${prId}/labels/${label}`)
.reply(200)
t.plan(1)
const response = await nodeRepo.removeLabelFromPR({ owner, repo, prId, logger }, label)
t.same(label, response)
scope.done()
})