Skip to content

PR Test Report

PR Test Report #3

name: PR Test Report
on:
workflow_run:
workflows: [ "Test" ]
types: [ completed ]
permissions:
contents: read
actions: read
issues: write
pull-requests: write
jobs:
post-report:
if: >-
github.event.workflow_run.event == 'pull_request' &&
(github.event.workflow_run.conclusion == 'success' ||
github.event.workflow_run.conclusion == 'failure')
runs-on: ubuntu-latest
steps:
- name: Checkout main
uses: actions/checkout@v4
- name: Download test stats
uses: actions/download-artifact@v4
continue-on-error: true
with:
name: test-stats
path: test-stats
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Download coverage report
uses: actions/download-artifact@v4
continue-on-error: true
with:
name: coverage-report
path: coverage
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Generate comment body
run: node .github/scripts/test-report.js comment --stats test-stats/test-stats.json --baseline test-baseline.json --coverage coverage/jacocoAggregateReport.xml --output comment.md
- name: Upsert PR comment
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
let prNumber = (context.payload.workflow_run.pull_requests || [])[0]?.number;
if (!prNumber) {
const headBranch = context.payload.workflow_run.head_branch;
const headOwner = context.payload.workflow_run.head_repository.owner.login;
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: `${headOwner}:${headBranch}`,
state: 'open',
});
if (prs.length > 0) {
prNumber = prs[0].number;
}
}
if (!prNumber) {
console.log('No pull request associated with this workflow run, skipping.');
return;
}
const body = fs.readFileSync('comment.md', 'utf8');
const marker = '<!-- test-report -->';
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existing = comments.find(comment => comment.body.includes(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}