PR Comment Generation #426
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: PR Comment Generation | |
on: | |
workflow_run: | |
workflows: ["Build and Test"] | |
types: | |
- completed | |
jobs: | |
comment_on_pr: | |
runs-on: ubuntu-latest | |
if: > | |
github.event.workflow_run.event == 'pull_request' && | |
github.event.workflow_run.conclusion == 'success' | |
steps: | |
- name: 'Download artifact' | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const fs = require('fs'); | |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
...context.repo, | |
run_id: ${{github.event.workflow_run.id }}, | |
}); | |
const matchArtifact = artifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == "pr" | |
})[0]; | |
const download = await github.rest.actions.downloadArtifact({ | |
...context.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(download.data)); | |
- run: unzip pr.zip | |
- name: 'Comment on PR' | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const fs = require('fs'); | |
const issueNumber = Number(fs.readFileSync('./NR')); | |
const summaryContent = fs.readFileSync('./step_summary.md', 'utf-8'); | |
const existingCommentsOpts = github.rest.issues.listComments.endpoint.merge({ | |
...context.repo, issue_number: issueNumber | |
}); | |
const existingComments = await github.paginate(existingCommentsOpts); | |
const TAG = 'execution'; | |
const tagPattern = `<!-- pr_asset_summary_comment "${TAG}" -->`; | |
const body = `${summaryContent}\n${tagPattern}`; | |
const preExistingComment = existingComments.find((comment) => comment.body?.includes(tagPattern)); | |
if(preExistingComment) { | |
await github.rest.issues.updateComment({ ...context.repo, comment_id: preExistingComment.id, body }); | |
} else { | |
await github.rest.issues.createComment({ ...context.repo, issue_number: issueNumber, body }); | |
} |