[HELP] How to use GitHub bot #10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: 🤖 Auto Reply to Issues & PRs | |
| on: | |
| issues: | |
| types: [opened, reopened] | |
| # Use pull_request_target so this job can write comments/labels on PRs from forks. | |
| # SECURITY: Do NOT check out or execute untrusted PR code in this workflow. | |
| pull_request_target: | |
| types: [opened, reopened] | |
| permissions: | |
| issues: write | |
| pull-requests: write | |
| jobs: | |
| auto-comment: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: 🧠 Auto Comment | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const repo = context.repo; | |
| // Issue opened/reopened | |
| if (context.payload.issue) { | |
| const issue = context.payload.issue; | |
| const user = issue.user?.login || 'contributor'; | |
| const issueNumber = issue.number; | |
| 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.`; | |
| await github.rest.issues.createComment({ | |
| owner: repo.owner, | |
| repo: repo.repo, | |
| issue_number: issueNumber, | |
| body: message | |
| }); | |
| await github.rest.reactions.createForIssue({ | |
| owner: repo.owner, | |
| repo: repo.repo, | |
| issue_number: issueNumber, | |
| content: "tada" | |
| }); | |
| try { | |
| await github.rest.issues.addLabels({ | |
| owner: repo.owner, | |
| repo: repo.repo, | |
| issue_number: issueNumber, | |
| labels: ["triage", "needs-info"] | |
| }); | |
| } catch (err) { | |
| core && core.info && core.info("Could not add labels to issue: " + err.message); | |
| } | |
| return; | |
| } | |
| // Pull request opened/reopened (or synchronized) | |
| if (context.payload.pull_request) { | |
| const pr = context.payload.pull_request; | |
| const user = pr.user?.login || 'contributor'; | |
| const prNumber = pr.number; | |
| const message = `🚀 Hi @${user}!\n\nThank you for contributing to **MyCMD**. A maintainer will review your PR shortly. 🎉`; | |
| await github.rest.issues.createComment({ | |
| owner: repo.owner, | |
| repo: repo.repo, | |
| issue_number: prNumber, | |
| body: message | |
| }); | |
| await github.rest.reactions.createForIssue({ | |
| owner: repo.owner, | |
| repo: repo.repo, | |
| issue_number: prNumber, | |
| content: "rocket" | |
| }); | |
| const headRepoFull = pr.head && pr.head.repo && pr.head.repo.full_name ? pr.head.repo.full_name : ''; | |
| const baseFull = `${repo.owner}/${repo.repo}`; | |
| const isFork = headRepoFull.toLowerCase() !== baseFull.toLowerCase(); | |
| const labels = ["needs-review"]; | |
| if (isFork) labels.push("from-fork"); | |
| try { | |
| await github.rest.issues.addLabels({ | |
| owner: repo.owner, | |
| repo: repo.repo, | |
| issue_number: prNumber, | |
| labels: labels | |
| }); | |
| } catch (err) { | |
| core && core.info && core.info("Could not add labels to PR: " + err.message); | |
| } | |
| return; | |
| } | |
| core && core.info && core.info('No issue or pull_request payload found. Nothing to do.'); |