Checking code convention validation, run unit test and check coverage threshold #72
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Quality check | |
run-name: Checking code convention validation, run unit test and check coverage threshold | |
on: | |
pull_request: | |
branches: | |
- master | |
- release/* | |
jobs: | |
quality-check: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- uses: actions/setup-go@v4 | |
with: | |
go-version-file: 'go.mod' | |
- name: Setup Golang caches | |
uses: actions/cache@v3 | |
with: | |
path: | | |
~/.cache/go-build | |
~/go/pkg/mod | |
key: ${{ runner.os }}-golang-${{ hashFiles('**/go.sum') }} | |
restore-keys: | | |
${{ runner.os }}-golang- | |
- name: Setup GO environment | |
run: | | |
go mod vendor | |
go install -v github.com/incu6us/goimports-reviser/[email protected] | |
go install github.com/golangci/golangci-lint/cmd/[email protected] | |
- name: Format | |
run: go fmt ./... | |
- name: Vet | |
run: go vet ./... | |
- name: Sort imports | |
run: goimports-reviser ./... | |
- name: Run linter | |
run: golangci-lint run | |
# If there are any diffs from goimports or go generate, fail. | |
- name: Verify no changes from goimports or vet or fmt. | |
run: | | |
if [ -n "$(git status --porcelain)" ]; then | |
echo 'To fix this check, run "goimports-reviser ./... && go vet ./... && go fmt ./..."' | |
git status # Show the files that failed to pass the check. | |
exit 1 | |
fi | |
- name: Unit tests | |
run: | | |
sudo go test ./... -coverprofile coverage.out -coverpkg=./... -covermode count | |
sudo go tool cover -func coverage.out | |
- name: Quality Gate - Test coverage shall be above threshold | |
env: | |
TEST_COVERAGE_THRESHOLD: ${{ vars.TEST_COVERAGE_THRESHOLD }} | |
run: | | |
echo "Quality Gate: checking test coverage is above threshold ..." | |
echo "Threshold : $TEST_COVERAGE_THRESHOLD %" | |
totalCoverage=`go tool cover -func=coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+'` | |
echo "Current test coverage : $totalCoverage %" | |
if (( $(echo "$totalCoverage $TEST_COVERAGE_THRESHOLD" | awk '{print ($1 > $2)}') )); then | |
echo "OK" | |
else | |
echo "Current test coverage is below threshold. Please add more unit tests or adjust threshold to a lower value." | |
echo "Failed" | |
exit 1 | |
fi |