Coverage Report Comment #2833
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Coverage Report Comment | |
on: | |
workflow_run: | |
workflows: | |
- test | |
types: | |
- completed | |
workflow_dispatch: | |
defaults: | |
run: | |
shell: bash | |
jobs: | |
comment: | |
runs-on: ubuntu-latest | |
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' | |
steps: | |
- name: Download coverage report | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: ${{ github.event.workflow_run.id }}, | |
}) | |
const matchArtifact = artifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == 'report' | |
})[0] | |
const download = await github.rest.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}) | |
const fs = require('fs') | |
fs.writeFileSync('${{github.workspace}}/report.zip', Buffer.from(download.data)) | |
- name: Unzip report | |
run: unzip report.zip | |
- name: Comment coverage result to Pull Requests | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const fs = require('fs') | |
const baseReport = fs.readFileSync('report.txt', 'utf8').toString().split('\n') | |
let report = '' | |
for (let i = 0; i < baseReport.length; i++) { | |
const line = baseReport[i].split(' ').filter(v => v) | |
if (i === 1 && line.length === 1) { | |
report += "|:---|---:|---:|---:|\n" | |
} else if (line.length === 1) { | |
continue | |
} else { | |
if (i !== 0 && line.length === 4) { | |
const parcent = Number(line[3].replace("%", "")) | |
let color = 'green' | |
if (parcent < 50) { | |
color = 'red' | |
} else if (parcent < 90) { | |
color = 'orange' | |
} | |
line[3] = `![coverage-${parcent}%](https://img.shields.io/badge/coverage-${parcent}%25-${color}.svg)` | |
} | |
report += "|" + line.join("|") + "|\n" | |
} | |
if (line[0] === 'TOTAL') break | |
} | |
const issue_number = Number(fs.readFileSync('pr_num.txt')) | |
const body = `## Coverage Result\n\n<details>\n<summary>Resultを開く</summary>\n\n${report}\n</details>` | |
let listComments = await github.rest.issues.listComments({ | |
issue_number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
}) | |
listComments = listComments.data.filter((comment) => { | |
return comment.body.includes('Coverage Result') && comment.user.login.includes('github-actions') | |
}) | |
if (listComments.length === 0) { | |
github.rest.issues.createComment({ | |
issue_number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body, | |
}) | |
} else { | |
github.rest.issues.updateComment({ | |
comment_id: listComments[0].id, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body, | |
}) | |
} |