Skip to content

Commit 876fe06

Browse files
Refactor GitHub Actions workflow for issue labeling
1 parent 4480fb5 commit 876fe06

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

.github/workflows/linked-issue.yml

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
name: Sync Issue Labels to PR
2-
32
on:
43
pull_request_target:
54
types: [opened, reopened, synchronize, edited]
6-
75
permissions:
8-
contents: write
9-
issues: write
6+
contents: read
107
pull-requests: write
11-
128
jobs:
139
sync-labels:
1410
runs-on: ubuntu-latest
@@ -18,39 +14,35 @@ jobs:
1814

1915
- name: Extract linked issues from PR body
2016
id: issues
17+
env:
18+
PR_BODY: ${{ github.event.pull_request.body }}
2119
run: |
22-
BODY=$(printf '%s' "${{ github.event.pull_request.body }}")
23-
24-
ISSUES=$(echo "$BODY" | grep -oE '#[0-9]+' || true)
25-
ISSUES=$(echo "$ISSUES" | tr -d '#' | tr '\n' ' ')
26-
20+
ISSUES=$(echo "$PR_BODY" | grep -oE '#[0-9]+' | tr -d '#' | tr '\n' ' ' || true)
2721
echo "issues=$ISSUES" >> $GITHUB_OUTPUT
2822
2923
- name: Collect labels from linked issues
3024
if: ${{ steps.issues.outputs.issues != '' }}
3125
id: labels
3226
env:
3327
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28+
ISSUE_NUMBERS: ${{ steps.issues.outputs.issues }}
29+
REPO: ${{ github.repository }}
3430
run: |
3531
ALL_LABELS=""
3632
REAL_ISSUES=""
37-
38-
for ISSUE_NUM in ${{ steps.issues.outputs.issues }}; do
39-
IS_PR=$(gh api repos/${{ github.repository }}/issues/$ISSUE_NUM --jq '.pull_request // empty' || echo "")
33+
for ISSUE_NUM in $ISSUE_NUMBERS; do
34+
IS_PR=$(gh api "repos/$REPO/issues/$ISSUE_NUM" --jq '.pull_request // empty' || echo "")
4035
if [ -n "$IS_PR" ]; then
4136
echo "#$ISSUE_NUM is a PR, skipping"
4237
continue
4338
fi
44-
4539
REAL_ISSUES="$REAL_ISSUES $ISSUE_NUM"
46-
LABELS=$(gh issue view $ISSUE_NUM --json labels -q '.labels[].name' || echo "")
40+
LABELS=$(gh issue view "$ISSUE_NUM" --json labels -q '.labels[].name' || echo "")
4741
if [ -n "$LABELS" ]; then
4842
ALL_LABELS="$ALL_LABELS"$'\n'"$LABELS"
4943
fi
5044
done
51-
5245
ALL_LABELS=$(echo "$ALL_LABELS" | sed '/^$/d' | sort -u | paste -sd, - || true)
53-
5446
echo "real_issues=$REAL_ISSUES" >> $GITHUB_OUTPUT
5547
echo "labels=$ALL_LABELS" >> $GITHUB_OUTPUT
5648
@@ -60,13 +52,15 @@ jobs:
6052
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6153
PR_NUM: ${{ github.event.pull_request.number }}
6254
run: |
63-
gh pr comment $PR_NUM --body "⚠️ No issue was linked in the PR description.
55+
gh pr comment "$PR_NUM" --body "⚠️ No issue was linked in the PR description.
6456
Please make sure to link an issue (e.g., 'Fixes #issue_number')"
6557
6658
- name: Apply labels to PR
6759
if: ${{ steps.labels.outputs.labels != '' }}
6860
env:
6961
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62+
LABELS_TO_APPLY: ${{ steps.labels.outputs.labels }}
63+
PR_NUMBER: ${{ github.event.pull_request.number }}
7064
run: |
71-
echo "Applying labels: ${{ steps.labels.outputs.labels }}"
72-
gh pr edit ${{ github.event.pull_request.number }} --add-label "${{ steps.labels.outputs.labels }}"
65+
echo "Applying labels: $LABELS_TO_APPLY"
66+
gh pr edit "$PR_NUMBER" --add-label "$LABELS_TO_APPLY"

0 commit comments

Comments
 (0)