ci: read PR number from workflow run
This commit is contained in:
48
.github/workflows/comment-pr-artifacts.yml
vendored
48
.github/workflows/comment-pr-artifacts.yml
vendored
@@ -27,50 +27,22 @@ jobs:
|
|||||||
const owner = context.repo.owner;
|
const owner = context.repo.owner;
|
||||||
const repo = context.repo.repo;
|
const repo = context.repo.repo;
|
||||||
|
|
||||||
const prs = run.pull_requests || [];
|
let prNumber = run.pull_requests?.[0]?.number;
|
||||||
let prNumber = prs[0]?.number;
|
|
||||||
|
|
||||||
// `workflow_run` payload does not reliably include pull request numbers.
|
// `workflow_run` payload does not reliably include pull request numbers.
|
||||||
// First try to resolve by head repo owner + head branch, then fall back to head SHA.
|
// Ask the Actions API for the triggering run itself and use that as source of truth.
|
||||||
if (!prNumber) {
|
if (!prNumber) {
|
||||||
const headOwner = run.head_repository?.owner?.login;
|
const runDetails = await github.rest.actions.getWorkflowRun({
|
||||||
const headBranch = run.head_branch;
|
owner,
|
||||||
|
repo,
|
||||||
if (headOwner && headBranch) {
|
run_id: run.id,
|
||||||
const matches = await github.paginate(
|
});
|
||||||
github.rest.pulls.list,
|
prNumber = runDetails.data.pull_requests?.[0]?.number;
|
||||||
{
|
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
state: 'open',
|
|
||||||
head: `${headOwner}:${headBranch}`,
|
|
||||||
per_page: 100,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
prNumber = matches[0]?.number;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!prNumber) {
|
if (!prNumber) {
|
||||||
const headSha = run.head_sha;
|
core.info(`No PR number found on workflow run ${run.id}; skipping.`);
|
||||||
if (!headSha) {
|
return;
|
||||||
core.info('No PR number, no matching head branch, and no head_sha found; skipping.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const associated = await github.rest.repos.listPullRequestsAssociatedWithCommit({
|
|
||||||
owner,
|
|
||||||
repo,
|
|
||||||
commit_sha: headSha,
|
|
||||||
});
|
|
||||||
|
|
||||||
const open = (associated.data || []).find((p) => p.state === 'open');
|
|
||||||
prNumber = open?.number;
|
|
||||||
if (!prNumber) {
|
|
||||||
core.info(`No open PR found for ${run.head_repository?.owner?.login || 'unknown-owner'}:${run.head_branch || 'unknown-branch'} or commit ${headSha}; skipping.`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only comment when the PR build job actually ran (i.e. authorization passed).
|
// Only comment when the PR build job actually ran (i.e. authorization passed).
|
||||||
|
|||||||
Reference in New Issue
Block a user