| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | |
| 5 | INPUT="${INPUT_COVERAGE-}" |
| 6 | |
| 7 | # Create an HTML report. |
| 8 | if [[ "${INPUT_REPORT-true}" == "true" ]]; then |
| 9 | go tool cover -html="$INPUT" -o coverage.html |
| 10 | fi |
| 11 | |
| 12 | # Extract total coverage: the decimal number from the last line of the function report. |
| 13 | COVERAGE=$(go tool cover -func="$INPUT" | tail -1 | grep -Eo '[0-9]+\.[0-9]') |
| 14 | |
| 15 | echo "coverage: $COVERAGE% of statements" |
| 16 | |
| 17 | # Pick a color for the badge. |
| 18 | if awk "BEGIN {exit !($COVERAGE >= 90)}"; then |
| 19 | COLOR=brightgreen |
| 20 | elif awk "BEGIN {exit !($COVERAGE >= 80)}"; then |
| 21 | COLOR=green |
| 22 | elif awk "BEGIN {exit !($COVERAGE >= 70)}"; then |
| 23 | COLOR=yellowgreen |
| 24 | elif awk "BEGIN {exit !($COVERAGE >= 60)}"; then |
| 25 | COLOR=yellow |
| 26 | elif awk "BEGIN {exit !($COVERAGE >= 50)}"; then |
| 27 | COLOR=orange |
| 28 | else |
| 29 | COLOR=red |
| 30 | fi |
| 31 | |
| 32 | # Style for the badge. |
| 33 | STYLE="${INPUT_BADGE_STYLE-flat}" |
| 34 | # Title for the badge. |
| 35 | TITLE="${INPUT_BADGE_TITLE-coverage}" |
| 36 | |
| 37 | # Download the badge. |
| 38 | curl -s "https://img.shields.io/badge/$(printf %s "$TITLE" | jq -sRr @uri)-$COVERAGE%25-$COLOR?style=$STYLE" > "coverage.svg" |