From 989b5c699ab0271be8467226795925ec480fef03 Mon Sep 17 00:00:00 2001 From: manas-yu Date: Tue, 12 Nov 2024 20:11:34 +0530 Subject: [PATCH] comment coverage --- .github/workflows/code_coverage_report.yml | 75 ++++++++++++++++ .github/workflows/comment_coverage_report.yml | 86 +++++-------------- 2 files changed, 97 insertions(+), 64 deletions(-) create mode 100644 .github/workflows/code_coverage_report.yml diff --git a/.github/workflows/code_coverage_report.yml b/.github/workflows/code_coverage_report.yml new file mode 100644 index 00000000000..0169895aa6a --- /dev/null +++ b/.github/workflows/code_coverage_report.yml @@ -0,0 +1,75 @@ +# Contains jobs corresponding to publishing coverage reports generated by code_coverage.yml. + +name: Code Coverage Report + +# Controls when the action will run. Triggers the workflow on pull request events +# (opened, synchronize, reopened) + +on: + pull_request: + types: [opened, synchronize, reopened] + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }} + cancel-in-progress: true + +jobs: + check_code_coverage_completed: + name: Check code coverage completed + runs-on: ubuntu-latest + steps: + - name: Wait for code coverage to complete + id: wait-for-coverage + uses: ArcticLampyrid/action-wait-for-workflow@v1.2.0 + with: + workflow: code_coverage.yml + sha: auto + allowed-conclusions: | + success + failure + + code_coverage_report: + name: Code Coverage Report + needs: check_code_coverage_completed + if: ${{ !cancelled() }} + runs-on: ubuntu-latest + steps: + - name: Find CI workflow run for PR + id: find-workflow-run + uses: actions/github-script@v7 + with: + script: | + const { owner, repo } = context.repo; + const runsResponse = await github.rest.actions.listWorkflowRuns({ + owner, + repo, + workflow_id: 'code_coverage.yml', + event: 'pull_request', // Ensure only PR events are fetched + head_sha: '${{ github.event.pull_request.head.sha }}', + }); + + const runs = runsResponse.data.workflow_runs.filter(run => run.event === 'pull_request'); + runs.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()); + + const run = runs[0]; + if (!run) { + core.setFailed('Could not find a successful workflow run for the PR'); + return; + } + + core.setOutput('run-id', run.id); + + - name: Download Generated Markdown Report + uses: actions/download-artifact@v4 + if: ${{ !cancelled() }} # IMPORTANT: Upload reports regardless of success or failure status + with: + name: final-coverage-report + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ steps.find-workflow-run.outputs.run-id }} + + - name: Upload Coverage Report for Comments + uses: actions/upload-artifact@v4 + if: ${{ !cancelled() }} + with: + name: coverage-report-for-comment + path: 'CoverageReport.md' diff --git a/.github/workflows/comment_coverage_report.yml b/.github/workflows/comment_coverage_report.yml index 0c04ff5dbcb..fcc0f6fba0c 100644 --- a/.github/workflows/comment_coverage_report.yml +++ b/.github/workflows/comment_coverage_report.yml @@ -1,82 +1,40 @@ -# Contains jobs corresponding to publishing coverage reports generated by code_coverage.yml. +# A separate workflow to comment the coverage report on the pull request name: Comment Coverage Report -# Controls when the action will run. Triggers the workflow on pull request events -# (opened, synchronize, reopened) +# This triggers the workflow when the Code Coverage Report workflow completes on: - pull_request_target: - types: [opened, synchronize, reopened] - -concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }} - cancel-in-progress: true + workflow_run: + workflows: ["Code Coverage Report"] + types: [completed] jobs: - check_code_coverage_completed: - name: Check code coverage completed + comment_coverage: + name: Comment Coverage Report on PR + if: ${{ github.event.workflow_run.conclusion == 'success' }} runs-on: ubuntu-latest - steps: - - name: Wait for code coverage to complete - id: wait-for-coverage - uses: ArcticLampyrid/action-wait-for-workflow@v1.2.0 - with: - workflow: code_coverage.yml - sha: auto - allowed-conclusions: | - success - failure - action_required - - comment_coverage_report: - name: Comment Code Coverage Report - needs: check_code_coverage_completed permissions: pull-requests: write - - # The expression if: ${{ !cancelled() }} runs a job or step regardless of its success or failure while responding to cancellations, - # serving as a cancellation-compliant alternative to if: ${{ always() }} in concurrent workflows. - if: ${{ !cancelled() }} - runs-on: ubuntu-latest steps: - - name: Find CI workflow run for PR - id: find-workflow-run - uses: actions/github-script@v7 - continue-on-error: true + - name: Get PR information + uses: potiuk/get-workflow-origin@v1_3 + id: source-run-info with: - script: | - // Find the last successful workflow run for the current PR's head - const { owner, repo } = context.repo; - const runsResponse = await github.rest.actions.listWorkflowRuns({ - owner, - repo, - workflow_id: 'code_coverage.yml', - event: 'pull_request', - head_sha: '${{ github.event.pull_request.head.sha }}', - }); - - const runs = runsResponse.data.workflow_runs; - runs.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()); - - const run = runs[0]; - if(!run) { - core.setFailed('Could not find a succesful workflow run for the PR'); - return; - } - - core.setOutput('run-id', run.id); + token: ${{ secrets.GITHUB_TOKEN }} + sourceRunId: ${{ github.event.workflow_run.id }} - - name: Download Generated Markdown Report - uses: actions/download-artifact@v4 - if: ${{ !cancelled() }} # IMPORTANT: Upload reports regardless of success or failure status + - name: Download Coverage Report + uses: dawidd6/action-download-artifact@v3 with: - name: final-coverage-report - github-token: ${{ secrets.GITHUB_TOKEN }} - run-id: ${{ steps.find-workflow-run.outputs.run-id }} + name: coverage-report-for-comment + workflow: ${{ github.event.workflow_run.workflow_id }} + run_id: ${{ github.event.workflow_run.id }} + github_token: ${{ secrets.GITHUB_TOKEN }} - - name: Upload Coverage Report as PR Comment + - name: Comment on PR + if: steps.source-run-info.outputs.pullRequestNumber != '' uses: peter-evans/create-or-update-comment@v4 with: - issue-number: ${{ github.event.pull_request.number }} + issue-number: ${{ steps.source-run-info.outputs.pullRequestNumber }} body-path: 'CoverageReport.md'