Skip to content

Instantly share code, notes, and snippets.

@maelvls
Last active September 15, 2023 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maelvls/bd9b48ed236a4622e5c7794a04d73752 to your computer and use it in GitHub Desktop.
Save maelvls/bd9b48ed236a4622e5c7794a04d73752 to your computer and use it in GitHub Desktop.
gh-gocover – Share the coverage.html for a given pull request on GitHub. Requires git, gocover, gsutil, and gh.
#! /bin/bash
set -euo pipefail
# Source: https://gist.github.com/maelvls/bd9b48ed236a4622e5c7794a04d73752
help() {
cat <<EOF
Usage: gh gocover --pr <number> --bucket gs://bucket/path [flags]
Description:
gh-gocover – Share the coverage.html for a given pull request on GitHub.
Requires git, gocover, gsutil, and gh.
Flags:
--pr The pull-request number.
--bucket The bucket to upload the coverage report to. For example:
gs://jetstack-mael-valais/gocover.
Optional flags:
--repo The repository to compare against. Defaults to the current
repository.
--branch The ref to compare against. Defaults to origin/<default-branch>,
<default-branch> being the default branch configured for that
repository (e.g., main).
--origin The origin to fetch from. Defaults to origin.
-h | --help Show this message and exit.
EOF
}
# If gocover is not installed, install it.
if ! command -v gocover &>/dev/null; then
echo "gocover is not installed. Installing..."
go install github.com/Azure/gocover@latest
fi
origin=origin
pr=
repo=
bucket=
while [[ $# -gt 0 ]]; do
case "$1" in
--pr)
if [[ -z "$2" ]]; then
echo "Missing argument for $1"
exit 1
fi
pr="$2"
shift
;;
--bucket)
if [[ -z "$2" ]]; then
echo "Missing argument for $1"
exit 1
fi
if [[ "$2" != gs://* ]]; then
echo "Bucket must be a gs:// URL"
exit 1
fi
bucket="$2"
shift
;;
--repo)
if [[ -z "$2" ]]; then
echo "Missing argument for $1"
exit 1
fi
repo="$2"
shift
;;
--origin)
if [[ -z "$2" ]]; then
echo "Missing argument for $1"
exit 1
fi
origin="$2"
shift
;;
-h | --help)
help
exit 0
;;
*)
echo "Unknown flag: $1"
help
exit 1
;;
esac
shift
done
if [[ -z "$pr" ]]; then
echo "Missing --pr flag"
exit 1
fi
if [[ -z "$bucket" ]]; then
echo "Missing --bucket flag"
exit 1
fi
if [[ -z "${repo:-}" ]]; then
repo=$(gh repo view --json name,owner --jq '"\(.owner.login)/\(.name)"')
fi
if [[ -z "${branch:-}" ]]; then
branch=$(gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name')
fi
git fetch --quiet "$origin" "$branch"
gh pr checkout "$pr"
go test ./... -coverprofile=/tmp/cover.out
gocover diff --repository-path="$PWD" --cover-profile=/tmp/cover.out --compare-branch="$branch" --outputdir=/tmp --coverage-baseline=0
gsutil cp /tmp/coverage.html "$bucket/${repo/\//-}-pr-$pr/coverage.html"
gsutil acl ch -u AllUsers:R "$bucket/${repo/\//-}-pr-$pr/coverage.html"
printf "URL: https://storage.googleapis.com/%s/%s-pr-%s/coverage.html\n" \
"${bucket#gs://}" "${repo/\//-}" "$pr"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment