forked from nasa/spacewasm
-
Notifications
You must be signed in to change notification settings - Fork 0
152 lines (132 loc) · 5.68 KB
/
Copy pathcomment.yml
File metadata and controls
152 lines (132 loc) · 5.68 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
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
name: PR Comments
on:
workflow_run:
workflows: ["CI"]
types:
- completed
permissions:
pull-requests: write
contents: read
jobs:
comment:
name: Post PR comments
runs-on: ubuntu-latest
# Only act on CI runs that were triggered by a pull request.
if: github.event.workflow_run.event == 'pull_request'
steps:
- name: Download CI artifacts
uses: actions/download-artifact@v4
with:
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
path: artifacts
# The benchmark/coverage artifacts may not both exist (e.g. one job
# failed); tolerate missing ones instead of failing the whole run.
continue-on-error: true
- name: Post benchmark and coverage comments
uses: actions/github-script@v7
env:
TRUSTED_PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
with:
script: |
const fs = require('fs');
const prNumber = parseInt(process.env.TRUSTED_PR_NUMBER, 10);
if (!Number.isInteger(prNumber) || prNumber <= 0) {
console.log('No trusted PR number on the workflow_run event; skipping comments.');
return;
}
// Reads an artifact JSON file if it was downloaded, else null.
function readResults(name) {
const path = `artifacts/${name}/${name}.json`;
if (!fs.existsSync(path)) {
console.log(`No ${name} artifact found; skipping.`);
return null;
}
return JSON.parse(fs.readFileSync(path, 'utf8'));
}
// Parses an artifact-supplied numeric field. Returns null for any
// value that is not a finite number so untrusted content can never
// reach the comment body verbatim.
function finiteOrNull(value) {
const n = parseFloat(value);
return Number.isFinite(n) ? n : null;
}
// Creates a new comment or updates the existing bot comment that
// contains `marker` in its body.
async function upsertComment(prNumber, marker, body) {
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' && c.body.includes(marker)
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}
}
const benchmark = readResults('benchmark-results');
if (benchmark) {
const currentScore = finiteOrNull(benchmark.currentScore);
const baselineScore = finiteOrNull(benchmark.baselineScore);
if (currentScore === null) {
console.log('Benchmark artifact has no valid currentScore; skipping.');
} else {
let comment = '## CoreMark Benchmark Results\n\n';
comment += `**Current Score:** ${currentScore.toFixed(3)}\n`;
if (baselineScore) {
const diff = currentScore - baselineScore;
const percentChange = ((diff / baselineScore) * 100).toFixed(2);
comment += `**Baseline Score (main):** ${baselineScore.toFixed(3)}\n`;
comment += `**Difference:** ${diff >= 0 ? '+' : ''}${diff.toFixed(3)} (${percentChange}%)\n\n`;
} else {
comment += '\n_No baseline available for comparison_\n';
}
await upsertComment(
prNumber,
'CoreMark Benchmark Results',
comment
);
}
}
const coverage = readResults('coverage-results');
if (coverage) {
const currentCoverage = finiteOrNull(coverage.currentCoverage);
const baselineCoverage = finiteOrNull(coverage.baselineCoverage);
if (currentCoverage === null) {
console.log('Coverage artifact has no valid currentCoverage; skipping.');
} else {
let comment = '## Code Coverage Report\n\n';
comment += `**Current Coverage:** ${currentCoverage.toFixed(2)}%\n`;
if (baselineCoverage) {
const diff = currentCoverage - baselineCoverage;
comment += `**Baseline Coverage (main):** ${baselineCoverage.toFixed(2)}%\n`;
comment += `**Difference:** ${diff >= 0 ? '+' : ''}${diff.toFixed(2)}%\n\n`;
if (diff < -1.0) {
comment += '**Warning:** Coverage decreased by more than 1%\n';
} else if (diff >= 1.0) {
comment += '**Improvement:** Coverage increased!\n';
}
} else {
comment += '\n_No baseline available for comparison_\n';
}
await upsertComment(
prNumber,
'Code Coverage Report',
comment
);
}
}