Skip to content

Commit

Permalink
feat: add ability to set a min threshold for coverage (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilianc authored May 14, 2024
1 parent aaef680 commit 07c5332
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ jobs:
make test
- name: Go Beautiful HTML Coverage
if: always()
uses: './'
with:
path: go-test-app-01/
threshold: 66.7

- name: Go Beautiful HTML Coverage
uses: './'
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ Once your test has ran and `cover.out` has been generated, the GHA does the foll
# The relative path of your go project. Useful for monorepos and custom folder structures.
# Default: ./
path: ''
# The minimum % of coverage required.
# Default: 0
threshold: ''
```

## Examples
Expand Down
16 changes: 15 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ inputs:
path:
description: The relative path of your go project. Useful for monorepos and custom folder structures.
default: './'
threshold:
description: The minimum % of coverage required.
default: '0'
runs:
using: composite
steps:
Expand Down Expand Up @@ -69,4 +72,15 @@ runs:
const script = require(`${process.env.GITHUB_ACTION_PATH}/src/update-comment.js`)
const revision = '${{ github.event.pull_request.head.sha || github.sha }}'
const path = '${{ inputs.path }}'
await script({ context, github }, path, revision)
const threshold = parseFloat('${{ inputs.threshold }}', 10)
await script({ context, github, path, revision, threshold })
- name: Check Coverage Threshold
uses: actions/github-script@v6
with:
github-token: ${{ inputs.token }}
script: |
const script = require(`${process.env.GITHUB_ACTION_PATH}/src/check-threshold.js`)
const revision = '${{ github.event.pull_request.head.sha || github.sha }}'
const threshold = parseFloat('${{ inputs.threshold }}', 10)
await script({ threshold, revision })
15 changes: 15 additions & 0 deletions src/check-threshold.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fs = require('fs')

const checkThreshold = module.exports = async ({ threshold, revision }) => {
const coverageText = fs.readFileSync(`go-cover/${revision}.txt`, 'utf8').split('\n').slice(0, -1)
const coverageTextSummary = coverageText[coverageText.length-1].split('\t').pop()

const coverage = parseFloat(coverageTextSummary.replace('%', ''), 10)

if (coverage < threshold) {
console.log(`\x1b[91m✘ coverage ${coverage}% < ${threshold}%`)
process.exit(1)
}

console.log(`\x1b[92m✔ coverage ${coverage}% >= ${threshold}%`)
}
41 changes: 29 additions & 12 deletions src/update-comment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs')

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

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

const commentBody = [
`<!-- coverage (${path})-->`,
`### [Code Coverage Report 🔗](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision})${pathText} at ${revision}`,
`##### ${coverageEmoji}<kbd>[🔗 Code Coverage Report](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision})</kbd>${pathText} at <kbd>${revision}</kbd>`,
'```',
`Total: ${coverageTextSummary}`,
'```',
'<details>',
'<summary>Full coverage report</summary>',
'',
'```',
...coverageText,
'```',
'</details>',
`📔 Total: ${coverageTextSummary}`,
]

if (threshold > 0) {
commentBody.push(
`🎯 Threshold: ${threshold}%`,
)

if (coverage >= threshold) {
commentBody.push(`✅ ${coverageTextSummary} >= ${threshold}%`)
} else {
commentBody.push(`❌ ${coverageTextSummary} < ${threshold}%`)
}
}

commentBody.push(
'```',
'<details>',
'<summary>Full coverage report</summary>',
'',
'```',
...coverageText,
'```',
'</details>',
)

const upsertCommentOptions = {
owner: context.repo.owner,
repo: context.repo.repo,
Expand Down

0 comments on commit 07c5332

Please sign in to comment.