|
| 1 | +# PR Bot: comments & labels (runs in base-repo context) |
| 2 | +# SECURITY: This workflow uses pull_request_target so it can write comments/labels on PRs from forks. |
| 3 | +# DO NOT check out or execute untrusted PR code in this workflow. |
| 4 | +name: 🤖 PR Bot — Auto Comment & Label |
| 5 | + |
| 6 | +on: |
| 7 | + issues: |
| 8 | + types: [opened, reopened] |
| 9 | + # Use pull_request_target to be able to write to PRs that originate from forks. |
| 10 | + # Do NOT checkout or run untrusted repo code here. |
| 11 | + pull_request_target: |
| 12 | + types: [opened, reopened, synchronize] |
| 13 | + |
| 14 | +permissions: |
| 15 | + issues: write |
| 16 | + pull-requests: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + comment_and_label: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - name: Auto comment and label issues / PRs |
| 23 | + uses: actions/github-script@v7 |
| 24 | + with: |
| 25 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 26 | + script: | |
| 27 | + const repo = context.repo; |
| 28 | +
|
| 29 | + // ISSUES (opened / reopened) |
| 30 | + if (context.payload.issue) { |
| 31 | + const issue = context.payload.issue; |
| 32 | + const user = issue.user?.login || 'contributor'; |
| 33 | + const issueNumber = issue.number; |
| 34 | +
|
| 35 | + const message = `👋 Hi @${user}!\n\nThanks for opening an issue in **MyCMD**.\n\nWe’ll review it soon — please ensure reproduction steps and logs are included.`; |
| 36 | +
|
| 37 | + await github.rest.issues.createComment({ |
| 38 | + owner: repo.owner, |
| 39 | + repo: repo.repo, |
| 40 | + issue_number: issueNumber, |
| 41 | + body: message |
| 42 | + }); |
| 43 | +
|
| 44 | + await github.rest.reactions.createForIssue({ |
| 45 | + owner: repo.owner, |
| 46 | + repo: repo.repo, |
| 47 | + issue_number: issueNumber, |
| 48 | + content: "tada" |
| 49 | + }); |
| 50 | +
|
| 51 | + // Optionally add labels to issues |
| 52 | + try { |
| 53 | + await github.rest.issues.addLabels({ |
| 54 | + owner: repo.owner, |
| 55 | + repo: repo.repo, |
| 56 | + issue_number: issueNumber, |
| 57 | + labels: ["triage", "needs-info"] |
| 58 | + }); |
| 59 | + } catch (err) { |
| 60 | + core && core.info && core.info("Could not add labels to issue: " + err.message); |
| 61 | + } |
| 62 | +
|
| 63 | + return; |
| 64 | + } |
| 65 | +
|
| 66 | + // PULL REQUESTS (opened / reopened / synchronize) |
| 67 | + if (context.payload.pull_request) { |
| 68 | + const pr = context.payload.pull_request; |
| 69 | + const user = pr.user?.login || 'contributor'; |
| 70 | + const prNumber = pr.number; |
| 71 | +
|
| 72 | + // Comment |
| 73 | + const message = `🚀 Hi @${user}!\n\nThank you for contributing to **MyCMD**. A maintainer will review your PR shortly. 🎉`; |
| 74 | +
|
| 75 | + await github.rest.issues.createComment({ |
| 76 | + owner: repo.owner, |
| 77 | + repo: repo.repo, |
| 78 | + issue_number: prNumber, |
| 79 | + body: message |
| 80 | + }); |
| 81 | +
|
| 82 | + await github.rest.reactions.createForIssue({ |
| 83 | + owner: repo.owner, |
| 84 | + repo: repo.repo, |
| 85 | + issue_number: prNumber, |
| 86 | + content: "rocket" |
| 87 | + }); |
| 88 | +
|
| 89 | + // Determine if PR is from a fork |
| 90 | + const headRepoFull = pr.head && pr.head.repo && pr.head.repo.full_name ? pr.head.repo.full_name : ''; |
| 91 | + const baseFull = `${repo.owner}/${repo.repo}`; |
| 92 | + const isFork = headRepoFull.toLowerCase() !== baseFull.toLowerCase(); |
| 93 | +
|
| 94 | + // Example labeling strategy: |
| 95 | + // - mark fork PRs with 'from-fork' so reviewers know it's external |
| 96 | + // - always mark with 'needs-review' |
| 97 | + const labels = ["needs-review"]; |
| 98 | + if (isFork) labels.push("from-fork"); |
| 99 | +
|
| 100 | + try { |
| 101 | + await github.rest.issues.addLabels({ |
| 102 | + owner: repo.owner, |
| 103 | + repo: repo.repo, |
| 104 | + issue_number: prNumber, |
| 105 | + labels: labels |
| 106 | + }); |
| 107 | + } catch (err) { |
| 108 | + core && core.info && core.info("Could not add labels to PR: " + err.message); |
| 109 | + } |
| 110 | +
|
| 111 | + return; |
| 112 | + } |
| 113 | +
|
| 114 | + // If neither payload present, do nothing |
| 115 | + core && core.info && core.info('No issue or pull_request payload found. Nothing to do.'); |
0 commit comments