Skip to content

Commit 551e6fa

Browse files
committed
ci: add PR auto-labeler
1 parent 616b62d commit 551e6fa

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

.github/workflows/auto-label.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Auto Label Merged PRs
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
branches:
7+
- main
8+
- "release/**"
9+
10+
jobs:
11+
label_prs:
12+
runs-on: ubuntu-latest
13+
14+
if: github.event.pull_request.merged == true
15+
16+
permissions:
17+
pull-requests: write
18+
19+
steps:
20+
- name: Apply labels based on Conventional Commits
21+
uses: actions/github-script@v7
22+
with:
23+
script: |
24+
const title = context.payload.pull_request.title.toLowerCase();
25+
const prNumber = context.payload.pull_request.number;
26+
const owner = context.repo.owner;
27+
const repo = context.repo.repo;
28+
29+
let labelsToAdd = [];
30+
31+
// The Regex checks: Starts with type (^type), optional scope (\([^)]+\))?, optional bang (!?), and a colon (:)
32+
if (/^feat(\([^)]+\))?!?:/.test(title)) {
33+
labelsToAdd.push('enhancement');
34+
} else if (/^docs(\([^)]+\))?!?:/.test(title)) {
35+
labelsToAdd.push('documentation');
36+
} else if (/^fix(\([^)]+\))?!?:/.test(title)) {
37+
labelsToAdd.push('bug'); // Change this to 'fix' if you don't use the default 'bug' label
38+
}
39+
40+
if (labelsToAdd.length > 0) {
41+
console.log(`Adding labels: ${labelsToAdd} to merged PR #${prNumber}`);
42+
43+
await github.rest.issues.addLabels({
44+
owner: owner,
45+
repo: repo,
46+
issue_number: prNumber,
47+
labels: labelsToAdd
48+
});
49+
} else {
50+
console.log('No matching Conventional Commit prefix found. No labels added.');
51+
}

0 commit comments

Comments
 (0)