Skip to content

Commit a9f00dd

Browse files
authored
feat(ci): automatic PR labeling (#1143)
so i decided to make labeling prs a bit easier and this workflow automatically syncs issue labels to a newly created pr (if it has a closing keyword for that issue of course). this WILL NOT always be accurate so we still need to review manually. it's not a replacement, just makes stuff easier. fully tested on my fork
1 parent fbfd12f commit a9f00dd

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

.github/workflows/pr-labels.yaml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Label PR
2+
3+
on:
4+
pull_request:
5+
types: [opened]
6+
7+
jobs:
8+
labelPR:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
issues: read
12+
pull-requests: write
13+
14+
steps:
15+
- uses: actions/checkout@v5
16+
17+
- name: Extract referenced issues
18+
run: |
19+
BODY="${{ github.event.pull_request.body }}"
20+
ISSUES=$(echo "$BODY" | grep -Eio '(Fixes|Resolves|Closes):? #[0-9]+' | grep -Eo '[0-9]+' | uniq)
21+
22+
echo "ISSUES=$ISSUES" >> $GITHUB_ENV
23+
24+
- name: Get issue labels
25+
if: env.ISSUES != ''
26+
env:
27+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28+
run: |
29+
LABELS=()
30+
31+
for ISSUE in ${{ env.ISSUES }}; do
32+
ISSUE_LABELS=$(curl -s -H "Authorization: token $GH_TOKEN" \
33+
"https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE" \
34+
| jq -r '.labels[].name')
35+
36+
while IFS= read -r LABEL; do
37+
if [[ "$LABEL" == *:* ]]; then
38+
LABELS+=("$LABEL")
39+
fi
40+
done <<< "$ISSUE_LABELS"
41+
done
42+
43+
UNIQUE_LABELS=$(printf "%s\n" "${LABELS[@]}" | sort -u | tr '\n' ',' | sed 's/,$//')
44+
echo "LABELS=$UNIQUE_LABELS" >> $GITHUB_ENV
45+
46+
- name: Assign labels
47+
env:
48+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
49+
run: |
50+
if [[ -n ${{ env.LABELS }} ]]; then
51+
gh pr edit ${{ github.event.pull_request.number }} --add-label "initial review required,${{ env.LABELS }}"
52+
else
53+
gh pr edit ${{ github.event.pull_request.number }} --add-label "initial review required"
54+
fi

0 commit comments

Comments
 (0)