Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
Adding 'Triggers do not apply to' option with values DECLINED and MER…
Browse files Browse the repository at this point in the history
…GED #71
  • Loading branch information
tomasbjerre committed Sep 26, 2015
1 parent 737d040 commit 7e95606
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 76 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Changelog of Pull Request Notifier for Bitbucket.

## 2.4
* Adding "Triggers do not apply to" option with values DECLINED and MERGED.
* Changing wording of trigger conditions in admin GUI.

## 2.3
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/se/bjurr/prnfb/admin/AdminFormValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public enum FIELDS {
user_allowed, //
injection_url, //
injection_url_regexp, //
trigger_if_isconflicting
trigger_if_isconflicting, //
trigger_ignore_state//
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ public boolean notificationTriggeredByAction(PrnfbNotification notification, Prn
return FALSE;
}

if (notification.getTriggerIgnoreStateList().contains(pullRequest.getState())) {
return FALSE;
}

if (notification.getTriggerIfCanMerge() != ALWAYS && pullRequest.isOpen()) {
// Cannot perform canMerge unless PR is open
boolean isConflicted = pullRequestService.canMerge(pullRequest.getToRef().getRepository().getId(),
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import se.bjurr.prnfb.listener.PrnfbPullRequestAction;
import se.bjurr.prnfb.listener.UrlInvoker.HTTP_METHOD;

import com.atlassian.bitbucket.pull.PullRequestState;
import com.google.common.base.Optional;

public class PrnfbNotification {
Expand All @@ -47,6 +48,7 @@ public class PrnfbNotification {
private final String injectionUrl;
private final String injectionUrlRegexp;
private final TRIGGER_IF_MERGE triggerIfCanMerge;
private final List<PullRequestState> triggerIgnoreStateList;

public PrnfbNotification(PrnfbNotificationBuilder builder) throws ValidationException {
this.proxyUser = emptyToNull(nullToEmpty(builder.getProxyUser()).trim());
Expand Down Expand Up @@ -86,6 +88,11 @@ public PrnfbNotification(PrnfbNotificationBuilder builder) throws ValidationExce
this.name = firstNonNull(emptyToNull(nullToEmpty(builder.getName()).trim()), DEFAULT_NAME);
this.injectionUrl = emptyToNull(nullToEmpty(builder.getInjectionUrl()).trim());
this.injectionUrlRegexp = emptyToNull(nullToEmpty(builder.getInjectionUrlRegexp()).trim());
this.triggerIgnoreStateList = builder.getTriggerIgnoreStateList();
}

public List<PullRequestState> getTriggerIgnoreStateList() {
return triggerIgnoreStateList;
}

public TRIGGER_IF_MERGE getTriggerIfCanMerge() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import se.bjurr.prnfb.listener.PrnfbPullRequestAction;

import com.atlassian.bitbucket.pull.PullRequestState;

public class PrnfbNotificationBuilder {
public static PrnfbNotificationBuilder prnfbNotificationBuilder() {
return new PrnfbNotificationBuilder();
Expand All @@ -21,6 +23,7 @@ public static PrnfbNotificationBuilder prnfbNotificationBuilder() {
private String method;
private String postContent;
private final List<Header> headers = newArrayList();
private final List<PullRequestState> triggerIgnoreStateList = newArrayList();
private String proxyUser;
private String proxyPassword;
private String proxyServer;
Expand Down Expand Up @@ -102,6 +105,10 @@ public PrnfbNotificationBuilder withProxyPort(String s) {
return this;
}

public List<PullRequestState> getTriggerIgnoreStateList() {
return triggerIgnoreStateList;
}

public PrnfbNotificationBuilder withProxyUser(String s) {
this.proxyUser = checkNotNull(s);
return this;
Expand Down Expand Up @@ -189,4 +196,9 @@ public String getUrl() {
public String getUser() {
return user;
}

public PrnfbNotificationBuilder withTriggerIgnoreState(PullRequestState triggerIgnoreState) {
this.triggerIgnoreStateList.add(checkNotNull(triggerIgnoreState));
return this;
}
}
160 changes: 85 additions & 75 deletions src/main/java/se/bjurr/prnfb/settings/SettingsStorage.java

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/main/resources/admin.vm
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@
<label><input type="radio" name="trigger_if_isconflicting" value="CONFLICTING">To PRs with merge conflicts</label><br>
</fieldset>
</div>
<div>
<fieldset>
<legend>Triggers do not apply to</legend>
<label><input type="checkbox" name="trigger_ignore_state" value="MERGED">Merged PRs</label><br>
<label><input type="checkbox" name="trigger_ignore_state" value="DECLINED">Declined PRs</label><br>
</fieldset>
</div>
<div>
<fieldset>
<legend>Triggers</legend>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.bitbucket.pull.PullRequestAction;
import com.atlassian.bitbucket.pull.PullRequestParticipant;
import com.atlassian.bitbucket.pull.PullRequestState;

public class PullRequestEventBuilder {
public static final String PREVIOUS_TO_HASH = "previousToHash";
Expand All @@ -27,6 +28,7 @@ public class PullRequestEventBuilder {
private boolean beingClosed;
private final boolean beingOpen = TRUE;
private Long pullRequestId = 0L;
private PullRequestState pullRequestState;

private PullRequestEventBuilder(PrnfbTestBuilder prnfbTestBuilder) {
this.prnfbTestBuilder = prnfbTestBuilder;
Expand Down Expand Up @@ -99,6 +101,7 @@ public PullRequestEvent build() {
when(pullRequest.isClosed()).thenReturn(beingClosed);
when(pullRequest.isOpen()).thenReturn(beingOpen);
when(pullRequest.getId()).thenReturn(pullRequestId);
when(pullRequest.getState()).thenReturn(pullRequestState);
when(pullRequestEvent.getAction()).thenReturn(pullRequestAction);
when(pullRequestEvent.getPullRequest()).thenReturn(pullRequest);
when(pullRequestEvent.getPullRequest().getAuthor()).thenReturn(author);
Expand All @@ -120,4 +123,9 @@ public PullRequestEventBuilder withPullRequestId(Long id) {
this.pullRequestId = id;
return this;
}

public PullRequestEventBuilder withPullRequestInState(PullRequestState pullRequestState) {
this.pullRequestState = pullRequestState;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.atlassian.bitbucket.pull.PullRequestAction.MERGED;
import static com.atlassian.bitbucket.pull.PullRequestAction.OPENED;
import static com.atlassian.bitbucket.pull.PullRequestAction.RESCOPED;
import static com.atlassian.bitbucket.pull.PullRequestState.DECLINED;
import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Joiner.on;
import static com.google.common.collect.Lists.newArrayList;
Expand Down Expand Up @@ -39,6 +40,7 @@
import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.proxy_server;
import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.proxy_user;
import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.trigger_if_isconflicting;
import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.trigger_ignore_state;
import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.url;
import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.user;
import static se.bjurr.prnfb.admin.AdminFormValues.FORM_TYPE.BUTTON_CONFIG_FORM;
Expand Down Expand Up @@ -83,6 +85,7 @@
import se.bjurr.prnfb.listener.PrnfbPullRequestAction;
import se.bjurr.prnfb.listener.PrnfbRenderer.PrnfbVariable;

import com.atlassian.bitbucket.pull.PullRequestState;
import com.google.common.io.Resources;

public class PrnfbPullRequestEventListenerTest {
Expand Down Expand Up @@ -1779,6 +1782,69 @@ public void testThatUrlMayBeInvokedWhenIsOrIsNotConflicting() throws Exception {
.invokedUrl(0, "http://bjurr.se/");
}

@Test
public void testThatUrlMayNotBeInvokedWhenIsDeclined() throws Exception {
prnfbTestBuilder()//
.isLoggedInAsAdmin()//
.withNotification(//
notificationBuilder()//
.withFieldValue(url, "http://bjurr.se/")//
.withFieldValue(events, COMMENTED.name())//
.withFieldValue(trigger_ignore_state, DECLINED.name())//
.build()//
)//
.store()//
.trigger(//
pullRequestEventBuilder()//
.withPullRequestAction(COMMENTED)//
.withPullRequestInState(DECLINED)//
.build()//
)//
.invokedNoUrl();
}

@Test
public void testThatUrlMayNotBeInvokedWhenIsMerged() throws Exception {
prnfbTestBuilder()//
.isLoggedInAsAdmin()//
.withNotification(//
notificationBuilder()//
.withFieldValue(url, "http://bjurr.se/")//
.withFieldValue(events, COMMENTED.name())//
.withFieldValue(trigger_ignore_state, PullRequestState.MERGED.name())//
.build()//
)//
.store()//
.trigger(//
pullRequestEventBuilder()//
.withPullRequestAction(COMMENTED)//
.withPullRequestInState(PullRequestState.MERGED)//
.build()//
)//
.invokedNoUrl();
}

@Test
public void testThatUrlMayBeInvokedWhenIsMergedAndDeclinedIsIgnored() throws Exception {
prnfbTestBuilder()//
.isLoggedInAsAdmin()//
.withNotification(//
notificationBuilder()//
.withFieldValue(url, "http://bjurr.se/")//
.withFieldValue(events, COMMENTED.name())//
.withFieldValue(trigger_ignore_state, DECLINED.name())//
.build()//
)//
.store()//
.trigger(//
pullRequestEventBuilder()//
.withPullRequestAction(COMMENTED)//
.withPullRequestInState(PullRequestState.MERGED)//
.build()//
)//
.invokedUrl(0, "http://bjurr.se/");
}

@Test
public void testThatVariablesAreImplementedForBothFromAndTo() throws Exception {
final List<String> from = newArrayList();
Expand Down

0 comments on commit 7e95606

Please sign in to comment.