Repository navigation
148 lines (127 loc) · 5.41 KB
/
Copy pathcoverage-comment.yaml
File metadata and controls
148 lines (127 loc) · 5.41 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
###========================================
## Coverage PR Comment (via workflow_run)
#==========================================
# This workflow runs after the CI workflow completes and posts the
# test coverage report as a PR comment. It uses `workflow_run` so that
# it always executes in the context of the base repository with write
# permissions — this is required for fork-based PRs where the
# GITHUB_TOKEN is read-only.
#
# Ref: https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#workflow_run
name: Coverage comment
on:
workflow_run:
workflows: ["CI"]
types:
- completed
permissions:
pull-requests: write
jobs:
comment:
name: Post coverage comment
runs-on: ubuntu-latest
# Only run when the triggering workflow was a PR and produced artifacts.
if: >-
github.event.workflow_run.event == 'pull_request'
steps:
- name: Download coverage artifact
id: download
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const fs = require('fs');
// List artifacts for the triggering workflow run
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
const match = artifacts.data.artifacts.find(a => a.name === 'coverage-report');
if (!match) {
core.setOutput('found', 'false');
console.log('No coverage-report artifact found — skipping.');
return;
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: match.id,
archive_format: 'zip',
});
fs.writeFileSync('/tmp/coverage-report.zip', Buffer.from(download.data));
core.setOutput('found', 'true');
- name: Extract artifact
if: steps.download.outputs.found == 'true'
run: |
mkdir -p /tmp/coverage-artifact
unzip -o /tmp/coverage-report.zip -d /tmp/coverage-artifact
- name: Post PR comment
if: steps.download.outputs.found == 'true'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const fs = require('fs/promises')
// 1. Read artifact data
const prNumber = parseInt(
(await fs.readFile('/tmp/coverage-artifact/pr_number', 'utf8')).trim()
);
const threshold = (await fs.readFile('/tmp/coverage-artifact/threshold', 'utf8')).trim();
const newCovStr = (await fs.readFile('/tmp/coverage-artifact/new_coverage', 'utf8')).trim();
const prevCovStr = (await fs.readFile('/tmp/coverage-artifact/prev_coverage', 'utf8')).trim();
let reportContent = "No coverage report generated (Build likely failed).";
try {
reportContent = await fs.readFile('/tmp/coverage-artifact/coverage_report.txt', 'utf8');
} catch (e) {
console.log("Could not read coverage report file:", e.message);
}
// 2. Calculate status
const thresholdVal = parseFloat(threshold) || 0;
const newCov = parseFloat(newCovStr);
let overallStatus = "⚠️ SKIPPED (Build Failed?)";
if (!isNaN(newCov)) {
if (thresholdVal > newCov) {
overallStatus = `❌ FAIL: Coverage (${newCov}%) < Threshold (${thresholdVal}%)`;
} else {
overallStatus = "✅ PASS";
}
}
// 3. Build comment body
const comment = `### 🔬 Go Test Coverage Report
#### Summary
| Coverage Type | Result |
| ---------------------- | -------------------------------------- |
| Threshold | ${thresholdVal}% |
| Previous Test Coverage | ${prevCovStr || 'Unknown'}% |
| New Test Coverage | ${newCovStr || 'Unknown'}% |
#### Status
${overallStatus}
#### Detail
<details><summary>Show New Coverage</summary>
\`\`\`
${reportContent}
\`\`\`
</details>
`;
// 4. Minimize old bot comments
const { data: comments } = await github.rest.issues.listComments({
...context.repo,
issue_number: prNumber,
});
const botComments = comments.filter(c =>
c.user.type === 'Bot' &&
c.body.includes('Go Test Coverage Report')
);
for (const botComment of botComments) {
const query = `mutation {
minimizeComment(input: {classifier: OUTDATED, subjectId: "${botComment.node_id}"}) {
clientMutationId
}
}`
await github.graphql(query)
}
// 5. Create new comment
await github.rest.issues.createComment({
...context.repo,
issue_number: prNumber,
body: comment,
})