|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const fs = require("node:fs"); |
| 4 | +const path = require("node:path"); |
| 5 | +const { describe, it } = require("node:test"); |
| 6 | +const assert = require("node:assert/strict"); |
| 7 | +const { |
| 8 | + detectTypeLabelFromTitle, |
| 9 | + hasHumanTypeLabelOverride, |
| 10 | + planTypeLabelSync, |
| 11 | + TYPE_LABELS, |
| 12 | +} = require("./pr-labeler.cjs"); |
| 13 | + |
| 14 | +describe("detectTypeLabelFromTitle", () => { |
| 15 | + it("maps conventional prefixes to type labels", () => { |
| 16 | + assert.equal(detectTypeLabelFromTitle("fix(codex): warn after sync"), "bug"); |
| 17 | + assert.equal(detectTypeLabelFromTitle("feat(images): add bridge"), "enhancement"); |
| 18 | + assert.equal(detectTypeLabelFromTitle("docs: update guide"), "documentation"); |
| 19 | + assert.equal(detectTypeLabelFromTitle("chore!: drop legacy"), "chore"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("maps sentence-case prefixes when no conventional colon is present", () => { |
| 23 | + assert.equal( |
| 24 | + detectTypeLabelFromTitle("Fix Console Go tool schema sanitization"), |
| 25 | + "bug", |
| 26 | + ); |
| 27 | + assert.equal(detectTypeLabelFromTitle("Feat add Grok image bridge"), "enhancement"); |
| 28 | + assert.equal(detectTypeLabelFromTitle("Docs update setup guide"), "documentation"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("returns null without a recognized prefix", () => { |
| 32 | + assert.equal(detectTypeLabelFromTitle("Warn or restart stale app-server"), null); |
| 33 | + assert.equal(detectTypeLabelFromTitle(""), null); |
| 34 | + assert.equal(detectTypeLabelFromTitle("constructor: drop legacy"), null); |
| 35 | + assert.equal(detectTypeLabelFromTitle("Fixed Console Go tool schema"), null); |
| 36 | + assert.equal(detectTypeLabelFromTitle("Fix"), null); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe("hasHumanTypeLabelOverride", () => { |
| 41 | + it("is false when only the Actions bot touched type labels", () => { |
| 42 | + const events = [ |
| 43 | + { event: "labeled", label: { name: "bug" }, actor: { login: "github-actions[bot]" } }, |
| 44 | + ]; |
| 45 | + assert.equal(hasHumanTypeLabelOverride(events), false); |
| 46 | + }); |
| 47 | + |
| 48 | + it("is true after a human replaces the bot type label (PR #518)", () => { |
| 49 | + const events = [ |
| 50 | + { event: "labeled", label: { name: "bug" }, actor: { login: "github-actions[bot]" } }, |
| 51 | + { event: "unlabeled", label: { name: "bug" }, actor: { login: "Wibias" } }, |
| 52 | + { event: "labeled", label: { name: "enhancement" }, actor: { login: "Wibias" } }, |
| 53 | + ]; |
| 54 | + assert.equal(hasHumanTypeLabelOverride(events), true); |
| 55 | + }); |
| 56 | + |
| 57 | + it("stays true even if the bot later reverts the human choice", () => { |
| 58 | + const events = [ |
| 59 | + { event: "labeled", label: { name: "bug" }, actor: { login: "github-actions[bot]" } }, |
| 60 | + { event: "unlabeled", label: { name: "bug" }, actor: { login: "Wibias" } }, |
| 61 | + { event: "labeled", label: { name: "enhancement" }, actor: { login: "Wibias" } }, |
| 62 | + { event: "unlabeled", label: { name: "enhancement" }, actor: { login: "github-actions[bot]" } }, |
| 63 | + { event: "labeled", label: { name: "bug" }, actor: { login: "github-actions[bot]" } }, |
| 64 | + ]; |
| 65 | + assert.equal(hasHumanTypeLabelOverride(events), true); |
| 66 | + }); |
| 67 | + |
| 68 | + it("ignores non-type labels from humans", () => { |
| 69 | + const events = [ |
| 70 | + { event: "labeled", label: { name: "bug" }, actor: { login: "github-actions[bot]" } }, |
| 71 | + { event: "labeled", label: { name: "needs-triage" }, actor: { login: "Wibias" } }, |
| 72 | + ]; |
| 73 | + assert.equal(hasHumanTypeLabelOverride(events), false); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe("planTypeLabelSync", () => { |
| 78 | + it("adds the detected label and removes other type labels when bot-owned", () => { |
| 79 | + const plan = planTypeLabelSync({ |
| 80 | + title: "fix(codex): warn after sync", |
| 81 | + currentLabels: ["enhancement", "needs-triage"], |
| 82 | + events: [ |
| 83 | + { event: "labeled", label: { name: "enhancement" }, actor: { login: "github-actions[bot]" } }, |
| 84 | + ], |
| 85 | + }); |
| 86 | + assert.deepEqual(plan, { |
| 87 | + skip: false, |
| 88 | + detected: "bug", |
| 89 | + add: "bug", |
| 90 | + remove: ["enhancement"], |
| 91 | + }); |
| 92 | + assert.ok(TYPE_LABELS.has("bug")); |
| 93 | + }); |
| 94 | + |
| 95 | + it("is a no-op add when the detected label is already present", () => { |
| 96 | + const plan = planTypeLabelSync({ |
| 97 | + title: "fix(codex): warn after sync", |
| 98 | + currentLabels: ["bug"], |
| 99 | + events: [ |
| 100 | + { event: "labeled", label: { name: "bug" }, actor: { login: "github-actions[bot]" } }, |
| 101 | + ], |
| 102 | + }); |
| 103 | + assert.deepEqual(plan, { |
| 104 | + skip: false, |
| 105 | + detected: "bug", |
| 106 | + add: null, |
| 107 | + remove: [], |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it("skips when a human has overridden the type label", () => { |
| 112 | + const plan = planTypeLabelSync({ |
| 113 | + title: "fix(codex): warn after sync", |
| 114 | + currentLabels: ["enhancement"], |
| 115 | + events: [ |
| 116 | + { event: "labeled", label: { name: "bug" }, actor: { login: "github-actions[bot]" } }, |
| 117 | + { event: "unlabeled", label: { name: "bug" }, actor: { login: "Wibias" } }, |
| 118 | + { event: "labeled", label: { name: "enhancement" }, actor: { login: "Wibias" } }, |
| 119 | + ], |
| 120 | + }); |
| 121 | + assert.deepEqual(plan, { skip: true, reason: "human-override" }); |
| 122 | + }); |
| 123 | + |
| 124 | + it("labels sentence-case bug-fix titles (PR #524)", () => { |
| 125 | + const plan = planTypeLabelSync({ |
| 126 | + title: "Fix Console Go tool schema sanitization", |
| 127 | + currentLabels: [], |
| 128 | + events: [], |
| 129 | + }); |
| 130 | + assert.deepEqual(plan, { |
| 131 | + skip: false, |
| 132 | + detected: "bug", |
| 133 | + add: "bug", |
| 134 | + remove: [], |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + it("skips titles without a recognized prefix", () => { |
| 139 | + const plan = planTypeLabelSync({ |
| 140 | + title: "Warn or restart stale app-server", |
| 141 | + currentLabels: [], |
| 142 | + events: [], |
| 143 | + }); |
| 144 | + assert.deepEqual(plan, { skip: true, reason: "no-prefix" }); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +describe("pr-labeler workflow", () => { |
| 149 | + const workflowPath = path.join(__dirname, "../workflows/pr-labeler.yml"); |
| 150 | + const workflow = fs.readFileSync(workflowPath, "utf8"); |
| 151 | + |
| 152 | + function pullRequestTargetTypes() { |
| 153 | + const match = workflow.match(/pull_request_target:\s*\n(?:[ \t].*\n)*?[ \t]+types:\s*\[([^\]]+)\]/); |
| 154 | + assert.ok(match, "expected pull_request_target types array in pr-labeler.yml"); |
| 155 | + return match[1].split(",").map((type) => type.trim()); |
| 156 | + } |
| 157 | + |
| 158 | + it("listens for labeled and unlabeled so human overrides cancel stale sync runs", () => { |
| 159 | + const types = pullRequestTargetTypes(); |
| 160 | + assert.ok(types.includes("labeled"), "missing pull_request_target type: labeled"); |
| 161 | + assert.ok(types.includes("unlabeled"), "missing pull_request_target type: unlabeled"); |
| 162 | + assert.ok(types.includes("synchronize"), "missing pull_request_target type: synchronize"); |
| 163 | + }); |
| 164 | + |
| 165 | + it("keeps trusted default-branch checkout, concurrency cancel, and minimal permissions", () => { |
| 166 | + assert.match(workflow, /ref:\s*\$\{\{\s*github\.event\.repository\.default_branch\s*\}\}/); |
| 167 | + assert.match(workflow, /cancel-in-progress:\s*true/); |
| 168 | + assert.match(workflow, /pull-requests:\s*read/); |
| 169 | + assert.match(workflow, /issues:\s*write/); |
| 170 | + assert.doesNotMatch(workflow, /pull-requests:\s*write/); |
| 171 | + }); |
| 172 | +}); |
0 commit comments