Skip to content

Commit 59d4ec4

Browse files
committed
fix(ci): address PR labeler review feedback
Guard prefix lookup with an own-property check, pin checkout to the default branch, narrow workflow permissions to pulls:read/issues:write, and tolerate concurrent label creation races.
1 parent 9a43d78 commit 59d4ec4

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

‎.github/scripts/pr-labeler.cjs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ const BOT_ACTORS = new Set(["github-actions[bot]"]);
3737
function detectTypeLabelFromTitle(title) {
3838
const match = String(title || "").match(/^([a-zA-Z]+)(?:\([^)]*\))?[!]?\s*:/);
3939
if (!match) return null;
40-
return PREFIX_TO_LABEL[match[1].toLowerCase()] || null;
40+
const prefix = match[1].toLowerCase();
41+
if (!Object.prototype.hasOwnProperty.call(PREFIX_TO_LABEL, prefix)) return null;
42+
return PREFIX_TO_LABEL[prefix];
4143
}
4244

4345
/**

‎.github/scripts/pr-labeler.test.cjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe("detectTypeLabelFromTitle", () => {
2020
it("returns null without a conventional prefix", () => {
2121
assert.equal(detectTypeLabelFromTitle("Warn or restart stale app-server"), null);
2222
assert.equal(detectTypeLabelFromTitle(""), null);
23+
assert.equal(detectTypeLabelFromTitle("constructor: drop legacy"), null);
2324
});
2425
});
2526

‎.github/workflows/pr-labeler.yml‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ concurrency:
1414

1515
permissions:
1616
contents: read
17-
pull-requests: write
18-
issues: read
17+
# pulls.get only needs read; label mutations use the issues API.
18+
pull-requests: read
19+
issues: write
1920

2021
jobs:
2122
label:
@@ -24,6 +25,7 @@ jobs:
2425
- name: Checkout labeler script (default-branch trusted code)
2526
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2627
with:
28+
ref: ${{ github.event.repository.default_branch }}
2729
persist-credentials: false
2830

2931
- name: Apply type label from PR title
@@ -79,12 +81,17 @@ jobs:
7981
documentation: '0075ca',
8082
chore: 'e4e669',
8183
};
82-
await github.rest.issues.createLabel({
83-
owner, repo,
84-
name: plan.detected,
85-
color: colors[plan.detected] || 'ededed',
86-
});
87-
core.info(`Created missing label "${plan.detected}"`);
84+
try {
85+
await github.rest.issues.createLabel({
86+
owner, repo,
87+
name: plan.detected,
88+
color: colors[plan.detected] || 'ededed',
89+
});
90+
core.info(`Created missing label "${plan.detected}"`);
91+
} catch (createErr) {
92+
if (createErr.status !== 422) throw createErr;
93+
core.info(`Label "${plan.detected}" was created concurrently; continuing.`);
94+
}
8895
} else {
8996
throw err;
9097
}

0 commit comments

Comments
 (0)