-
Notifications
You must be signed in to change notification settings - Fork 15
300 lines (263 loc) · 11.5 KB
/
on-push-comment.yaml
File metadata and controls
300 lines (263 loc) · 11.5 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
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
# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This workflow posts coverage comments on PRs after the main workflow completes.
# Uses workflow_run with guarded checkout to avoid executing untrusted code.
# See: https://securitylab.github.com/resources/github-actions-preventing-pwn-requests/
name: Post PR Comment
on:
workflow_run:
workflows: ["On Push Qualification"]
types:
- completed
permissions:
contents: read
jobs:
# COUPLING: This workflow depends on "On Push Qualification" producing these artifacts:
# - coverage-pr (coverage.out from PR build)
# - coverage-comment-data (JSON: coverage, threshold, pass, color, pr_number)
# - coverage-baseline (coverage.out from last successful main build)
# Renaming the workflow or these artifacts will silently break PR comments.
post-coverage-comment:
name: Post Coverage Comment
runs-on: ubuntu-latest
timeout-minutes: 10
# Run for PRs regardless of conclusion - coverage might exist even if other steps failed
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.head_repository.fork == false
permissions:
actions: read
contents: read
pull-requests: write
steps:
- name: Checkout for go.mod version
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
sparse-checkout: go.mod
sparse-checkout-cone-mode: false
persist-credentials: false
- name: Setup Go
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
with:
go-version-file: go.mod
cache: false
- name: Install go-coverage-report
run: go install github.com/fgrosse/go-coverage-report/cmd/go-coverage-report@v1.2.0
- name: Download PR Coverage
id: download-pr
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
continue-on-error: true
with:
name: coverage-pr
path: /tmp/pr-coverage
github-token: ${{ github.token }}
run-id: ${{ github.event.workflow_run.id }}
- name: Download Coverage Metadata
id: download-meta
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
continue-on-error: true
with:
name: coverage-comment-data
path: /tmp/coverage-comment-data
github-token: ${{ github.token }}
run-id: ${{ github.event.workflow_run.id }}
- name: Download Baseline Coverage
id: download-baseline
if: steps.download-pr.outcome == 'success'
env:
GH_TOKEN: ${{ github.token }}
run: |
set -e
mkdir -p /tmp/baseline-coverage
# Find last successful main run
LAST_RUN=$(gh run list \
--repo ${{ github.repository }} \
--workflow "On Push Qualification" \
--branch main \
--status success \
--event push \
--json databaseId \
--limit 1 \
-q '.[0].databaseId' 2>/dev/null || echo "")
if [[ -n "$LAST_RUN" ]]; then
echo "Found baseline run: $LAST_RUN"
if gh run download "$LAST_RUN" \
--repo ${{ github.repository }} \
--name coverage-baseline \
--dir /tmp/baseline-coverage 2>/dev/null; then
echo "baseline_found=true" >> $GITHUB_OUTPUT
echo "✅ Baseline coverage downloaded"
else
echo "baseline_found=false" >> $GITHUB_OUTPUT
echo "⚠️ Failed to download baseline (first run?)"
# Create empty baseline
echo "mode: set" > /tmp/baseline-coverage/coverage.out
fi
else
echo "baseline_found=false" >> $GITHUB_OUTPUT
echo "⚠️ No successful baseline run found"
echo "mode: set" > /tmp/baseline-coverage/coverage.out
fi
- name: Get Changed Files
id: changed-files
if: steps.download-pr.outcome == 'success'
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
mkdir -p .github/outputs
PR_URL="${{ github.event.workflow_run.pull_requests[0].url }}"
if [[ -z "$PR_URL" || "$PR_URL" == "null" ]]; then
echo "No PR URL found in workflow_run payload. Skipping changed files." >&2
echo "any_changed=false" >> "$GITHUB_OUTPUT"
exit 0
fi
FILES_JSON=$(curl -sS \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
"$PR_URL/files?per_page=300")
echo "$FILES_JSON" | jq -c '
[.[] |
select(.filename | endswith(".go")) |
select(.filename | test("^vendor/") | not) |
select(.filename | test("_test\\.go$") | not) |
.filename
]' > .github/outputs/all_modified_files.json
if [[ "$(cat .github/outputs/all_modified_files.json)" == "[]" ]]; then
echo "any_changed=false" >> "$GITHUB_OUTPUT"
else
echo "any_changed=true" >> "$GITHUB_OUTPUT"
fi
- name: Generate Coverage Delta Report
id: delta
if: steps.download-pr.outcome == 'success' && steps.changed-files.outputs.any_changed == 'true' && steps.download-baseline.outputs.baseline_found == 'true'
run: |
set -e
# Generate delta report
if go-coverage-report \
-root=github.com/NVIDIA/aicr \
/tmp/baseline-coverage/coverage.out \
/tmp/pr-coverage/coverage.out \
.github/outputs/all_modified_files.json > delta-report.md 2> delta-error.log; then
echo "delta_generated=true" >> $GITHUB_OUTPUT
else
echo "delta_generated=false" >> $GITHUB_OUTPUT
echo "⚠️ Delta generation failed:"
cat delta-error.log || true
fi
# Check if report shows no change (to reduce noise)
if grep -q "will \*\*not change\*\* overall coverage" delta-report.md 2>/dev/null; then
echo "no_change=true" >> $GITHUB_OUTPUT
else
echo "no_change=false" >> $GITHUB_OUTPUT
fi
- name: Post PR Comment
if: steps.download-pr.outcome == 'success' && steps.download-meta.outcome == 'success'
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('/tmp/coverage-comment-data/coverage-data.json', 'utf8'));
const coverage = data.coverage;
const threshold = data.threshold;
const pass = data.pass === 'true';
const color = data.color;
const prNumber = parseInt(data.pr_number, 10);
const status = pass ? '✅' : '❌';
const statusText = pass ? 'Pass' : 'Fail';
// Check if we have a delta report
let deltaSection = '';
const deltaGenerated = '${{ steps.delta.outputs.delta_generated }}' === 'true';
const noChange = '${{ steps.delta.outputs.no_change }}' === 'true';
const anyChanged = '${{ steps.changed-files.outputs.any_changed }}' === 'true';
const baselineFound = '${{ steps.download-baseline.outputs.baseline_found }}' === 'true';
if (deltaGenerated && !noChange) {
try {
const deltaReport = fs.readFileSync('delta-report.md', 'utf8');
deltaSection = `\n\n${deltaReport}`;
} catch (e) {
console.log('No delta report found');
}
} else if (!anyChanged) {
deltaSection = '\n\n*No Go source files changed in this PR.*';
} else if (noChange) {
deltaSection = '\n\n*Coverage unchanged by this PR.*';
} else if (!baselineFound) {
deltaSection = '\n\n*No baseline coverage available yet. Delta reporting will be available after the next main branch build.*';
}
const body = `## Coverage Report ${status}
| Metric | Value |
|--------|-------|
| Coverage | **${coverage}%** |
| Threshold | ${threshold}% |
| Status | ${statusText} |
<details>
<summary>Coverage Badge</summary>
\`\`\`

\`\`\`
</details>${deltaSection}`;
// Find existing comment
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('## Coverage Report')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body
});
console.log(`Updated existing comment ${botComment.id}`);
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: body
});
console.log(`Created new comment on PR #${prNumber}`);
}
- name: Post unavailable notice
if: steps.download-pr.outcome != 'success' || steps.download-meta.outcome != 'success'
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
// Get PR number from workflow_run payload
const prs = context.payload.workflow_run.pull_requests;
if (!prs || prs.length === 0) return;
const prNumber = prs[0].number;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('## Coverage Report')
);
// Only post if there's no existing coverage comment
if (!botComment) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: '## Coverage Report\n\nCoverage data unavailable for this run. This can happen if the qualification workflow was cancelled or failed before generating coverage artifacts.',
});
}