-
Notifications
You must be signed in to change notification settings - Fork 2k
159 lines (139 loc) · 5.3 KB
/
github-actions-bot.yml
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
name: GitHubActionsBot
on:
issue_comment:
types:
- created
# We need to be call in context of the main branch to have write permissions
# "pull_request" target is called in context of a fork
# "pull_request_target" is called in context of the repository but not necessary latest main
workflow_run:
workflows:
- PullRequestOpened
types:
- completed
env:
SUPPORTED_COMMANDS: |
<details>
<summary> Supported commands </summary>
Please post this commands in separate comments and only one per comment:
* `@github-actions run-benchmark` - Run benchmark comparing base and merge commits for this PR
* `@github-actions publish-pr-on-npm` - Build package from this PR and publish it on NPM
</details>
jobs:
hello-message:
if: github.event_name == 'workflow_run'
runs-on: ubuntu-latest
steps:
- name: Download event.json
run: gh run download "$WORKFLOW_ID" --repo "$REPO" --name event.json
env:
REPO: ${{ github.repository }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WORKFLOW_ID: ${{github.event.workflow_run.id}}
- name: Add comment on PR
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const event = JSON.parse(fs.readFileSync('./event.json', 'utf8'));
github.rest.issues.createComment({
...context.repo,
issue_number: event.pull_request.number,
body:
`Hi @${event.sender.login}, I'm @github-actions bot happy to help you with this PR 👋\n\n` +
process.env.SUPPORTED_COMMANDS,
})
accept-cmd:
if: |
github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
startsWith(github.event.comment.body, '@github-actions ')
runs-on: ubuntu-latest
outputs:
cmd: ${{ steps.parse-cmd.outputs.cmd }}
pullRequestJSON: ${{ steps.parse-cmd.outputs.pullRequestJSON }}
steps:
- uses: actions/github-script@v7
with:
script: |
github.rest.reactions.createForIssueComment({
...context.repo,
comment_id: context.payload.comment.id,
content: 'eyes',
});
- id: parse-cmd
uses: actions/github-script@v7
with:
script: |
const comment = context.payload.comment.body;
core.setOutput('cmd', comment.replace('@github-actions ', '').trim());
const { url } = context.payload.issue.pull_request;
const { data } = await github.request(url);
core.setOutput('pullRequestJSON', JSON.stringify(data, null, 2));
cmd-publish-pr-on-npm:
needs: [accept-cmd]
if: needs.accept-cmd.outputs.cmd == 'publish-pr-on-npm'
uses: ./.github/workflows/cmd-publish-pr-on-npm.yml
with:
pullRequestJSON: ${{ needs.accept-cmd.outputs.pullRequestJSON }}
secrets:
NPM_CANARY_PR_PUBLISH_TOKEN: ${{ secrets.NPM_CANARY_PR_PUBLISH_TOKEN }}
cmd-run-benchmark:
needs: [accept-cmd]
if: needs.accept-cmd.outputs.cmd == 'run-benchmark'
uses: ./.github/workflows/cmd-run-benchmark.yml
with:
pullRequestJSON: ${{ needs.accept-cmd.outputs.pullRequestJSON }}
respond-to-cmd:
needs:
- accept-cmd
- cmd-publish-pr-on-npm
- cmd-run-benchmark
if: needs.accept-cmd.result != 'skipped' && always()
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: replyMessage
- if: failure()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const needs = JSON.parse(process.env.NEEDS);
let allSkipped = true;
for (const [ name, job ] of Object.entries(needs)) {
if (name.startsWith('cmd-')) {
allSkipped = allSkipped && job.result === 'skipped';
}
}
const replyMessage = allSkipped
? 'Unknown command 😕\n\n' + process.env.SUPPORTED_COMMANDS
: `Something went wrong, [please check log](${process.env.RUN_URL}).`;
fs.writeFileSync('./replyMessage.txt', replyMessage, 'utf-8');
env:
NEEDS: ${{ toJSON(needs) }}
RUN_URL: ${{github.server_url}}/${{github.repository}}/actions/runs/${{github.run_id}}
- if: always()
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const replyMessage = fs.readFileSync('./replyMessage.txt', 'utf-8');
const { issue, comment, sender } = context.payload;
const quoteRequest = comment.body
.split('\n')
.map((line) => '> ' + line)
.join('\n');
github.rest.issues.createComment({
...context.repo,
issue_number: issue.number,
body: quoteRequest + `\n\n@${sender.login} ` + replyMessage,
});
// `github.rest` doesn't have this method :( so use graphql instead
github.graphql(`
mutation ($subjectId: ID!) {
minimizeComment(input: { subjectId: $subjectId, classifier: RESOLVED})
{ __typename }
}
`, { subjectId: comment.node_id });