From b8235e461fdbfef94726def071380b150559baf8 Mon Sep 17 00:00:00 2001 From: Tony Reed <116904476+DI-Tony-Reed@users.noreply.github.com> Date: Mon, 14 Oct 2024 15:32:43 -0500 Subject: [PATCH] Add test coverage pipeline step (#4) * test WIP * Update coverage badge * add coverage echo for testing * update if statement * update if statement * Update coverage badge * update shield color * Update coverage badge * update readme.md directly rather than a separate md * update workflow to remove redundant steps * update shell var & add baseline coverage badge * update badge to something wrong for pipeline to fix as test * debug * update markdown * debug * debug * update syntax * update syntax * Update coverage badge to % * finish workflow --------- Co-authored-by: GitHub Actions Co-authored-by: GitHub Action --- .github/workflows/update-tests-coverage.yml | 63 +++++++++++++++++++++ README.md | 1 + 2 files changed, 64 insertions(+) create mode 100644 .github/workflows/update-tests-coverage.yml diff --git a/.github/workflows/update-tests-coverage.yml b/.github/workflows/update-tests-coverage.yml new file mode 100644 index 0000000..00ccd12 --- /dev/null +++ b/.github/workflows/update-tests-coverage.yml @@ -0,0 +1,63 @@ +# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Update Tests Coverage + +on: + push: + branches: [ "main" ] + +jobs: + + build: + name: Go test coverage + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.22' + + - name: Install dependencies + run: go get . + + - name: Build + run: go build -v ./... + + - name: Run tests and generate coverage badge + id: badge + run: | + go test -coverprofile=coverage.out ./... + COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//') + echo "Coverage: $COVERAGE" + + COLOR="gray" + if (( $(echo "$COVERAGE < 50" | bc -l) )); then + COLOR="red" + elif (( $(echo "$COVERAGE >= 50 && $COVERAGE < 80" | bc -l) )); then + COLOR="yellow" + elif (( $(echo "$COVERAGE >= 80 && $COVERAGE < 90" | bc -l) )); then + COLOR="green" + elif (( $(echo "$COVERAGE >= 90" | bc -l) )); then + COLOR="brightgreen" + fi + + BADGE_MARKDOWN="![Coverage Badge](https://img.shields.io/badge/Coverage-${COVERAGE}%25-${COLOR}.svg)" + sed -i "s|!\[Coverage Badge\](.*)|$BADGE_MARKDOWN|g" README.md + + - name: Commit coverage badge + continue-on-error: true + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git add README.md + if ! git diff-index --quiet HEAD --; then + git commit -m "Update coverage badge to $COVERAGE%" + git push + else + echo "No changes to commit." + fi + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + diff --git a/README.md b/README.md index 70b4cb8..7716b91 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ [![Tests](https://github.com/DI-Tony-Reed/JSONDiff/actions/workflows/tests.yaml/badge.svg)](https://github.com/DI-Tony-Reed/JSONDiff/actions/workflows/tests.yaml) +![Coverage Badge](https://img.shields.io/badge/Coverage-93.8%25-brightgreen.svg) # What is this This tool accepts two JSON Snyk scans and returns the difference between them. It utilizes a slightly modified version of https://github.com/hezro/snyk-code-pr-diff for the comparison.