|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { createRequire } from "node:module"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import test from "node:test"; |
| 7 | + |
| 8 | +// Execute the actual workflow script so the tests cannot drift from production. |
| 9 | +const workflow = readFileSync( |
| 10 | + new URL("../workflows/pr-risk-jev.yml", import.meta.url), |
| 11 | + "utf8", |
| 12 | +); |
| 13 | +const script = workflow.match(/^ {10}script: \|\n((?: {12}.*\n|\n)+)/m)?.[1]; |
| 14 | +assert.ok(script, "Publish risk label script must exist"); |
| 15 | +const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor; |
| 16 | +const publish = new AsyncFunction( |
| 17 | + "require", |
| 18 | + "github", |
| 19 | + "context", |
| 20 | + "core", |
| 21 | + script, |
| 22 | +); |
| 23 | +const require = createRequire(import.meta.url); |
| 24 | +const repo = { owner: "example", repo: "cli" }; |
| 25 | +const head = "a".repeat(40); |
| 26 | +const base = "b".repeat(40); |
| 27 | + |
| 28 | +async function withPR(options, check) { |
| 29 | + const directory = mkdtempSync(join(tmpdir(), "pr-risk-labels-")); |
| 30 | + const previousDirectory = process.cwd(); |
| 31 | + const previousNumber = process.env.PR_NUMBER; |
| 32 | + const labels = new Set(options.labels ?? ["area: auth", "risk: high"]); |
| 33 | + const repositoryLabels = new Set( |
| 34 | + options.repositoryLabels ?? ["risk: low", "risk: medium", "risk: high"], |
| 35 | + ); |
| 36 | + const writes = []; |
| 37 | + const warnings = []; |
| 38 | + const pr = { |
| 39 | + state: "open", |
| 40 | + head: { sha: head }, |
| 41 | + base: { sha: base }, |
| 42 | + title: "Update behavior", |
| 43 | + body: "Describe this change", |
| 44 | + ...options.pr, |
| 45 | + }; |
| 46 | + const result = { |
| 47 | + repo: "example/cli", |
| 48 | + number: 42, |
| 49 | + status: "ok", |
| 50 | + risk_label: "low", |
| 51 | + snapshot: { source_head_sha: head, source_base_sha: base }, |
| 52 | + ...options.result, |
| 53 | + }; |
| 54 | + const error = (status) => |
| 55 | + Object.assign(new Error(`GitHub HTTP ${status}`), { status }); |
| 56 | + const github = { |
| 57 | + rest: { |
| 58 | + pulls: { |
| 59 | + get: async () => ({ |
| 60 | + data: structuredClone({ |
| 61 | + ...pr, |
| 62 | + labels: [...labels].map((name) => ({ name })), |
| 63 | + }), |
| 64 | + }), |
| 65 | + }, |
| 66 | + issues: { |
| 67 | + getLabel: async ({ name }) => { |
| 68 | + if (!repositoryLabels.has(name)) throw error(404); |
| 69 | + return { data: { name } }; |
| 70 | + }, |
| 71 | + createLabel: async ({ name }) => { |
| 72 | + repositoryLabels.add(name); |
| 73 | + if (options.createRace) throw error(422); |
| 74 | + return { data: { name } }; |
| 75 | + }, |
| 76 | + addLabels: async ({ labels: added }) => { |
| 77 | + writes.push(["add", ...added]); |
| 78 | + for (const label of added) labels.add(label); |
| 79 | + options.afterAdd?.(pr); |
| 80 | + }, |
| 81 | + removeLabel: async ({ name }) => { |
| 82 | + writes.push(["remove", name]); |
| 83 | + if (!labels.delete(name)) throw error(404); |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }; |
| 88 | + try { |
| 89 | + process.chdir(directory); |
| 90 | + process.env.PR_NUMBER = "42"; |
| 91 | + if (!options.missingResult) |
| 92 | + writeFileSync("risk-pr-result.json", JSON.stringify(result)); |
| 93 | + await check({ |
| 94 | + run: () => |
| 95 | + publish( |
| 96 | + require, |
| 97 | + github, |
| 98 | + { repo }, |
| 99 | + { warning: (message) => warnings.push(message), info: () => {} }, |
| 100 | + ), |
| 101 | + labels, |
| 102 | + writes, |
| 103 | + warnings, |
| 104 | + }); |
| 105 | + } finally { |
| 106 | + process.chdir(previousDirectory); |
| 107 | + if (previousNumber === undefined) delete process.env.PR_NUMBER; |
| 108 | + else process.env.PR_NUMBER = previousNumber; |
| 109 | + rmSync(directory, { recursive: true, force: true }); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +for (const risk of ["low", "medium", "high"]) { |
| 114 | + test(`publishes ${risk}, replacing only risk labels`, async () => { |
| 115 | + await withPR( |
| 116 | + { |
| 117 | + result: { risk_label: risk }, |
| 118 | + labels: ["area: auth", "risk: low", "risk: medium", "risk: high"], |
| 119 | + }, |
| 120 | + async ({ run, labels }) => { |
| 121 | + await run(); |
| 122 | + assert.deepEqual([...labels].sort(), ["area: auth", `risk: ${risk}`]); |
| 123 | + }, |
| 124 | + ); |
| 125 | + }); |
| 126 | +} |
| 127 | + |
| 128 | +test("context rejection removes stale risk labels and remains unclassified", async () => { |
| 129 | + await withPR( |
| 130 | + { result: { status: "context_rejected", risk_label: null } }, |
| 131 | + async ({ run, labels, warnings }) => { |
| 132 | + await run(); |
| 133 | + assert.deepEqual([...labels], ["area: auth"]); |
| 134 | + assert.ok(warnings.some((message) => message.includes("unclassified"))); |
| 135 | + }, |
| 136 | + ); |
| 137 | +}); |
| 138 | + |
| 139 | +test("moved or closed PRs are left untouched before publication", async (t) => { |
| 140 | + for (const pr of [ |
| 141 | + { head: { sha: "c".repeat(40) } }, |
| 142 | + { base: { sha: "d".repeat(40) } }, |
| 143 | + { state: "closed" }, |
| 144 | + ]) { |
| 145 | + await t.test(JSON.stringify(pr), async () => { |
| 146 | + await withPR({ pr }, async ({ run, labels, writes }) => { |
| 147 | + await run(); |
| 148 | + assert.deepEqual(writes, []); |
| 149 | + assert.deepEqual([...labels], ["area: auth", "risk: high"]); |
| 150 | + }); |
| 151 | + }); |
| 152 | + } |
| 153 | +}); |
| 154 | + |
| 155 | +test("movement or metadata edits during publication roll back the verdict", async (t) => { |
| 156 | + for (const [name, afterAdd] of [ |
| 157 | + [ |
| 158 | + "head", |
| 159 | + (pr) => { |
| 160 | + pr.head.sha = "c".repeat(40); |
| 161 | + }, |
| 162 | + ], |
| 163 | + [ |
| 164 | + "title", |
| 165 | + (pr) => { |
| 166 | + pr.title = "A different change"; |
| 167 | + }, |
| 168 | + ], |
| 169 | + [ |
| 170 | + "closed", |
| 171 | + (pr) => { |
| 172 | + pr.state = "closed"; |
| 173 | + }, |
| 174 | + ], |
| 175 | + ]) { |
| 176 | + await t.test(name, async () => { |
| 177 | + await withPR({ afterAdd }, async ({ run, labels }) => { |
| 178 | + await run(); |
| 179 | + assert.deepEqual([...labels], ["area: auth"]); |
| 180 | + }); |
| 181 | + }); |
| 182 | + } |
| 183 | +}); |
| 184 | + |
| 185 | +test("results for another PR or an invalid risk cannot publish", async (t) => { |
| 186 | + for (const result of [ |
| 187 | + { repo: "example/another" }, |
| 188 | + { number: 43 }, |
| 189 | + { risk_label: "critical" }, |
| 190 | + { risk_label: "toString" }, |
| 191 | + ]) { |
| 192 | + await t.test(JSON.stringify(result), async () => { |
| 193 | + await withPR({ result }, async ({ run, writes }) => { |
| 194 | + await assert.rejects(run, /does not match/); |
| 195 | + assert.deepEqual(writes, []); |
| 196 | + }); |
| 197 | + }); |
| 198 | + } |
| 199 | +}); |
| 200 | + |
| 201 | +test("a missing result does not publish or remove labels", async () => { |
| 202 | + await withPR({ missingResult: true }, async ({ run, writes, warnings }) => { |
| 203 | + await run(); |
| 204 | + assert.deepEqual(writes, []); |
| 205 | + assert.equal(warnings.length, 1); |
| 206 | + }); |
| 207 | +}); |
| 208 | + |
| 209 | +test("concurrent repository label creation still publishes", async () => { |
| 210 | + await withPR( |
| 211 | + { repositoryLabels: [], createRace: true }, |
| 212 | + async ({ run, labels }) => { |
| 213 | + await run(); |
| 214 | + assert.deepEqual([...labels], ["area: auth", "risk: low"]); |
| 215 | + }, |
| 216 | + ); |
| 217 | +}); |
| 218 | + |
| 219 | +for (const risk of ["low", "medium", "high"]) { |
| 220 | + test(`unchanged ${risk} leaves the PR timeline untouched`, async () => { |
| 221 | + await withPR( |
| 222 | + { result: { risk_label: risk }, labels: ["area: auth", `risk: ${risk}`] }, |
| 223 | + async ({ run, labels, writes }) => { |
| 224 | + await run(); |
| 225 | + assert.deepEqual(writes, []); |
| 226 | + assert.deepEqual([...labels], ["area: auth", `risk: ${risk}`]); |
| 227 | + }, |
| 228 | + ); |
| 229 | + }); |
| 230 | +} |
| 231 | + |
| 232 | +test("an existing verdict removes competing risk labels without re-adding itself", async () => { |
| 233 | + await withPR( |
| 234 | + { |
| 235 | + result: { risk_label: "medium" }, |
| 236 | + labels: ["area: auth", "risk: medium", "risk: high"], |
| 237 | + }, |
| 238 | + async ({ run, labels, writes }) => { |
| 239 | + await run(); |
| 240 | + assert.deepEqual(writes, [["remove", "risk: high"]]); |
| 241 | + assert.deepEqual([...labels], ["area: auth", "risk: medium"]); |
| 242 | + }, |
| 243 | + ); |
| 244 | +}); |
| 245 | + |
| 246 | +test("a second identical publication makes no label mutations", async () => { |
| 247 | + await withPR({}, async ({ run, labels, writes }) => { |
| 248 | + await run(); |
| 249 | + assert.deepEqual([...labels], ["area: auth", "risk: low"]); |
| 250 | + writes.length = 0; |
| 251 | + await run(); |
| 252 | + assert.deepEqual(writes, []); |
| 253 | + assert.deepEqual([...labels], ["area: auth", "risk: low"]); |
| 254 | + }); |
| 255 | +}); |
0 commit comments