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 06ba615 commit b509285
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 106 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 Stash.

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

## 1.29
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/se/bjurr/prnfs/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 @@ -197,6 +197,10 @@ public boolean notificationTriggeredByAction(PrnfsNotification 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
60 changes: 32 additions & 28 deletions src/main/java/se/bjurr/prnfs/settings/PrnfsNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import se.bjurr.prnfs.listener.PrnfsPullRequestAction;
import se.bjurr.prnfs.listener.UrlInvoker.HTTP_METHOD;

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

public class PrnfsNotification {
Expand All @@ -47,48 +48,51 @@ public class PrnfsNotification {
private final String injectionUrl;
private final String injectionUrlRegexp;
private final TRIGGER_IF_MERGE triggerIfCanMerge;

public PrnfsNotification(List<PrnfsPullRequestAction> triggers, String url, String user, String password,
String filterString, String filterRegexp, String method, String postContent, List<Header> headers, String proxyUser,
String proxyPassword, String proxyServer, String proxyPort, String name, String injectionUrl,
String injectionUrlRegexp, String triggerIfCanMerge) throws ValidationException {
this.proxyUser = emptyToNull(nullToEmpty(proxyUser).trim());
this.proxyPassword = emptyToNull(nullToEmpty(proxyPassword).trim());
this.proxyServer = emptyToNull(nullToEmpty(proxyServer).trim());
this.proxyPort = Integer.valueOf(firstNonNull(emptyToNull(nullToEmpty(proxyPort).trim()), "-1"));
this.headers = checkNotNull(headers);
this.postContent = emptyToNull(nullToEmpty(postContent).trim());
this.method = HTTP_METHOD.valueOf(firstNonNull(emptyToNull(nullToEmpty(method).trim()), GET.name()));
this.triggerIfCanMerge = TRIGGER_IF_MERGE.valueOf(firstNonNull(emptyToNull(nullToEmpty(triggerIfCanMerge).trim()),
ALWAYS.name()));
if (nullToEmpty(url).trim().isEmpty()) {
private final List<PullRequestState> triggerIgnoreStateList;

public PrnfsNotification(PrnfsNotificationBuilder builder) throws ValidationException {
this.proxyUser = emptyToNull(nullToEmpty(builder.getProxyUser()).trim());
this.proxyPassword = emptyToNull(nullToEmpty(builder.getProxyPassword()).trim());
this.proxyServer = emptyToNull(nullToEmpty(builder.getProxyServer()).trim());
this.proxyPort = Integer.valueOf(firstNonNull(emptyToNull(nullToEmpty(builder.getProxyPort()).trim()), "-1"));
this.headers = checkNotNull(builder.getHeaders());
this.postContent = emptyToNull(nullToEmpty(builder.getPostContent()).trim());
this.method = HTTP_METHOD.valueOf(firstNonNull(emptyToNull(nullToEmpty(builder.getMethod()).trim()), GET.name()));
this.triggerIfCanMerge = TRIGGER_IF_MERGE.valueOf(firstNonNull(
emptyToNull(nullToEmpty(builder.getTriggerIfCanMerge()).trim()), ALWAYS.name()));
if (nullToEmpty(builder.getUrl()).trim().isEmpty()) {
throw new ValidationException(FIELDS.url.name(), "URL not set!");
}
try {
new URL(url);
new URL(builder.getUrl());
} catch (final Exception e) {
throw new ValidationException(FIELDS.url.name(), "URL not valid!");
}
if (!nullToEmpty(filterRegexp).trim().isEmpty()) {
if (!nullToEmpty(builder.getFilterRegexp()).trim().isEmpty()) {
try {
compile(filterRegexp);
compile(builder.getFilterRegexp());
} catch (final Exception e) {
throw new ValidationException(filter_regexp.name(), "Filter regexp not valid! "
+ e.getMessage().replaceAll("\n", " "));
}
if (nullToEmpty(filterString).trim().isEmpty()) {
if (nullToEmpty(builder.getFilterString()).trim().isEmpty()) {
throw new ValidationException(filter_string.name(), "Filter string not set, nothing to match regexp against!");
}
}
this.url = url;
this.user = emptyToNull(nullToEmpty(user).trim());
this.password = emptyToNull(nullToEmpty(password).trim());
this.triggers = checkNotNull(triggers);
this.filterString = filterString;
this.filterRegexp = filterRegexp;
this.name = firstNonNull(emptyToNull(nullToEmpty(name).trim()), DEFAULT_NAME);
this.injectionUrl = emptyToNull(nullToEmpty(injectionUrl).trim());
this.injectionUrlRegexp = emptyToNull(nullToEmpty(injectionUrlRegexp).trim());
this.url = builder.getUrl();
this.user = emptyToNull(nullToEmpty(builder.getUser()).trim());
this.password = emptyToNull(nullToEmpty(builder.getPassword()).trim());
this.triggers = checkNotNull(builder.getTriggers());
this.filterString = builder.getFilterString();
this.filterRegexp = builder.getFilterRegexp();
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.prnfs.listener.PrnfsPullRequestAction;

import com.atlassian.stash.pull.PullRequestState;

public class PrnfsNotificationBuilder {
public static final String YES = "YES";
public static final String NO = "NO";
Expand All @@ -32,13 +34,89 @@ public static PrnfsNotificationBuilder prnfsNotificationBuilder() {
private String injectionUrl;
private String injectionUrlRegexp;
private String triggerIfCanMerge;
private final List<PullRequestState> triggerIgnoreStateList = newArrayList();

private PrnfsNotificationBuilder() {
}

public PrnfsNotification build() throws ValidationException {
return new PrnfsNotification(triggers, url, user, password, filterString, filterRegexp, method, postContent, headers,
proxyUser, proxyPassword, proxyServer, proxyPort, name, injectionUrl, injectionUrlRegexp, triggerIfCanMerge);
return new PrnfsNotification(this);
}

public String getFilterRegexp() {
return filterRegexp;
}

public String getFilterString() {
return filterString;
}

public List<Header> getHeaders() {
return headers;
}

public String getInjectionUrl() {
return injectionUrl;
}

public String getInjectionUrlRegexp() {
return injectionUrlRegexp;
}

public String getMethod() {
return method;
}

public String getName() {
return name;
}

public String getPassword() {
return password;
}

public String getPostContent() {
return postContent;
}

public String getProxyPassword() {
return proxyPassword;
}

public String getProxyPort() {
return proxyPort;
}

public String getProxyServer() {
return proxyServer;
}

public String getProxyUser() {
return proxyUser;
}

public String getTriggerIfCanMerge() {
return triggerIfCanMerge;
}

public List<PrnfsPullRequestAction> getTriggers() {
return triggers;
}

public String getUrl() {
return url;
}

public String getUser() {
return user;
}

public static String getNo() {
return NO;
}

public static String getYes() {
return YES;
}

public PrnfsNotificationBuilder withInjectionUrl(String injectionUrl) {
Expand Down Expand Up @@ -125,4 +203,13 @@ public PrnfsNotificationBuilder withTriggerIfCanMerge(String triggerIfCanMerge)
this.triggerIfCanMerge = triggerIfCanMerge;
return this;
}

public PrnfsNotificationBuilder withTriggerIgnoreState(PullRequestState triggerIgnoreState) {
this.triggerIgnoreStateList.add(triggerIgnoreState);
return this;
}

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

0 comments on commit b509285

Please sign in to comment.