-
Notifications
You must be signed in to change notification settings - Fork 2
271 lines (232 loc) · 8.03 KB
/
Copy pathpr-labels.yml
File metadata and controls
271 lines (232 loc) · 8.03 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
name: PR Labels
on:
workflow_dispatch:
inputs:
pr_numbers:
description: One or more PR numbers to label. Separate multiple PRs with spaces, commas, or new lines.
required: true
type: string
pull_request_target:
types:
- opened
- reopened
- labeled
- unlabeled
- synchronize
- edited
concurrency:
group: pr-labels-${{ github.event_name == 'workflow_dispatch' && github.run_id || github.event.pull_request.number }}
cancel-in-progress: true
jobs:
labeler:
name: apply labels
if: github.repository == 'wallentx/jobscout'
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: read
issues: write
pull-requests: write
steps:
- name: Normalize manual PR numbers
id: pr-numbers
if: github.event_name == 'workflow_dispatch'
env:
INPUT_PR_NUMBERS: ${{ inputs.pr_numbers }}
run: |
set -eu
pr_numbers=$(
printf '%s\n' "$INPUT_PR_NUMBERS" |
tr ',;' '\n' |
tr ' ' '\n' |
awk '
NF {
if ($0 !~ /^[0-9]+$/) {
printf "Invalid PR number: %s\n", $0 > "/dev/stderr"
exit 1
}
print
}
'
)
if [ -z "$pr_numbers" ]; then
echo "No PR numbers provided" >&2
exit 1
fi
{
echo "value<<EOF"
printf '%s\n' "$pr_numbers"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Label pull request
uses: actions/labeler@v6
with:
configuration-path: .github/labeler.yml
pr-number: ${{ github.event_name == 'workflow_dispatch' && steps.pr-numbers.outputs.value || github.event.pull_request.number }}
release-label-status:
name: check release label
needs: labeler
if: github.repository == 'wallentx/jobscout' && github.event_name == 'pull_request_target'
runs-on: ubuntu-latest
timeout-minutes: 5
permissions:
contents: read
issues: read
pull-requests: read
outputs:
has_release_label: ${{ steps.release-label.outputs.has_release_label }}
release_labels: ${{ steps.release-label.outputs.release_labels }}
steps:
- name: Actions Toolbox
uses: wallentx/gh-actions/composite/actions-toolbox@main
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
checkout: false
- name: Check release-note labels
id: release-label
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
: "${GH_PR:?Actions Toolbox did not set GH_PR}"
release_labels="$(
gh pr view "$GH_PR" --repo "$GITHUB_REPOSITORY" --json labels \
--jq '.labels[].name' |
awk '/^(added|changed|fixed|dependencies)$/ { print }'
)"
if [ -n "$release_labels" ]; then
echo "Release-note label already present:"
printf '%s\n' "$release_labels"
echo "has_release_label=true" >> "$GITHUB_OUTPUT"
else
echo "No release-note label present"
echo "has_release_label=false" >> "$GITHUB_OUTPUT"
fi
{
echo "release_labels<<EOF"
printf '%s\n' "$release_labels"
echo "EOF"
} >> "$GITHUB_OUTPUT"
classify-release-label:
name: classify release label
needs:
- labeler
- release-label-status
if: github.repository == 'wallentx/jobscout' && github.event_name == 'pull_request_target' && needs.release-label-status.outputs.has_release_label != 'true'
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
pull-requests: write
issues: write
steps:
- name: Check out repository
uses: actions/checkout@v6
- name: Actions Toolbox
uses: wallentx/gh-actions/composite/actions-toolbox@main
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
checkout: false
- name: Set up Node.js
uses: actions/setup-node@v6
with:
node-version: 22
- name: Install Copilot CLI
run: npm install -g @github/copilot
- name: Classify PR for release label
id: classify
env:
COPILOT_GITHUB_TOKEN: ${{ secrets.COPILOT_GITHUB_TOKEN }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
: "${GH_PR:?Actions Toolbox did not set GH_PR}"
: "${GH_PR_TITLE:?Actions Toolbox did not set GH_PR_TITLE}"
: "${GH_PR_URL:?Actions Toolbox did not set GH_PR_URL}"
pr_body="$(printf '%s\n' "${GH_PR_BODY_JSON:-\"\"}" | jq -r '.')"
pr_files="$(gh pr view "$GH_PR" --repo "$GITHUB_REPOSITORY" --json files --jq '.files[].path')"
raw_label="$(
{
echo "Classify this pull request and return only the release-note label."
printf '\nPR CONTEXT:\n'
echo "PR TITLE:"
printf '%s\n' "$GH_PR_TITLE" | sed -n '1,80p'
printf '\nPR BODY:\n'
printf '%s\n' "$pr_body" | sed -n '1,240p'
printf '\nCHANGED FILES:\n'
printf '%s\n' "$pr_files" | sed -n '1,300p'
printf '\nDIFF:\n'
curl -fsSL "${GH_PR_URL}.diff" | sed -n '1,1500p'
} | copilot \
-s \
--no-color \
--no-auto-update \
--no-ask-user \
--deny-tool=shell \
--deny-tool=write \
--deny-tool=read \
--deny-tool=url \
--deny-tool=memory \
| tr -d '\r'
)"
first_label_line="$(
printf '%s\n' "$raw_label" \
| awk 'NF { print; exit }'
)"
label_tokens="$(
printf '%s\n' "$first_label_line" \
| tr '[:upper:]' '[:lower:]' \
| tr -cs '[:alnum:]_-' '\n' \
| awk '/^(added|changed|fixed|dependencies)$/ { print }'
)"
label_count="$(
printf '%s\n' "$label_tokens" \
| awk 'NF { count++ } END { print count + 0 }'
)"
if [ "$label_count" -ne 1 ]; then
echo "Copilot returned invalid label: ${raw_label:-<empty>}" >&2
exit 1
fi
label="$(
printf '%s\n' "$label_tokens" \
| awk 'NF { print; exit }'
)"
echo "label=$label" >> "$GITHUB_OUTPUT"
- name: Replace release-note labels
env:
GH_TOKEN: ${{ github.token }}
NEW_LABEL: ${{ steps.classify.outputs.label }}
run: |
set -euo pipefail
: "${GH_PR:?Actions Toolbox did not set GH_PR}"
existing="$(
gh pr view "$GH_PR" --repo "$GITHUB_REPOSITORY" --json labels \
--jq '.labels[].name'
)"
release_labels="added changed fixed dependencies"
has_new_label=false
has_other_release_label=false
for label in $release_labels; do
if printf '%s\n' "$existing" | grep -Fxq "$label"; then
if [ "$label" = "$NEW_LABEL" ]; then
has_new_label=true
else
has_other_release_label=true
fi
fi
done
if [ "$has_new_label" = true ] && [ "$has_other_release_label" = false ]; then
echo "Release label already set to $NEW_LABEL"
exit 0
fi
for label in $release_labels; do
if [ "$label" != "$NEW_LABEL" ] && printf '%s\n' "$existing" | grep -Fxq "$label"; then
gh pr edit "$GH_PR" --repo "$GITHUB_REPOSITORY" --remove-label "$label"
fi
done
if [ "$has_new_label" = false ]; then
gh pr edit "$GH_PR" --repo "$GITHUB_REPOSITORY" --add-label "$NEW_LABEL"
fi
echo "Applied label: $NEW_LABEL"