|
1 | 1 | name: 🏷️ Auto Label Issues & PRs |
2 | 2 |
|
| 3 | +# Use pull_request_target so labels can be applied for PRs from forks. |
| 4 | +# SECURITY: Do NOT check out or execute untrusted PR code here. |
3 | 5 | on: |
4 | | - pull_request: |
5 | | - types: [opened, reopened, synchronize] |
6 | 6 | issues: |
7 | | - types: [opened, edited] |
| 7 | + types: [opened, reopened] |
| 8 | + pull_request_target: |
| 9 | + types: [opened, reopened, synchronize] |
8 | 10 |
|
9 | 11 | permissions: |
10 | | - contents: read |
11 | 12 | issues: write |
12 | 13 | pull-requests: write |
13 | 14 |
|
14 | 15 | jobs: |
15 | | - label: |
| 16 | + apply-labels: |
16 | 17 | runs-on: ubuntu-latest |
17 | 18 | steps: |
18 | | - - name: 🏷️ Apply labels automatically |
19 | | - uses: actions/labeler@v5 |
| 19 | + - name: Determine and apply labels |
| 20 | + uses: actions/github-script@v7 |
20 | 21 | with: |
21 | | - repo-token: ${{ secrets.GITHUB_TOKEN }} |
| 22 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 23 | + script: | |
| 24 | + const repo = context.repo; |
| 25 | +
|
| 26 | + if (context.payload.issue) { |
| 27 | + const number = context.payload.issue.number; |
| 28 | + // Example: add triage label to new issues |
| 29 | + try { |
| 30 | + await github.rest.issues.addLabels({ |
| 31 | + owner: repo.owner, |
| 32 | + repo: repo.repo, |
| 33 | + issue_number: number, |
| 34 | + labels: ["triage"] |
| 35 | + }); |
| 36 | + } catch (err) { |
| 37 | + core && core.info && core.info("Could not add labels to issue: " + err.message); |
| 38 | + } |
| 39 | + return; |
| 40 | + } |
| 41 | +
|
| 42 | + if (context.payload.pull_request) { |
| 43 | + const pr = context.payload.pull_request; |
| 44 | + const prNumber = pr.number; |
| 45 | + const headRepoFull = pr.head && pr.head.repo && pr.head.repo.full_name ? pr.head.repo.full_name : ''; |
| 46 | + const baseFull = `${repo.owner}/${repo.repo}`; |
| 47 | + const isFork = headRepoFull.toLowerCase() !== baseFull.toLowerCase(); |
| 48 | +
|
| 49 | + const labels = ["needs-review"]; |
| 50 | + if (isFork) labels.push("from-fork"); |
| 51 | +
|
| 52 | + try { |
| 53 | + await github.rest.issues.addLabels({ |
| 54 | + owner: repo.owner, |
| 55 | + repo: repo.repo, |
| 56 | + issue_number: prNumber, |
| 57 | + labels: labels |
| 58 | + }); |
| 59 | + } catch (err) { |
| 60 | + core && core.info && core.info("Could not add labels to PR: " + err.message); |
| 61 | + } |
| 62 | + return; |
| 63 | + } |
| 64 | +
|
| 65 | + core && core.info && core.info("No issue or PR payload found; skipping labeling."); |
0 commit comments