Skip to content

Commit

Permalink
Provide coverage report after build (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
cklutz authored Oct 11, 2024
1 parent e587a86 commit c5c0f3f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 21 deletions.
111 changes: 90 additions & 21 deletions .github/workflows/upload-coverage-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Upload Coverage Report

on:
workflow_dispatch:
schedule:
- cron: '0 5 * * *' # daily at 00:05

jobs:
update-coverage-report:
Expand All @@ -19,38 +21,105 @@ jobs:
concurrency:
group: "pages"
cancel-in-progress: false
strategy:
matrix:
spec:
- .github/workflows/dotnet-windows.yml codecoverage-report-windows-net8.0-Release, codecoverage-report-windows-net481-Release
- .github/workflows/dotnet-ubuntu.yml codecoverage-report-ubuntu-net8.0-Release

env:
OUTPUT_DIR: _site/coverage
WORKFLOW_SPEC: >
dotnet-windows.yml: coverage-report-windows-net8.0-Release,coverage-report-windows-net481-Release |
dotnet-ubuntu.yml: coverage-report-ubuntu-net8.0-Release
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # avoid shallow clone so that nbgv can do its work.

- name: Check for commits
id: check
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
echo "update_report=true" >> "$GITHUB_OUTPUT"
else
commit_count=$(git log --oneline --since '24 hours ago' | wc -l)
echo "$commit_count found in the last 24 hours"
if [[ $commit_count -gt 0 ]]; then
echo "update_report=true" >> "$GITHUB_OUTPUT"
else
echo "update_report=false" >> "$GITHUB_OUTPUT"
fi
fi
- name: 'Download latest successful build artifact'
run: |
IFS=' ' read -ra spec <<< "${{ matrix.spec }}"
workflow_id=$(echo ${spec[0]} | xargs)
run_id=$(gh run list -w $workflow_id --json conclusion,headBranch,databaseId --jq 'first(.[] | select(.conclusion | contains("success"))) | .databaseId')
[ -z "$run_id" ] && echo "::error:::No successful run found for workflow '$workflow_id'" && exit 1 || true
echo "::notice::Last successful run for workflow '$workflow_id' is '$run_id'"
IFS=',' read -ra artifacts <<< "${spec[@]:1}"
for i in "${artifacts[@]}"; do
artifact_id=$(echo $i | xargs)
artifact_subdir=$(echo $artifact_id | sed 's/codecoverage-report-//g')
echo "::notice::Downloading '$artifact_id'"
gh run download $run_id -n $artifact_id -D $ARTIFACTS_TMP_DIR
unzip $ARTIFACTS_TMP_DIR/${artifact_id}.zip -d $OUTPUT_DIR/$artifact_subdir
IFS='|' read -ra wsspec <<< "${{ env.WORKFLOW_SPEC }}"
for ispec in "${wsspec[@]}"; do
IFS=':' read -ra spec <<< "$ispec"
workflow_id=$(echo ${spec[0]} | xargs)
echo "Getting last successful run for workflow '$workflow_id'"
run_id=$(gh run list -w $workflow_id --json conclusion,headBranch,databaseId --jq 'first(.[] | select(.conclusion | contains("success"))) | .databaseId')
if [ -z "$run_id" ]; then
echo "::error::No successful run found for workflow '$workflow_id'" 1>&2
exit 1
fi
echo "Last successful run for workflow '$workflow_id' is '$run_id'"
IFS=',' read -ra artifacts <<< "${spec[@]:1}"
for i in "${artifacts[@]}"; do
artifact_id=$(echo $i | xargs)
artifact_subdir=$OUTPUT_DIR/$(echo $artifact_id | sed 's/coverage-report-//g' | tr '[:upper:]' '[:lower:]')
echo "Downloading artifact '$artifact_id' for workflow '$workflow_id'"
gh run download $run_id -n $artifact_id -D $artifact_subdir || exit 1
done
done
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WF_SPEC: ${{ matrix.spec }}
ARTIFACTS_TMP_DIR: artifacts
OUTPUT_DIR: coverage-results
if: ${{ steps.check.outputs.update_report }}

- name: Generate Badges
run: |
for summary in $OUTPUT_DIR/**/Summary.json
do
echo "Generate badge for $summary"
badge_content=$(echo $(basename $(dirname $summary)) | sed -e 's/-release//g' -e 's/-debug//g' -e 's/_/__/g' -e 's/-/--/g' -e 's/ /_/g')
branch_coverage=$(printf "%.0f" $(jq ".summary.branchcoverage" $summary))
line_coverage=$(printf "%.0f" $(jq ".summary.linecoverage" $summary))
if (( branch_coverage < line_coverage )); then
min_coverage=$branch_coverage
else
min_coverage=$line_coverage
fi
if [[ $min_coverage -le 50 ]]; then
badge_color=red
elif [[ $min_coverage -le 70 ]]; then
badge_color=magenta
elif [[ $min_coverage -le 90 ]]; then
badge_color=yellow
else
badge_color=green
fi
badge_url="https://img.shields.io/badge/coverage_${badge_content}-b_${branch_coverage}%25_l_${line_coverage}%25-${badge_color}?style=flat-square"
echo "Badge URL: $badge_url"
curl -o $(dirname $summary)/badge.svg "$badge_url"
done
if: ${{ steps.check.outputs.update_report }}

- name: Setup Pages
uses: actions/configure-pages@v5
if: ${{ steps.check.outputs.update_report }}

- name: Upload Pages
uses: actions/upload-pages-artifact@v3
with:
path: coverage-results
path: _site/coverage
if: ${{ steps.check.outputs.update_report }}

- name: Deploy Pages
id: deployment
uses: actions/deploy-pages@v4
if: ${{ steps.check.outputs.update_report }}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Uses platform APIs to find processes locking one or multiple files or directorie
[![windows](https://github.com/cklutz/LockCheck/workflows/Windows/badge.svg)](https://github.com/cklutz/LockCheck/actions?query=workflow%3AWindows)
[![ubuntu](https://github.com/cklutz/LockCheck/workflows/Ubuntu/badge.svg)](https://github.com/cklutz/LockCheck/actions?query=workflow%3AUbuntu)

<!-- coverage badges are static files, include "cache-control" in url to make sure they refresh; values same as for GitHub badge.svg on builds -->
[![](https://cklutz.github.io/LockCheck/ubuntu-net8.0-release/badge.svg?cache-control=max-age=300,private)](https://cklutz.github.io/LockCheck/ubuntu-net8.0-release)
[![](https://cklutz.github.io/LockCheck/windows-net8.0-release/badge.svg?cache-control=max-age=300,private)](https://cklutz.github.io/LockCheck/windows-net8.0-release)
[![](https://cklutz.github.io/LockCheck/windows-net481-release/badge.svg?cache-control=max-age=300,private)](https://cklutz.github.io/LockCheck/windows-net481-release)


### Platforms

## Windows
Expand Down

0 comments on commit c5c0f3f

Please sign in to comment.