ci: Fix PR reporter for non-main branch targets by rschristian · Pull Request #4900 · preactjs/preact (original) (raw)
Re: #4891 (comment). My guinea pig is still testing this, but seems like it'll work. My guinea pig has confirmed it'll work.
TLDR: PR events, when coming from those who have maintainer rights, include a field called pull_requests that looks like this:
github.event.workflow_run.pull_requests
"pull_requests": [ { "base": { "ref": "master", "repo": { "id": 918443277, "name": "debug__workflow_run-fork", "url": "https://api.github.com/repos/rschristian/debug__workflow_run-fork" }, "sha": "8a91303965a6e8f6ceba7a9a54c631a60b6d7ce5" }, "head": { "ref": "debug/event-fields", "repo": { "id": 918443277, "name": "debug__workflow_run-fork", "url": "https://api.github.com/repos/rschristian/debug__workflow_run-fork" }, "sha": "6cbbe6a0d854cf3c4817f22560b3c4434ac65457" }, "id": 2634577244, "number": 10, "url": "https://api.github.com/repos/rschristian/debug__workflow_run-fork/pulls/10" } ]
Forked repo, on the other hand, gets this:
...which is lame & there are no other references to the base SHA anywhere else in the event payload. Alternatively, one would think they could use GitHub's API to query for PRs containing a commit, or just query the commit & get the PRs that include it, but these too don't work for unknown reasons when the commit comes from a fork.
Instead, we're using the owner+branch name, querying for all PRs on the repo that match, getting the latest result & extracting the base SHA from it. This is weird but GitHub's data doesn't give us a choice.