{{ message }}
Repository navigation
Expand file tree
/
Copy pathcontribution-gate.test.ts
More file actions
288 lines (262 loc) · 9.37 KB
/
Copy pathcontribution-gate.test.ts
File metadata and controls
288 lines (262 loc) · 9.37 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
import { afterEach, describe, expect, spyOn, test } from "bun:test";
import {
evaluateAllOpenPrs,
evaluateGate,
fetchAuthorPermission,
GATE_LABEL,
type GateIo,
isInternalAuthor,
type LinkedIssue,
type OpenPr,
} from "./contribution-gate.ts";
const REPO = "supabase/cli";
describe("isInternalAuthor", () => {
test.each(["OWNER", "MEMBER", "COLLABORATOR"])(
"treats internal association %s as internal without a permission lookup",
(authorAssociation) => {
expect(isInternalAuthor(authorAssociation, undefined)).toBe(true);
},
);
test.each(["admin", "write"])("treats push-capable permission %s as internal", (permission) => {
expect(isInternalAuthor("CONTRIBUTOR", permission)).toBe(true);
expect(isInternalAuthor("NONE", permission)).toBe(true);
});
test.each(["read", "none", undefined])(
"treats external author with permission %s as external",
(permission) => {
expect(isInternalAuthor("CONTRIBUTOR", permission)).toBe(false);
expect(isInternalAuthor("NONE", permission)).toBe(false);
},
);
});
describe("evaluateGate", () => {
test("skips bot authors", () => {
const result = evaluateGate({
repository: REPO,
isInternal: false,
isBot: true,
linkedIssues: [],
});
expect(result.pass).toBe(true);
expect(result.reason).toBe("bot");
});
test("skips internal authors", () => {
const result = evaluateGate({
repository: REPO,
isInternal: true,
isBot: false,
linkedIssues: [],
});
expect(result.pass).toBe(true);
expect(result.reason).toBe("internal");
});
test("fails when no issue is linked", () => {
const result = evaluateGate({
repository: REPO,
isInternal: false,
isBot: false,
linkedIssues: [],
});
expect(result.pass).toBe(false);
expect(result.reason).toBe("no-linked-issue");
expect(result.message).toContain(GATE_LABEL);
expect(result.message).toContain("CONTRIBUTING.md");
});
test("fails when the linked issue is open but not labeled", () => {
const result = evaluateGate({
repository: REPO,
isInternal: false,
isBot: false,
linkedIssues: [{ repository: REPO, number: 12, state: "OPEN", labels: ["🐛 Bug"] }],
});
expect(result.pass).toBe(false);
expect(result.reason).toBe("missing-label");
expect(result.message).toContain(GATE_LABEL);
});
test("fails when the only labeled issue is closed", () => {
const result = evaluateGate({
repository: REPO,
isInternal: false,
isBot: false,
linkedIssues: [{ repository: REPO, number: 7, state: "CLOSED", labels: [GATE_LABEL] }],
});
expect(result.pass).toBe(false);
expect(result.reason).toBe("issue-closed");
});
test("passes when a linked issue is open and carries the gate label", () => {
const result = evaluateGate({
repository: REPO,
isInternal: false,
isBot: false,
linkedIssues: [
{
repository: REPO,
number: 42,
state: "OPEN",
labels: [GATE_LABEL, "🐛 Bug"],
},
],
});
expect(result.pass).toBe(true);
expect(result.reason).toBe("ok");
});
test("passes when any one of several linked issues qualifies", () => {
const result = evaluateGate({
repository: REPO,
isInternal: false,
isBot: false,
linkedIssues: [
{ repository: REPO, number: 1, state: "CLOSED", labels: [GATE_LABEL] },
{ repository: REPO, number: 2, state: "OPEN", labels: ["✨ Feature"] },
{ repository: REPO, number: 3, state: "OPEN", labels: [GATE_LABEL] },
],
});
expect(result.pass).toBe(true);
expect(result.reason).toBe("ok");
});
test("ignores an open, labeled issue from a different repository", () => {
const result = evaluateGate({
repository: REPO,
isInternal: false,
isBot: false,
linkedIssues: [
{
repository: "attacker/repo",
number: 1,
state: "OPEN",
labels: [GATE_LABEL],
},
],
});
expect(result.pass).toBe(false);
expect(result.reason).toBe("no-linked-issue");
});
test("matches the repository case-insensitively", () => {
const result = evaluateGate({
repository: REPO,
isInternal: false,
isBot: false,
linkedIssues: [
{
repository: "Supabase/CLI",
number: 5,
state: "OPEN",
labels: [GATE_LABEL],
},
],
});
expect(result.pass).toBe(true);
expect(result.reason).toBe("ok");
});
});
describe("evaluateAllOpenPrs", () => {
function makeIo(
openPrs: OpenPr[],
linkedByPr: Record<number, LinkedIssue[]>,
permissionByLogin: Record<string, string> = {},
): {
io: GateIo;
closed: Array<{ number: number; message: string }>;
permissionLookups: string[];
} {
const closed: Array<{ number: number; message: string }> = [];
const permissionLookups: string[] = [];
const io: GateIo = {
listOpenPrs: () => Promise.resolve(openPrs),
fetchPermission: (login) => {
permissionLookups.push(login);
return Promise.resolve(permissionByLogin[login]);
},
fetchLinkedIssues: (prNumber) => Promise.resolve(linkedByPr[prNumber] ?? []),
closePr: (prNumber, message) => {
closed.push({ number: prNumber, message });
return Promise.resolve();
},
};
return { io, closed, permissionLookups };
}
test("closes only non-conforming external PRs and leaves the rest", async () => {
const { io, closed, permissionLookups } = makeIo(
[
// no issue -> close
{ number: 1, authorLogin: "ext-a", authorAssociation: "NONE", isBot: false },
// public member -> skip (no permission lookup needed)
{ number: 2, authorLogin: "maint", authorAssociation: "MEMBER", isBot: false },
// bot -> skip
{ number: 3, authorLogin: "dependabot", authorAssociation: "NONE", isBot: true },
// conforming external -> keep
{ number: 4, authorLogin: "ext-b", authorAssociation: "CONTRIBUTOR", isBot: false },
// missing label -> close
{ number: 5, authorLogin: "ext-c", authorAssociation: "NONE", isBot: false },
// private-membership maintainer (CONTRIBUTOR + write access) -> skip
{ number: 6, authorLogin: "hidden-maint", authorAssociation: "CONTRIBUTOR", isBot: false },
],
{
4: [{ repository: REPO, number: 40, state: "OPEN", labels: [GATE_LABEL] }],
5: [{ repository: REPO, number: 50, state: "OPEN", labels: ["🐛 Bug"] }],
},
{ "hidden-maint": "write" },
);
const entries = await evaluateAllOpenPrs(io, REPO);
expect(closed.map((c) => c.number).sort((a, b) => a - b)).toEqual([1, 5]);
const byNumber = Object.fromEntries(entries.map((entry) => [entry.number, entry.result]));
expect(byNumber[1]?.reason).toBe("no-linked-issue");
expect(byNumber[2]?.pass).toBe(true);
expect(byNumber[2]?.reason).toBe("internal");
expect(byNumber[3]?.reason).toBe("bot");
expect(byNumber[4]?.pass).toBe(true);
expect(byNumber[5]?.reason).toBe("missing-label");
expect(byNumber[6]?.pass).toBe(true);
expect(byNumber[6]?.reason).toBe("internal");
expect(closed.find((c) => c.number === 1)?.message).toContain(GATE_LABEL);
expect(permissionLookups).not.toContain("maint");
expect(permissionLookups).not.toContain("dependabot");
});
test("returns an entry per PR and closes none when all conform", async () => {
const { io, closed } = makeIo(
[{ number: 9, authorLogin: "ext", authorAssociation: "NONE", isBot: false }],
{
9: [{ repository: REPO, number: 90, state: "OPEN", labels: [GATE_LABEL] }],
},
);
const entries = await evaluateAllOpenPrs(io, REPO);
expect(entries).toHaveLength(1);
expect(closed).toHaveLength(0);
expect(entries[0]?.result.pass).toBe(true);
});
});
describe("fetchAuthorPermission", () => {
const fetchSpy = spyOn(globalThis, "fetch");
afterEach(() => {
fetchSpy.mockReset();
});
function stubFetch(status: number, body: unknown): void {
// `typeof fetch` carries a static `preconnect`, so attach a no-op to keep
// the implementation assignable without casting.
fetchSpy.mockImplementation(
Object.assign(() => Promise.resolve(new Response(JSON.stringify(body), { status })), {
preconnect: () => {},
}),
);
}
test("returns the effective permission for a collaborator", async () => {
stubFetch(200, { permission: "admin" });
const permission = await fetchAuthorPermission("t", "supabase", "cli", "maint");
expect(permission).toBe("admin");
});
test("maps a 404 (non-collaborator fork author) to undefined", async () => {
stubFetch(404, { message: "Not Found" });
const permission = await fetchAuthorPermission("t", "supabase", "cli", "ext");
expect(permission).toBeUndefined();
});
test("throws on other API failures so the run aborts without closing PRs", async () => {
stubFetch(403, { message: "Forbidden" });
await expect(fetchAuthorPermission("t", "supabase", "cli", "ext")).rejects.toThrow(/403/);
});
test("skips the network call for a blank login", async () => {
stubFetch(200, { permission: "admin" });
const permission = await fetchAuthorPermission("t", "supabase", "cli", "");
expect(permission).toBeUndefined();
expect(fetchSpy).not.toHaveBeenCalled();
});
});
You can’t perform that action at this time.
