Search code examples
gitgithubgithub-codereviews

GitHub Api check number of approvals on protected branch


I'm writing a server that should connect to GitHub and merge pull requests, but only if the required number of approvals is met.

For my purpose it's not good enough to automatically merge the branch, I need to be able to track and record the number of approvers before merging.

The github API for pull requests page provides a lot of detail, but doesnt have the number of approvers for a pull request on a protected branch. Is this number accessible somewhere? Is there another site out there that does have this kind of API hook?


Solution

  • doesnt have the number of approvers for a pull request on a protected branch.

    You can get the number by calling the list-reviews-on-a-pull-request endpoint and counting each review where state is APPROVED.

    This, however, doesn't include the ability to require more than one approved reviews. The current implementation of Reviews (as of Feburary 2017) will allow merging if there is one review that is status approved and none that request changes.

    Reviewing using Integrations

    The simplest solution is to use some 3rd party integration like ReviewNinja to provide Reviews. ReviewNinja has the ability to require multiple approvals but is no longer being actively developed, requires hosting the service yourself, and takes the developer outside of the pull request.

    You can always roll your own solution and control merging using the Status Checks.

    Controlling merges using branch restrictions

    Some workflows restrict who can merge code into protected branches using branch restrictions. If you know a group of users or team are required to merge, then you may be able to work around the limitation not being able to require multiple users. Rather, the user that merges the code implicitly approves the code.