Comment with API Coverage #155
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: Comment with API Coverage | |
on: | |
workflow_run: | |
workflows: ["Gather API Coverage"] | |
types: | |
- completed | |
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/download-artifact@v4 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
pattern: coverage-* | |
merge-multiple: true | |
run-id: ${{ github.event.workflow_run.id }} | |
- name: 'Comment on PR' | |
uses: actions/github-script@v3 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const fs = require('fs'); | |
const coverage_api = JSON.parse(fs.readFileSync('./coverage-api.json')); | |
console.log(coverage_api); | |
body = `API specs implemented for ${coverage_api.current}/${coverage_api.total} (${coverage_api.percent}%) APIs.`; | |
commit = `Commit ${coverage_api.sha}.` | |
const { data: comments } = await github.issues.listComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: coverage_api.pull_request | |
}); | |
const existing_comment = comments.find( | |
comment => comment.body.startsWith("API specs implemented for ") | |
); | |
if (existing_comment && ! existing_comment.body.startsWith(body)) { | |
// change in coverage, delete existing comment | |
console.log(`Deleting ${existing_comment.url}.`); | |
await github.issues.deleteComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: coverage_api.pull_request, | |
comment_id: existing_comment.id | |
}); | |
} | |
if (existing_comment && existing_comment.body.startsWith(body)) { | |
// no change in coverage, update commit id | |
await github.issues.updateComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: coverage_api.pull_request, | |
comment_id: existing_comment.id, | |
body: body + "\n" + commit | |
}); | |
} else { | |
// create a new comment | |
await github.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: coverage_api.pull_request, | |
body: body + "\n" + commit | |
}); | |
} |