Skip to content

Commit bd63cef

Browse files
committed
ci: automate labeling for PRs
[skip ci]
1 parent 822f370 commit bd63cef

3 files changed

Lines changed: 103 additions & 2 deletions

File tree

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
- [ ] New feature (non-breaking change which adds functionality)
1313
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
1414
- [ ] Documentation update (improvements to the project's docs)
15-
- [ ] Specification changes (updates to WebDriver command specifications)
1615
- [ ] Internal updates (everything related to internal scripts, governance documentation and CI files)
1716

1817
## Checklist

.github/workflows/pr-labeler.yml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: Auto Label PR
2+
3+
on:
4+
pull_request:
5+
types: [opened, edited, closed]
6+
7+
concurrency:
8+
group: pr-labeler-${{ github.workflow }}-${{ github.ref }}
9+
cancel-in-progress: true
10+
11+
jobs:
12+
auto-label:
13+
runs-on: ubuntu-latest
14+
if: github.event.action != 'closed' || github.event.pull_request.merged == true
15+
permissions:
16+
pull-requests: write
17+
contents: read
18+
19+
steps:
20+
- name: Checkout
21+
uses: actions/checkout@v4
22+
23+
- name: Auto Label Based on Checklist
24+
uses: actions/github-script@v7
25+
with:
26+
script: |
27+
const { owner, repo, number } = context.issue;
28+
29+
const pr = await github.rest.pulls.get({
30+
owner,
31+
repo,
32+
pull_number: number
33+
})
34+
35+
const prBody = pr.data.body || ''
36+
37+
const checklistToLabel = {
38+
'Polish': 'PR: Polish :nail_care:',
39+
'Bugfix':'PR: Bug Fix :bug:',
40+
'New feature': 'PR: New Feature :rocket:',
41+
'Breaking change': 'PR: Breaking Change :boom:',
42+
'Documentation update':'PR: Docs :memo:',
43+
'Internal updates': 'PR: Internal :house:',
44+
}
45+
46+
const currentLabels = await github.rest.issues.listLabelsOnIssue({
47+
owner,
48+
repo,
49+
issue_number: number
50+
})
51+
52+
const currentLabelNames = currentLabels.data.map(label => label.name)
53+
console.log('Current labels:', currentLabelNames)
54+
55+
const labelsToAdd = []
56+
const labelsToRemove = []
57+
58+
for (const [checklistKeyword, labelName] of Object.entries(checklistToLabel)) {
59+
const checkedPattern = new RegExp(`- \\[[Xx]\\]\\s+${checklistKeyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`, 'i')
60+
const uncheckedPattern = new RegExp(`- \\[\\s\\]\\s+${checklistKeyword.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`, 'i')
61+
62+
if (checkedPattern.test(prBody)) {
63+
if (!currentLabelNames.includes(labelName)) {
64+
labelsToAdd.push(labelName)
65+
}
66+
} else if (uncheckedPattern.test(prBody)) {
67+
if (currentLabelNames.includes(labelName)) {
68+
labelsToRemove.push(labelName)
69+
}
70+
}
71+
}
72+
73+
if (labelsToAdd.length > 0) {
74+
labelsToAdd.forEach((label)=>console.log('Adding label:', label))
75+
await github.rest.issues.addLabels({
76+
owner,
77+
repo,
78+
issue_number: number,
79+
labels: labelsToAdd
80+
})
81+
}
82+
83+
for (const labelName of labelsToRemove) {
84+
console.log('Removing label:', labelName)
85+
try {
86+
await github.rest.issues.removeLabel({
87+
owner,
88+
repo,
89+
issue_number: number,
90+
name: labelName
91+
})
92+
} catch (error) {
93+
if (error.status !== 404) {
94+
throw error
95+
}
96+
}
97+
}
98+
99+
if (labelsToAdd.length > 0 || labelsToRemove.length > 0) {
100+
console.log(`Labels updated: +${labelsToAdd.length}, -${labelsToRemove.length}`)
101+
} else {
102+
console.log('No label changes needed')
103+
}

lerna.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"repo": "webdriverio/vscode-webdriverio",
77
"cacheDir": ".changelog",
88
"labels": {
9-
"PR: Spec Compliancy :eyeglasses:": ":eyeglasses: Spec Compliancy",
109
"PR: Breaking Change :boom:": ":boom: Breaking Change",
1110
"PR: New Feature :rocket:": ":rocket: New Feature",
1211
"PR: Bug Fix :bug:": ":bug: Bug Fix",

0 commit comments

Comments
 (0)