Add coverage failure as comment to PR (#15903)

* Add coverage failure as comment to PR

* whitespace

* missed one

* Typo

* Add env

* fixing condition check, moving to separate step

* move env token to the right step

* Added comment/label update on success/failure

* fixing gh api

* fixing jq
This commit is contained in:
William Allen 2023-08-07 23:50:49 -05:00 committed by GitHub
parent bcf3da8779
commit dd2abb0ad4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -159,11 +159,58 @@ jobs:
run: |
set -o pipefail
diff-cover --compare-branch=${{ env.compare-branch }} --fail-under=100 --html-report=coverage-reports/diff-cover.html --markdown-report=coverage-reports/diff-cover.md coverage-reports/coverage.xml | tee coverage-reports/diff-cover-stdout
COV_STATUS="${PIPESTATUS[0]}"
echo "COV_STATUS=$COV_STATUS" >> "$GITHUB_ENV"
- name: Remove previous coverage report comment and label from PR
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
PR_NUM=$(jq -r '.number' "$GITHUB_EVENT_PATH")
comments=$(gh api -X GET /repos/${{ github.repository }}/issues/${PR_NUM}/comments)
comment_id=$(echo "$comments" | jq '.[] | select(.user.login == "github-actions[bot]" and (.body | tostring | contains("<!-- COVERAGE_COMMENT_'${PR_NUM}' -->"))) | .id')
if [[ -n "$comment_id" ]]; then
gh api -X DELETE /repos/${{ github.repository }}/issues/comments/"$comment_id"
fi
if [[ ${{ env.COV_STATUS }} == 0 ]]; then
if gh pr view "$PR_NUM" --json labels --jq ' .labels[].name | select(. == "coverage-diff")'; then
gh pr edit "$PR_NUM" --remove-label "coverage-diff"
fi
fi
- name: Add diff coverage report comment to PR
if: env.COV_STATUS != '0'
env:
GH_TOKEN: ${{ github.token }}
run: |
# Add unique identifier to the comment file
PR_NUM=$(jq -r '.number' "$GITHUB_EVENT_PATH")
echo "<!-- COVERAGE_COMMENT_${PR_NUM} -->" > coverage-reports/diff-cover-md-table
# Transform diff-cover output into a markdown table
awk 'BEGIN{print "| File | Coverage | Missing Lines |"; print "|------|----------|---------------|"}
/.*\.py/ {
split($0,a,"(");
gsub(")","",a[2])
if (index(a[2], ":") == 0) {
print "| " a[1] " | " a[2] " | None |"
} else {
split(a[2],b,":");
print "| " a[1] " | " b[1] " | " substr(b[2], 15) " |"
}
}' coverage-reports/diff-cover-stdout >> coverage-reports/diff-cover-md-table
cat coverage-reports/diff-cover-md-table >> "$GITHUB_STEP_SUMMARY"
# Add comment to PR
gh pr comment "$PR_NUM" -F coverage-reports/diff-cover-md-table
# If label doesn't exist, add it
if ! gh pr view "$PR_NUM" --json labels --jq ' .labels[].name | select(. == "coverage-diff")'; then
gh pr edit "$PR_NUM" --add-label "coverage-diff"
fi
- name: Add diff coverage report to workflow summary
if: always()
run: |
cat coverage-reports/diff-cover.md >> $GITHUB_STEP_SUMMARY
cat coverage-reports/diff-cover.md >> "$GITHUB_STEP_SUMMARY"
- name: Publish coverage reports
if: always()