Skip to content

[.github/context.js] Fails if commits/pulls API returns PRs from other repos #33654

@mikeharder

Description

@mikeharder

Code

const pullRequests = await github.paginate(
github.rest.repos.listPullRequestsAssociatedWithCommit,
{
owner: head_owner,
repo: head_repo,
commit_sha: head_sha,
per_page: PER_PAGE_MAX,
},
);
if (pullRequests.length === 0) {
// There are three cases where the "commits" REST API called above can return
// empty, even if there is an open PR from the commit:
//
// 1. If the head branch of a fork PR is the default branch of the fork repo, the
// API always returns empty. (#33315)
// 2. If a PR was just merged, the API may return empty for a brief window (#33416).
// 3. The API may fail occasionally for no known reason (#33417).
//
// In any case, the solution is to fall back to the (lower-rate-limit) search API.
// The search API is confirmed to work in case #1, but has not been tested in #2 or #3.
issue_number = (await getIssueNumber({ head_sha, github, core }))
.issueNumber;
} else if (pullRequests.length === 1) {
issue_number = pullRequests[0].number;
} else {
throw new Error(
`Unexpected number of pull requests associated with commit '${head_sha}'. Expected: '1'. Actual: '${pullRequests.length}'.`,
);
}

Fix

  • Filter pull requests where base.repo.id matches
  • Don't filter on anything else (like open vs closed), until we have live examples of needing it. We may also need to handle multiple open PRs (rare but possible).
        const pullRequests = (
          await github.paginate(
            github.rest.repos.listPullRequestsAssociatedWithCommit,
            {
              owner: head_owner,
              repo: head_repo,
              commit_sha: head_sha,
              per_page: PER_PAGE_MAX,
            },
          )
        ).filter(
          // A commit can be associated with PRs to multiple repos
          // We are only interested in PRs matching the repo of the triggering workflow
          (pr) =>
            pr.base.repo.id ===
            payload.workflow_run.repository.id,
        );

Example

Check Failure

https://github.com/Azure/azure-rest-api-specs/actions/runs/14203528276/job/39826045659#step:3:153

Error: Unexpected number of pull requests associated with commit '70d22984c63f6df5e91cc05cd77786bb5d16f355'. Expected: '1'. Actual: '2'.
    at extractInputs (file:///home/runner/work/azure-rest-api-specs/azure-rest-api-specs/.github/workflows/src/context.js:159:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getLabelAction (file:///home/runner/work/azure-rest-api-specs/azure-rest-api-specs/.github/workflows/src/arm-auto-signoff.js:21:18)
    at async eval (eval at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v7/dist/index.js:35424:16), <anonymous>:5:8)
    at async main (/home/runner/work/_actions/actions/github-script/v7/dist/index.js:35522:20)

PRs for commit

https://api.github.com/repos/koderjoker/azure-rest-api-specs/commits/70d22984c63f6df5e91cc05cd77786bb5d16f355/pulls?per_page=100

PR 1

PR 2

Matching Failures

Image

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions