Skip to content

Commit

Permalink
[fix] Let GitHub tell us which PR (or PRs) this workflow run is for
Browse files Browse the repository at this point in the history
This fixes a bug whence someone else pushes to a PR branch than whomever opened it, and then the “bot” used to get confused.

- Use `${{ github.event.workflow_run.pull_requests }}` as seen in https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#webhook-payload-object-1 — Only it's an array (for the unlikely case that multiple PRs are open on the same branch at the same time); so pass it through `toJSON()`
- For the same reason (need to tolerate the multiple PRs case), change the call to `upsertComment` to happen in a loop.
  • Loading branch information
Dominique Quatravaux committed Dec 15, 2021
1 parent 8b6691c commit 99348f4
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions .github/workflows/pr-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,10 @@ jobs:
const {owner, repo} = context.repo;
const run_id = ${{github.event.workflow_run.id}};
const pull_head_sha = '${{github.event.workflow_run.head_sha}}';
const pull_user_id = ${{github.event.sender.id}};
const issue_number = await (async () => {
const pulls = await github.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 && pull.user.id === pull_user_id) {
return pull.number;
}
}
}
})();
if (issue_number) {
core.info(`Using pull request ${issue_number}`);
} else {
return core.error(`No matching pull request found`);
const pull_requests = ${{ toJSON(github.event.workflow_run.pull_requests) }};
if (!pull_requests.length) {
return core.error("This workflow doesn't match any pull requests!");
}
const {data: {artifacts}} = await github.actions.listWorkflowRunArtifacts({owner, repo, run_id});
Expand All @@ -65,4 +52,7 @@ jobs:
body += `\n* [${art.name}.zip](https://nightly.link/${owner}/${repo}/actions/artifacts/${art.id}.zip)`;
}
await upsertComment(owner, repo, issue_number, "nightly-link", body);
for (const pr of pull_requests) {
await upsertComment(owner, repo, pr.number,
"nightly-link", body);
}

0 comments on commit 99348f4

Please sign in to comment.