Skip to content

Post Coverage Comment #159

Post Coverage Comment

Post Coverage Comment #159

name: Post Coverage Comment
on:
workflow_run:
workflows: ["Build and Deploy Snapshot"]
types: [completed]
permissions:
pull-requests: write
issues: write
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: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
with:
name: pr-comment
path: pr-comment/
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Post coverage comment
run: |
set -e # Exit on error
echo "Reading PR number..."
PR_NUMBER=$(cat pr-comment/pr-number.txt)
echo "PR Number: $PR_NUMBER"
echo "Checking comment body file..."
if [ ! -f pr-comment/comment-body.txt ]; then
echo "ERROR: comment-body.txt not found!"
exit 1
fi
echo "Finding existing coverage comment..."
COMMENT_ID=$(gh api "repos/${{ github.repository }}/issues/$PR_NUMBER/comments" \
--jq '.[] | select(.body | contains("<!-- jacoco-coverage-comment -->")) | .id' | head -1 || true)
if [ -n "$COMMENT_ID" ]; then
echo "Found existing comment ID: $COMMENT_ID, deleting..."
gh api -X DELETE "repos/${{ github.repository }}/issues/comments/$COMMENT_ID" || echo "Warning: Failed to delete comment, continuing..."
else
echo "No existing comment found"
fi
echo "Posting new coverage comment..."
# Use jq to properly encode the comment body as JSON
jq -n --rawfile body pr-comment/comment-body.txt '{body: $body}' | \
gh api "repos/${{ github.repository }}/issues/$PR_NUMBER/comments" --input -
echo "Comment posted successfully!"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}