Skip to content

Main Package Coverage Comment #13

Main Package Coverage Comment

Main Package Coverage Comment #13

name: Main Package Coverage Comment
on:
workflow_run:
workflows:
- Main Package Test
types:
- completed
jobs:
comment:
if: ${{ github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' }}
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
issues: write
pull-requests: write
steps:
- name: Download coverage artifact from triggering run
uses: actions/github-script@v7
env:
ARTIFACT_NAME: main-package-coverage
ARTIFACT_ZIP: ${{ runner.temp }}/main-package-coverage.zip
with:
script: |
const fs = require('fs');
const runId = context.payload.workflow_run.id;
const artifactName = process.env.ARTIFACT_NAME;
const { data } = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId,
});
const artifact = data.artifacts.find((item) => item.name === artifactName);
if (!artifact) {
core.setFailed(`Artifact "${artifactName}" was not found for workflow run ${runId}.`);
return;
}
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
fs.writeFileSync(process.env.ARTIFACT_ZIP, Buffer.from(download.data));
- name: Extract coverage summary
run: |
mkdir -p "${RUNNER_TEMP}/main-package-coverage"
unzip -o "${RUNNER_TEMP}/main-package-coverage.zip" -d "${RUNNER_TEMP}/main-package-coverage"
- name: Comment coverage on pull request
uses: actions/github-script@v7
env:
COMMENT_MARKER: "<!-- main-package-coverage-comment -->"
COVERAGE_SUMMARY_PATH: ${{ runner.temp }}/main-package-coverage/coverage-summary.json
with:
script: |
const fs = require('fs');
const pr = context.payload.workflow_run.pull_requests[0];
if (!pr) {
core.info('No pull request is associated with this workflow run.');
return;
}
const summaryPath = process.env.COVERAGE_SUMMARY_PATH;
if (!fs.existsSync(summaryPath)) {
core.setFailed(`Coverage summary was not found at ${summaryPath}.`);
return;
}
const { total } = JSON.parse(fs.readFileSync(summaryPath, 'utf8'));
const formatMetric = (metric) => `${metric.pct.toFixed(2)}% (${metric.covered}/${metric.total})`;
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.payload.workflow_run.id}`;
const body = [
process.env.COMMENT_MARKER,
'## packages/main coverage',
'',
'| Metric | Result |',
'| --- | --- |',
`| Lines | ${formatMetric(total.lines)} |`,
`| Statements | ${formatMetric(total.statements)} |`,
`| Functions | ${formatMetric(total.functions)} |`,
`| Branches | ${formatMetric(total.branches)} |`,
'',
`[Workflow run](${runUrl})`,
].join('\n');
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
per_page: 100,
});
const existingComment = comments.find((comment) =>
comment.user?.type === 'Bot' && comment.body?.includes(process.env.COMMENT_MARKER)
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body,
});
return;
}
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body,
});