Skip to content

Commit a931f13

Browse files
committed
tools: automatically remove lts-watch-* labels
1 parent 6b5178f commit a931f13

3 files changed

Lines changed: 114 additions & 3 deletions

File tree

.github/workflows/lint-release-proposal.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,11 @@ jobs:
9292
"/repos/${GITHUB_REPOSITORY}/compare/v${MAJOR}.x...$GITHUB_SHA" --paginate \
9393
| node tools/actions/lint-release-proposal-commit-list.mjs "$CHANGELOG_PATH" "$GITHUB_SHA" \
9494
| while IFS= read -r PR_URL; do
95-
DONT_LAND_LABEL="dont-land-on-v${MAJOR}.x" LTS_WATCH_LABEL="lts-watch-v${MAJOR}.x" gh pr view \
95+
DONT_LAND_LABEL="dont-land-on-v${MAJOR}.x" gh pr view \
9696
--json labels,url \
9797
--jq '
9898
if (.labels|any(.name==env.DONT_LAND_LABEL)) then
9999
error("\(.url) has the \(env.DONT_LAND_LABEL) label, forbidding it to be in this release proposal")
100-
elif (.labels|any(.name==env.LTS_WATCH_LABEL)) then
101-
error("\(.url) has the \(env.LTS_WATCH_LABEL) label, please remove the label now that the PR is included in a release proposal")
102100
end
103101
' \
104102
"$PR_URL" > /dev/null
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Remove `lts-watch-vXX.x` labels
2+
3+
on:
4+
push:
5+
branches:
6+
- v[0-9]+.x
7+
8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
10+
cancel-in-progress: false
11+
12+
env:
13+
NODE_VERSION: lts/*
14+
15+
permissions:
16+
contents: read
17+
18+
jobs:
19+
remove-lts-watch-labels:
20+
runs-on: ubuntu-slim
21+
permissions:
22+
contents: read
23+
pull-requests: write
24+
steps:
25+
- name: Parse commits and remove labels
26+
run: tools/actions/remove-lts-watch-label.sh
27+
env:
28+
GH_TOKEN: ${{ github.token }}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/bin/sh
2+
3+
set -ex
4+
5+
[ -n "$GITHUB_REPOSITORY_OWNER" ] || {
6+
echo "Required environment variable GITHUB_REPOSITORY_OWNER not found" >&2
7+
exit 1
8+
}
9+
[ -n "$GITHUB_REPOSITORY" ] || {
10+
echo "Required environment variable GITHUB_REPOSITORY not found" >&2
11+
exit 1
12+
}
13+
[ -n "$GITHUB_REF_NAME" ] || {
14+
echo "Required environment variable GITHUB_REF_NAME not found" >&2
15+
exit 1
16+
}
17+
[ -n "$GITHUB_EVENT_PATH" ] || {
18+
echo "Required environment variable GITHUB_EVENT_PATH not found" >&2
19+
exit 1
20+
}
21+
22+
owner=$GITHUB_REPOSITORY_OWNER
23+
repo=$(echo "$GITHUB_REPOSITORY" | cut -d/ -f2)
24+
label="lts-watch-$GITHUB_REF_NAME"
25+
26+
# Lazy-get label node_id once
27+
label_id=
28+
fetch_label_id () {
29+
# shellcheck disable=SC2016
30+
label_id="$(gh api graphql -f query='
31+
query($owner:String!, $repo:String!, $name:String!) {
32+
repository(owner:$owner, name:$repo) {
33+
label(name:$name) { id }
34+
}
35+
}' -f owner="$owner" -f repo="$repo" -f name="$label" --jq '.data.repository.label.id')"
36+
37+
if [ -z "$label_id" ] || [ "$label_id" = "null" ]; then
38+
echo "Could not resolve label id for '$label' in $owner/$repo" >&2
39+
exit 1
40+
fi
41+
}
42+
43+
jq -r '.commits[].message' < "$GITHUB_EVENT_PATH" \
44+
| while IFS= read -r msg; do
45+
pr_url=$(echo "$msg" | git interpret-trailers --parse | awk -F': ' '$1 == "PR-URL" { print $2; exit }')
46+
pr_number=${pr_url##"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/"}
47+
48+
# Filter out pr_number that are not numbers (e.g. if PR-URL points to a different repo)
49+
case "$pr_number" in
50+
''|*[!0-9]*) continue ;;
51+
*) ;;
52+
esac
53+
54+
# shellcheck disable=SC2016
55+
pr_id="$(gh api graphql -f query='
56+
query($owner:String!, $repo:String!, $number:Int!, $label:String!) {
57+
repository(owner:$owner, name:$repo) {
58+
pullRequest(number:$number) {
59+
id
60+
labels(first: 1, query: $label) {
61+
nodes { name }
62+
}
63+
}
64+
}
65+
}' -f owner="$owner" -f repo="$repo" -F number="$pr_number" -f label="$label" \
66+
--jq ' if (.data.repository.pullRequest.labels.nodes | length) then .data.repository.pullRequest.id end')"
67+
68+
if [ -z "$pr_id" ] || [ "$pr_id" = "null" ]; then
69+
echo "PR #$pr_number not found or does not have the label" >&2
70+
continue
71+
fi
72+
73+
[ -n "$label_id" ] || fetch_label_id
74+
75+
# shellcheck disable=SC2016
76+
gh api graphql -f query='
77+
mutation($labelableId:ID!, $labelIds:[ID!]!) {
78+
removeLabelsFromLabelable(input:{labelableId:$labelableId, labelIds:$labelIds}) {
79+
clientMutationId
80+
}
81+
}' -f labelableId="$pr_id" -f labelIds="[$label_id]" >/dev/null
82+
83+
# gentle throttle
84+
sleep 0.2
85+
done

0 commit comments

Comments
 (0)