Repository navigation
Expand file tree
/
Copy pathrules.test.ts
More file actions
369 lines (327 loc) · 13.2 KB
/
Copy pathrules.test.ts
File metadata and controls
369 lines (327 loc) · 13.2 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
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
computeIssuePatch,
computePrPatch,
linkedIssueNumbers,
BOUNTY_GENERIC,
BOUNTY_CLAIMED,
BOUNTY_REWARDED,
} from "../src/rules.js";
import type { Issue, PullRequest, IssueState, PrState } from "../src/types.js";
// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------
function makeIssue(overrides: Partial<Issue> & { number: number }): Issue {
return {
title: "Test issue",
html_url: `https://github.com/owner/repo/issues/${overrides.number}`,
state: "open",
labels: [],
assignees: [],
...overrides,
};
}
function makePr(overrides: Partial<PullRequest> & { number: number }): PullRequest {
return {
title: "Test PR",
state: "open",
merged: false,
body: null,
labels: [],
user: { login: "contributor" },
html_url: `https://github.com/owner/repo/pull/${overrides.number}`,
...overrides,
};
}
function labelNames(...names: string[]): { name: string }[] {
return names.map((name) => ({ name }));
}
function issueState(issue: Issue): IssueState {
return { issue, currentLabels: new Set(issue.labels.map((l) => l.name)) };
}
// ---------------------------------------------------------------------------
// computeIssuePatch — issue rules
// ---------------------------------------------------------------------------
describe("computeIssuePatch", () => {
describe("no bounty label", () => {
it("produces no patch when issue has no bounty labels at all", () => {
const issue = makeIssue({ number: 1, labels: labelNames("type: bug") });
const patch = computeIssuePatch(issueState(issue));
assert.deepEqual(patch.ops, []);
});
it("removes generic bounty label when no value label present", () => {
const issue = makeIssue({ number: 1, labels: labelNames(BOUNTY_GENERIC) });
const patch = computeIssuePatch(issueState(issue));
assert.equal(patch.ops.length, 1);
assert.deepEqual(patch.ops[0]!.remove, [BOUNTY_GENERIC]);
assert.deepEqual(patch.ops[0]!.add, []);
});
it("removes claimed label when no value label present", () => {
const issue = makeIssue({
number: 1,
labels: labelNames(BOUNTY_CLAIMED),
assignees: [{ login: "alice" }],
});
const patch = computeIssuePatch(issueState(issue));
assert.equal(patch.ops.length, 1);
assert.deepEqual(patch.ops[0]!.remove, [BOUNTY_CLAIMED]);
});
});
describe("has value label, no assignees", () => {
it("adds generic label when missing", () => {
const issue = makeIssue({ number: 1, labels: labelNames("bounty: 💰 $500") });
const patch = computeIssuePatch(issueState(issue));
assert.equal(patch.ops.length, 1);
assert.deepEqual(patch.ops[0]!.add, [BOUNTY_GENERIC]);
assert.deepEqual(patch.ops[0]!.remove, []);
});
it("recognises legacy label format (no emoji) as a value label", () => {
const issue = makeIssue({ number: 1, labels: labelNames("bounty: $300") });
const patch = computeIssuePatch(issueState(issue));
assert.equal(patch.ops.length, 1);
assert.deepEqual(patch.ops[0]!.add, [BOUNTY_GENERIC]);
assert.deepEqual(patch.ops[0]!.remove, []);
});
it("produces no patch when generic already present and no assignees", () => {
const issue = makeIssue({
number: 1,
labels: labelNames("bounty: 💰 $500", BOUNTY_GENERIC),
});
const patch = computeIssuePatch(issueState(issue));
assert.deepEqual(patch.ops, []);
});
it("removes claimed when no assignees", () => {
const issue = makeIssue({
number: 1,
labels: labelNames("bounty: 💰 $500", BOUNTY_GENERIC, BOUNTY_CLAIMED),
assignees: [],
});
const patch = computeIssuePatch(issueState(issue));
assert.equal(patch.ops.length, 1);
assert.deepEqual(patch.ops[0]!.remove, [BOUNTY_CLAIMED]);
assert.deepEqual(patch.ops[0]!.add, []);
});
});
describe("has value label and assignees", () => {
it("adds both generic and claimed when both missing", () => {
const issue = makeIssue({
number: 1,
labels: labelNames("bounty: 💰 $300"),
assignees: [{ login: "alice" }],
});
const patch = computeIssuePatch(issueState(issue));
assert.equal(patch.ops.length, 1);
assert.deepEqual(patch.ops[0]!.add.sort(), [BOUNTY_CLAIMED, BOUNTY_GENERIC].sort());
assert.deepEqual(patch.ops[0]!.remove, []);
});
it("produces no patch when generic and claimed both already present", () => {
const issue = makeIssue({
number: 1,
labels: labelNames("bounty: 💰 $300", BOUNTY_GENERIC, BOUNTY_CLAIMED),
assignees: [{ login: "alice" }],
});
const patch = computeIssuePatch(issueState(issue));
assert.deepEqual(patch.ops, []);
});
it("preserves non-bounty labels untouched", () => {
const issue = makeIssue({
number: 1,
labels: labelNames("type: bug", "bounty: 💰 $100", BOUNTY_GENERIC, BOUNTY_CLAIMED),
assignees: [{ login: "alice" }],
});
const patch = computeIssuePatch(issueState(issue));
assert.deepEqual(patch.ops, []);
});
});
describe("rewarded state", () => {
it("leaves rewarded label alone (owned by PR sync)", () => {
const issue = makeIssue({
number: 1,
labels: labelNames("bounty: 💰 $500", BOUNTY_GENERIC, BOUNTY_REWARDED),
assignees: [],
});
const patch = computeIssuePatch(issueState(issue));
// rewarded is not touched; only generic should be kept since value label present
assert.deepEqual(patch.ops, []);
});
});
});
// ---------------------------------------------------------------------------
// linkedIssueNumbers
// ---------------------------------------------------------------------------
describe("linkedIssueNumbers", () => {
it("returns empty array for null body", () => {
assert.deepEqual(linkedIssueNumbers(null), []);
});
it("parses closes/fixes/resolves keywords", () => {
const body = "Closes #10\nFixes #20\nResolves #30";
assert.deepEqual(linkedIssueNumbers(body), [10, 20, 30]);
});
it("is case-insensitive", () => {
const body = "CLOSES #5 fixes #6 RESOLVES #7";
assert.deepEqual(linkedIssueNumbers(body), [5, 6, 7]);
});
it("handles plural forms: close/fix/resolve", () => {
const body = "close #1 fix #2 resolve #3";
assert.deepEqual(linkedIssueNumbers(body), [1, 2, 3]);
});
it("returns empty when no keywords present", () => {
assert.deepEqual(linkedIssueNumbers("See issue #99 for context."), []);
});
});
// ---------------------------------------------------------------------------
// computePrPatch — PR rules
// ---------------------------------------------------------------------------
describe("computePrPatch", () => {
describe("label propagation (pre-merge)", () => {
it("produces no patch when PR body has no linked issues", () => {
const pr = makePr({ number: 10, body: "No references here." });
const patch = computePrPatch({ pr, currentLabels: new Set(), linkedIssues: [] });
assert.deepEqual(patch.ops, []);
});
it("produces no patch when linked issues have no bounty labels", () => {
const pr = makePr({ number: 10, body: "Closes #1" });
const issue = makeIssue({ number: 1, labels: labelNames("type: bug") });
const patch = computePrPatch({ pr, currentLabels: new Set(), linkedIssues: [issue] });
assert.deepEqual(patch.ops, []);
});
it("copies bounty value label from linked issue to PR", () => {
const pr = makePr({ number: 10, body: "Closes #1" });
const issue = makeIssue({ number: 1, labels: labelNames("bounty: 💰 $500") });
const patch = computePrPatch({ pr, currentLabels: new Set(), linkedIssues: [issue] });
const prOp = patch.ops.find((o) => o.target === 10);
assert.ok(prOp);
assert.deepEqual(prOp.add, ["bounty: 💰 $500"]);
assert.deepEqual(prOp.remove, []);
});
it("does not duplicate a value label already on the PR", () => {
const pr = makePr({ number: 10, body: "Closes #1" });
const issue = makeIssue({ number: 1, labels: labelNames("bounty: 💰 $500") });
const patch = computePrPatch({
pr,
currentLabels: new Set(["bounty: 💰 $500"]),
linkedIssues: [issue],
});
assert.deepEqual(patch.ops, []);
});
it("posts a comment on the linked issue when label is propagated", () => {
const pr = makePr({ number: 10, body: "Closes #1" });
const issue = makeIssue({ number: 1, labels: labelNames("bounty: 💰 $200") });
const patch = computePrPatch({ pr, currentLabels: new Set(), linkedIssues: [issue] });
const issueOp = patch.ops.find((o) => o.target === 1);
assert.ok(issueOp);
assert.ok(issueOp.comment?.includes("PR"));
assert.ok(issueOp.comment?.includes("#10"));
assert.ok(issueOp.comment?.includes("contributor"));
});
it("does not post a comment when label was already on the PR", () => {
const pr = makePr({ number: 10, body: "Closes #1" });
const issue = makeIssue({ number: 1, labels: labelNames("bounty: 💰 $200") });
const patch = computePrPatch({
pr,
currentLabels: new Set(["bounty: 💰 $200"]),
linkedIssues: [issue],
});
const issueOp = patch.ops.find((o) => o.target === 1);
assert.ok(!issueOp);
});
it("merges value labels from multiple linked issues onto the PR", () => {
const pr = makePr({ number: 10, body: "Closes #1\nCloses #2" });
const issue1 = makeIssue({ number: 1, labels: labelNames("bounty: 💰 $100") });
const issue2 = makeIssue({ number: 2, labels: labelNames("bounty: 💰 $200") });
const patch = computePrPatch({
pr,
currentLabels: new Set(),
linkedIssues: [issue1, issue2],
});
const prOp = patch.ops.find((o) => o.target === 10);
assert.ok(prOp);
assert.deepEqual(prOp.add.sort(), ["bounty: 💰 $100", "bounty: 💰 $200"].sort());
});
});
describe("rewarded on merge", () => {
it("produces no patch when PR was closed but not merged", () => {
const pr = makePr({ number: 10, state: "closed", merged: false, body: "Closes #1" });
const issue = makeIssue({ number: 1, labels: labelNames("bounty: 💰 $500") });
const patch = computePrPatch({
pr,
currentLabels: new Set(["bounty: 💰 $500"]),
linkedIssues: [issue],
});
const prOp = patch.ops.find((o) => o.target === 10);
assert.ok(!prOp?.add.includes(BOUNTY_REWARDED));
});
it("adds rewarded to PR on merge", () => {
const pr = makePr({
number: 10,
merged: true,
body: "Closes #1",
labels: labelNames("bounty: 💰 $500"),
});
const issue = makeIssue({ number: 1, labels: labelNames("bounty: 💰 $500") });
const patch = computePrPatch({
pr,
currentLabels: new Set(["bounty: 💰 $500"]),
linkedIssues: [issue],
});
const prOp = patch.ops.find((o) => o.target === 10);
assert.ok(prOp);
assert.ok(prOp.add.includes(BOUNTY_REWARDED));
});
it("adds rewarded to linked issue on merge", () => {
const pr = makePr({
number: 10,
merged: true,
body: "Closes #1",
labels: labelNames("bounty: 💰 $500"),
});
const issue = makeIssue({ number: 1, labels: labelNames("bounty: 💰 $500", BOUNTY_CLAIMED) });
const patch = computePrPatch({
pr,
currentLabels: new Set(["bounty: 💰 $500"]),
linkedIssues: [issue],
});
const issueOp = patch.ops.find((o) => o.target === 1);
assert.ok(issueOp);
assert.ok(issueOp.add.includes(BOUNTY_REWARDED));
assert.ok(issueOp.remove.includes(BOUNTY_CLAIMED));
});
it("does not add rewarded again when already present", () => {
const pr = makePr({
number: 10,
merged: true,
body: "Closes #1",
labels: labelNames("bounty: 💰 $500", BOUNTY_REWARDED),
});
const issue = makeIssue({
number: 1,
labels: labelNames("bounty: 💰 $500", BOUNTY_REWARDED),
});
const patch = computePrPatch({
pr,
currentLabels: new Set(["bounty: 💰 $500", BOUNTY_REWARDED]),
linkedIssues: [issue],
});
assert.deepEqual(patch.ops, []);
});
it("skips issues with no bounty label when merged PR has a bounty", () => {
const pr = makePr({
number: 10,
merged: true,
body: "Closes #1\nCloses #2",
labels: labelNames("bounty: 💰 $500"),
});
const issue1 = makeIssue({ number: 1, labels: labelNames("bounty: 💰 $500") });
const issue2 = makeIssue({ number: 2, labels: labelNames("type: bug") });
const patch = computePrPatch({
pr,
currentLabels: new Set(["bounty: 💰 $500"]),
linkedIssues: [issue1, issue2],
});
// issue2 has no bounty so its desired state equals current: no op
const issue2Op = patch.ops.find((o) => o.target === 2);
assert.ok(!issue2Op);
});
});
});