|
| 1 | +name: Add Template to Pull Request |
| 2 | +on: |
| 3 | + workflow_run: |
| 4 | + workflows: ['Build Template'] |
| 5 | + types: [completed] |
| 6 | +jobs: |
| 7 | + pr_comment: |
| 8 | + # if Build Template was successful and ran on a branch that is currently the head in a pull request |
| 9 | + if: github.event.workflow_run.conclusion == 'success' |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + permissions: |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | + steps: |
| 16 | + - uses: actions/github-script@v6 |
| 17 | + with: |
| 18 | + # This snippet is public-domain, taken from |
| 19 | + # https://github.com/oprypin/nightly.link/blob/master/.github/workflows/pr-comment.yml |
| 20 | + script: | |
| 21 | + async function upsertComment(owner, repo, issue_number, purpose,old_body, body) { |
| 22 | + const {data: comments} = await github.rest.issues.listComments( |
| 23 | + {owner, repo, issue_number}); |
| 24 | + |
| 25 | + const marker = `\r\n<!-- DO NOT REMOVE!! -->\r\n<!-- bot: ${purpose} -->\r\n`; |
| 26 | +
|
| 27 | + const old_marker = new RegExp(marker.replace("\r\n", "\r?\n")).exec(old_body)?.[0] ?? marker; |
| 28 | + |
| 29 | + body = old_body.split(old_marker)[0] + marker + body; |
| 30 | + await github.request('PATCH /repos/{owner}/{repo}/pulls/{pull_number}', { |
| 31 | + owner: owner, |
| 32 | + repo: repo, |
| 33 | + pull_number: issue_number, |
| 34 | + body: body, |
| 35 | + headers: { |
| 36 | + 'X-GitHub-Api-Version': '2022-11-28' |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + const {owner, repo} = context.repo; |
| 42 | + const run_id = ${{github.event.workflow_run.id}}; |
| 43 | + |
| 44 | + const pull_head_sha = '${{github.event.workflow_run.head_sha}}'; |
| 45 | + |
| 46 | + /** @type {{old_body: string}} */ |
| 47 | + const {issue_number, old_body} = await (async () => { |
| 48 | + const pulls = await github.rest.pulls.list({owner, repo}); |
| 49 | + for await (const {data} of github.paginate.iterator(pulls)) { |
| 50 | + for (const pull of data) { |
| 51 | + if (pull.head.sha === pull_head_sha) { |
| 52 | + return {issue_number: pull.number, old_body: pull.body}; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + return {}; |
| 57 | + })(); |
| 58 | + if (issue_number) { |
| 59 | + core.info(`Using pull request ${issue_number}`); |
| 60 | + } else { |
| 61 | + return core.error(`No matching pull request found`); |
| 62 | + } |
| 63 | + |
| 64 | + let old_sha = /\<\!-- commit-sha: (?<sha>[a-z0-9]+) --\>/i.exec(old_body)?.groups?.sha |
| 65 | + if (old_sha != undefined && pull_head_sha == old_sha) return core.error("Comment is already up-to-date!") |
| 66 | + |
| 67 | + const artifacts = await github.paginate( |
| 68 | + github.rest.actions.listWorkflowRunArtifacts, {owner, repo, run_id}); |
| 69 | + if (!artifacts.length) { |
| 70 | + return core.error(`No artifacts found, perhaps Build Template was skipped`); |
| 71 | + } |
| 72 | + const template = artifacts[0]; |
| 73 | + |
| 74 | + let body = `<!-- commit-sha: ${pull_head_sha} -->\n`; |
| 75 | + body +=`## Download the template for this pull request: \n\n`; |
| 76 | + body += `> [!NOTE] |
| 77 | + > This is auto generated from [\`${{ github.workflow }}\`](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})\n`; |
| 78 | + body += `- via manual download: [${template.name}.zip](https://nightly.link/${owner}/${repo}/actions/artifacts/${template.id}.zip)\n`; |
| 79 | + body += `- via PROS Integrated Terminal: \n \`\`\` |
| 80 | + curl -o ${template.name}.zip https://nightly.link/${owner}/${repo}/actions/artifacts/${template.id}.zip; |
| 81 | + pros c fetch ${template.name}.zip; |
| 82 | + pros c apply ${template.name}; |
| 83 | + rm ${template.name}.zip; |
| 84 | + \`\`\``; |
| 85 | + |
| 86 | + core.info(`Review thread message body: \n${body}`); |
| 87 | + |
| 88 | + await upsertComment(owner, repo, issue_number, |
| 89 | + "nightly-link", old_body, body); |
0 commit comments