-
-
Notifications
You must be signed in to change notification settings - Fork 17
157 lines (135 loc) · 5.04 KB
/
Copy pathpr_labeler.yml
File metadata and controls
157 lines (135 loc) · 5.04 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
---
name: Pull Request labeler
permissions: {}
on:
pull_request_target:
types:
- opened
- reopened
- synchronize
concurrency:
group: "${{ github.workflow }}-${{ github.event.pull_request.number }}"
cancel-in-progress: true
jobs:
changed-files:
runs-on: ubuntu-latest
permissions:
pull-requests: read
outputs:
files: ${{ steps.changed-files.outputs.files }}
steps:
- name: List changed files
id: changed-files
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
github-token: ${{ github.token }}
script: |
const files = await github.paginate(github.rest.pulls.listFiles, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
per_page: 100
});
const names = files.flatMap((file) => (
file.previous_filename ? [file.filename, file.previous_filename] : [file.filename]
));
core.setOutput('files', [...new Set(names)].join('\n'));
detect-formulas:
needs: changed-files
uses: ./.github/workflows/_detect-formulas.yml
permissions:
contents: read
with:
changed-files: ${{ needs.changed-files.outputs.files }}
checkout-ref: ${{ github.event.pull_request.base.sha }}
event-name: pull_request
run-homebrew-setup: false
label-formulas:
needs: detect-formulas
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Set formula labels
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
FORMULA_LABELS: ${{ needs.detect-formulas.outputs.formula-labels }}
TESTING_FORMULAE: ${{ needs.detect-formulas.outputs.testing-formulae }}
with:
github-token: ${{ github.token }}
script: |
const reservedAutomationLabels = new Set([
'bottle-published',
'pr-pull'
]);
const parseLabels = (value) => [...new Set((value || '')
.split(',')
.map((label) => label.trim())
.filter(Boolean))];
const isReservedAutomationLabel = (label) => (
reservedAutomationLabels.has(label.toLowerCase())
);
const formulaLabels = parseLabels(process.env.FORMULA_LABELS)
.filter((label) => !isReservedAutomationLabel(label));
const labels = parseLabels(process.env.TESTING_FORMULAE)
.sort();
const deniedLabels = labels.filter(isReservedAutomationLabel);
if (deniedLabels.length > 0) {
throw new Error(
`Formula-derived labels cannot use reserved automation labels: ${deniedLabels.join(', ')}`
);
}
const allFormulaLabels = new Set(formulaLabels);
for (const label of labels) {
allFormulaLabels.add(label);
}
const currentLabels = await github.paginate(github.rest.issues.listLabelsOnIssue, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
per_page: 100
});
const staleLabels = currentLabels
.map((label) => label.name)
.filter((label) => allFormulaLabels.has(label) && !labels.includes(label));
for (const label of staleLabels) {
console.log(`Removing stale formula label: ${label}`);
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
name: label
});
}
if (labels.length === 0) {
console.log('No formula labels detected.');
return;
}
for (const label of labels) {
try {
await github.rest.issues.getLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: label
});
} catch (error) {
if (error.status !== 404) {
throw error;
}
console.log(`Creating missing formula label: ${label}`);
await github.rest.issues.createLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: label,
color: 'ededed',
description: `Formula: ${label}`
});
}
}
console.log(`Adding formula labels: ${labels.join(', ')}`);
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
labels
});