Skip to content

Commit

Permalink
supporting build-triggered automerging (finally a fix for #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
monitorjbl committed Feb 28, 2016
1 parent bbd60ca commit 498857f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
15 changes: 5 additions & 10 deletions pom.xml
Expand Up @@ -44,16 +44,11 @@
<artifactId>sal-api</artifactId>
<scope>provided</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>com.atlassian.stash</groupId>-->
<!--<artifactId>stash-build-integration</artifactId>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.atlassian.stash</groupId>-->
<!--<artifactId>stash-page-objects</artifactId>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->
<dependency>
<groupId>com.atlassian.bitbucket.server</groupId>
<artifactId>bitbucket-build-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.templaterenderer</groupId>
<artifactId>atlassian-template-renderer-api</artifactId>
Expand Down
28 changes: 10 additions & 18 deletions src/main/java/com/monitorjbl/plugins/PullRequestListener.java
@@ -1,5 +1,6 @@
package com.monitorjbl.plugins;

import com.atlassian.bitbucket.build.BuildStatusSetEvent;
import com.atlassian.bitbucket.commit.Commit;
import com.atlassian.bitbucket.event.pull.PullRequestParticipantStatusUpdatedEvent;
import com.atlassian.bitbucket.permission.Permission;
Expand All @@ -10,27 +11,22 @@
import com.atlassian.bitbucket.pull.PullRequestState;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.user.SecurityService;
import com.atlassian.bitbucket.util.Operation;
import com.atlassian.bitbucket.util.Page;
import com.atlassian.bitbucket.util.PageRequestImpl;
import com.atlassian.event.api.EventListener;
import com.monitorjbl.plugins.config.Config;
import com.monitorjbl.plugins.config.ConfigDao;

//import com.atlassian.stash.build.BuildStatusSetEvent;

public class PullRequestListener {
public static final int MAX_COMMITS = 1048576;

private final ConfigDao configDao;
private final UserUtils utils;
private final PullRequestService prService;
private final SecurityService securityService;
private final RegexUtils regexUtils;

public PullRequestListener(ConfigDao configDao, UserUtils utils, PullRequestService prService, SecurityService securityService, RegexUtils regexUtils) {
public PullRequestListener(ConfigDao configDao, PullRequestService prService, SecurityService securityService, RegexUtils regexUtils) {
this.configDao = configDao;
this.utils = utils;
this.prService = prService;
this.securityService = securityService;
this.regexUtils = regexUtils;
Expand All @@ -41,13 +37,13 @@ public void prApprovalListener(PullRequestParticipantStatusUpdatedEvent event) {
automergePullRequest(event.getPullRequest());
}

// @EventListener
// public void buildStatusListener(BuildStatusSetEvent event) {
// PullRequest pr = findPRByCommitId(event.getCommitId());
// if (pr != null) {
// automergePullRequest(pr);
// }
// }
@EventListener
public void buildStatusListener(BuildStatusSetEvent event) {
PullRequest pr = findPRByCommitId(event.getCommitId());
if(pr != null) {
automergePullRequest(pr);
}
}

void automergePullRequest(final PullRequest pr) {
Repository repo = pr.getToRef().getRepository();
Expand All @@ -57,11 +53,7 @@ void automergePullRequest(final PullRequest pr) {

if((regexUtils.match(config.getAutomergePRs(), toBranch) || regexUtils.match(config.getAutomergePRsFrom(), fromBranch)) &&
!regexUtils.match(config.getBlockedPRs(), toBranch) && prService.canMerge(repo.getId(), pr.getId()).canMerge()) {
securityService.withPermission(Permission.ADMIN, "Automerging pull request").call(new Operation<Object, RuntimeException>() {
public Object perform() throws RuntimeException {
return prService.merge(new PullRequestMergeRequest.Builder(pr).build());
}
});
securityService.withPermission(Permission.ADMIN, "Automerging pull request").call(() -> prService.merge(new PullRequestMergeRequest.Builder(pr).build()));
}
}

Expand Down

0 comments on commit 498857f

Please sign in to comment.