-
-
Notifications
You must be signed in to change notification settings - Fork 3
119 lines (107 loc) · 4.45 KB
/
Copy pathcoverage-comment.yml
File metadata and controls
119 lines (107 loc) · 4.45 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
name: Coverage Comment
on:
workflow_run:
workflows: [C++ CI]
types: [completed]
permissions:
actions: read
contents: read
pull-requests: write
concurrency:
group: coverage-comment-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }}
cancel-in-progress: true
jobs:
comment:
if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
steps:
- name: Download coverage summary
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: coverage-report
path: coverage-data
repository: ${{ github.repository }}
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Create or update coverage comment
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
LINE_COVERAGE_THRESHOLD: '80'
FUNCTION_COVERAGE_THRESHOLD: '80'
BRANCH_COVERAGE_THRESHOLD: '70'
with:
script: |
const fs = require('fs');
const summary = JSON.parse(fs.readFileSync('coverage-data/summary.json', 'utf8'));
function readMetric(prefix, label, threshold) {
const percent = Number(summary[`${prefix}_percent`]);
const covered = Number(summary[`${prefix}_covered`]);
const total = Number(summary[`${prefix}_total`]);
const valid = Number.isFinite(percent)
&& Number.isInteger(covered)
&& Number.isInteger(total)
&& percent >= 0
&& percent <= 100
&& covered >= 0
&& total >= covered;
if (!valid) {
throw new Error(`Invalid ${label} coverage summary`);
}
return { label, percent, covered, total, threshold };
}
const metrics = [
readMetric('line', 'Lines', Number(process.env.LINE_COVERAGE_THRESHOLD)),
readMetric('function', 'Functions', Number(process.env.FUNCTION_COVERAGE_THRESHOLD)),
readMetric('branch', 'Branches', Number(process.env.BRANCH_COVERAGE_THRESHOLD)),
];
const rows = metrics.map((metric) => {
const status = metric.percent >= metric.threshold ? 'PASS' : 'FAIL';
return `| ${status} | ${metric.label} | ${metric.percent.toFixed(2)}% (${metric.threshold}% target) | ${metric.covered} / ${metric.total} |`;
});
const run = context.payload.workflow_run;
const associatedPulls = await github.paginate(
github.rest.repos.listPullRequestsAssociatedWithCommit,
{
...context.repo,
commit_sha: run.head_sha,
per_page: 100,
},
);
const pull = associatedPulls.find((candidate) =>
candidate.state === 'open' && candidate.head.sha === run.head_sha);
if (!pull) {
core.notice(`No open pull request currently points to ${run.head_sha}`);
return;
}
const marker = '<!-- cpp-coverage-report -->';
const body = [
marker,
'## Coverage Report',
'',
'| Status | Category | Percentage | Covered / Total |',
'|:--:|---|---:|---:|',
...rows,
'',
`[Generated in workflow #${run.run_number}](${run.html_url}) for commit \`${run.head_sha.slice(0, 7)}\` by the C++ coverage workflow.`,
].join('\n');
const issue_number = pull.number;
const comments = await github.paginate(github.rest.issues.listComments, {
...context.repo,
issue_number,
per_page: 100,
});
const previous = comments.find((comment) =>
comment.user.type === 'Bot' && comment.body.includes(marker));
if (previous) {
await github.rest.issues.updateComment({
...context.repo,
comment_id: previous.id,
body,
});
} else {
await github.rest.issues.createComment({
...context.repo,
issue_number,
body,
});
}