-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain-package-coverage-comment.yml
More file actions
122 lines (106 loc) · 4.33 KB
/
main-package-coverage-comment.yml
File metadata and controls
122 lines (106 loc) · 4.33 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
name: Main Package Coverage Comment
on:
workflow_run:
workflows:
- Main Package Test
types:
- completed
jobs:
comment:
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' }}
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
issues: write
pull-requests: write
steps:
- name: Download coverage artifact from triggering run
uses: actions/github-script@v7
env:
ARTIFACT_NAME: main-package-coverage
ARTIFACT_ZIP: ${{ runner.temp }}/main-package-coverage.zip
with:
script: |
const fs = require('fs');
const runId = context.payload.workflow_run.id;
const artifactName = process.env.ARTIFACT_NAME;
const { data } = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId,
});
const artifact = data.artifacts.find((item) => item.name === artifactName);
if (!artifact) {
core.setFailed(`Artifact "${artifactName}" was not found for workflow run ${runId}.`);
return;
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
fs.writeFileSync(process.env.ARTIFACT_ZIP, Buffer.from(download.data));
- name: Extract coverage summary
run: |
mkdir -p "${RUNNER_TEMP}/main-package-coverage"
unzip -o "${RUNNER_TEMP}/main-package-coverage.zip" -d "${RUNNER_TEMP}/main-package-coverage"
- name: Comment coverage on pull request
uses: actions/github-script@v7
env:
COMMENT_MARKER: "<!-- main-package-coverage-comment -->"
COVERAGE_SUMMARY_PATH: ${{ runner.temp }}/main-package-coverage/coverage-summary.json
with:
script: |
const fs = require('fs');
const pr = context.payload.workflow_run.pull_requests[0];
if (!pr) {
core.info('No pull request is associated with this workflow run.');
return;
}
const summaryPath = process.env.COVERAGE_SUMMARY_PATH;
if (!fs.existsSync(summaryPath)) {
core.setFailed(`Coverage summary was not found at ${summaryPath}.`);
return;
}
const { total } = JSON.parse(fs.readFileSync(summaryPath, 'utf8'));
const formatMetric = (metric) => `${metric.pct.toFixed(2)}% (${metric.covered}/${metric.total})`;
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.payload.workflow_run.id}`;
const body = [
process.env.COMMENT_MARKER,
'## packages/main coverage',
'',
'| Metric | Result |',
'| --- | --- |',
`| Lines | ${formatMetric(total.lines)} |`,
`| Statements | ${formatMetric(total.statements)} |`,
`| Functions | ${formatMetric(total.functions)} |`,
`| Branches | ${formatMetric(total.branches)} |`,
'',
`[Workflow run](${runUrl})`,
].join('\n');
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
per_page: 100,
});
const existingComment = comments.find((comment) =>
comment.user?.type === 'Bot' && comment.body?.includes(process.env.COMMENT_MARKER)
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body,
});
return;
}
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body,
});