diff --git a/.gitignore b/.gitignore index 849ddff..d63f61d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ dist/ +.github/outputs/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ca397a..18f8078 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - Fix issue with code coverage information missing when test files are deleted (fgrosse/go-coverage-report#35) - Document permissions needed to use this action (fgrosse/go-coverage-report#32) +- Add new `skip-comment` input to skip adding a comment to the PR (fgrosse/go-coverage-report#34) ## [v1.0.2] - 2024-06-11 - Fix issue when coverage artifact contains more files than just the `coverage.txt` file (fgrosse/go-coverage-report#25) diff --git a/README.md b/README.md index ae006a7..de282bf 100644 --- a/README.md +++ b/README.md @@ -142,14 +142,22 @@ inputs: default: "github.com/${{ github.repository }}" trim: - description: Trim a prefix in the "Impacted Packages" column of the markdown report. + description: 'Trim a prefix in the "Impacted Packages" column of the markdown report.' required: false + + skip-comment: + description: | + Skip creating or updating the pull request comment. This may be useful when you want + to generate the coverage report and modify it in your own scripts. + required: false + default: 'false' ``` ### Outputs -This action does not provide any outputs, but it will comment on your pull request -with the summary of the code coverage changes. +This action provides the following outputs: + +- `coverage_report`: The generated coverage report in Markdown format. ## Limitations diff --git a/action.yml b/action.yml index 6b59de2..0b75906 100644 --- a/action.yml +++ b/action.yml @@ -35,10 +35,22 @@ inputs: required: false default: "github.com/${{ github.repository }}" + skip-comment: + description: | + Skip creating or updating the pull request comment. This may be useful when you want + to generate the coverage report and modify it in your own scripts. + required: false + default: 'false' + trim: description: Trim a prefix in the "Impacted Packages" column of the markdown report. required: false +outputs: + coverage_report: + description: 'The generated coverage report in Markdown format.' + value: ${{ steps.coverage.outputs.coverage_report }} + runs: using: "composite" @@ -64,6 +76,7 @@ runs: - name: Code coverage report shell: bash + id: coverage run: $GITHUB_ACTION_PATH/scripts/github-action.sh "${{ github.repository }}" "${{ github.event.pull_request.number }}" "${{ github.run_id }}" env: GH_REPO: ${{ github.repository }} @@ -74,4 +87,5 @@ runs: COVERAGE_ARTIFACT_NAME: ${{ inputs.coverage-artifact-name }} COVERAGE_FILE_NAME: ${{ inputs.coverage-file-name }} ROOT_PACKAGE: ${{ inputs.root-package }} + SKIP_COMMENT: ${{ inputs.skip-comment }} TRIM_PACKAGE: ${{ inputs.trim }} diff --git a/scripts/github-action.sh b/scripts/github-action.sh index b0602b5..486fa25 100755 --- a/scripts/github-action.sh +++ b/scripts/github-action.sh @@ -31,6 +31,7 @@ You can use the following environment variables to configure the script: - CHANGED_FILES_PATH: The path to the file containing the list of changed files (default: .github/outputs/all_modified_files.json) - ROOT_PACKAGE: The import path of the tested repository to add as a prefix to all paths of the changed files (optional) - TRIM_PACKAGE: Trim a prefix in the \"Impacted Packages\" column of the markdown report (optional) +- SKIP_COMMENT: Skip creating or updating the pull request comment (default: false) " if [[ $# != 3 ]]; then @@ -42,7 +43,6 @@ fi GITHUB_REPOSITORY=$1 GITHUB_PULL_REQUEST_NUMBER=$2 GITHUB_RUN_ID=$3 - GITHUB_WORKFLOW=${GITHUB_WORKFLOW:-CI} TARGET_BRANCH=${GITHUB_BASE_REF:-main} COVERAGE_ARTIFACT_NAME=${COVERAGE_ARTIFACT_NAME:-code-coverage} @@ -52,6 +52,7 @@ OLD_COVERAGE_PATH=.github/outputs/old-coverage.txt NEW_COVERAGE_PATH=.github/outputs/new-coverage.txt COVERAGE_COMMENT_PATH=.github/outputs/coverage-comment.md CHANGED_FILES_PATH=${CHANGED_FILES_PATH:-.github/outputs/all_modified_files.json} +SKIP_COMMENT=${SKIP_COMMENT:-false} if [[ -z ${GITHUB_REPOSITORY+x} ]]; then echo "Missing github_repository argument" @@ -68,6 +69,13 @@ if [[ -z ${GITHUB_RUN_ID+x} ]]; then exit 1 fi +if [[ -z ${GITHUB_OUTPUT+x} ]]; then + echo "Missing GITHUB_OUTPUT environment variable" + exit 1 +fi + +export GH_REPO="$GITHUB_REPOSITORY" + start_group(){ echo "::group::$*" { set -x; return; } 2>/dev/null @@ -107,7 +115,20 @@ go-coverage-report \ end_group if [ ! -s $COVERAGE_COMMENT_PATH ]; then - echo "::notice::No coverage report to comment" + echo "::notice::No coverage report to output" + exit 0 +fi + +# Output the coverage report as a multiline GitHub output parameter +echo "Writing GitHub output parameter to \"$GITHUB_OUTPUT\"" +{ + echo "coverage_report<> "$GITHUB_OUTPUT" + +if [ "$SKIP_COMMENT" = "true" ]; then + echo "Skipping pull request comment (\$SKIP_COMMENT=true))" exit 0 fi