Add Template to Pull Request #263
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: Add Template to Pull Request | |
on: | |
workflow_run: | |
workflows: ['Build Template'] | |
types: [completed] | |
jobs: | |
pr_comment: | |
# if Build Template was successful and ran on a branch that is currently the head in a pull request | |
if: github.event.workflow_run.conclusion == 'success' | |
runs-on: ubuntu-latest | |
permissions: | |
pull-requests: write | |
steps: | |
- uses: actions/github-script@v6 | |
with: | |
# This snippet is public-domain, taken from | |
# https://github.com/oprypin/nightly.link/blob/master/.github/workflows/pr-comment.yml | |
script: | | |
async function upsertComment(owner, repo, issue_number, purpose,old_body, body) { | |
const {data: comments} = await github.rest.issues.listComments( | |
{owner, repo, issue_number}); | |
const marker = `\r\n<!-- DO NOT REMOVE!! -->\r\n<!-- bot: ${purpose} -->\r\n`; | |
const old_marker = new RegExp(marker.replace("\r\n", "\r?\n")).exec(old_body)?.[0] ?? marker; | |
body = (old_body ?? "").split(old_marker)[0] + marker + body; | |
await github.request('PATCH /repos/{owner}/{repo}/pulls/{pull_number}', { | |
owner: owner, | |
repo: repo, | |
pull_number: issue_number, | |
body: body, | |
headers: { | |
'X-GitHub-Api-Version': '2022-11-28' | |
} | |
}) | |
} | |
const {owner, repo} = context.repo; | |
const run_id = ${{github.event.workflow_run.id}}; | |
const pull_head_sha = '${{github.event.workflow_run.head_sha}}'; | |
/** @type {{old_body: string}} */ | |
const {issue_number, old_body} = await (async () => { | |
const pulls = await github.rest.pulls.list({owner, repo}); | |
for await (const {data} of github.paginate.iterator(pulls)) { | |
for (const pull of data) { | |
if (pull.head.sha === pull_head_sha) { | |
return {issue_number: pull.number, old_body: pull.body}; | |
} | |
} | |
} | |
return {}; | |
})(); | |
if (issue_number) { | |
core.info(`Using pull request ${issue_number}`); | |
} else { | |
return core.error(`No matching pull request found`); | |
} | |
let old_sha = /\<\!-- commit-sha: (?<sha>[a-z0-9]+) --\>/i.exec(old_body)?.groups?.sha | |
if (old_sha != undefined && pull_head_sha == old_sha) return core.error("Comment is already up-to-date!") | |
const artifacts = await github.paginate( | |
github.rest.actions.listWorkflowRunArtifacts, {owner, repo, run_id}); | |
if (!artifacts.length) { | |
return core.error(`No artifacts found, perhaps Build Template was skipped`); | |
} | |
const template = artifacts[0]; | |
let body = `<!-- commit-sha: ${pull_head_sha} -->\n`; | |
body +=`## Download the template for this pull request: \n\n`; | |
body += `> [!NOTE] | |
> This is auto generated from [\`${{ github.workflow }}\`](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})\n`; | |
body += `- via manual download: [${template.name}.zip](https://nightly.link/${owner}/${repo}/actions/artifacts/${template.id}.zip)\n`; | |
body += `- via PROS Integrated Terminal: \n \`\`\` | |
curl -o ${template.name}.zip https://nightly.link/${owner}/${repo}/actions/artifacts/${template.id}.zip; | |
pros c fetch ${template.name}.zip; | |
pros c apply ${template.name}; | |
rm ${template.name}.zip; | |
\`\`\``; | |
core.info(`Review thread message body: \n${body}`); | |
await upsertComment(owner, repo, issue_number, | |
"nightly-link", old_body, body); |