diff --git a/.github/workflows/coverage-comment.yml b/.github/workflows/coverage-comment.yml new file mode 100644 index 0000000..d57dbe4 --- /dev/null +++ b/.github/workflows/coverage-comment.yml @@ -0,0 +1,60 @@ +name: Post Coverage Comment +# Runs in the context of the base repository (with write access) after "Build" +# completes for a pull request, so comments can be posted on PRs from forks too. +# Only consumes the pre-rendered comment artifact; never checks out PR code. +on: + workflow_run: + workflows: ["Build"] + types: [completed] + +permissions: + actions: read + pull-requests: write + +# Serialize comment updates per PR branch so overlapping runs do not race +concurrency: + group: coverage-comment-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} + cancel-in-progress: false + +jobs: + comment: + runs-on: ubuntu-24.04 + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' + steps: + - name: Download comment artifact + uses: dawidd6/action-download-artifact@a92c7ae74cf1df8810783758581e485ac3aaa309 # v28 + with: + name: pr-comment + path: pr-comment/ + run_id: ${{ github.event.workflow_run.id }} + github_token: ${{ secrets.GITHUB_TOKEN }} + + - name: Post coverage comment + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + RUN_HEAD_SHA: ${{ github.event.workflow_run.head_sha }} + run: | + PR_NUMBER=$(cat pr-comment/pr-number.txt) + # Artifact comes from an untrusted PR run: make sure PR number is numeric... + if ! [[ "$PR_NUMBER" =~ ^[0-9]+$ ]]; then + echo "Invalid PR number: '$PR_NUMBER'" + exit 1 + fi + # ...and that it refers to the PR this run actually built (trusted event data). + # Also skips stale runs if the PR has been updated since. + PR_HEAD_SHA=$(gh api "repos/$REPO/pulls/$PR_NUMBER" --jq '.head.sha') + if [ "$PR_HEAD_SHA" != "$RUN_HEAD_SHA" ]; then + echo "PR #$PR_NUMBER head ($PR_HEAD_SHA) does not match built commit ($RUN_HEAD_SHA); skipping" + exit 0 + fi + + # Find and delete existing coverage comment(s) posted by this bot + # (REST API gives numeric ids) + for COMMENT_ID in $(gh api --paginate "repos/$REPO/issues/$PR_NUMBER/comments" \ + --jq '.[] | select(.user.login == "github-actions[bot]" and (.body | contains(""))) | .id'); do + gh api -X DELETE "repos/$REPO/issues/comments/$COMMENT_ID" || true + done + + # Post new comment + gh pr comment "$PR_NUMBER" --repo "$REPO" --body-file pr-comment/comment-body.txt diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b820d21..a6efa66 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,10 +20,17 @@ permissions: jobs: build: runs-on: 'ubuntu-latest' + permissions: + contents: read + # to download base branch coverage artifact from another workflow run + actions: read strategy: fail-fast: false matrix: java_version: ['8', '17', '21', '25'] + include: + - java_version: '8' + release_build: 'R' env: JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" steps: @@ -36,13 +43,128 @@ jobs: cache: 'maven' - name: Build run: ./mvnw -B -ff -ntp clean verify - - name: Generate code coverage - if: ${{ github.event_name != 'pull_request' && matrix.java_version == '8' }} - run: ./mvnw -B -q -ff -ntp test + # JaCoCo report (incl. jacoco.csv) is already produced by "verify" (bound to "test" phase) - name: Publish code coverage - if: ${{ github.event_name != 'pull_request' && matrix.java_version == '8' }} + if: ${{ matrix.release_build && github.event_name != 'pull_request' && github.repository == 'FasterXML/java-classmate' }} uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1 with: token: ${{ secrets.CODECOV_TOKEN }} files: ./target/site/jacoco/jacoco.xml flags: unittests + - name: Upload coverage report as artifact + if: ${{ matrix.release_build && github.event_name != 'pull_request' && github.repository == 'FasterXML/java-classmate' }} + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: jacoco-report + path: target/site/jacoco/jacoco.csv + retention-days: 30 + - name: Download base branch coverage + if: ${{ matrix.release_build && github.event_name == 'pull_request' }} + uses: dawidd6/action-download-artifact@a92c7ae74cf1df8810783758581e485ac3aaa309 # v28 + continue-on-error: true + with: + workflow: main.yml + branch: ${{ github.event.pull_request.base.ref }} + # Only push builds upload the artifact; avoid picking up PR runs from + # forks whose head branch happens to have the same name as base branch + event: push + name: jacoco-report + path: base-coverage/ + - name: Generate coverage comment + if: ${{ matrix.release_build && github.event_name == 'pull_request' }} + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + run: | + # Parse JaCoCo CSV to compute coverage percentage + # Columns: GROUP,PACKAGE,CLASS,INSTRUCTION_MISSED(4),INSTRUCTION_COVERED(5),BRANCH_MISSED(6),BRANCH_COVERED(7),... + parse_coverage() { + local csv_file=$1 col_missed=$2 col_covered=$3 + awk -F',' -v m="$col_missed" -v c="$col_covered" \ + 'NR>1 { + total_missed += $m; + total_covered += $c; + } + END { + if (total_missed + total_covered > 0) + printf "%.2f", (total_covered * 100.0) / (total_missed + total_covered) + }' "$csv_file" + } + + format_delta() { + local delta=$1 + if awk -v d="$delta" 'BEGIN { exit (d >= 0) ? 0 : 1 }'; then + echo "+${delta}%|📈" + else + echo "${delta}%|📉" + fi + } + + get_color() { + awk -v v="$1" 'BEGIN { + if (v >= 80) print "brightgreen" + else if (v >= 60) print "green" + else if (v >= 40) print "yellow" + else print "red" + }' + } + + # Compute current coverage from JaCoCo CSV + COVERAGE=$(parse_coverage "target/site/jacoco/jacoco.csv" 4 5) + BRANCHES=$(parse_coverage "target/site/jacoco/jacoco.csv" 6 7) + + # Check if base coverage artifact was downloaded and calculate deltas + HAS_DELTA=false + if [ -f "base-coverage/jacoco.csv" ]; then + echo "Found base branch coverage from artifact" + BASE_COVERAGE=$(parse_coverage "base-coverage/jacoco.csv" 4 5) + BASE_BRANCHES=$(parse_coverage "base-coverage/jacoco.csv" 6 7) + + if [ -n "$BASE_COVERAGE" ] && [ -n "$BASE_BRANCHES" ]; then + COV_DELTA=$(awk -v curr="$COVERAGE" -v base="$BASE_COVERAGE" 'BEGIN { printf "%.3f", curr - base }') + BR_DELTA=$(awk -v curr="$BRANCHES" -v base="$BASE_BRANCHES" 'BEGIN { printf "%.3f", curr - base }') + + IFS='|' read -r COV_DELTA_STR COV_DELTA_EMOJI <<< "$(format_delta "$COV_DELTA")" + IFS='|' read -r BR_DELTA_STR BR_DELTA_EMOJI <<< "$(format_delta "$BR_DELTA")" + + HAS_DELTA=true + fi + else + echo "No base branch coverage artifact found; reporting without deltas" + fi + + # Determine badge colors + COV_COLOR=$(get_color "$COVERAGE") + BR_COLOR=$(get_color "$BRANCHES") + + # Build coverage table with or without deltas + if [ "$HAS_DELTA" = "true" ]; then + COVERAGE_TABLE="| Metric | Coverage | Change | + |--------|----------|--------| + | **Instructions** | ![coverage](https://img.shields.io/badge/coverage-${COVERAGE}%25-${COV_COLOR}) **${COVERAGE}%** | ${COV_DELTA_EMOJI} **${COV_DELTA_STR}** | + | **Branches** | ![branches](https://img.shields.io/badge/branches-${BRANCHES}%25-${BR_COLOR}) **${BRANCHES}%** | ${BR_DELTA_EMOJI} **${BR_DELTA_STR}** |" + else + COVERAGE_TABLE="| Metric | Coverage | + |--------|----------| + | **Instructions** | ![coverage](https://img.shields.io/badge/coverage-${COVERAGE}%25-${COV_COLOR}) **${COVERAGE}%** | + | **Branches** | ![branches](https://img.shields.io/badge/branches-${BRANCHES}%25-${BR_COLOR}) **${BRANCHES}%** |" + fi + + COMMENT_BODY="## :test_tube: Code Coverage Report + + $COVERAGE_TABLE + + > Coverage data generated from JaCoCo test results + + " + + # Save comment for the workflow_run-triggered "Post Coverage Comment" workflow + mkdir -p pr-comment + echo "$PR_NUMBER" > pr-comment/pr-number.txt + echo "$COMMENT_BODY" > pr-comment/comment-body.txt + - name: Upload PR comment + if: ${{ matrix.release_build && github.event_name == 'pull_request' }} + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: pr-comment + path: pr-comment/ + retention-days: 1