简体   繁体   中英

Use Github API to disable the merge button on a pull request and reenable it using REST

I have developed my own web server that does automation on my android app. If there are issues when the automation runs, I want to programmatically disable the "merge" button on a github pull request using a cURL REST command. I cannot find out the proper way to do this but it seems that many people would benefit from this functionality.

The api for github pull requests can be found here: https://developer.github.com/v3/pulls/

I know that this is possible because if you have merge conflicts on your branch, the button gets grayed out and you cannot click/merge it. That is the exact functionality I am looking for. Any help would be much appreciated.

This is possible. There are couple of steps you should take to enable this feature.

  1. Configure some of your branches (typically it's master or/and develop , depends on your workflow) as protected .
  2. Using Statuses API you can send Pending , Success , Error and Failure statuses. Pending , Error and Failed statuses will block Merge button.

Once it's done you can POST statuses based on your business rules.

POST /repos/:owner/:repo/statuses/:sha

:sha is the hash of the latest commit in the Pull Request

With payload like this one:

{
  "state": "success",
  "target_url": "https://link.to/some/repotring/page",
  "description": "Automation tests passed!",
  "context": "continuous-integration/automation-tests"
}

One thing is worth mentioning. When you have posted at least one status, the value from context filed will be shown on the protected branch settings page. Don't forget to mark this status as required:

合并前需要状态检查才能通过

Protected branches and required status checks

Protecting the branch and then blocking merging if the status checks doesn't pass or reviews are not done works. This is a very straight solution and it works perfectly. But if you are more curious then try doing it without protecting the branch. I could not find a way to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM