|
| 1 | +name: GitHubActionsBot |
| 2 | +on: |
| 3 | + issue_comment: |
| 4 | + types: |
| 5 | + - created |
| 6 | + |
| 7 | + # We need to be call in context of the main branch to have write permissions |
| 8 | + # "pull_request" target is called in context of a fork |
| 9 | + # "pull_request_target" is called in context of the repository but not necessary latest main |
| 10 | + workflow_run: |
| 11 | + workflows: |
| 12 | + - PullRequestOpened |
| 13 | + types: |
| 14 | + - completed |
| 15 | +jobs: |
| 16 | + hello-message: |
| 17 | + if: github.event_name == 'workflow_run' |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Download event.json |
| 21 | + run: gh run download "$WORKFLOW_ID" --repo "$REPO" --name event.json |
| 22 | + env: |
| 23 | + REPO: ${{ github.repository }} |
| 24 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 25 | + WORKFLOW_ID: ${{github.event.workflow_run.id}} |
| 26 | + |
| 27 | + - name: Add comment on PR |
| 28 | + uses: actions/github-script@v5 |
| 29 | + with: |
| 30 | + script: | |
| 31 | + const fs = require('fs'); |
| 32 | +
|
| 33 | + const event = JSON.parse(fs.readFileSync('./event.json', 'utf8')); |
| 34 | + github.rest.issues.createComment({ |
| 35 | + ...context.repo, |
| 36 | + issue_number: event.pull_request.number, |
| 37 | + body: |
| 38 | + `Hi @${event.sender.login}, I'm @github-actions bot happy to help you with this PR 👋\n\n` + |
| 39 | + process.env.SUPPORTED_COMMANDS, |
| 40 | + }) |
| 41 | + env: |
| 42 | + SUPPORTED_COMMANDS: | |
| 43 | + <details> |
| 44 | + <summary> Supported commands </summary> |
| 45 | +
|
| 46 | + Please post this commands in separate comments and only one per comment: |
| 47 | + * `@github-actions run-benchmark` - Run benchmark comparing base and merge commits for this PR |
| 48 | + * `@github-actions publish-pr-on-npm` - Build package from this PR and publish it on NPM |
| 49 | + </details> |
| 50 | +
|
| 51 | + accept-cmd: |
| 52 | + if: | |
| 53 | + github.event_name == 'issue_comment' && |
| 54 | + github.event.issue.pull_request && |
| 55 | + startsWith(github.event.comment.body, '@github-actions ') |
| 56 | + runs-on: ubuntu-latest |
| 57 | + outputs: |
| 58 | + cmd: ${{ steps.parse-cmd.outputs.cmd }} |
| 59 | + replyMessage: ${{ steps.parse-cmd.outputs.replyMessage }} |
| 60 | + pullRequestJSON: ${{ steps.get-pull_request-json.outputs.data }} |
| 61 | + steps: |
| 62 | + - uses: actions/github-script@v5 |
| 63 | + with: |
| 64 | + script: | |
| 65 | + github.rest.reactions.createForIssueComment({ |
| 66 | + ...context.repo, |
| 67 | + comment_id: context.payload.comment.id, |
| 68 | + content: 'eyes', |
| 69 | + }); |
| 70 | +
|
| 71 | + - id: parse-cmd |
| 72 | + uses: actions/github-script@v5 |
| 73 | + with: |
| 74 | + script: | |
| 75 | + const cmd = context.payload.comment.body.replace('@github-actions', '').trim(); |
| 76 | + core.setOutput('cmd', cmd); |
| 77 | +
|
| 78 | + - id: get-pull_request-json |
| 79 | + uses: octokit/request-action@v2.x |
| 80 | + with: |
| 81 | + route: GET ${{ github.event.issue.pull_request.url }} |
| 82 | + env: |
| 83 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 84 | + |
| 85 | + cmd-publish-pr-on-npm: |
| 86 | + needs: [accept-cmd] |
| 87 | + if: needs.accept-cmd.outputs.cmd == 'publish-pr-on-npm' |
| 88 | + uses: ./.github/workflows/cmd-publish-pr-on-npm.yml |
| 89 | + with: |
| 90 | + pullRequestJSON: ${{ needs.accept-cmd.outputs.pullRequestJSON }} |
| 91 | + |
| 92 | + cmd-run-benchmark: |
| 93 | + needs: [accept-cmd] |
| 94 | + if: needs.accept-cmd.outputs.cmd == 'run-benchmark' |
| 95 | + uses: ./.github/workflows/cmd-run-benchmark.yml |
| 96 | + with: |
| 97 | + pullRequestJSON: ${{ needs.accept-cmd.outputs.pullRequestJSON }} |
| 98 | + |
| 99 | + respond-to-cmd: |
| 100 | + needs: |
| 101 | + - cmd-publish-pr-on-npm |
| 102 | + - cmd-run-benchmark |
| 103 | + if: github.event_name == 'issue_comment' && always() |
| 104 | + runs-on: ubuntu-latest |
| 105 | + steps: |
| 106 | + - uses: actions/github-script@v5 |
| 107 | + with: |
| 108 | + script: | |
| 109 | + const { issue, comment, sender } = context.payload; |
| 110 | + const needs = JSON.parse(process.env.NEEDS); |
| 111 | +
|
| 112 | + let replyMessage; |
| 113 | + let allSkipped = true; |
| 114 | + for (const { result, outputs } of Object.values(needs)) { |
| 115 | + allSkipped = allSkipped && result === 'skipped'; |
| 116 | + replyMessage = replyMessage || outputs.replyMessage; |
| 117 | + } |
| 118 | +
|
| 119 | + if (!replyMessage) { |
| 120 | + replyMessage = allSkipped |
| 121 | + ? 'Unknown command, please check help message at the top of PR.' |
| 122 | + : `Something went wrong, please check logs here:\n${process.env.RUN_URL}`; |
| 123 | + } |
| 124 | +
|
| 125 | + const quoteRequest = comment.body |
| 126 | + .split('\n') |
| 127 | + .map((line) => '> ' + line) |
| 128 | + .join('\n'); |
| 129 | +
|
| 130 | + github.rest.issues.createComment({ |
| 131 | + ...context.repo, |
| 132 | + issue_number: issue.number, |
| 133 | + body: quoteRequest + `\n\n@${sender.login} ` + replyMessage, |
| 134 | + }); |
| 135 | +
|
| 136 | + // `github.rest` doesn't have this method :( so use graphql instead |
| 137 | + github.graphql(` |
| 138 | + mutation ($subjectId: ID!) { |
| 139 | + minimizeComment(input: { subjectId: $subjectId, classifier: RESOLVED}) |
| 140 | + { __typename } |
| 141 | + } |
| 142 | + `, { subjectId: comment.node_id }); |
| 143 | + env: |
| 144 | + RUN_URL: ${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}} |
| 145 | + NEEDS: ${{ toJSON(needs) }} |
0 commit comments