|
| 1 | +# go-test-coverage GitHub Action |
| 2 | + |
| 3 | + |
| 4 | +# Usage |
| 5 | + |
| 6 | +Here’s an example of how to integrate `go-test-coverage` in a GitHub workflow that uses a config file. This is the preferred way because the same config file can be used for running coverage checks locally. |
| 7 | + |
| 8 | +```yml |
| 9 | + - name: check test coverage |
| 10 | + uses: vladopajic/go-test-coverage@v2 |
| 11 | + with: |
| 12 | + config: ./.testcoverage.yml |
| 13 | +``` |
| 14 | +
|
| 15 | +Alternatively, if you don't need advanced configuration options from a config file, you can specify thresholds directly in the action properties. |
| 16 | +
|
| 17 | +```yml |
| 18 | + - name: check test coverage |
| 19 | + uses: vladopajic/go-test-coverage@v2 |
| 20 | + with: |
| 21 | + profile: cover.out |
| 22 | + local-prefix: github.com/org/project |
| 23 | + threshold-file: 80 |
| 24 | + threshold-package: 80 |
| 25 | + threshold-total: 95 |
| 26 | +``` |
| 27 | +
|
| 28 | +Note: When using a config file alongside action properties, specifying these parameters will override the corresponding values in the config file. |
| 29 | +
|
| 30 | +# Action Outputs |
| 31 | +
|
| 32 | +The GitHub Action will set the following outputs, which can be used later in your workflow: |
| 33 | +
|
| 34 | +| Name | Description | |
| 35 | +|-----------------|------------------------------| |
| 36 | +|`total-coverage` | Integer value in the range [0-100], representing the total coverage percentage. | |
| 37 | +|`badge-color` | String value holding the color hex code for the badge (e.g., `#44cc11`). | |
| 38 | +|`badge-text` | The total-coverage value concatenated with a % (e.g., `100%`). | |
| 39 | +|`report` | JSON-encoded string representing the coverage report from `go-test-coverage`. | |
| 40 | + |
| 41 | + |
| 42 | +# Post Coverage Report to PR |
| 43 | + |
| 44 | +Here is an example of how to post comments with the coverage report to your pull request. The same logic is used in workflow in [this repo](/.github/workflows/test.yml). |
| 45 | + |
| 46 | +```yml |
| 47 | + - name: check test coverage |
| 48 | + id: coverage |
| 49 | + uses: vladopajic/go-test-coverage@v2 |
| 50 | + with: |
| 51 | + config: ./.github/.testcoverage.yml |
| 52 | + |
| 53 | + # Post coverage report as comment (in 3 steps) |
| 54 | + - name: find pull request ID |
| 55 | + run: | |
| 56 | + PR_DATA=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 57 | + "https://api.github.com/repos/${{ github.repository }}/pulls?head=${{ github.repository_owner }}:${{ github.ref_name }}&state=open") |
| 58 | + PR_ID=$(echo "$PR_DATA" | jq -r '.[0].number') |
| 59 | + |
| 60 | + if [ "$PR_ID" != "null" ]; then |
| 61 | + echo "pull_request_id=$PR_ID" >> $GITHUB_ENV |
| 62 | + else |
| 63 | + echo "No open pull request found for this branch." |
| 64 | + fi |
| 65 | + - name: find if coverage report is already present |
| 66 | + if: env.pull_request_id |
| 67 | + uses: peter-evans/find-comment@v3 |
| 68 | + id: fc |
| 69 | + with: |
| 70 | + issue-number: ${{ env.pull_request_id }} |
| 71 | + comment-author: 'github-actions[bot]' |
| 72 | + body-includes: 'go-test-coverage report:' |
| 73 | + - name: post coverage report |
| 74 | + if: env.pull_request_id |
| 75 | + uses: peter-evans/create-or-update-comment@v4 |
| 76 | + with: |
| 77 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + issue-number: ${{ env.pull_request_id }} |
| 79 | + comment-id: ${{ steps.fc.outputs.comment-id }} |
| 80 | + body: | |
| 81 | + go-test-coverage report: |
| 82 | + ``` |
| 83 | + ${{ fromJSON(steps.coverage.outputs.report) }} |
| 84 | + ``` |
| 85 | + edit-mode: replace |
| 86 | +``` |
0 commit comments