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