Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skip-comment flag and input as well as coverage_report output #37

Merged
merged 6 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,20 @@ inputs:
trim:
description: Trim a prefix in the "Impacted Packages" column of the markdown report.
required: false

skip-comment:
description: 'If set, skip creating or updating the PR comment.'
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 output:

- `coverage_report`: The generated coverage report in markdown format.

This output can be used whether or not `skip-comment` is set.

## Limitations

Expand Down
16 changes: 15 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,21 @@ inputs:
required: false
default: "github.com/${{ github.repository }}"

skip-comment:
description: 'If true, skip creating or updating the PR comment.'
fgrosse marked this conversation as resolved.
Show resolved Hide resolved
required: false
default: false
type: boolean
fgrosse marked this conversation as resolved.
Show resolved Hide resolved

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"

Expand All @@ -61,10 +72,13 @@ runs:
**.go
files_ignore: |
vendor/**
- run: cat .github/outputs/all_modified_files.json
shell: bash

- name: Code coverage report
shell: bash
run: $GITHUB_ACTION_PATH/scripts/github-action.sh "${{ github.repository }}" "${{ github.event.pull_request.number }}" "${{ github.run_id }}"
id: coverage
run: $GITHUB_ACTION_PATH/scripts/github-action.sh --skip-comment ${{ inputs.skip-comment }} "${{ github.repository }}" "${{ github.event.pull_request.number }}" "${{ github.run_id }}"
fgrosse marked this conversation as resolved.
Show resolved Hide resolved
env:
GH_REPO: ${{ github.repository }}
GH_TOKEN: ${{ github.token }}
Expand Down
46 changes: 41 additions & 5 deletions scripts/github-action.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
described in https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions

Usage:
$0 github_repository github_pull_request_number github_run_id
$0 [--skip-comment] github_repository github_pull_request_number github_run_id

Example:
$0 fgrosse/prioqueue 12 8221109494
$0 --skip-comment fgrosse/prioqueue 12 8221109494

You can largely rely on the default environment variables set by GitHub Actions. The script should be invoked like
this in the workflow file:

-name: Code coverage report
run: github-action.sh \${{ github.repository }} \${{ github.event.pull_request.number }} \${{ github.run_id }}
run: github-action.sh --skip-comment \${{ inputs.skip-comment }} \${{ github.repository }} \${{ github.event.pull_request.number }} \${{ github.run_id }}
env: …

You can use the following environment variables to configure the script:
Expand All @@ -33,16 +34,37 @@
- TRIM_PACKAGE: Trim a prefix in the \"Impacted Packages\" column of the markdown report (optional)
"

SKIP_COMMENT=false

# Parse optional flags
while [[ $# -gt 0 ]]; do
case $1 in
--skip-comment)
SKIP_COMMENT=true
shift
# Check if the next argument is a boolean value
if [[ "$1" == "false" ]]; then
SKIP_COMMENT=false
shift
elif [[ "$1" == "true" ]]; then
shift
fi
;;
*)
break
;;
esac
done

if [[ $# != 3 ]]; then
echo -e "Error: script requires exactly three arguments\n"
echo -e "Error: script requires exactly three positional arguments\n"
echo "$USAGE"
exit 1
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}
Expand Down Expand Up @@ -107,7 +129,21 @@
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

start_group "Output coverage report as GitHub Action output"
fgrosse marked this conversation as resolved.
Show resolved Hide resolved
COVERAGE_REPORT=$(cat $COVERAGE_COMMENT_PATH)
# Encode newlines
COVERAGE_REPORT="${COVERAGE_REPORT//$'\n'/'\n'}"
COVERAGE_REPORT="${COVERAGE_REPORT//$'\r'/'\r'}"
fgrosse marked this conversation as resolved.
Show resolved Hide resolved
# Save to GITHUB_OUTPUT file
echo "coverage_report=$COVERAGE_REPORT" >> $GITHUB_OUTPUT
Fixed Show fixed Hide fixed
end_group

if [ "$SKIP_COMMENT" = "true" ]; then
echo "Skipping PR comment creation as requested"
exit 0
fi

Expand Down
Loading