From dee628683f8e7b17c50dc863adb63ad779787ebe Mon Sep 17 00:00:00 2001 From: Kilian Ciuffolo Date: Tue, 14 May 2024 15:34:51 -0700 Subject: [PATCH 1/6] feat: add ability to set a min threshold for coverage closes #13 --- .github/workflows/ci.yaml | 10 ++++++++++ README.md | 4 ++++ action.yaml | 16 +++++++++++++++- src/check-threshold.js | 15 +++++++++++++++ src/update-comment.js | 39 ++++++++++++++++++++++++++++----------- 5 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 src/check-threshold.js diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 735ee33..7cbfc12 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -25,13 +25,23 @@ jobs: run: | cd go-test-app-01 make test + cp cover.out ../cover.out - name: Go Beautiful HTML Coverage + if: always() + uses: './' + with: + threshold: 80 + + - name: Go Beautiful HTML Coverage + if: always() uses: './' with: path: go-test-app-01/ + threshold: 80 - name: Go Beautiful HTML Coverage + if: always() uses: './' with: path: go-test-app-02/ diff --git a/README.md b/README.md index 99b39dc..3e6620c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.yaml b/action.yaml index 89e3cb8..d3c95d0 100644 --- a/action.yaml +++ b/action.yaml @@ -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: @@ -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 }) diff --git a/src/check-threshold.js b/src/check-threshold.js new file mode 100644 index 0000000..036165a --- /dev/null +++ b/src/check-threshold.js @@ -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}%`) +} diff --git a/src/update-comment.js b/src/update-comment.js index 85183ff..9a88ff0 100644 --- a/src/update-comment.js +++ b/src/update-comment.js @@ -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, @@ -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 coverage = parseFloat(coverageTextSummary.replace('%', ''), 10) + const coverageEmoji = coverage >= threshold ? '' : '🔻 ' const pathText = (path !== './' ? ` for \`${path}/\`` : '').replace('//', '/') const commentBody = [ ``, - `### [Code Coverage Report 🔗](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision})${pathText} at ${revision}`, + `# ${coverageEmoji}[🔗 Code Coverage Report](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision})${pathText} at ${revision}`, '```', - `Total: ${coverageTextSummary}`, - '```', - '
', - 'Full coverage report', - '', - '```', - ...coverageText, - '```', - '
', + `📔 Total: ${coverageTextSummary}`, ] + if (threshold > 0) { + commentBody.push( + `🎯 Threshold: ${threshold}%`, + ) + + if (coverage >= threshold) { + commentBody.push(`✅ ${coverageTextSummary} >= ${threshold}%`) + } else { + commentBody.push(`❌ ${coverageTextSummary} < ${threshold}%`) + } + } + + commentBody.push( + '```', + '
', + 'Full coverage report', + '', + '```', + ...coverageText, + '```', + '
', + ) + const upsertCommentOptions = { owner: context.repo.owner, repo: context.repo.repo, From 7403370c8d4294af3ca4465de51c7f80e3455a4a Mon Sep 17 00:00:00 2001 From: Kilian Ciuffolo Date: Tue, 14 May 2024 15:49:19 -0700 Subject: [PATCH 2/6] fix --- .github/workflows/ci.yaml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7cbfc12..fdb0dba 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -25,13 +25,6 @@ jobs: run: | cd go-test-app-01 make test - cp cover.out ../cover.out - - - name: Go Beautiful HTML Coverage - if: always() - uses: './' - with: - threshold: 80 - name: Go Beautiful HTML Coverage if: always() From 21f7d98e5d97d99f5629e8227d8853851a4e72e5 Mon Sep 17 00:00:00 2001 From: Kilian Ciuffolo Date: Tue, 14 May 2024 16:00:57 -0700 Subject: [PATCH 3/6] test --- src/update-comment.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/update-comment.js b/src/update-comment.js index 9a88ff0..d89940d 100644 --- a/src/update-comment.js +++ b/src/update-comment.js @@ -15,12 +15,12 @@ const updateCodeCoverageComment = module.exports = async ({ context, github, pat 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) - const coverageEmoji = coverage >= threshold ? '' : '🔻 ' - const pathText = (path !== './' ? ` for \`${path}/\`` : '').replace('//', '/') + const coverageEmoji = coverage >= threshold ? '' : `🔻 ${threshold - coverage}%` + const pathText = (path !== './' ? ` for ${path}/` : '').replace('//', '/') const commentBody = [ ``, - `# ${coverageEmoji}[🔗 Code Coverage Report](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision})${pathText} at ${revision}`, + `##### ${coverageEmoji}[🔗 Code Coverage Report](https://${context.repo.owner}.github.io/${context.repo.repo}/?hash=${revision})${pathText} at ${revision}`, '```', `📔 Total: ${coverageTextSummary}`, ] From 339519fba4f0acdd8847c0c584cf4e0b1bf71af8 Mon Sep 17 00:00:00 2001 From: Kilian Ciuffolo Date: Tue, 14 May 2024 16:03:23 -0700 Subject: [PATCH 4/6] fix --- src/update-comment.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/update-comment.js b/src/update-comment.js index d89940d..9bcc766 100644 --- a/src/update-comment.js +++ b/src/update-comment.js @@ -15,7 +15,7 @@ const updateCodeCoverageComment = module.exports = async ({ context, github, pat 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) - const coverageEmoji = coverage >= threshold ? '' : `🔻 ${threshold - coverage}%` + const coverageEmoji = coverage >= threshold ? '' : `🔻 ${(coverage - threshold).toFixed(1)}%` const pathText = (path !== './' ? ` for ${path}/` : '').replace('//', '/') const commentBody = [ From 7aaa59b30d5d6568f341da75585e4936cecfbe74 Mon Sep 17 00:00:00 2001 From: Kilian Ciuffolo Date: Tue, 14 May 2024 16:04:19 -0700 Subject: [PATCH 5/6] fix --- src/update-comment.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/update-comment.js b/src/update-comment.js index 9bcc766..fd990ec 100644 --- a/src/update-comment.js +++ b/src/update-comment.js @@ -15,7 +15,7 @@ const updateCodeCoverageComment = module.exports = async ({ context, github, pat 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) - const coverageEmoji = coverage >= threshold ? '' : `🔻 ${(coverage - threshold).toFixed(1)}%` + const coverageEmoji = coverage >= threshold ? '' : `🔻 ${(coverage - threshold).toFixed(1)}% ` const pathText = (path !== './' ? ` for ${path}/` : '').replace('//', '/') const commentBody = [ From d8f2263e8d6da19d733c11085c8ab088ed348a6f Mon Sep 17 00:00:00 2001 From: Kilian Ciuffolo Date: Tue, 14 May 2024 16:13:00 -0700 Subject: [PATCH 6/6] ready --- .github/workflows/ci.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index fdb0dba..acce089 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -31,10 +31,9 @@ jobs: uses: './' with: path: go-test-app-01/ - threshold: 80 + threshold: 66.7 - name: Go Beautiful HTML Coverage - if: always() uses: './' with: path: go-test-app-02/