-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.spec.js
256 lines (222 loc) · 7.13 KB
/
index.spec.js
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
const nock = require('nock');
const BITBUCKET_USERNAME = 'renovate-approve-bot';
const BITBUCKET_PASSWORD = 'r3novate-@pprove-b0t';
const RENOVATE_BOT_USER = 'renovate-bot';
process.env = Object.assign(process.env, {
BITBUCKET_USERNAME,
BITBUCKET_PASSWORD,
RENOVATE_BOT_USER,
});
const bot = require('./index');
const API_BASE_URL = 'https://api.bitbucket.org';
const BASIC_AUTH = { user: BITBUCKET_USERNAME, pass: BITBUCKET_PASSWORD };
const autoMergeDescription = '...\n\n🚦 **Automerge**: Enabled.\n\n...';
const manualMergeDescription =
'...\n\n🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.\n\n...';
afterEach(() => {
if (!nock.isDone()) {
throw new Error(
`Not all nock interceptors were used: ${JSON.stringify(
nock.pendingMocks()
)}`
);
}
nock.cleanAll();
});
describe('isAutomerging', () => {
it('is automerging', () => {
const pr = {
description: autoMergeDescription,
// omitted attributes...
};
const isAutomerging = bot.__get__('isAutomerging');
expect(isAutomerging(pr)).toBe(true);
});
it('is not automerging', () => {
const pr = {
description: manualMergeDescription,
// omitted attributes...
};
const isAutomerging = bot.__get__('isAutomerging');
expect(isAutomerging(pr)).toBe(false);
});
});
describe('getPullRequests', () => {
const pullRequestsEndpoint = `/2.0/pullrequests/${RENOVATE_BOT_USER}`;
it('gets pull-requests in a single page', async () => {
nock(API_BASE_URL)
.get(pullRequestsEndpoint)
.basicAuth(BASIC_AUTH)
.reply(200, {
values: [
{
description: autoMergeDescription,
links: {
self: {
href: 'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/1',
},
// omitted attributes...
},
// omitted attributes...
},
{
description: manualMergeDescription,
links: {
self: {
href: 'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/2',
},
// omitted attributes...
},
// omitted attributes...
},
{
description: autoMergeDescription,
links: {
self: {
href: 'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/3',
},
// omitted attributes...
},
// omitted attributes...
},
],
// omitted attributes...
});
const getPullRequests = bot.__get__('getPullRequests');
const pullRequests = [];
const prGenerator = await getPullRequests();
for await (const prHref of prGenerator) {
pullRequests.push(prHref);
}
expect(pullRequests).toStrictEqual([
'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/1',
'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/3',
]);
});
it('gets pull-requests in multiple pages', async () => {
nock(API_BASE_URL)
.get(pullRequestsEndpoint)
.basicAuth(BASIC_AUTH)
.reply(200, {
values: [
{
description: autoMergeDescription,
links: {
self: {
href: 'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/1',
},
// omitted attributes...
},
// omitted attributes...
},
{
description: manualMergeDescription,
links: {
self: {
href: 'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/2',
},
// omitted attributes...
},
// omitted attributes...
},
{
description: autoMergeDescription,
links: {
self: {
href: 'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/3',
},
// omitted attributes...
},
// omitted attributes...
},
],
next: `${API_BASE_URL}${pullRequestsEndpoint}?page=2`,
// omitted attributes...
});
nock(API_BASE_URL)
.get(pullRequestsEndpoint)
.basicAuth(BASIC_AUTH)
.query({ page: 2 })
.reply(200, {
values: [
{
description: manualMergeDescription,
links: {
self: {
href: 'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/5',
},
// omitted attributes...
},
// omitted attributes...
},
{
description: autoMergeDescription,
links: {
self: {
href: 'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/6',
},
// omitted attributes...
},
// omitted attributes...
},
],
// omitted attributes...
});
const getPullRequests = bot.__get__('getPullRequests');
const prHrefs = [];
const prGenerator = await getPullRequests();
for await (const prHref of prGenerator) {
prHrefs.push(prHref);
}
expect(prHrefs).toStrictEqual([
'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/1',
'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/3',
'https://api.bitbucket.org/2.0/repositories/myworkspace/myrepo/pullrequests/6',
]);
});
it('gets no pull-requests', async () => {
nock(API_BASE_URL)
.get(pullRequestsEndpoint)
.basicAuth(BASIC_AUTH)
.reply(200, {
values: [],
// omitted attributes...
});
const getPullRequests = bot.__get__('getPullRequests');
const prHrefs = [];
const prGenerator = await getPullRequests();
for await (const prHref of prGenerator) {
prHrefs.push(prHref);
}
expect(prHrefs).toStrictEqual([]);
});
});
describe('approvePullRequest', () => {
it('approves', async () => {
const prHref = '/2.0/repositories/myworkspace/myrepo/pullrequests/1';
nock(API_BASE_URL)
.post(`${prHref}/approve/`)
.basicAuth(BASIC_AUTH)
.reply(200, {
// omitted attributes...
});
const approvePullRequest = bot.__get__('approvePullRequest');
const response = await approvePullRequest(API_BASE_URL + prHref);
expect(response.statusCode).toBe(200);
});
it('is already approved', async () => {
const prHref = '/2.0/repositories/myworkspace/myrepo/pullrequests/2';
nock(API_BASE_URL)
.post(`${prHref}/approve/`)
.basicAuth(BASIC_AUTH)
.reply(409, {
type: 'error',
error: {
message: 'You already approved this pull request.',
},
});
const approvePullRequest = bot.__get__('approvePullRequest');
const response = await approvePullRequest(API_BASE_URL + prHref);
expect(response.statusCode).toBe(409);
});
});