-
-
Notifications
You must be signed in to change notification settings - Fork 635
66 lines (62 loc) · 2.42 KB
/
Copy pathlinked-issue.yml
File metadata and controls
66 lines (62 loc) · 2.42 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
name: Sync Issue Labels to PR
on:
pull_request_target:
types: [opened, reopened, synchronize, edited]
permissions:
contents: read
pull-requests: write
jobs:
sync-labels:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Extract linked issues from PR body
id: issues
env:
PR_BODY: ${{ github.event.pull_request.body }}
run: |
ISSUES=$(echo "$PR_BODY" | grep -oE '#[0-9]+' | tr -d '#' | tr '\n' ' ' || true)
echo "issues=$ISSUES" >> $GITHUB_OUTPUT
- name: Collect labels from linked issues
if: ${{ steps.issues.outputs.issues != '' }}
id: labels
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_NUMBERS: ${{ steps.issues.outputs.issues }}
REPO: ${{ github.repository }}
run: |
ALL_LABELS=""
REAL_ISSUES=""
for ISSUE_NUM in $ISSUE_NUMBERS; do
IS_PR=$(gh api "repos/$REPO/issues/$ISSUE_NUM" --jq '.pull_request // empty' || echo "")
if [ -n "$IS_PR" ]; then
echo "#$ISSUE_NUM is a PR, skipping"
continue
fi
REAL_ISSUES="$REAL_ISSUES $ISSUE_NUM"
LABELS=$(gh issue view "$ISSUE_NUM" --json labels -q '.labels[].name' || echo "")
if [ -n "$LABELS" ]; then
ALL_LABELS="$ALL_LABELS"$'\n'"$LABELS"
fi
done
ALL_LABELS=$(echo "$ALL_LABELS" | sed '/^$/d' | sort -u | paste -sd, - || true)
echo "real_issues=$REAL_ISSUES" >> $GITHUB_OUTPUT
echo "labels=$ALL_LABELS" >> $GITHUB_OUTPUT
- name: Comment if no valid issues linked
if: ${{ steps.labels.outputs.real_issues == '' }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUM: ${{ github.event.pull_request.number }}
run: |
gh pr comment "$PR_NUM" --body "⚠️ No issue was linked in the PR description.
Please make sure to link an issue (e.g., 'Fixes #issue_number')"
- name: Apply labels to PR
if: ${{ steps.labels.outputs.labels != '' }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
LABELS_TO_APPLY: ${{ steps.labels.outputs.labels }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
echo "Applying labels: $LABELS_TO_APPLY"
gh pr edit "$PR_NUMBER" --add-label "$LABELS_TO_APPLY"