From 9bda4eb999bf47a90e61b7d21c74e81f553bd5c8 Mon Sep 17 00:00:00 2001 From: Yuval Shavit Date: Sun, 9 Jun 2024 15:15:15 -0400 Subject: [PATCH] add code coverage Push coverage (lines %) to a gist, which then backs a https://shields.io badge. --- .github/workflows/coverage.yml | 26 ++++++++++++++++++ .github/workflows/rust.yml | 4 +-- README.md | 2 ++ {.github/workflows => scripts}/cargo_to_gh | 3 ++- scripts/common.sh | 10 +++++++ scripts/percent_to_color | 31 ++++++++++++++++++++++ scripts/percent_to_shields_gist | 22 +++++++++++++++ 7 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/coverage.yml create mode 100644 README.md rename {.github/workflows => scripts}/cargo_to_gh (94%) create mode 100644 scripts/common.sh create mode 100755 scripts/percent_to_color create mode 100755 scripts/percent_to_shields_gist diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 0000000..ecd93c5 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,26 @@ +name: Coverage + +on: + push: + branches: [ "main" ] + +jobs: + coverage: + runs-on: ubuntu-latest + environment: Code Coverage Badge + env: + CARGO_TERM_COLOR: always + steps: + - uses: actions/checkout@v4 + - name: Install cargo-llvm-cov + uses: taiki-e/install-action@cargo-llvm-cov + - name: Generate code coverage + run: | + percent_coverage="$(cargo llvm-cov --json | jq '.data[].totals.lines.percent')" + printf '::notice title=Coverage (lines %%)::%s' "$percent_coverage" + echo "LINES_PERCENT=$percent_coverage" >> "$GITHUB_ENV" + - name: Upload to gist + run: ./scripts/percent_to_shields_gist coverage "$LINES_PERCENT" + env: + GIST_URL: ${{ vars.GIST_URL }} + GH_TOKEN: ${{ secrets.API_TOKEN }} diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index cff28d7..ffa0e69 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -15,7 +15,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: cargo build - run: .github/workflows/cargo_to_gh rustc --message-format json -- -Awarnings + run: scripts/cargo_to_gh rustc --message-format json -- -Awarnings check: runs-on: ubuntu-latest @@ -23,7 +23,7 @@ jobs: steps: - uses: actions/checkout@v4 - name: cargo check - run: .github/workflows/cargo_to_gh check + run: scripts/cargo_to_gh check test: runs-on: ubuntu-latest diff --git a/README.md b/README.md new file mode 100644 index 0000000..b261212 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +![Code Coverage](https://img.shields.io/endpoint?url=https%3A%2F%2Fgist.githubusercontent.com%2Fyshavit%2F53901a0115b596e015a891c41fb0f256%2Fraw%2Fmdq-coverage.json) + diff --git a/.github/workflows/cargo_to_gh b/scripts/cargo_to_gh similarity index 94% rename from .github/workflows/cargo_to_gh rename to scripts/cargo_to_gh index 4971e97..2261dda 100755 --- a/.github/workflows/cargo_to_gh +++ b/scripts/cargo_to_gh @@ -1,8 +1,9 @@ #!/bin/bash +source scripts/common.sh || exit 1 + json_opts=() if ! echo "$*" | grep -q -- '--message-format json' ; then - echo "NO: $*" json_opts=(--message-format json) fi diff --git a/scripts/common.sh b/scripts/common.sh new file mode 100644 index 0000000..693a821 --- /dev/null +++ b/scripts/common.sh @@ -0,0 +1,10 @@ +set -euo pipefail + +function msg() { + echo >&2 "$@" +} + +function err() { + msg "$@" + exit 1 +} diff --git a/scripts/percent_to_color b/scripts/percent_to_color new file mode 100755 index 0000000..f8f3360 --- /dev/null +++ b/scripts/percent_to_color @@ -0,0 +1,31 @@ +#!/bin/bash + +source scripts/common.sh || exit 1 + +if [ $# -ne 1 ]; then + err 'require exactly one argument' +fi +percent="$1" + +percent_0_to_5="$(printf 'scale=0; %s / 20\n' "$percent" | bc)" + +case "$percent_0_to_5" in + 0) # 0 - 19% + echo darkred + ;; + 1) # 20 - 39% + echo crimson + ;; + 2) # 40 - 59% + echo orangered + ;; + 3) # 60 - 79% + echo yellowgreen + ;; + 4 | 5) + echo forestgreen + ;; + *) + err 'invalid percent' + ;; +esac diff --git a/scripts/percent_to_shields_gist b/scripts/percent_to_shields_gist new file mode 100755 index 0000000..44bebf1 --- /dev/null +++ b/scripts/percent_to_shields_gist @@ -0,0 +1,22 @@ +#!/bin/bash + +source scripts/common.sh || exit 1 + +if [ -z "${GIST_URL:-}" ]; then + err "no GIST_URL env set" +fi +if [ $# -ne 2 ]; then + err 'requires exactly two arguments' +fi +label="$1" +percent="$2" + +color="$(scripts/percent_to_color "$percent")" + +json_text="$(echo '{}' | + jq -c '{schemaVersion: 1, label: $badge_label, color: $color, message: $percent}' \ + --arg badge_label "$label" --arg color "$color" --arg percent "$(printf '%.1f%%' "$percent")" )" + +gh gist edit "$GIST_URL" <(echo "$json_text") + +