6

I need to check via GitHub API if a pull request passed all required status checks. I use GitHub Enterprise 2.8 at this moment.

I know that I can get all status checks for last commit (following statuses_url in pull request). However, I don't know which status checks are set up to be required in a given repository. This is my main problem.

I also need to aggregate these status checks, group them by context and take latest in each context. It's ok, but seems to be reimplementation of logic, that GitHub performs internally when decides if a pull request can be merged.

For my case, it would be ideal to have something like can_be_merged in pull request fields, which meaning is mergeable && all required status checks passed && approved, but as far as I know there is no such field.

osowskit
  • 5,904
  • 2
  • 29
  • 38
algebraic
  • 306
  • 2
  • 10

2 Answers2

5

Finally solved this! You actually need to get the information off the protected branch, not off the check itself. Here are some API details: https://developer.github.com/v3/repos/branches/#list-required-status-checks-contexts-of-protected-branch.

So the flow to solve this is:

  1. Check if the base branch for the PR is protected, and if so;
  2. Use the above endpoint to determine which checks are required;
  3. Compare the checks on the latest PR commit to the required checks determined in step 2.
moustachio
  • 2,924
  • 3
  • 36
  • 68
  • I have a slight variation of this: I want to get the required checks _which are GitHub Actions_ and this seems to complicate things a lot: https://stackoverflow.com/questions/75063703/github-api-find-status-of-required-checks-on-a-pull-request-towards-protected-b – jakub.g Jan 09 '23 at 22:47
0

Based on @moustachio's answer, if you are using v4 graphql API, you may use

    pullRequest(...) {
      baseRef {
        refUpdateRule {
          requiredStatusCheckContexts
        }
      }
    }

to get the same info without additional API request

BlahGeek
  • 41
  • 1
  • 4