|
| 1 | +name: "Nightly Build Comment on Pull Request" |
| 2 | +on: |
| 3 | + workflow_run: |
| 4 | + workflows: ['Nightly Build'] |
| 5 | + types: [completed] |
| 6 | +jobs: |
| 7 | + pr_comment: |
| 8 | + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - name: Get the PR number |
| 12 | + run: | |
| 13 | + # Query the issue search API to get the PR associated with it |
| 14 | + PR_RAW=$(curl 'https://api.github.com/search/issues?q=${{ github.event.workflow_run.head_commit.id }}') |
| 15 | + # Get the event number from the search results, which will |
| 16 | + # be the PR number |
| 17 | + # Filter by PRs only in this repository, as a PR with an identical head commit may be made in another repository (e.g. a fork) |
| 18 | + # Assume the 0th index in the array of found PRs is the correct one (it seems to usually be the latest one) |
| 19 | + PR_NUM=$(echo $PR_RAW | jq '.items | map(select(.repository_url=="https://api.github.com/repos/${{ github.repository }}")) | .[0].number') |
| 20 | + echo "PR_NUM=${PR_NUM}" >> ${GITHUB_ENV} |
| 21 | +
|
| 22 | + - name: Comment on PR |
| 23 | + uses: actions/github-script@v7 |
| 24 | + with: |
| 25 | + # This snippet is public-domain, taken from |
| 26 | + # https://github.com/oprypin/nightly.link/blob/master/.github/workflows/pr-comment.yml |
| 27 | + # and modified to allow comments on external PRs |
| 28 | + script: | |
| 29 | + async function upsertComment(owner, repo, issue_number, purpose, body) { |
| 30 | + const {data: comments} = await github.rest.issues.listComments( |
| 31 | + {owner, repo, issue_number}); |
| 32 | +
|
| 33 | + const marker = `<!-- bot: ${purpose} -->`; |
| 34 | + body = marker + "\n" + body; |
| 35 | +
|
| 36 | + const existing = comments.filter((c) => c.body.includes(marker)); |
| 37 | + if (existing.length > 0) { |
| 38 | + const last = existing[existing.length - 1]; |
| 39 | + core.info(`Updating comment ${last.id}`); |
| 40 | + await github.rest.issues.updateComment({ |
| 41 | + owner, repo, |
| 42 | + body, |
| 43 | + comment_id: last.id, |
| 44 | + }); |
| 45 | + } else { |
| 46 | + core.info(`Creating a comment in issue / PR #${issue_number}`); |
| 47 | + await github.rest.issues.createComment({issue_number, body, owner, repo}); |
| 48 | + } |
| 49 | + } |
| 50 | +
|
| 51 | + const {owner, repo} = context.repo; |
| 52 | + const run_id = ${{github.event.workflow_run.id}}; |
| 53 | +
|
| 54 | + const artifacts = await github.paginate( |
| 55 | + github.rest.actions.listWorkflowRunArtifacts, {owner, repo, run_id}); |
| 56 | + if (!artifacts.length) { |
| 57 | + return core.error(`No artifacts found`); |
| 58 | + } |
| 59 | + let body = `Nightly build for this pull request:\n`; |
| 60 | + for (const art of artifacts) { |
| 61 | + body += `\n* [${art.name}.zip](https://nightly.link/${owner}/${repo}/actions/artifacts/${art.id}.zip)`; |
| 62 | + } |
| 63 | +
|
| 64 | + body += `\nThese artifacts will expire in 90 days and will not be available for download after that time.`; |
| 65 | + body += `\n\n_This comment is automatic and is meant to allow guests to get latest nightly builds for this pull request without registering. It is updated on every successful build._`; |
| 66 | +
|
| 67 | + core.info("Review thread message body:", body); |
| 68 | + await upsertComment(owner, repo, ${{ env.PR_NUM }}, "nightly-link", body); |
0 commit comments