Reputation: 311
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.
Upvotes: 8
Views: 3165
Reputation: 46
You can achieve this with the github CLI. The following snippet will check if all non-workflow checks have succeeded. No need for graphql or api:
- name: check the checks
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# check if all non-workflow checks are successful
gh pr checks ${{ github.event.pull_request.number }} --json workflow,state | jq '.[] | select(.workflow == "") | .state' | grep -q -v SUCCESS && check=false || check=true
if [[ $check == 'false' ]]; then
echo "Not all checks were successful!"
exit 1
else
echo "All PR checks passed!"
fi
Upvotes: 0
Reputation: 41
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
Upvotes: 0
Reputation: 3004
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:
Upvotes: 6