-
Notifications
You must be signed in to change notification settings - Fork 38
189 lines (180 loc) · 6 KB
/
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
on:
issue_comment:
types: [created]
name: Bot
permissions: {}
# This workflow only runs for pull request comments
jobs:
pr_pre_comment:
name: Comment before taking snapshots
if: ${{ github.event.issue.pull_request && github.event.comment.body == '/snapshot' }}
runs-on: ubuntu-latest
timeout-minutes: 1
permissions:
issues: write
pull-requests: write
steps:
- uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "Start taking snapshots for this pull request. Please note that this process requires manual approval from a maintainer so that you may need to wait hours. Please be patient.\n" +
`https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`
});
pr_snapshot_approval:
name: Wait approval for bot execution
needs: [pr_pre_comment]
runs-on: ubuntu-latest
permissions: {}
timeout-minutes: 1
environment: bot
steps:
- run: echo "Approved by a maintainer"
pr_snapshot:
name: Generate snapshots
needs: [pr_snapshot_approval]
timeout-minutes: 15
if: ${{ github.event.issue.pull_request && github.event.comment.body == '/snapshot' }}
strategy:
matrix:
name: [windows, linux]
include:
- name: windows
os: windows-2022
- name: linux
os: ubuntu-latest
runs-on: ${{ matrix.os }}
permissions:
contents: read
outputs:
linux: ${{ steps.gen_patch.outputs.linux }}
windows: ${{ steps.gen_patch.outputs.windows }}
env:
# Set TRYCMD=overwrite to update snapshot
TRYCMD: overwrite
steps:
- name: Determine a target repository and a commit hash from the pull request
uses: actions/github-script@v7
id: target-branch
with:
result-encoding: json
script: |
const pull_request = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
console.log("Target repo: " + pull_request.data.head.repo.full_name);
console.log("Target ref: " + pull_request.data.head.ref);
return { "repo": pull_request.data.head.repo.full_name, "ref": pull_request.data.head.ref };
- name: Checkout the target repository
uses: actions/checkout@v4
with:
repository: ${{ fromJSON(steps.target-branch.outputs.result).repo }}
ref: ${{ fromJSON(steps.target-branch.outputs.result).ref }}
- name: Install rust toolchain
run: |
rustup set profile minimal
rustup install stable
rustup component add rustfmt clippy
- name: Load cargo project cache
uses: Swatinem/rust-cache@640a22190e7a783d4c409684cea558f081f92012
# This is the most recent commit as of 2024-03-22.
# Also, it uses Node.js 20.
with:
shared-key: pr-snapshot-commented-${{ matrix.name }}
- name: Generate a new snapshot
run: |
cargo test cli_tests
- id: gen_patch
name: Generate patches
uses: actions/github-script@v7
with:
script: |
// Create a diff and comment it on the pull request.
let stdOutput = '';
let errOutput = '';
const options = {};
options.listeners = {
stdout: (data) => {
stdOutput += data.toString();
},
stderr: (data) => {
errOutput += data.toString();
}
};
const code = await exec.exec('git', ['diff', '--patch', '--indent-heuristic'], options);
// Output diff
if (stdOutput !== '') {
console.log("-- begin stdout --");
console.log(stdOutput);
console.log("-- end stdout --");
}
// Output error
if (errOutput !== '') {
console.log("-- begin stderr --");
console.log(errOutput);
console.log("-- end stderr --");
}
if (stdOutput !== '') {
const body = `You can apply a snapshot for ${{ matrix.name }} using \`git apply <diff-file>\`. The patch file is the following.
<details>
<summary>git diff</summary>
\`\`\`diff
${stdOutput}
\`\`\`
</details>`;
core.setOutput('${{ matrix.name }}', body);
}
pr_post_snapshot:
name: Comment snapshots on the pull request
needs: [pr_snapshot]
timeout-minutes: 1
strategy:
matrix:
name: [windows, linux]
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
env:
linux: ${{needs.pr_snapshot.outputs.linux}}
windows: ${{needs.pr_snapshot.outputs.windows}}
steps:
- name: Comment the generated patch to the pull request
uses: actions/github-script@v7
with:
script: |
const body = process.env.${{ matrix.name }};
if (body !== '') {
console.log("There are differences. Creating a comment...");
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body,
});
} else {
console.log("There are no differences. Skipping...");
}
pr_post_comment:
name: Comment after taking snapshots
needs: [pr_post_snapshot]
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
timeout-minutes: 1
steps:
- uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Taking snapshots has been completed.'
});