-
Notifications
You must be signed in to change notification settings - Fork 4
100 lines (92 loc) · 3.53 KB
/
coverage-comment.yml
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
name: Coverage Report Comment
on:
workflow_run:
workflows:
- test
types:
- completed
workflow_dispatch:
defaults:
run:
shell: bash
jobs:
comment:
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
steps:
- name: Download coverage report
uses: actions/github-script@v7
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 matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == 'report'
})[0]
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
})
const fs = require('fs')
fs.writeFileSync('${{github.workspace}}/report.zip', Buffer.from(download.data))
- name: Unzip report
run: unzip report.zip
- name: Comment coverage result to Pull Requests
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs')
const baseReport = fs.readFileSync('report.txt', 'utf8').toString().split('\n')
let report = ''
for (let i = 0; i < baseReport.length; i++) {
const line = baseReport[i].split(' ').filter(v => v)
if (i === 1 && line.length === 1) {
report += "|:---|---:|---:|---:|\n"
} else if (line.length === 1) {
continue
} else {
if (i !== 0 && line.length === 4) {
const parcent = Number(line[3].replace("%", ""))
let color = 'green'
if (parcent < 50) {
color = 'red'
} else if (parcent < 90) {
color = 'orange'
}
line[3] = `![coverage-${parcent}%](https://img.shields.io/badge/coverage-${parcent}%25-${color}.svg)`
}
report += "|" + line.join("|") + "|\n"
}
if (line[0] === 'TOTAL') break
}
const issue_number = Number(fs.readFileSync('pr_num.txt'))
const body = `## Coverage Result\n\n<details>\n<summary>Resultを開く</summary>\n\n${report}\n</details>`
let listComments = await github.rest.issues.listComments({
issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
})
listComments = listComments.data.filter((comment) => {
return comment.body.includes('Coverage Result') && comment.user.login.includes('github-actions')
})
if (listComments.length === 0) {
github.rest.issues.createComment({
issue_number,
owner: context.repo.owner,
repo: context.repo.repo,
body,
})
} else {
github.rest.issues.updateComment({
comment_id: listComments[0].id,
owner: context.repo.owner,
repo: context.repo.repo,
body,
})
}