|
6 | 6 | addedLines, |
7 | 7 | assessHygiene, |
8 | 8 | collectDeterministicHygieneFailures, |
| 9 | + countHeadRewrites, |
| 10 | + formatApprovalRecord, |
| 11 | + HEAD_BOUND_APPROVAL_LABELS, |
9 | 12 | HYGIENE_FAILURE_HINTS, |
10 | 13 | HYGIENE_GATE_LABELS, |
11 | 14 | hasEmptyCatch, |
| 15 | + needsHeadRewriteCount, |
| 16 | + parseApprovalRecord, |
| 17 | + reconcileHeadBoundApprovals, |
12 | 18 | resultLines, |
13 | 19 | resultLinesByHunk, |
14 | 20 | } = require("./pr-hygiene.cjs"); |
@@ -391,3 +397,100 @@ describe("pr-hygiene workflow trust boundary", () => { |
391 | 397 | ); |
392 | 398 | }); |
393 | 399 | }); |
| 400 | + |
| 401 | +describe("head-bound approvals", () => { |
| 402 | + const HEAD = "c".repeat(40); |
| 403 | + const OTHER = "d".repeat(40); |
| 404 | + const record = (head, headRewrites) => |
| 405 | + "<!-- pr-hygiene-approvals " + JSON.stringify({ head, headRewrites }) + " -->"; |
| 406 | + |
| 407 | + it("covers exactly the exception approvals, not sponsorship", () => { |
| 408 | + assert.deepEqual( |
| 409 | + [...HEAD_BOUND_APPROVAL_LABELS].sort(), |
| 410 | + HYGIENE_GATE_LABELS.filter( |
| 411 | + (label) => label !== "intake: hygiene-blocked" && label !== "maintainer-sponsored", |
| 412 | + ).sort(), |
| 413 | + ); |
| 414 | + }); |
| 415 | + |
| 416 | + it("round-trips a record, including an unknown rewrite count", () => { |
| 417 | + const line = formatApprovalRecord({ head: HEAD, headRewrites: 2 }); |
| 418 | + assert.equal(line, record(HEAD, 2)); |
| 419 | + assert.deepEqual(parseApprovalRecord("status\n\n" + line), { head: HEAD, headRewrites: 2 }); |
| 420 | + assert.deepEqual(parseApprovalRecord(formatApprovalRecord({ head: HEAD, headRewrites: undefined })), { |
| 421 | + head: HEAD, |
| 422 | + headRewrites: null, |
| 423 | + }); |
| 424 | + assert.deepEqual(parseApprovalRecord(record("e".repeat(64), 0)), { head: "e".repeat(64), headRewrites: 0 }); |
| 425 | + }); |
| 426 | + |
| 427 | + it("rejects anything but one well-formed record on the last line", () => { |
| 428 | + for (const source of [ |
| 429 | + null, |
| 430 | + "", |
| 431 | + record(HEAD, 0) + "\nlater line", |
| 432 | + record(HEAD, 0) + "\n" + record(HEAD, 0), |
| 433 | + "mentions pr-hygiene-approvals elsewhere\n" + record(HEAD, 0), |
| 434 | + "<!-- pr-hygiene-approvals {not json} -->", |
| 435 | + "<!-- pr-hygiene-approvals " + JSON.stringify({ head: HEAD, headRewrites: 0, approvals: [] }) + " -->", |
| 436 | + "<!-- pr-hygiene-approvals " + JSON.stringify({ head: HEAD }) + " -->", |
| 437 | + "<!-- pr-hygiene-approvals [1] -->", |
| 438 | + record("abc", 0), |
| 439 | + record(HEAD.toUpperCase(), 0), |
| 440 | + record(HEAD, -1), |
| 441 | + record(HEAD, 1.5), |
| 442 | + record(HEAD, "1"), |
| 443 | + ]) { |
| 444 | + assert.equal(parseApprovalRecord(source), null, String(source)); |
| 445 | + } |
| 446 | + }); |
| 447 | + |
| 448 | + it("counts only head rewrite events", () => { |
| 449 | + assert.equal( |
| 450 | + countHeadRewrites([ |
| 451 | + { event: "head_ref_force_pushed" }, |
| 452 | + { event: "labeled" }, |
| 453 | + { event: "head_ref_restored" }, |
| 454 | + null, |
| 455 | + ]), |
| 456 | + 2, |
| 457 | + ); |
| 458 | + assert.equal(countHeadRewrites(undefined), 0); |
| 459 | + }); |
| 460 | + |
| 461 | + it("keeps approvals only under an unchanged head and rewrite count", () => { |
| 462 | + const labels = ["test-exception-approved", "maintainer-sponsored"]; |
| 463 | + const base = { labels, headSha: HEAD, record: { head: HEAD, headRewrites: 1 }, eventAction: "edited" }; |
| 464 | + const kept = { kept: ["test-exception-approved"], stale: [] }; |
| 465 | + const stale = { kept: [], stale: ["test-exception-approved"] }; |
| 466 | + assert.deepEqual(reconcileHeadBoundApprovals({ ...base, headRewriteCount: 1 }), kept); |
| 467 | + assert.deepEqual(reconcileHeadBoundApprovals({ ...base, headRewriteCount: 2 }), stale); |
| 468 | + assert.deepEqual(reconcileHeadBoundApprovals({ ...base, headRewriteCount: null }), stale); |
| 469 | + assert.deepEqual(reconcileHeadBoundApprovals({ ...base, headRewriteCount: 1, eventAction: "synchronize" }), stale); |
| 470 | + assert.deepEqual(reconcileHeadBoundApprovals({ ...base, headRewriteCount: 1, headSha: OTHER }), stale); |
| 471 | + assert.deepEqual(reconcileHeadBoundApprovals({ ...base, headRewriteCount: 1, record: null }), stale); |
| 472 | + assert.deepEqual( |
| 473 | + reconcileHeadBoundApprovals({ ...base, headRewriteCount: 1, record: { head: HEAD, headRewrites: null } }), |
| 474 | + stale, |
| 475 | + ); |
| 476 | + assert.deepEqual( |
| 477 | + reconcileHeadBoundApprovals({ ...base, labels: ["maintainer-sponsored"], headRewriteCount: 1 }), |
| 478 | + { kept: [], stale: [] }, |
| 479 | + ); |
| 480 | + }); |
| 481 | + |
| 482 | + it("asks for a rewrite count only when one could confirm an approval", () => { |
| 483 | + const base = { |
| 484 | + labels: ["suppression-approved"], |
| 485 | + headSha: HEAD, |
| 486 | + record: { head: HEAD, headRewrites: 0 }, |
| 487 | + eventAction: "labeled", |
| 488 | + }; |
| 489 | + assert.equal(needsHeadRewriteCount(base), true); |
| 490 | + assert.equal(needsHeadRewriteCount({ ...base, labels: [] }), false); |
| 491 | + assert.equal(needsHeadRewriteCount({ ...base, eventAction: "synchronize" }), false); |
| 492 | + assert.equal(needsHeadRewriteCount({ ...base, headSha: OTHER }), false); |
| 493 | + assert.equal(needsHeadRewriteCount({ ...base, record: null }), false); |
| 494 | + assert.equal(needsHeadRewriteCount({ ...base, record: { head: HEAD, headRewrites: null } }), false); |
| 495 | + }); |
| 496 | +}); |
0 commit comments