Debian Codegraph @

.github/workflows/SizeComment.yml

raw
1 name: SizeComment
2
3 # spell-checker:ignore zizmor backquote
4
5 on:
6 workflow_run:
7 workflows: ["make"]
8 types:
9 - completed # zizmor: ignore[dangerous-triggers]
10
11 permissions: {}
12 jobs:
13 post-comment:
14 permissions:
15 actions: read # to list workflow runs artifacts
16 pull-requests: write # to comment on pr
17
18 runs-on: ubuntu-latest
19 if: >
20 github.event.workflow_run.event == 'pull_request'
21 steps:
22 - name: 'Download artifact'
23 uses: actions/github-script@v9
24 with:
25 script: |
26 // List all artifacts from the make workflow run
27 var artifacts = await github.rest.actions.listWorkflowRunArtifacts({
28 owner: context.repo.owner,
29 repo: context.repo.repo,
30 run_id: ${{ github.event.workflow_run.id }},
31 });
32
33 // Download the "size-comment" artifact, which contains a PR
34 // number (NR) and result.txt produced by compare_size_results.py.
35 var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
36 return artifact.name == "size-comment"
37 })[0];
38 if (!matchArtifact) {
39 core.info("No size-comment artifact found; nothing to post.");
40 return;
41 }
42 var download = await github.rest.actions.downloadArtifact({
43 owner: context.repo.owner,
44 repo: context.repo.repo,
45 artifact_id: matchArtifact.id,
46 archive_format: 'zip',
47 });
48 var fs = require('fs');
49 fs.writeFileSync('${{ github.workspace }}/size-comment.zip', Buffer.from(download.data));
50 - name: 'Unzip artifact'
51 run: |
52 if [ -f size-comment.zip ]; then
53 unzip size-comment.zip
54 fi
55
56 - name: 'Comment on PR'
57 uses: actions/github-script@v9
58 with:
59 github-token: ${{ secrets.GITHUB_TOKEN }}
60 script: |
61 const fs = require('fs');
62 if (!fs.existsSync('./NR') || !fs.existsSync('./result.txt')) {
63 core.info("No size comment payload to post.");
64 return;
65 }
66 const issue_number = Number(fs.readFileSync('./NR'));
67 if (!issue_number) {
68 core.info("No PR number; skipping.");
69 return;
70 }
71 const content = fs.readFileSync('./result.txt').toString();
72
73 const marker = '<!-- size-comment-bot -->';
74 const body = `${marker}\nBinary size comparison:\n\`\`\`\n${content}\`\`\``;
75
76 const { data: comments } = await github.rest.issues.listComments({
77 ...context.repo,
78 issue_number
79 });
80
81 const existing = comments.find(c =>
82 c.user.login === 'github-actions[bot]' &&
83 c.body.includes(marker)
84 );
85
86 if (existing) {
87 await github.rest.issues.updateComment({
88 ...context.repo,
89 comment_id: existing.id,
90 body
91 });
92 } else {
93 // Only comment when there is something meaningful to say.
94 // compare_size_results.py only writes the file when there is a
95 // significant change, so an empty/short body is treated as
96 // "nothing to report".
97 if (content.trim().length == 0) {
98 return;
99 }
100 await github.rest.issues.createComment({
101 ...context.repo,
102 issue_number,
103 body
104 });
105 }