axel22
axel22

Reputation: 32335

Automatically check that pull request has a label before merging

We're using Github, and we have the following process for pull request submissions:

  1. We want a pull request reviewer to put a special LGTM label on a pull request, letting the pull request author know that he can merge the PR himself.
  2. The author is not allowed to press "Merge pull request" button before the LGTM label exists on the pull request.

Currently, we have no way to automatically enforce the second requirement.

Is there a hook that runs after the "Merge pull request" button is pressed, but before the pull request is really merged, which can be used to invalidate the PR? If not, is there some other way to ensure the second requirement above?

Upvotes: 3

Views: 4390

Answers (1)

Anton Sizikov
Anton Sizikov

Reputation: 9230

Yes, It's possible. In general there are at least two ways of doing that. First one I don't like, but it might be an easy way if you have no control over repository.

  1. Build something like Do Not Merge WIP for GitHub Chrome Extension.

  2. Use statuses API and protected branches

Since this question has github-api tag I'm going to talk about the second option.

First you need to configure some branches as protected. Usually they are develop and master. Depends on your branching model.

Than you need some simple application to post statuses via GitHub Statuses API. Using Statuses API you can send Pending, Success, Error and Failure statuses. Pending,Error and Failed statuses will block Merge button.

As a reference you can use my demo app which counts :shipit: comments and marks PR as Successful. That enables 'Merge' button.

This app is written in C#, but I hope it's simple enough to understand even if you have no C# knowledge.

You need to register a new WebHook. So your script/app is notified. Think about events you are interested in. I think this set of events will work for you:

enter image description here

From the notification event you can extract Pull Request Id and perform different checks.

In your case you need to use List labels on an issue

GET /repos/:owner/:repo/issues/:number/labels

It's a bit weird to use Issues as a recourse, but that's how it works. Issues and Pull Requests used to be the same thing back in time.

After that make sure you have all the labels in place and send the notification to the repo. I'm sending "Success" status to the last commit

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

{
  "state": "success",
  "target_url": "https://example.com/build/status",
  "description": "Merge label found",
  "context": "review/labels"
}

Pay attention on the "review/labels" line. This is an important part.

Once you sent it first time, you should go to Repository -> Settings-> Branches->YourBranchName->Edit and click the checkbox with your context name. So that this status becomes mandatory to perform "merge action".

Should look something like that: enter image description here

Other links to read

Upvotes: 6

Related Questions