diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 025fb14cd20e7..e550aa1271977 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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(""))) | .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-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()