Skip to content

Commit

Permalink
Prepare for v1 release (#14)
Browse files Browse the repository at this point in the history
* Update README.md and CHANGELOG.md to point to v1

* Rename $COVERAGE_REPORT_TRIM to $TRIM_PACKAGE

* Rename "-root-pkg" flag to "-root"

* Fail "Download go-coverage-report" job on non-zero status codes

* Move downloading of binary into its own script

* Use v0.4.0 version in GitHub CI workflow until v1.0.0 is actually released

* Move Go code into "cmd" directory

* Tell goreleaser where to find the main package

* Support install go-coverage-report from local and remote source

* Some more tweaking of input and variable naming
  • Loading branch information
fgrosse authored Mar 18, 2024
1 parent a29f921 commit a284a4c
Show file tree
Hide file tree
Showing 18 changed files with 164 additions and 80 deletions.
Binary file added .github/outputs/go-coverage-report
Binary file not shown.
15 changes: 11 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
branches:
- 'main'

env:
GO_VERSION: "^1.22"

jobs:
unit_tests:
name: "Unit tests"
Expand All @@ -18,7 +21,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ^1.22
go-version: ${{ env.GO_VERSION }}

- name: Restore Go modules cache
uses: actions/cache@v4
Expand Down Expand Up @@ -52,12 +55,16 @@ jobs:
# Grant permission to add comments to the PR.
pull-requests: write
steps:
# Normally,users of the action don't have to check out the source code.
# Normally,users of the action don't have to check out the source code or setup Go.
# We need this here however because we are using a local action.yml.
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}

- uses: ./
with:
coverage-artifact-name: "code-coverage"
coverage-file-name: "coverage.txt"
version: local
trim: "github.com/fgrosse/go-coverage-report/"
1 change: 1 addition & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ builds:
- linux
- windows
- darwin
main: ./cmd/go-coverage-report

archives:
- format: tar.gz
Expand Down
15 changes: 4 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

> [!NOTE]
> We are currently working towards **v1.0.0**
## [Unreleased]
_Nothing yet_

## [v0.4.0] - 2024-03-16
- Skip coverage report if no file changed (fgrosse/go-coverage-report#11)
- _… changes before we started the CHANGELOG.md …_
## [v1.0.0] - 2024-03-18
- Initial release

[Unreleased]: https://github.com/fgrosse/go-coverage-report/compare/v0.4.0...HEAD
[v0.4.0]: https://github.com/fgrosse/go-coverage-report/compare/v0.3.0...v0.4.0
[v0.3.0]: https://github.com/fgrosse/go-coverage-report/compare/v0.2.0...v0.3.0
[v0.2.0]: https://github.com/fgrosse/go-coverage-report/compare/v0.1.0...v0.2.0
[v0.1.0]: https://github.com/fgrosse/go-coverage-report/releases/tag/v1.0.0
[Unreleased]: https://github.com/fgrosse/go-coverage-report/compare/v1.0.0...HEAD
[v1.0.0]: https://github.com/fgrosse/go-coverage-report/releases/tag/v1.0.0
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ jobs:
runs-on: ubuntu-latest
needs: unit_tests # Depends on the artifact uploaded by the "unit_tests" job
steps:
- uses: fgrosse/go-coverage-report@v0.4.0 # Consider using a Git revision for maximum security
- uses: fgrosse/go-coverage-report@v1.0.0 # Consider using a Git revision for maximum security
with:
coverage-artifact-name: "code-coverage" # can be omitted if you used this default value
coverage-file-name: "coverage.txt" # can be omitted if you used this default value
Expand All @@ -112,10 +112,10 @@ inputs:
version:
description: 'The exact version of the go-coverage-report tool to use.'
required: true
default: "v0.4.0"
default: "v1.0.0"

sha256sum:
description: 'Expected SHA256 checksum of the tarball when downloading the go-coverage-report binary.'
description: 'Optional SHA256 checksum of the tarball when downloading the go-coverage-report binary.'
required: false

coverage-artifact-name:
Expand All @@ -128,7 +128,7 @@ inputs:
required: true
default: "coverage.txt"

repo-import-path:
root-package:
description: |
The Go import path of the tested repository to add as a prefix to all paths of the
changed files. This is useful to map the changed files (e.g., ["foo/my_file.go"]
Expand Down
55 changes: 10 additions & 45 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ branding:

inputs:
version:
description: 'The exact version of the go-coverage-report tool to use.'
description: 'The exact version tag of the go-coverage-report tool to use.'
required: true
default: "v0.4.0"
default: "v1.0.0"

sha256sum:
description: 'Expected SHA256 checksum of the tarball when downloading the go-coverage-report binary.'
description: 'Optional SHA256 checksum of the tarball when downloading the go-coverage-report binary.'
required: false

coverage-artifact-name:
Expand All @@ -25,7 +25,7 @@ inputs:
required: true
default: "coverage.txt"

repo-import-path:
root-package:
description: |
The Go import path of the tested repository to add as a prefix to all paths of the
changed files. This is useful to map the changed files (e.g., ["foo/my_file.go"]
Expand All @@ -46,45 +46,10 @@ runs:
- name: Download go-coverage-report
shell: bash
id: download
run: $GITHUB_ACTION_PATH/scripts/download-cli-tool.sh "${{ inputs.version }}" "${{ inputs.sha256sum }}"
env:
VERSION: ${{ inputs.version }}
SHA256SUM: ${{ inputs.sha256sum }}
run: |
if [ ${{ runner.arch }} = "ARM64" ]; then
ARCH="arm64"
elif [ ${{ runner.arch }} = "ARM" ]; then
ARCH="arm"
elif [ ${{ runner.arch }} = "X86" ]; then
ARCH="386"
elif [ ${{ runner.arch }} = "X64" ]; then
ARCH="amd64"
else
ARCH="amd64"
fi
mkdir -p .github/outputs
RUNNER_OS=$(echo '${{ runner.os }}' | tr '[:upper:]' '[:lower:]')
FILENAME="go-coverage-report-${VERSION}-${RUNNER_OS}-${ARCH}.tar.gz"
URL="https://github.com/fgrosse/go-coverage-report/releases/download/${VERSION}/${FILENAME}"
echo "Downloading \"$FILENAME\" from \"$URL\""
curl --fail --location "$URL" --output .github/outputs/$FILENAME
if ! [[ "$SHA256SUM" ]] ; then
URL="https://github.com/fgrosse/go-coverage-report/releases/download/${VERSION}/checksums.txt"
cd .github/outputs
echo "Downloading checksums from \"$URL\""
curl -fsSL "$URL" | sha256sum --check --ignore-missing
cd -
else
echo "Expected sha256: $SHA256SUM"
echo "Actual sha256: $(sha256sum .github/outputs/go-coverage-report.tar.gz)"
echo "$SHA256SUM .github/outputs/$FILENAME" | sha256sum -c
fi
tar -xzf .github/outputs/$FILENAME -C .github/outputs/ go-coverage-report
rm .github/outputs/$FILENAME
sudo mv .github/outputs/go-coverage-report /usr/bin
RUNNER_OS: ${{ runner.os }}
RUNNER_ARCH: ${{ runner.arch }}

- name: Determine changed files
id: changed-files
Expand All @@ -100,7 +65,7 @@ runs:
- name: Code coverage report
shell: bash
run: $GITHUB_ACTION_PATH/github-action.sh ${{ github.repository }} ${{ github.event.pull_request.number }} ${{ github.run_id }}
run: $GITHUB_ACTION_PATH/scripts/github-action.sh "${{ github.repository }}" "${{ github.event.pull_request.number }}" "${{ github.run_id }}"
env:
GH_REPO: ${{ github.repository }}
GH_TOKEN: ${{ github.token }}
Expand All @@ -109,5 +74,5 @@ runs:
CHANGED_FILES_PATH: .github/outputs/all_changed_files.json
COVERAGE_ARTIFACT_NAME: ${{ inputs.coverage-artifact-name }}
COVERAGE_FILE_NAME: ${{ inputs.coverage-file-name }}
REPOSITORY_IMPORT_PATH: ${{ inputs.repo-import-path }}
COVERAGE_REPORT_TRIM: ${{ inputs.trim }}
ROOT_PACKAGE: ${{ inputs.root-package }}
TRIM_PACKAGE: ${{ inputs.trim }}
File renamed without changes.
1 change: 0 additions & 1 deletion coverage.go → cmd/go-coverage-report/coverage.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Package coverage contains code to parse and format code coverage profiles.
package main

import (
Expand Down
File renamed without changes.
18 changes: 9 additions & 9 deletions main.go → cmd/go-coverage-report/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Parse the OLD_COVERAGE_FILE and NEW_COVERAGE_FILE and compare the coverage of th
files listed in CHANGED_FILES_FILE. The result is printed to stdout as a simple
Markdown table with emojis indicating the coverage change per package.
You can use the -root-pkg flag to add a prefix to all paths in the list of changed
You can use the -root flag to add a prefix to all paths in the list of changed
files. This is useful to map the changed files (e.g., ["foo/my_file.go"] to their
coverage profile which uses the full package name to identify the files
(e.g., "github.com/fgrosse/example/foo/my_file.go"). Note that currently,
Expand All @@ -31,9 +31,9 @@ OPTIONS:
`, filepath.Base(os.Args[0])))

type options struct {
rootPkg string
trim string
format string
root string
trim string
format string
}

func main() {
Expand All @@ -44,7 +44,7 @@ func main() {
flag.PrintDefaults()
}

flag.String("root-pkg", "", "The import path of the tested repository to add as prefix to all paths of the changed files")
flag.String("root", "", "The import path of the tested repository to add as prefix to all paths of the changed files")
flag.String("trim", "", "trim a prefix in the \"Impacted Packages\" column of the markdown report")
flag.String("format", "markdown", "output format (currently only 'markdown' is supported)")

Expand All @@ -67,9 +67,9 @@ func programArgs() (oldCov, newCov, changedFile string, opts options) {
}

opts = options{
rootPkg: flag.Lookup("root-pkg").Value.String(),
trim: flag.Lookup("trim").Value.String(),
format: flag.Lookup("format").Value.String(),
root: flag.Lookup("root").Value.String(),
trim: flag.Lookup("trim").Value.String(),
format: flag.Lookup("format").Value.String(),
}

return args[0], args[1], args[2], opts
Expand All @@ -86,7 +86,7 @@ func run(oldCovPath, newCovPath, changedFilesPath string, opts options) error {
return fmt.Errorf("failed to parse new coverage: %w", err)
}

changedFiles, err := ParseChangedFiles(changedFilesPath, opts.rootPkg)
changedFiles, err := ParseChangedFiles(changedFilesPath, opts.root)
if err != nil {
return fmt.Errorf("failed to load changed files: %w", err)
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
119 changes: 119 additions & 0 deletions scripts/download-cli-tool.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env bash

set -e -o pipefail

type curl > /dev/null 2>&1 || { echo >&2 'ERROR: Script requires "curl"'; exit 1; }
type sha256sum > /dev/null 2>&1 || { echo >&2 'ERROR: Script requires "sha256sum"'; exit 1; }
type tar > /dev/null 2>&1 || { echo >&2 'ERROR: Script requires "tar"'; exit 1; }

USAGE="$0: Download the go-coverage-report binary from GitHub.
This script is meant to be used as part of a GitHub action and makes use of Workflow commands as
described in https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
Usage:
$0 version [sha256sum]
Example:
$0 Linux X64
You can use the following environment variables to configure the script:
- RUNNER_OS: The operating system of the runner (default: Linux)
- RUNNER_ARCH: The architecture of the runner (default: X64)
- DRY_RUN: Do not actually move the binary to /usr/bin (default: false)
"

if [[ $# == 0 ]]; then
echo -e "Error: script requires at least one argument\n"
echo "$USAGE"
exit 1
fi

VERSION=$1
RUNNER_OS=${RUNNER_OS:-Linux}
RUNNER_ARCH=${RUNNER_ARCH:-X64}

if [[ -z ${VERSION+x} ]]; then
echo "Missing version argument"
exit 1
fi

if [[ $# == 2 ]]; then
SHA256SUM=$2
fi

sudo_or_dry_run="sudo"
if [[ "$DRY_RUN" == "true" ]]; then
sudo_or_dry_run='echo [DRY-RUN]: sudo'
fi

start_group(){
echo "::group::$*"
{ set -x; return; } 2>/dev/null
}

end_group(){
{ set +x; return; } 2>/dev/null
echo "::endgroup::"
}

if [[ $VERSION == "local" ]]; then
start_group "Installing go-coverage-report from local source"
go install -v ./cmd/go-coverage-report
end_group
exit 0
fi

if [[ ${#VERSION} == 40 ]]; then
start_group "Installing go-coverage-report from remote source"
go install -v "github.com/fgrosse/go-coverage-report@$VERSION"
end_group
exit 0
fi

start_group "Determining runner architecture"
if [ "$RUNNER_ARCH" = "ARM64" ]; then
ARCH="arm64"
elif [ "$RUNNER_ARCH" = "ARM" ]; then
ARCH="arm"
elif [ "$RUNNER_ARCH" = "X86" ]; then
ARCH="386"
elif [ "$RUNNER_ARCH" = "X64" ]; then
ARCH="amd64"
else
ARCH="amd64"
fi
end_group

start_group "Downloading tar archive from GitHub"
mkdir -p .github/outputs
OS=$(echo "$RUNNER_OS" | tr '[:upper:]' '[:lower:]')
FILENAME="go-coverage-report-${VERSION}-${OS}-${ARCH}.tar.gz"
URL="https://github.com/fgrosse/go-coverage-report/releases/download/${VERSION}/${FILENAME}"
curl --fail --location "$URL" --output ".github/outputs/$FILENAME"
end_group

if ! [[ "$SHA256SUM" ]] ; then
start_group "Checking checksum using checksums.txt file from GitHub release"
URL="https://github.com/fgrosse/go-coverage-report/releases/download/${VERSION}/checksums.txt"
cd .github/outputs
curl -fsSL "$URL" | sha256sum --check --ignore-missing
cd -
end_group
else
start_group "Checking checksum using provided SHA256 hash"
echo "Actual sha256:"
sha256sum ".github/outputs/$FILENAME"
echo "Checking checksum"
echo "$SHA256SUM .github/outputs/$FILENAME" | sha256sum -c
end_group
fi

start_group "Decompressing tar archive"
tar -xzf ".github/outputs/$FILENAME" -C .github/outputs/ go-coverage-report
rm ".github/outputs/$FILENAME"
end_group

start_group "Move binary to /usr/bin"
$sudo_or_dry_run mv .github/outputs/go-coverage-report /usr/bin
end_group
12 changes: 6 additions & 6 deletions github-action.sh → scripts/github-action.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ You can use the following environment variables to configure the script:
- COVERAGE_ARTIFACT_NAME: The name of the artifact containing the code coverage results (default: code-coverage)
- COVERAGE_FILE_NAME: The name of the file containing the code coverage results (default: coverage.txt)
- CHANGED_FILES_PATH: The path to the file containing the list of changed files (default: .github/outputs/all_changed_files.json)
- REPOSITORY_IMPORT_PATH: The import path of the tested repository to add as a prefix to all paths of the changed files (optional)
- COVERAGE_REPORT_TRIM: Trim a prefix in the \"Impacted Packages\" column of the markdown report (optional)
- 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)
"

if [[ $# != 3 ]]; then
Expand Down Expand Up @@ -96,10 +96,10 @@ end_group

start_group "Compare code coverage results"
go-coverage-report \
-root-pkg="$REPOSITORY_IMPORT_PATH" \
-trim="$COVERAGE_REPORT_TRIM" \
$OLD_COVERAGE_PATH \
$NEW_COVERAGE_PATH \
-root="$ROOT_PACKAGE" \
-trim="$TRIM_PACKAGE" \
"$OLD_COVERAGE_PATH" \
"$NEW_COVERAGE_PATH" \
"$CHANGED_FILES_PATH" \
> $COVERAGE_COMMENT_PATH
end_group
Expand Down

0 comments on commit a284a4c

Please sign in to comment.