forked from bytedance/sonic-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
123 lines (112 loc) · 4.24 KB
/
Copy pathbenchmark_comment.yml
File metadata and controls
123 lines (112 loc) · 4.24 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
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
name: CI / Benchmark Comment
permissions:
actions: read
contents: read
issues: write
pull-requests: write
defaults:
run:
shell: bash
on:
workflow_run:
workflows: ["CI / Benchmark Compare"]
types: [completed]
jobs:
benchmark-comment:
if: github.event.workflow_run.event == 'pull_request'
runs-on: ubuntu-24.04
timeout-minutes: 10
steps:
- name: Find benchmark artifact
id: artifact
uses: actions/github-script@v7
with:
script: |
const run = context.payload.workflow_run;
const {owner, repo} = context.repo;
let pr = run.pull_requests && run.pull_requests[0];
if (!pr) {
const prs = await github.paginate(
github.rest.repos.listPullRequestsAssociatedWithCommit,
{owner, repo, commit_sha: run.head_sha});
pr = prs.find((item) => item.state === 'open') || prs[0];
}
if (!pr) {
core.setOutput('skip', 'true');
return;
}
const artifacts = await github.paginate(
github.rest.actions.listWorkflowRunArtifacts,
{owner, repo, run_id: run.id});
const artifact = artifacts.find((item) =>
item.name === 'benchmark-compare' && !item.expired);
if (!artifact) {
core.setOutput('skip', 'true');
return;
}
core.setOutput('skip', 'false');
core.setOutput('pr_number', String(pr.number));
core.setOutput('artifact_id', String(artifact.id));
- name: Download benchmark artifact
if: steps.artifact.outputs.skip != 'true'
env:
GH_TOKEN: ${{ github.token }}
ARTIFACT_ID: ${{ steps.artifact.outputs.artifact_id }}
REPOSITORY: ${{ github.repository }}
run: |
gh api "repos/${REPOSITORY}/actions/artifacts/${ARTIFACT_ID}/zip" > "$RUNNER_TEMP/benchmark-compare.zip"
python3 - <<'PY'
import os
import zipfile
zip_path = os.path.join(os.environ["RUNNER_TEMP"], "benchmark-compare.zip")
out_path = os.path.join(os.environ["RUNNER_TEMP"], "benchmark_comment.md")
with zipfile.ZipFile(zip_path) as archive:
names = [
name for name in archive.namelist()
if name.endswith("benchmark_comment.md")
or name.endswith("benchmark_summary.md")
]
if not names:
raise SystemExit("benchmark markdown result not found in artifact")
with archive.open(names[0]) as src, open(out_path, "wb") as dst:
dst.write(src.read())
PY
- name: Comment benchmark result
if: steps.artifact.outputs.skip != 'true'
uses: actions/github-script@v7
env:
PR_NUMBER: ${{ steps.artifact.outputs.pr_number }}
with:
script: |
const fs = require('fs');
const path = require('path');
const marker = '<!-- sonic-benchmark-compare -->';
const commentPath = path.join(process.env.RUNNER_TEMP, 'benchmark_comment.md');
const result = fs.existsSync(commentPath)
? fs.readFileSync(commentPath, 'utf8')
: '## Benchmark\n\nBenchmark comparison did not produce a result. Check the workflow logs and artifacts.';
const body = `${marker}\n${result}`;
const {owner, repo} = context.repo;
const issue_number = Number(process.env.PR_NUMBER);
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
});
const existing = comments.find((comment) =>
comment.body && comment.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
}