-
Notifications
You must be signed in to change notification settings - Fork 12
140 lines (122 loc) · 4.42 KB
/
coverage-comment.yaml
File metadata and controls
140 lines (122 loc) · 4.42 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
name: Coverage Comment
on:
workflow_run:
workflows: ["CI"]
types:
- completed
jobs:
comment:
name: Post Coverage Comment
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request'
permissions:
pull-requests: write
actions: read
steps:
- name: Download coverage artifacts
uses: actions/github-script@v8
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.event.workflow_run.id }},
});
const coverageArtifact = artifacts.data.artifacts.find(artifact =>
artifact.name === 'coverage-report'
);
if (!coverageArtifact) {
console.log('No coverage artifact found');
return;
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: coverageArtifact.id,
archive_format: 'zip',
});
const fs = require('fs');
fs.writeFileSync('coverage-report.zip', Buffer.from(download.data));
- name: Extract and process coverage
run: |
if [ -f coverage-report.zip ]; then
unzip -q coverage-report.zip
if [ -f coverage-summary.md ]; then
echo "Coverage summary found"
cat coverage-summary.md
else
echo "No coverage summary found in artifact"
exit 1
fi
else
echo "No coverage artifact downloaded"
exit 1
fi
- name: Get PR number
id: pr
uses: actions/github-script@v8
with:
script: |
const { data: pullRequests } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: '${{ github.event.workflow_run.head_repository.owner.login }}:${{ github.event.workflow_run.head_branch }}',
state: 'open'
});
if (pullRequests.length > 0) {
return pullRequests[0].number;
}
console.log('No matching PR found');
return null;
- name: Comment PR with coverage
if: steps.pr.outputs.result != 'null'
uses: actions/github-script@v8
with:
script: |
const fs = require('fs');
const prNumber = ${{ steps.pr.outputs.result }};
if (!prNumber) {
console.log('No PR number available');
return;
}
let coverageSummary;
try {
coverageSummary = fs.readFileSync('coverage-summary.md', 'utf8');
} catch (error) {
console.log('Could not read coverage summary:', error.message);
return;
}
// Look for existing coverage comment
const comments = await github.rest.issues.listComments({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
});
const botComment = comments.data.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('📊 Test Coverage Report')
);
try {
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
comment_id: botComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: coverageSummary
});
console.log('Updated existing coverage comment');
} else {
// Create new comment
await github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: coverageSummary
});
console.log('Created new coverage comment');
}
} catch (error) {
console.log('Failed to post comment:', error.message);
console.log('Coverage information is available in workflow artifacts');
}