Skip to content

Instantly share code, notes, and snippets.

@kevinsawicki
Created August 19, 2011 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinsawicki/1158114 to your computer and use it in GitHub Desktop.
Save kevinsawicki/1158114 to your computer and use it in GitHub Desktop.
Merge Button using GitHub Java API
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import org.eclipse.egit.github.core.IRepositoryIdProvider;
import org.eclipse.egit.github.core.MergeStatus;
import org.eclipse.egit.github.core.PullRequest;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.PullRequestService;
/**
* Merge button class
*/
public class MergeButton {
private final GitHubClient client;
/**
* Create merge button with given client
*
* @param client
*/
public MergeButton(GitHubClient client) {
this.client = client;
}
/**
* Merge all mergeable open pull requests in the given repository
*
* @param repository
* @throws IOException
*/
public void mergeAll(IRepositoryIdProvider repository) throws IOException {
PullRequestService service = new PullRequestService(client);
List<PullRequest> pullRequests = service.getPullRequests(repository,
"open");
// Merge oldest first
Collections.reverse(pullRequests);
for (PullRequest request : pullRequests)
merge(repository, request.getNumber());
}
/**
* Merge pull request with given number if mergeable
*
* @param repository
* @param number
* @return status of merge or null if pull request is not mergeable
* @throws IOException
*/
public MergeStatus merge(IRepositoryIdProvider repository, int number)
throws IOException {
PullRequestService service = new PullRequestService(client);
if (service.getPullRequest(repository, number).isMergeable())
return service.merge(repository, number, "");
else
return null;
}
/**
* Merge pull requests that can be merged
*
* @param args
* @throws IOException
*/
public static void main(String... args) throws IOException {
GitHubClient client = new GitHubClient();
// Update with actual user/password or OAuth2 access token
client.setCredentials("user", "password");
// Update with repository owner and name such as 'rails/rails'
RepositoryId repository = new RepositoryId("user", "project");
// Merge all open pull requests taht are mergeable
new MergeButton(client).mergeAll(repository);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment