-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapproval.test.js
183 lines (161 loc) · 6.66 KB
/
approval.test.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
const approval = require('./approval');
const owners = require('./owners');
describe('PR can be merged', () => {
let labels;
let author;
let maintainers;
let reviewers;
let approvers;
let minApprovingReviewsTotal;
let minApprovingReviewsPerArea;
let requireAreaLabel;
let succeedIfMaintainerApproves;
let failIfNotEnoughAvailableApproversPerArea;
let requestReviewsFromMaintainersIfNeeded;
let maintainersAreUniversalApprovers;
function canBeMerged(approvals) {
const areaReviewers = new Map(reviewers);
const areaApprovers = new Map(approvers);
const config = {
minApprovingReviewsTotal: minApprovingReviewsTotal,
minApprovingReviewsPerArea: minApprovingReviewsPerArea,
maintainers: maintainers,
areaReviewers: areaReviewers,
areaApprovers: areaApprovers,
areaReviewersRegexList: owners.buildRegexList(areaReviewers),
areaApproversRegexList: owners.buildRegexList(areaApprovers),
requireAreaLabel: requireAreaLabel,
succeedIfMaintainerApproves: succeedIfMaintainerApproves,
failIfNotEnoughAvailableApproversPerArea: failIfNotEnoughAvailableApproversPerArea,
requestReviewsFromMaintainersIfNeeded: requestReviewsFromMaintainersIfNeeded,
maintainersAreUniversalApprovers: maintainersAreUniversalApprovers,
};
return approval.canBeMerged(labels, author, approvals, config);
}
beforeAll(() => {
// silence all console logs, can be commented-out if needed for debugging
console.log = jest.fn();
console.warn = jest.fn();
});
beforeEach(() => {
labels = ['documentation'];
author = "alice";
reviewers = [];
approvers = [['documentation', ['alice', 'bob']], ['foo', ['bob', 'mike', 'joe']]];
maintainers = [];
minApprovingReviewsTotal = 2;
minApprovingReviewsPerArea = 1;
requireAreaLabel = true;
succeedIfMaintainerApproves = false;
requestReviewsFromMaintainersIfNeeded = false;
failIfNotEnoughAvailableApproversPerArea = false;
maintainersAreUniversalApprovers = false;
});
test('no approval', async () => {
const approvals = new Set();
expect(canBeMerged(approvals)).toBeFalsy();
});
test('not enough approvals', async () => {
const approvals = new Set(['alice']);
expect(canBeMerged(approvals)).toBeFalsy();
});
test('missing area approval', async () => {
labels = ['documentation', 'foo'];
const approvals = new Set(['mike', 'joe']);
expect(canBeMerged(approvals)).toBeFalsy();
});
test('no area label', async () => {
labels = [];
const approvals = new Set(['alice', 'bob']);
expect(canBeMerged(approvals)).toBeFalsy();
});
test('accept no area label', async () => {
labels = [];
requireAreaLabel = false;
const approvals = new Set(['alice', 'joe']);
expect(canBeMerged(approvals)).toBeTruthy();
});
test('approved documentation', async () => {
labels = ['documentation'];
const approvals = new Set(['bob', 'joe']);
expect(canBeMerged(approvals)).toBeTruthy();
});
test('approved foo', async () => {
labels = ['foo'];
const approvals = new Set(['joe', 'mike']);
expect(canBeMerged(approvals)).toBeTruthy();
});
test('approved with 2 labels', async () => {
labels = ['foo', 'documentation'];
const approvals = new Set(['bob', 'fred']); // fred is not an approver for any specific area
expect(canBeMerged(approvals)).toBeTruthy();
});
test('maintainer bypass', async () => {
maintainers = ['alice'];
succeedIfMaintainerApproves = true;
const approvals = new Set(['alice']);
expect(canBeMerged(approvals)).toBeTruthy();
});
test('not enough approvers for area - default', async () => {
minApprovingReviewsPerArea = 2;
approvers = [['documentation', ['alice']]];
const approvals = new Set(['alice', 'bob']);
expect(canBeMerged(approvals)).toBeTruthy();
});
test('no approvers for area - default', async () => {
approvers = [['documentation', []]];
const approvals = new Set(['alice', 'bob']);
expect(canBeMerged(approvals)).toBeTruthy();
});
test('not enough approvers for area - fail', async () => {
failIfNotEnoughAvailableApproversPerArea = true;
approvers = [['documentation', []]];
const approvals = new Set(['alice', 'bob']);
expect(canBeMerged(approvals)).toBeFalsy();
});
test('regex', async() => {
approvers = [['doc*', ['alice', 'bob']]];
const approvals = new Set(['alice', 'bob']);
expect(canBeMerged(approvals)).toBeTruthy();
});
test('regex precedence', async() => {
approvers = [['documentation', ['mike']], ['doc*', ['alice', 'bob']]];
const approvals = new Set(['alice', 'bob']);
// exact match takes precedence, review from mike is needed
expect(canBeMerged(approvals)).toBeFalsy();
});
test('request reviews from maintainers if needed - default', async() => {
maintainers = ['alice', 'mike'];
failIfNotEnoughAvailableApproversPerArea = true;
reviewers = [['documentation', ['bob']]];
approvers = [];
const approvals = new Set(['alice', 'mike']);
expect(canBeMerged(approvals)).toBeFalsy();
});
test('request reviews from maintainers if needed - true', async() => {
maintainers = ['alice', 'mike'];
failIfNotEnoughAvailableApproversPerArea = true;
requestReviewsFromMaintainersIfNeeded = true;
reviewers = [['documentation', ['bob']]];
approvers = [];
const approvals = new Set(['alice', 'mike']);
expect(canBeMerged(approvals)).toBeTruthy();
});
test('maintainers are universal approvers - default', async() => {
maintainers = ['alice', 'mike'];
failIfNotEnoughAvailableApproversPerArea = true;
reviewers = [['documentation', ['bob']]];
approvers = [];
const approvals = new Set(['bob', 'alice', 'mike']);
expect(canBeMerged(approvals)).toBeFalsy();
});
test('maintainers are universal approvers - true', async() => {
maintainers = ['alice', 'mike'];
failIfNotEnoughAvailableApproversPerArea = true;
maintainersAreUniversalApprovers = true;
reviewers = [['documentation', ['bob']]];
approvers = [];
const approvals = new Set(['alice', 'mike']);
expect(canBeMerged(approvals)).toBeTruthy();
});
});