forked from necojackarc/auto-request-review
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreviewer.test.js
More file actions
379 lines (328 loc) · 14 KB
/
Copy pathreviewer.test.js
File metadata and controls
379 lines (328 loc) · 14 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
'use strict';
const {
fetch_other_group_members,
identify_reviewers_by_changed_files,
identify_reviewers_by_author,
should_request_review,
fetch_default_reviewers,
randomly_pick_reviewers,
} = require('../src/reviewer');
const { expect } = require('chai');
describe('reviewer', function() {
describe('fetch_other_group_members()', function() {
const base_config = {
reviewers: {
groups: {
'mario-brothers': [ 'mario', 'dr-mario', 'luigi' ],
'mario-alike': [ 'mario', 'dr-mario', 'wario' ],
},
},
};
it('returns no members by default when "enable_group_assignment" is not supplied', function() {
const author = 'this-does-not-matter';
const config = {
...base_config,
};
expect(fetch_other_group_members({ author, config })).to.deep.equal([]);
});
it('returns no members when "enable_group_assignment" is false', function() {
const author = 'this-does-not-matter';
const config = {
...base_config,
options: {
enable_group_assignment: false,
},
};
expect(fetch_other_group_members({ author, config })).to.deep.equal([]);
});
context('when "enable_group_assignment" is true', function() {
const config = {
...base_config,
options: {
enable_group_assignment: true,
},
};
it('returns all of the other members of the team if the auther belongs to one team', function() {
const author = 'luigi';
const other_group_members = [ 'mario', 'dr-mario' ];
expect(fetch_other_group_members({ author, config })).to.have.members(other_group_members);
});
it('returns all of the other members of the teams if the author belongs to more than one team', function() {
const author = 'mario';
const other_group_members = [ 'dr-mario', 'luigi', 'wario' ];
expect(fetch_other_group_members({ author, config })).to.have.members(other_group_members);
});
});
});
describe('identify_reviewers_by_changed_files()', function() {
const config = {
reviewers: {
groups: {
'backend-engineers': [ 'mario', 'luigi', 'wario', 'waluigi' ],
'frontend-engineers': [ 'princess-peach' ],
},
},
files: {
'**/super-star': [ 'mario', 'luigi' ],
'backend/**/*': [ 'backend-engineers' ],
'backend/**/some-specific-file': [ 'mario', 'someone-specific' ],
'frontend/**/*': [ 'frontend-engineers', 'toad' ],
},
};
it('returns nothing when config does not have a "files" key', function() {
const changed_files = [ 'THIS DOES NOT MATTER' ];
expect(identify_reviewers_by_changed_files({ config: {}, changed_files })).to.deep.equal([]);
});
it('returns matching reviewers specified as individuals', function() {
const changed_files = [ 'dir/super-star' ];
expect(identify_reviewers_by_changed_files({ config, changed_files })).to.have.members([ 'mario', 'luigi' ]);
});
it('returns matching reviewers specified as groups', function() {
const changed_files = [ 'backend/path/to/file' ];
expect(identify_reviewers_by_changed_files({ config, changed_files })).to.have.members([ 'mario', 'luigi', 'wario', 'waluigi' ]);
});
it('works with a mix of groups and individuals', function() {
const changed_files = [ 'frontend/path/to/file' ];
expect(identify_reviewers_by_changed_files({ config, changed_files })).to.have.members([ 'princess-peach', 'toad' ]);
});
it('dedupes matching reviewers', function() {
const changed_files = [ 'super-star', 'frontend/file', 'backend/file' ];
expect(identify_reviewers_by_changed_files({ config, changed_files })).to.have.members([ 'mario', 'luigi', 'wario', 'waluigi', 'princess-peach', 'toad' ]);
});
it('excludes specified reviewers in the "excludes" option', function() {
const changed_files = [ 'super-star', 'frontend/file', 'backend/file' ];
const excludes = [ 'wario', 'waluigi' ];
expect(identify_reviewers_by_changed_files({ config, changed_files, excludes })).to.have.members([ 'mario', 'luigi', 'princess-peach', 'toad' ]);
});
it('uses the only last matching files-changed pattern with `last_files_match_only` `true` (CODEWONERS-compatible)', function() {
const changed_files = [ 'backend/some-specific-file' ];
const config_with_last_files_match_only = {
...config,
options: {
last_files_match_only: true,
},
};
expect(identify_reviewers_by_changed_files({ config: config_with_last_files_match_only, changed_files })).to.have.members([ 'mario', 'someone-specific' ]);
});
context('with `last_files_match_only` and a specific file override with empty reviewers', function() {
const override_config = {
reviewers: {
groups: {
'team-a': [ 'alice', 'bob' ],
},
},
files: {
'src/**/*': [ 'team-a' ],
'src/generated-file.dat': [],
},
options: {
last_files_match_only: true,
},
};
it('returns reviewers when a file only matches the wildcard pattern', function() {
const changed_files = [ 'src/main-code.js' ];
expect(identify_reviewers_by_changed_files({ config: override_config, changed_files })).to.have.members([ 'alice', 'bob' ]);
});
it('returns no reviewers when a file only matches the overriding empty pattern', function() {
const changed_files = [ 'src/generated-file.dat' ];
expect(identify_reviewers_by_changed_files({ config: override_config, changed_files })).to.deep.equal([]);
});
it('returns no reviewers when all files are unmatched or overridden with empty reviewers', function() {
const changed_files = [ 'unrelated-file.txt', 'src/generated-file.dat' ];
expect(identify_reviewers_by_changed_files({ config: override_config, changed_files })).to.deep.equal([]);
});
it('returns reviewers when one file matches the wildcard and another is overridden with empty reviewers', function() {
const changed_files = [ 'src/main-code.js', 'src/generated-file.dat' ];
expect(identify_reviewers_by_changed_files({ config: override_config, changed_files })).to.have.members([ 'alice', 'bob' ]);
});
});
});
describe('identify_reviewers_by_author()', function() {
const config = {
reviewers: {
groups: {
engineers: [ 'mario', 'luigi', 'wario', 'waluigi' ],
designers: [ 'mario', 'princess-peach', 'princess-daisy' ],
},
per_author: {
engineers: [ 'engineers', 'dr-mario' ],
designers: [ 'designers' ],
yoshi: [ 'mario', 'luige' ],
},
},
};
it('returns nothing when config does not have a "per-author" key', function() {
const author = 'THIS DOES NOT MATTER';
expect(identify_reviewers_by_author({ config: { reviewers: {} }, author })).to.deep.equal([]);
});
it('returns nothing when the author does not exist in the "per-author" settings', function() {
const author = 'toad';
expect(identify_reviewers_by_author({ config, author })).to.deep.equal([]);
});
it('returns the reviewers for the author', function() {
const author = 'yoshi';
expect(identify_reviewers_by_author({ config, author })).to.have.members([ 'mario', 'luige' ]);
});
it('works when a author setting is specified with a group', function() {
const author = 'luigi';
expect(identify_reviewers_by_author({ config, author })).to.have.members([ 'mario', 'wario', 'waluigi', 'dr-mario' ]);
});
it('works when the author belongs to more than one group', function() {
const author = 'mario';
expect(identify_reviewers_by_author({ config, author })).to.have.members([ 'dr-mario', 'luigi', 'wario', 'waluigi', 'princess-peach', 'princess-daisy' ]);
});
});
describe('should_request_review()', function() {
context('when "ignore_keywords" is not supplied', function() {
context('given "ignore_draft" is true', function() {
it('returns false if "is_draft" is true', function() {
const title = 'THIS DOES NOT MATTER';
const config = {
options: {
ignore_draft: true,
},
};
expect(should_request_review({ title, is_draft: true, config })).to.be.false;
});
it('returns true if "is_draft" is false', function() {
const title = 'THIS DOES NOT MATTER';
const config = {
options: {
ignore_draft: true,
},
};
expect(should_request_review({ title, is_draft: false, config })).to.be.true;
});
});
context('given "ignore_draft" is false', function() {
it('returns true if "is_draft" is true', function() {
const title = 'THIS DOES NOT MATTER';
const config = {
options: {
ignore_draft: false,
},
};
expect(should_request_review({ title, is_draft: true, config })).to.be.true;
});
it('returns true if "is_draft" is false', function() {
const title = 'THIS DOES NOT MATTER';
const config = {
options: {
ignore_draft: false,
},
};
expect(should_request_review({ title, is_draft: false, config })).to.be.true;
});
});
});
context('when "ignore_keywords" is supplied', function() {
context('given "ignore_draft" is true', function() {
it('returns false if "is_draft" is true', function() {
const title = 'THIS DOES NOT MATTER';
const config = {
options: {
ignore_draft: true,
ignored_keywords: [],
},
};
expect(should_request_review({ title, is_draft: true, config })).to.be.false;
});
it('returns true if "is_draft" is false', function() {
const title = 'THIS DOES NOT MATTER';
const config = {
options: {
ignore_draft: true,
ignored_keywords: [],
},
};
expect(should_request_review({ title, is_draft: false, config })).to.be.true;
});
});
context('given "ignore_draft" is false', function() {
it('returns false if the given title contains one of "ignored_keywords"', function() {
const title = '[WIP] THIS MATTERS';
const is_draft = true; // This doesn't matter
const config = {
options: {
ignore_draft: false,
ignored_keywords: [ 'WIP' ],
},
};
expect(should_request_review({ title, is_draft, config })).to.be.false;
});
it('returns true if the given title does not contains any of "ignored_keywords"', function() {
const title = 'THIS MATTERS';
const is_draft = true; // This doesn't matter
const config = {
options: {
ignore_draft: false,
ignored_keywords: [ 'I DO NOT EXIST' ],
},
};
expect(should_request_review({ title, is_draft, config })).to.be.true;
});
});
});
it('ignores a draft by default when "ignore_draft" is not supplied', function() {
const config = { ignored_keywords: [] };
expect(should_request_review({ title: 'THIS DOES NOT MATTER', is_draft: true, config })).to.be.false;
expect(should_request_review({ title: 'THIS DOES NOT MATTER', is_draft: false, config })).to.be.true;
});
it('ignores a pull request whose title contains "DO NOT REVIEW" by default when "ignored_keywords" is not supplied', function() {
const config = { ignore_draft: false };
expect(should_request_review({ title: '[DO NOT REVIEW] THIS MATTERS', is_draft: false, config })).to.be.false;
expect(should_request_review({ title: 'THIS MATTERS', is_draft: false, config })).to.be.true;
});
});
describe('fetch_default_reviewers()', function() {
it('fetches the default reviewers', function() {
const config = {
reviewers: {
defaults: [ 'dr-mario', 'mario-brothers' ],
groups: {
'mario-brothers': [ 'mario', 'luigi' ],
},
},
};
expect(fetch_default_reviewers({ config })).to.have.members([ 'dr-mario', 'mario', 'luigi' ]);
});
it('fetches the default reviewers exluding specified ones in the excludes option', function() {
const config = {
reviewers: {
defaults: [ 'dr-mario', 'mario-brothers' ],
groups: {
'mario-brothers': [ 'mario', 'luigi' ],
},
},
};
expect(fetch_default_reviewers({ config, excludes: [ 'luigi' ] })).to.have.members([ 'dr-mario', 'mario' ]);
});
});
describe('randomly_pick_reviewers()', function() {
it('returns all reviewers if the number of reviewers is not set', function() {
const reviewers = [ 'dr-mario', 'mario', 'luigi' ];
const config = {};
expect(randomly_pick_reviewers({ reviewers, config })).to.have.members([ 'dr-mario', 'mario', 'luigi' ]);
});
it('randommly pick up to the number of reviewers', function() {
const reviewers = [ 'dr-mario', 'mario', 'luigi' ];
const config = {
options: {
number_of_reviewers: 2,
},
};
const randomly_picked_reviewers = randomly_pick_reviewers({ reviewers, config });
expect([ 'dr-mario', 'mario', 'luigi' ]).to.include.members(randomly_picked_reviewers);
expect(new Set(randomly_picked_reviewers)).to.have.lengthOf(2);
});
it('returns all reviewers if the number of reviewers is greater than or equal to the given reviewers', function() {
const reviewers = [ 'dr-mario', 'mario', 'luigi' ];
const config = {
options: {
number_of_reviewers: 4,
},
};
expect(randomly_pick_reviewers({ reviewers, config })).to.have.members([ 'dr-mario', 'mario', 'luigi' ]);
});
});
});