Search code examples
githubgithub-api

How to get the status of required status checks from a GitHub pull request including checks that were not yet reported by third parties (e.g. Jenkins)


How do I get the status of required status checks from a GitHub pull request including checks that were not yet reported by third parties (e.g. Jenkins)?

I tried to use this API to get the list of checks, but it will only show those from GitHub Actions:

gh api \
  -H "Accept: application/vnd.github+json" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  /repos/OWNER/REPO/commits/REF/check-runs

So I tried using this API from gh cli:

gh pr checks "$pr_number" --repo="$OWNER/$repo" --required

This is better because it shows all the status checks, including those reported by external tools (e.g. Jenkins), but it doesn't show those that were not reported yet (even if they are required).

Any other ideas as to what to do?


Solution

  • My solution for now (although not ideal) is the following:

    Get the list of required status checks from the branch protection rules of the branch to which you want to make the merge on GitHub using this command:

    gh api repos/$OWNER/$REPO/branches/$BRANCH_NAME/protection/required_status_checks/contexts
    

    Then cross-reference the list of checks obtained to the list of checks obtained from the gh cli command:

    gh pr checks "$pr_number" --repo="$OWNER/$REPO" --required
    

    If there are more checks in the list obtained from the branch protection rules, you can know that there are still more pending status checks that were not reported yet.

    This is not ideal because in order to use the gh api command I provided above, you must have admin credentials, which isn't always what you want.