-
Notifications
You must be signed in to change notification settings - Fork 120
95 lines (86 loc) · 3.26 KB
/
Copy pathcli-bench-comment.yml
File metadata and controls
95 lines (86 loc) · 3.26 KB
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
name: cli-bench-comment
on:
workflow_run:
workflows: [cli-bench]
types: [completed]
permissions:
pull-requests: write
actions: read
jobs:
comment:
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-slim
steps:
- name: ⏬ Download report from workflow run
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
name: cli-bench-report
path: bench-report
- name: 💬 Post or update comment
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
script: |
const { readFileSync } = require('node:fs');
// Resolve the PR from the triggering run's head SHA
const run = context.payload.workflow_run;
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: `${run.head_repository.owner.login}:${run.head_branch}`,
per_page: 10,
});
const pr = prs.find(pr => pr.head.sha === run.head_sha);
if (!pr) {
console.log(`no open PR found for ${run.head_sha}, skipping`);
return;
}
const prNumber = pr.number;
const marker = '<!-- cli-bench-report -->';
// Cap size and strip everything that could render as HTML, a link,
// or an image.
const sanitise = path => readFileSync(path, 'utf8')
.slice(0, 50_000)
.replace(/</g, '<')
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1')
.replace(/(?:https?:\/\/|www\.)\S+/gi, '[url removed]')
.replace(/\[/g, '\\[');
const body = [
marker,
'### CLI benchmark',
'',
sanitise('bench-report/summary.md'),
'<details>',
'<summary>Full report</summary>',
'',
sanitise('bench-report/report.md'),
'',
'</details>',
'',
'_Interleaved runs on a shared runner: trust the deltas, not the absolute timings. The `dev`, `restart` and `build` suites run locally via `pnpm bench:cli`._',
].join('\n');
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = comments.data.find(comment =>
comment.user.type === 'Bot' && comment.body.includes(marker)
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}