Skip to content

Commit 07c5332

Browse files
authored
feat: add ability to set a min threshold for coverage (#32)
1 parent aaef680 commit 07c5332

File tree

5 files changed

+65
-13
lines changed

5 files changed

+65
-13
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ jobs:
2727
make test
2828
2929
- name: Go Beautiful HTML Coverage
30+
if: always()
3031
uses: './'
3132
with:
3233
path: go-test-app-01/
34+
threshold: 66.7
3335

3436
- name: Go Beautiful HTML Coverage
3537
uses: './'

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ Once your test has ran and `cover.out` has been generated, the GHA does the foll
8686
# The relative path of your go project. Useful for monorepos and custom folder structures.
8787
# Default: ./
8888
path: ''
89+
90+
# The minimum % of coverage required.
91+
# Default: 0
92+
threshold: ''
8993
```
9094

9195
## Examples

action.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ inputs:
1717
path:
1818
description: The relative path of your go project. Useful for monorepos and custom folder structures.
1919
default: './'
20+
threshold:
21+
description: The minimum % of coverage required.
22+
default: '0'
2023
runs:
2124
using: composite
2225
steps:
@@ -69,4 +72,15 @@ runs:
6972
const script = require(`${process.env.GITHUB_ACTION_PATH}/src/update-comment.js`)
7073
const revision = '${{ github.event.pull_request.head.sha || github.sha }}'
7174
const path = '${{ inputs.path }}'
72-
await script({ context, github }, path, revision)
75+
const threshold = parseFloat('${{ inputs.threshold }}', 10)
76+
await script({ context, github, path, revision, threshold })
77+
78+
- name: Check Coverage Threshold
79+
uses: actions/github-script@v6
80+
with:
81+
github-token: ${{ inputs.token }}
82+
script: |
83+
const script = require(`${process.env.GITHUB_ACTION_PATH}/src/check-threshold.js`)
84+
const revision = '${{ github.event.pull_request.head.sha || github.sha }}'
85+
const threshold = parseFloat('${{ inputs.threshold }}', 10)
86+
await script({ threshold, revision })

src/check-threshold.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const fs = require('fs')
2+
3+
const checkThreshold = module.exports = async ({ threshold, revision }) => {
4+
const coverageText = fs.readFileSync(`go-cover/${revision}.txt`, 'utf8').split('\n').slice(0, -1)
5+
const coverageTextSummary = coverageText[coverageText.length-1].split('\t').pop()
6+
7+
const coverage = parseFloat(coverageTextSummary.replace('%', ''), 10)
8+
9+
if (coverage < threshold) {
10+
console.log(`\x1b[91m✘ coverage ${coverage}% < ${threshold}%`)
11+
process.exit(1)
12+
}
13+
14+
console.log(`\x1b[92m✔ coverage ${coverage}% >= ${threshold}%`)
15+
}

src/update-comment.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('fs')
22

3-
const updateCodeCoverageComment = module.exports = async ({ context, github }, path, revision) => {
3+
const updateCodeCoverageComment = module.exports = async ({ context, github, path, revision, threshold }) => {
44
const comments = await github.rest.issues.listComments({
55
owner: context.repo.owner,
66
repo: context.repo.repo,
@@ -14,23 +14,40 @@ const updateCodeCoverageComment = module.exports = async ({ context, github }, p
1414

1515
const coverageText = fs.readFileSync(`go-cover/${revision}.txt`, 'utf8').split('\n').slice(0, -1)
1616
const coverageTextSummary = coverageText[coverageText.length-1].split('\t').pop()
17-
const pathText = (path !== './' ? ` for \`${path}/\`` : '').replace('//', '/')
17+
const coverage = parseFloat(coverageTextSummary.replace('%', ''), 10)
18+
const coverageEmoji = coverage >= threshold ? '' : `<kbd>🔻 ${(coverage - threshold).toFixed(1)}%</kbd> `
19+
const pathText = (path !== './' ? ` for <kbd>${path}/</kbd>` : '').replace('//', '/')
1820

1921
const commentBody = [
2022
`<!-- coverage (${path})-->`,
21-
`### [Code Coverage Report 🔗](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision})${pathText} at ${revision}`,
23+
`##### ${coverageEmoji}<kbd>[🔗 Code Coverage Report](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision})</kbd>${pathText} at <kbd>${revision}</kbd>`,
2224
'```',
23-
`Total: ${coverageTextSummary}`,
24-
'```',
25-
'<details>',
26-
'<summary>Full coverage report</summary>',
27-
'',
28-
'```',
29-
...coverageText,
30-
'```',
31-
'</details>',
25+
`📔 Total: ${coverageTextSummary}`,
3226
]
3327

28+
if (threshold > 0) {
29+
commentBody.push(
30+
`🎯 Threshold: ${threshold}%`,
31+
)
32+
33+
if (coverage >= threshold) {
34+
commentBody.push(`✅ ${coverageTextSummary} >= ${threshold}%`)
35+
} else {
36+
commentBody.push(`❌ ${coverageTextSummary} < ${threshold}%`)
37+
}
38+
}
39+
40+
commentBody.push(
41+
'```',
42+
'<details>',
43+
'<summary>Full coverage report</summary>',
44+
'',
45+
'```',
46+
...coverageText,
47+
'```',
48+
'</details>',
49+
)
50+
3451
const upsertCommentOptions = {
3552
owner: context.repo.owner,
3653
repo: context.repo.repo,

0 commit comments

Comments
 (0)