Skip to content

Commit

Permalink
Updated comment_coverage_report.yml based on previous comment
Browse files Browse the repository at this point in the history
Checks for the latest code coverage report and if they are identical then it skips posting a redundant comment else will proceed to post the currently generated code coverage report as comment.
  • Loading branch information
Rd4dev authored Nov 22, 2024
1 parent 2f68639 commit 4139e37
Showing 1 changed file with 48 additions and 19 deletions.
67 changes: 48 additions & 19 deletions .github/workflows/comment_coverage_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

name: Comment Coverage Report

# Controls when the action will run. Triggers the workflow on pull request events
# (opened, synchronize, reopened)
# Controls when the action will run. Triggers the workflow on pull request events
# (assigned, opened, synchronize, reopened)

on:
pull_request_target:
types: [opened, synchronize, reopened]
types: [assigned, opened, synchronize, reopened]

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }}
Expand All @@ -17,8 +17,6 @@ jobs:
check_code_coverage_completed:
name: Check code coverage completed
runs-on: ubuntu-latest
outputs:
conclusion: ${{ steps.wait-for-coverage.outputs.run-conclusion }}
steps:
- name: Wait for code coverage to complete
id: wait-for-coverage
Expand All @@ -29,13 +27,6 @@ jobs:
allowed-conclusions: |
success
failure
action_required
- name: Conclusion Analysis
if: steps.wait-for-coverage.outputs.run-conclusion == 'action_required'
run: |
echo "::error::First-time contributor workflows require manual approval. After approval, please re-run the comment coverage workflows to post the coverage report."
exit 1
comment_coverage_report:
name: Comment Code Coverage Report
Expand All @@ -45,9 +36,27 @@ jobs:

# 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
if: ${{ !cancelled() }}
runs-on: ubuntu-latest
steps:
- name: Find the latest Code Coverage Report Comment
uses: actions/github-script@v6
with:
script: |
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ github.event.pull_request.number }},
});
for (let i = comments.length - 1; i >= 0; i--) {
if (comments[i].body.includes("## Coverage Report")) {
const latestCodeCoverageComment = comments[i].body;
require('fs').writeFileSync('latest_code_coverage_comment.md', latestCodeCoverageComment, 'utf8');
return
}
}
- name: Find CI workflow run for PR
id: find-workflow-run
uses: actions/github-script@v7
Expand All @@ -59,32 +68,52 @@ jobs:
const runsResponse = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id: 'code_coverage.yml',
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 successful workflow run for the PR');
core.setFailed('Could not find a succesful 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: Compare Current Coverage Report with the Latest Coverage Report
run: |
if [ -f latest_code_coverage_comment.md ]; then
sed -i -e '$a\' CoverageReport.md
sed -i -e '$a\' latest_code_coverage_comment.md
if diff -B CoverageReport.md latest_code_coverage_comment.md > /dev/null; then
echo "No changes detected; skipping coverage comment."
echo "skip_coverage_comment=true" >> $GITHUB_ENV
else
echo "Changes detected; proceeding with the coverage comment."
diff CoverageReport.md latest_code_coverage_comment.md || true
echo "skip_coverage_comment=false" >> $GITHUB_ENV
fi
else
echo "No previous coverage comment found to compare; posting evaluated coverage comment."
echo "skip_coverage_comment=false" >> $GITHUB_ENV
fi
- name: Upload Coverage Report as PR Comment
if: ${{ env.skip_coverage_comment == 'false' }}
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
body-path: 'CoverageReport.md'
body-path: 'CoverageReport.md'

0 comments on commit 4139e37

Please sign in to comment.