-
Notifications
You must be signed in to change notification settings - Fork 798
592 lines (524 loc) · 21.4 KB
/
cmd.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
name: Command
on:
issue_comment: # listen for comments on issues
types: [created]
permissions: # allow the action to comment on the PR
contents: read
issues: write
pull-requests: write
actions: read
jobs:
is-org-member:
if: startsWith(github.event.comment.body, '/cmd')
runs-on: ubuntu-latest
outputs:
member: ${{ steps.is-member.outputs.result }}
steps:
- name: Generate token
id: generate_token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.CMD_BOT_APP_ID }}
private-key: ${{ secrets.CMD_BOT_APP_KEY }}
- name: Check if user is a member of the organization
id: is-member
uses: actions/github-script@v7
with:
github-token: ${{ steps.generate_token.outputs.token }}
result-encoding: string
script: |
const fs = require("fs");
try {
const org = '${{ github.event.repository.owner.login }}';
const username = '${{ github.event.comment.user.login }}';
const membership = await github.rest.orgs.checkMembershipForUser({
org: org,
username: username
});
console.log(membership, membership.status, membership.status === 204);
if (membership.status === 204) {
return 'true';
} else {
console.log(membership);
fs.appendFileSync(process.env["GITHUB_STEP_SUMMARY"], `${membership.data && membership.data.message || 'Unknown error happened, please check logs'}`);
}
} catch (error) {
console.log(error)
}
return 'false';
acknowledge:
if: ${{ startsWith(github.event.comment.body, '/cmd') }}
runs-on: ubuntu-latest
steps:
- name: Add reaction to triggered comment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.reactions.createForIssueComment({
comment_id: ${{ github.event.comment.id }},
owner: context.repo.owner,
repo: context.repo.repo,
content: 'eyes'
})
clean:
runs-on: ubuntu-latest
steps:
- name: Clean previous comments
uses: actions/github-script@v7
if: ${{ startsWith(github.event.comment.body, '/cmd') && contains(github.event.comment.body, '--clean') }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.issues.listComments({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo
}).then(comments => {
for (let comment of comments.data) {
console.log(comment)
if (
${{ github.event.comment.id }} !== comment.id &&
(
(
(
comment.body.startsWith('Command') ||
comment.body.startsWith('<details><summary>Command') ||
comment.body.startsWith('Sorry, only ')
) && comment.user.type === 'Bot'
) ||
(comment.body.startsWith('/cmd') && comment.user.login === context.actor)
)
) {
github.rest.issues.deleteComment({
comment_id: comment.id,
owner: context.repo.owner,
repo: context.repo.repo
})
}
}
})
get-pr-info:
if: ${{ startsWith(github.event.comment.body, '/cmd') }}
runs-on: ubuntu-latest
outputs:
CMD: ${{ steps.get-comment.outputs.group2 }}
pr-branch: ${{ steps.get-pr.outputs.pr_branch }}
repo: ${{ steps.get-pr.outputs.repo }}
steps:
- name: Get command
uses: actions-ecosystem/action-regex-match@v2
id: get-comment
with:
text: ${{ github.event.comment.body }}
regex: "^(\\/cmd )([-\\/\\s\\w.=:]+)$" # see explanation in docs/contributor/commands-readme.md#examples
# Get PR branch name, because the issue_comment event does not contain the PR branch name
- name: Check if the issue is a PR
id: check-pr
run: |
if [ -n "${{ github.event.issue.pull_request.url }}" ]; then
echo "This is a pull request comment"
else
echo "This is not a pull request comment"
exit 1
fi
- name: Get PR Branch Name and Repo
if: steps.check-pr.outcome == 'success'
id: get-pr
uses: actions/github-script@v7
with:
script: |
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
const prBranch = pr.data.head.ref;
const repo = pr.data.head.repo.full_name;
console.log(prBranch, repo)
core.setOutput('pr_branch', prBranch);
core.setOutput('repo', repo);
- name: Use PR Branch Name and Repo
env:
PR_BRANCH: ${{ steps.get-pr.outputs.pr_branch }}
REPO: ${{ steps.get-pr.outputs.repo }}
CMD: ${{ steps.get-comment.outputs.group2 }}
run: |
echo "The PR branch is $PR_BRANCH"
echo "The repository is $REPO"
echo "The CMD is $CMD"
help:
needs: [clean, get-pr-info]
if: ${{ startsWith(github.event.comment.body, '/cmd') && contains(github.event.comment.body, '--help') }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Save output of help
id: help
env:
CMD: ${{ needs.get-pr-info.outputs.CMD }} # to avoid "" around the command
run: |
python3 -m pip install -r .github/scripts/generate-prdoc.requirements.txt
echo 'help<<EOF' >> $GITHUB_OUTPUT
python3 .github/scripts/cmd/cmd.py $CMD >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
- name: Comment PR (Help)
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `<details><summary>Command help:</summary>
\`\`\`
${{ steps.help.outputs.help }}
\`\`\`
</details>`
})
- name: Add confused reaction on failure
uses: actions/github-script@v7
if: ${{ failure() }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.reactions.createForIssueComment({
comment_id: ${{ github.event.comment.id }},
owner: context.repo.owner,
repo: context.repo.repo,
content: 'confused'
})
- name: Add 👍 reaction on success
uses: actions/github-script@v7
if: ${{ !failure() }}
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.reactions.createForIssueComment({
comment_id: ${{ github.event.comment.id }},
owner: context.repo.owner,
repo: context.repo.repo,
content: '+1'
})
set-image:
needs: [clean, get-pr-info]
if: ${{ startsWith(github.event.comment.body, '/cmd') && !contains(github.event.comment.body, '--help') }}
runs-on: ubuntu-latest
env:
CMD: ${{ needs.get-pr-info.outputs.CMD }}
outputs:
IMAGE: ${{ steps.set-image.outputs.IMAGE }}
RUNNER: ${{ steps.set-image.outputs.RUNNER }}
steps:
- name: Checkout
uses: actions/checkout@v4
- id: set-image
run: |
BODY=$(echo "$CMD" | xargs) # remove whitespace
IMAGE_OVERRIDE=$(echo $BODY | grep -oe 'docker.io/paritytech/ci-unified:.*\s' | xargs)
cat .github/env >> $GITHUB_OUTPUT
if [ -n "$IMAGE_OVERRIDE" ]; then
IMAGE=$IMAGE_OVERRIDE
echo "IMAGE=$IMAGE" >> $GITHUB_OUTPUT
fi
if [[ $BODY == "bench"* ]]; then
echo "RUNNER=parity-weights" >> $GITHUB_OUTPUT
elif [[ $BODY == "update-ui"* ]]; then
echo "RUNNER=parity-large" >> $GITHUB_OUTPUT
else
echo "RUNNER=ubuntu-latest" >> $GITHUB_OUTPUT
fi
- name: Print outputs
run: |
echo "RUNNER=${{ steps.set-image.outputs.RUNNER }}"
echo "IMAGE=${{ steps.set-image.outputs.IMAGE }}"
before-cmd:
needs: [set-image, get-pr-info]
runs-on: ubuntu-latest
env:
JOB_NAME: "cmd"
CMD: ${{ needs.get-pr-info.outputs.CMD }}
PR_BRANCH: ${{ needs.get-pr-info.outputs.pr-branch }}
outputs:
job_url: ${{ steps.build-link.outputs.job_url }}
run_url: ${{ steps.build-link.outputs.run_url }}
steps:
- name: Build workflow link
if: ${{ !contains(github.event.comment.body, '--quiet') }}
id: build-link
run: |
# Get exactly the CMD job link, filtering out the other jobs
jobLink=$(curl -s \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs | jq '.jobs[] | select(.name | contains("${{ env.JOB_NAME }}")) | .html_url')
runLink=$(curl -s \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }} | jq '.html_url')
echo "job_url=${jobLink}"
echo "run_url=${runLink}"
echo "job_url=$jobLink" >> $GITHUB_OUTPUT
echo "run_url=$runLink" >> $GITHUB_OUTPUT
- name: Comment PR (Start)
# No need to comment on prdoc start or if --quiet
if: ${{ !contains(github.event.comment.body, '--quiet') && !startsWith(needs.get-pr-info.outputs.CMD, 'prdoc') && !startsWith(needs.get-pr-info.outputs.CMD, 'fmt')}}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
let job_url = ${{ steps.build-link.outputs.job_url }}
let cmd = process.env.CMD;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Command "${cmd}" has started 🚀 [See logs here](${job_url})`
})
cmd:
needs: [before-cmd, set-image, get-pr-info, is-org-member]
env:
CMD: ${{ needs.get-pr-info.outputs.CMD }}
PR_BRANCH: ${{ needs.get-pr-info.outputs.pr-branch }}
runs-on: ${{ needs.set-image.outputs.RUNNER }}
container:
image: ${{ needs.set-image.outputs.IMAGE }}
timeout-minutes: 1440 # 24 hours per runtime
# lowerdown permissions to separate permissions context for executable parts by contributors
permissions:
contents: read
pull-requests: none
actions: none
issues: none
outputs:
cmd_output: ${{ steps.cmd.outputs.cmd_output }}
subweight: ${{ steps.subweight.outputs.result }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
repository: ${{ needs.get-pr-info.outputs.repo }}
ref: ${{ needs.get-pr-info.outputs.pr-branch }}
# In order to run prdoc without specifying the PR number, we need to add the PR number as an argument automatically
- name: Prepare PR Number argument
id: pr-arg
run: |
CMD="${{ needs.get-pr-info.outputs.CMD }}"
if echo "$CMD" | grep -q "prdoc" && ! echo "$CMD" | grep -qE "\-\-pr[[:space:]=][0-9]+"; then
echo "arg=--pr ${{ github.event.issue.number }}" >> $GITHUB_OUTPUT
else
echo "arg=" >> $GITHUB_OUTPUT
fi
- name: Install dependencies for bench
if: startsWith(needs.get-pr-info.outputs.CMD, 'bench')
run: |
cargo install --path substrate/utils/frame/omni-bencher --locked --profile production
- name: Run cmd
id: cmd
env:
PR_ARG: ${{ steps.pr-arg.outputs.arg }}
IS_ORG_MEMBER: ${{ needs.is-org-member.outputs.member }}
run: |
echo "Running command: '$CMD $PR_ARG' on '${{ needs.set-image.outputs.RUNNER }}' runner, container: '${{ needs.set-image.outputs.IMAGE }}'"
echo "RUST_NIGHTLY_VERSION: $RUST_NIGHTLY_VERSION"
echo "IS_ORG_MEMBER: $IS_ORG_MEMBER"
git config --global --add safe.directory $GITHUB_WORKSPACE
git config user.name "cmd[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
# if the user is not an org member, we need to use the bot's path from master to avoid unwanted modifications
if [ "$IS_ORG_MEMBER" = "true" ]; then
# safe to run commands from current branch
BOT_PATH=.github
else
# going to run commands from master
TMP_DIR=/tmp/polkadot-sdk
git clone --depth 1 --branch master https://github.com/paritytech/polkadot-sdk $TMP_DIR
BOT_PATH=$TMP_DIR/.github
fi
# install deps and run a command from master
python3 -m pip install -r $BOT_PATH/scripts/generate-prdoc.requirements.txt
python3 $BOT_PATH/scripts/cmd/cmd.py $CMD $PR_ARG
git status
git diff
if [ -f /tmp/cmd/command_output.log ]; then
CMD_OUTPUT=$(cat /tmp/cmd/command_output.log)
# export to summary to display in the PR
echo "$CMD_OUTPUT" >> $GITHUB_STEP_SUMMARY
# should be multiline, otherwise it captures the first line only
echo 'cmd_output<<EOF' >> $GITHUB_OUTPUT
echo "$CMD_OUTPUT" >> $GITHUB_OUTPUT
echo 'EOF' >> $GITHUB_OUTPUT
fi
git add -A
git diff HEAD > /tmp/cmd/command_diff.patch -U0
git commit -m "tmp cmd: $CMD" || true
# without push, as we're saving the diff to an artifact and subweight will compare the local branch with the remote branch
- name: Upload command output
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
name: command-output
path: /tmp/cmd/command_output.log
- name: Upload command diff
uses: actions/upload-artifact@v4
with:
name: command-diff
path: /tmp/cmd/command_diff.patch
- name: Install subweight for bench
if: startsWith(needs.get-pr-info.outputs.CMD, 'bench')
run: cargo install subweight
- name: Run Subweight for bench
id: subweight
if: startsWith(needs.get-pr-info.outputs.CMD, 'bench')
shell: bash
run: |
git fetch
git remote -v
echo $(git log -n 2 --oneline)
result=$(subweight compare commits \
--path-pattern "./**/weights/**/*.rs,./**/weights.rs" \
--method asymptotic \
--format markdown \
--no-color \
--change added changed \
--ignore-errors \
refs/remotes/origin/master $PR_BRANCH)
# Save the multiline result to the output
{
echo "result<<EOF"
echo "$result"
echo "EOF"
} >> $GITHUB_OUTPUT
after-cmd:
needs: [cmd, get-pr-info, before-cmd]
env:
CMD: ${{ needs.get-pr-info.outputs.CMD }}
PR_BRANCH: ${{ needs.get-pr-info.outputs.pr-branch }}
runs-on: ubuntu-latest
steps:
# needs to be able to trigger CI, as default token does not retrigger
- uses: actions/create-github-app-token@v1
id: generate_token
with:
app-id: ${{ secrets.CMD_BOT_APP_ID }}
private-key: ${{ secrets.CMD_BOT_APP_KEY }}
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ steps.generate_token.outputs.token }}
repository: ${{ needs.get-pr-info.outputs.repo }}
ref: ${{ needs.get-pr-info.outputs.pr-branch }}
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
name: command-diff
path: command-diff
- name: Apply & Commit changes
run: |
ls -lsa .
git config --global --add safe.directory $GITHUB_WORKSPACE
git config user.name "cmd[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global pull.rebase false
echo "Applying $file"
git apply "command-diff/command_diff.patch" --unidiff-zero --allow-empty
rm -rf command-diff
git status
if [ -n "$(git status --porcelain)" ]; then
git remote -v
push_changes() {
git push origin "HEAD:$PR_BRANCH"
}
git add .
git restore --staged Cargo.lock # ignore changes in Cargo.lock
git commit -m "Update from ${{ github.actor }} running command '$CMD'" || true
# Attempt to push changes
if ! push_changes; then
echo "Push failed, trying to rebase..."
git pull --rebase origin $PR_BRANCH
# After successful rebase, try pushing again
push_changes
fi
else
echo "Nothing to commit";
fi
- name: Comment PR (End)
# No need to comment on prdoc success or --quiet
if: ${{ needs.cmd.result == 'success' && !contains(github.event.comment.body, '--quiet') && !startsWith(needs.get-pr-info.outputs.CMD, 'prdoc') && !startsWith(needs.get-pr-info.outputs.CMD, 'fmt') }}
uses: actions/github-script@v7
env:
SUBWEIGHT: "${{ needs.cmd.outputs.subweight }}"
CMD_OUTPUT: "${{ needs.cmd.outputs.cmd_output }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
let runUrl = ${{ needs.before-cmd.outputs.run_url }}
let subweight = process.env.SUBWEIGHT || '';
let cmdOutput = process.env.CMD_OUTPUT || '';
let cmd = process.env.CMD;
console.log(cmdOutput);
let subweightCollapsed = subweight.trim() !== ''
? `<details>\n\n<summary>Subweight results:</summary>\n\n${subweight}\n\n</details>`
: '';
let cmdOutputCollapsed = cmdOutput.trim() !== ''
? `<details>\n\n<summary>Command output:</summary>\n\n${cmdOutput}\n\n</details>`
: '';
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Command "${cmd}" has finished ✅ [See logs here](${runUrl})${subweightCollapsed}${cmdOutputCollapsed}`
})
finish:
needs: [get-pr-info, before-cmd, after-cmd, cmd]
if: ${{ always() }}
runs-on: ubuntu-latest
env:
CMD_OUTPUT: "${{ needs.cmd.outputs.cmd_output }}"
CMD: ${{ needs.get-pr-info.outputs.CMD }}
steps:
- name: Comment PR (Failure)
if: ${{ needs.cmd.result == 'failure' || needs.after-cmd.result == 'failure' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
let jobUrl = ${{ needs.before-cmd.outputs.job_url }}
let cmdOutput = process.env.CMD_OUTPUT;
let cmd = process.env.CMD;
let cmdOutputCollapsed = '';
if (cmdOutput && cmdOutput.trim() !== '') {
cmdOutputCollapsed = `<details>\n\n<summary>Command output:</summary>\n\n${cmdOutput}\n\n</details>`
}
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Command "${cmd}" has failed ❌! [See logs here](${jobUrl})${cmdOutputCollapsed}`
})
- name: Add 😕 reaction on failure
if: ${{ needs.cmd.result == 'failure' || needs.after-cmd.result == 'failure' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.reactions.createForIssueComment({
comment_id: ${{ github.event.comment.id }},
owner: context.repo.owner,
repo: context.repo.repo,
content: 'confused'
})
- name: Add 👍 reaction on success
if: ${{ needs.cmd.result == 'success' && needs.after-cmd.result == 'success' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.rest.reactions.createForIssueComment({
comment_id: ${{ github.event.comment.id }},
owner: context.repo.owner,
repo: context.repo.repo,
content: '+1'
})