From 7e956068fee19bec586c5401872d79c0ecf27402 Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Sat, 26 Sep 2015 11:35:01 +0200 Subject: [PATCH] Adding 'Triggers do not apply to' option with values DECLINED and MERGED #71 --- CHANGELOG.md | 1 + .../se/bjurr/prnfb/admin/AdminFormValues.java | 3 +- .../PrnfbPullRequestEventListener.java | 4 + .../prnfb/settings/PrnfbNotification.java | 7 + .../settings/PrnfbNotificationBuilder.java | 12 ++ .../bjurr/prnfb/settings/SettingsStorage.java | 160 ++++++++++-------- src/main/resources/admin.vm | 7 + .../admin/utils/PullRequestEventBuilder.java | 8 + .../PrnfbPullRequestEventListenerTest.java | 66 ++++++++ 9 files changed, 192 insertions(+), 76 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19466e5..b0c1218 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/se/bjurr/prnfb/admin/AdminFormValues.java b/src/main/java/se/bjurr/prnfb/admin/AdminFormValues.java index 1e2b02f..62ea654 100644 --- a/src/main/java/se/bjurr/prnfb/admin/AdminFormValues.java +++ b/src/main/java/se/bjurr/prnfb/admin/AdminFormValues.java @@ -52,6 +52,7 @@ public enum FIELDS { user_allowed, // injection_url, // injection_url_regexp, // - trigger_if_isconflicting + trigger_if_isconflicting, // + trigger_ignore_state// } } diff --git a/src/main/java/se/bjurr/prnfb/listener/PrnfbPullRequestEventListener.java b/src/main/java/se/bjurr/prnfb/listener/PrnfbPullRequestEventListener.java index 605016d..4a82db0 100644 --- a/src/main/java/se/bjurr/prnfb/listener/PrnfbPullRequestEventListener.java +++ b/src/main/java/se/bjurr/prnfb/listener/PrnfbPullRequestEventListener.java @@ -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(), diff --git a/src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java b/src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java index cff0651..54e79f9 100644 --- a/src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java +++ b/src/main/java/se/bjurr/prnfb/settings/PrnfbNotification.java @@ -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 { @@ -47,6 +48,7 @@ public class PrnfbNotification { private final String injectionUrl; private final String injectionUrlRegexp; private final TRIGGER_IF_MERGE triggerIfCanMerge; + private final List triggerIgnoreStateList; public PrnfbNotification(PrnfbNotificationBuilder builder) throws ValidationException { this.proxyUser = emptyToNull(nullToEmpty(builder.getProxyUser()).trim()); @@ -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 getTriggerIgnoreStateList() { + return triggerIgnoreStateList; } public TRIGGER_IF_MERGE getTriggerIfCanMerge() { diff --git a/src/main/java/se/bjurr/prnfb/settings/PrnfbNotificationBuilder.java b/src/main/java/se/bjurr/prnfb/settings/PrnfbNotificationBuilder.java index b463e28..e404cc7 100644 --- a/src/main/java/se/bjurr/prnfb/settings/PrnfbNotificationBuilder.java +++ b/src/main/java/se/bjurr/prnfb/settings/PrnfbNotificationBuilder.java @@ -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(); @@ -21,6 +23,7 @@ public static PrnfbNotificationBuilder prnfbNotificationBuilder() { private String method; private String postContent; private final List
headers = newArrayList(); + private final List triggerIgnoreStateList = newArrayList(); private String proxyUser; private String proxyPassword; private String proxyServer; @@ -102,6 +105,10 @@ public PrnfbNotificationBuilder withProxyPort(String s) { return this; } + public List getTriggerIgnoreStateList() { + return triggerIgnoreStateList; + } + public PrnfbNotificationBuilder withProxyUser(String s) { this.proxyUser = checkNotNull(s); return this; @@ -189,4 +196,9 @@ public String getUrl() { public String getUser() { return user; } + + public PrnfbNotificationBuilder withTriggerIgnoreState(PullRequestState triggerIgnoreState) { + this.triggerIgnoreStateList.add(checkNotNull(triggerIgnoreState)); + return this; + } } diff --git a/src/main/java/se/bjurr/prnfb/settings/SettingsStorage.java b/src/main/java/se/bjurr/prnfb/settings/SettingsStorage.java index 45d349b..c052018 100644 --- a/src/main/java/se/bjurr/prnfb/settings/SettingsStorage.java +++ b/src/main/java/se/bjurr/prnfb/settings/SettingsStorage.java @@ -13,6 +13,33 @@ import static se.bjurr.prnfb.admin.AdminFormValues.DEFAULT_NAME; import static se.bjurr.prnfb.admin.AdminFormValues.NAME; import static se.bjurr.prnfb.admin.AdminFormValues.VALUE; +import static se.bjurr.prnfb.admin.AdminFormValues.BUTTON_VISIBILITY.NONE; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.FORM_IDENTIFIER; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.admin_allowed; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.button_title; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.button_visibility; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.events; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.filter_regexp; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.filter_string; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.header_name; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.header_value; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.injection_url; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.injection_url_regexp; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.method; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.name; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.password; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.post_content; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.proxy_password; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.proxy_port; +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.FIELDS.user_allowed; +import static se.bjurr.prnfb.admin.AdminFormValues.FIELDS.valueOf; +import static se.bjurr.prnfb.admin.AdminFormValues.FORM_TYPE.TRIGGER_CONFIG_FORM; import static se.bjurr.prnfb.settings.PrnfbNotification.isOfType; import static se.bjurr.prnfb.settings.PrnfbNotificationBuilder.prnfbNotificationBuilder; import static se.bjurr.prnfb.settings.PrnfbPredicates.predicate; @@ -29,6 +56,7 @@ import se.bjurr.prnfb.admin.AdminFormValues.BUTTON_VISIBILITY; import se.bjurr.prnfb.listener.PrnfbPullRequestAction; +import com.atlassian.bitbucket.pull.PullRequestState; import com.atlassian.sal.api.pluginsettings.PluginSettings; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; @@ -71,106 +99,94 @@ public static Logger getLogger() { private static Map getNotificationsMap(PluginSettings pluginSettings) { final Map allNotificationsMap = newTreeMap(); for (final AdminFormValues a : getSettingsAsFormValues(pluginSettings)) { - if (tryFind(a, predicate(AdminFormValues.FIELDS.FORM_IDENTIFIER.name())).isPresent()) { - allNotificationsMap.put(find(a, predicate(AdminFormValues.FIELDS.FORM_IDENTIFIER.name())).get(VALUE), a); + if (tryFind(a, predicate(FORM_IDENTIFIER.name())).isPresent()) { + allNotificationsMap.put(find(a, predicate(FORM_IDENTIFIER.name())).get(VALUE), a); } } return allNotificationsMap; } public static PrnfbButton getPrnfbButton(AdminFormValues adminFormValues) throws ValidationException { - final Optional> titleOpt = tryFind(adminFormValues, - predicate(AdminFormValues.FIELDS.button_title.name())); + final Optional> titleOpt = tryFind(adminFormValues, predicate(button_title.name())); String title = "Trigger Notification"; if (titleOpt.isPresent()) { title = titleOpt.get().get(VALUE); } - final Optional> visibilityOpt = tryFind(adminFormValues, - predicate(AdminFormValues.FIELDS.button_visibility.name())); - BUTTON_VISIBILITY visibility = AdminFormValues.BUTTON_VISIBILITY.NONE; + final Optional> visibilityOpt = tryFind(adminFormValues, predicate(button_visibility.name())); + BUTTON_VISIBILITY visibility = NONE; if (visibilityOpt.isPresent()) { - visibility = AdminFormValues.BUTTON_VISIBILITY.valueOf(visibilityOpt.get().get(VALUE)); + visibility = BUTTON_VISIBILITY.valueOf(visibilityOpt.get().get(VALUE)); } - return new PrnfbButton(title, visibility, find(adminFormValues, - predicate(AdminFormValues.FIELDS.FORM_IDENTIFIER.name())).get(VALUE)); + return new PrnfbButton(title, visibility, find(adminFormValues, predicate(FORM_IDENTIFIER.name())).get(VALUE)); } public static PrnfbNotification getPrnfbNotification(AdminFormValues adminFormValues) throws ValidationException { - final Optional> urlOpt = tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.url.name())); + final Optional> urlOpt = tryFind(adminFormValues, predicate(url.name())); if (!urlOpt.isPresent()) { throw new ValidationException("url", "URL not set"); } final PrnfbNotificationBuilder prnfbNotificationBuilder = prnfbNotificationBuilder().withUrl(urlOpt.get().get(VALUE)); - for (final Map event : filter(adminFormValues, predicate(AdminFormValues.FIELDS.events.name()))) { + for (final Map event : filter(adminFormValues, predicate(events.name()))) { prnfbNotificationBuilder.withTrigger(PrnfbPullRequestAction.valueOf(event.get(VALUE))); } - Iterator> headerValues = filter(adminFormValues, - predicate(AdminFormValues.FIELDS.header_value.name())).iterator(); - for (final Map headerName : filter(adminFormValues, - predicate(AdminFormValues.FIELDS.header_name.name()))) { + Iterator> headerValues = filter(adminFormValues, predicate(header_value.name())).iterator(); + for (final Map headerName : filter(adminFormValues, predicate(header_name.name()))) { if (headerName.get(VALUE).trim().isEmpty()) { continue; } String headerValue = headerValues.next().get(VALUE); if (isNullOrEmpty(headerValue)) { - throw new ValidationException(AdminFormValues.FIELDS.header_value.name(), "Value cannot be null"); + throw new ValidationException(header_value.name(), "Value cannot be null"); } prnfbNotificationBuilder.withHeader(headerName.get(VALUE), headerValue); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.proxy_server.name())).isPresent()) { - prnfbNotificationBuilder - .withProxyServer(find(adminFormValues, predicate(AdminFormValues.FIELDS.proxy_server.name())).get(VALUE)); + if (tryFind(adminFormValues, predicate(proxy_server.name())).isPresent()) { + prnfbNotificationBuilder.withProxyServer(find(adminFormValues, predicate(proxy_server.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.proxy_port.name())).isPresent()) { - prnfbNotificationBuilder.withProxyPort(find(adminFormValues, predicate(AdminFormValues.FIELDS.proxy_port.name())) - .get(VALUE)); + if (tryFind(adminFormValues, predicate(proxy_port.name())).isPresent()) { + prnfbNotificationBuilder.withProxyPort(find(adminFormValues, predicate(proxy_port.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.proxy_user.name())).isPresent()) { - prnfbNotificationBuilder.withProxyUser(find(adminFormValues, predicate(AdminFormValues.FIELDS.proxy_user.name())) - .get(VALUE)); + if (tryFind(adminFormValues, predicate(proxy_user.name())).isPresent()) { + prnfbNotificationBuilder.withProxyUser(find(adminFormValues, predicate(proxy_user.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.proxy_password.name())).isPresent()) { - prnfbNotificationBuilder.withProxyPassword(find(adminFormValues, - predicate(AdminFormValues.FIELDS.proxy_password.name())).get(VALUE)); + if (tryFind(adminFormValues, predicate(proxy_password.name())).isPresent()) { + prnfbNotificationBuilder.withProxyPassword(find(adminFormValues, predicate(proxy_password.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.user.name())).isPresent()) { - prnfbNotificationBuilder.withUser(find(adminFormValues, predicate(AdminFormValues.FIELDS.user.name())).get(VALUE)); + if (tryFind(adminFormValues, predicate(user.name())).isPresent()) { + prnfbNotificationBuilder.withUser(find(adminFormValues, predicate(user.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.password.name())).isPresent()) { - prnfbNotificationBuilder.withPassword(find(adminFormValues, predicate(AdminFormValues.FIELDS.password.name())).get( - VALUE)); + if (tryFind(adminFormValues, predicate(password.name())).isPresent()) { + prnfbNotificationBuilder.withPassword(find(adminFormValues, predicate(password.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.filter_string.name())).isPresent()) { - prnfbNotificationBuilder.withFilterString(find(adminFormValues, - predicate(AdminFormValues.FIELDS.filter_string.name())).get(VALUE)); + if (tryFind(adminFormValues, predicate(filter_string.name())).isPresent()) { + prnfbNotificationBuilder.withFilterString(find(adminFormValues, predicate(filter_string.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.filter_regexp.name())).isPresent()) { - prnfbNotificationBuilder.withFilterRegexp(find(adminFormValues, - predicate(AdminFormValues.FIELDS.filter_regexp.name())).get(VALUE)); + if (tryFind(adminFormValues, predicate(filter_regexp.name())).isPresent()) { + prnfbNotificationBuilder.withFilterRegexp(find(adminFormValues, predicate(filter_regexp.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.method.name())).isPresent()) { - prnfbNotificationBuilder.withMethod(find(adminFormValues, predicate(AdminFormValues.FIELDS.method.name())) - .get(VALUE)); + if (tryFind(adminFormValues, predicate(method.name())).isPresent()) { + prnfbNotificationBuilder.withMethod(find(adminFormValues, predicate(method.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.post_content.name())).isPresent()) { - prnfbNotificationBuilder - .withPostContent(find(adminFormValues, predicate(AdminFormValues.FIELDS.post_content.name())).get(VALUE)); + if (tryFind(adminFormValues, predicate(post_content.name())).isPresent()) { + prnfbNotificationBuilder.withPostContent(find(adminFormValues, predicate(post_content.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.name.name())).isPresent()) { - prnfbNotificationBuilder.withName(find(adminFormValues, predicate(AdminFormValues.FIELDS.name.name())).get(VALUE)); + if (tryFind(adminFormValues, predicate(name.name())).isPresent()) { + prnfbNotificationBuilder.withName(find(adminFormValues, predicate(name.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.injection_url.name())).isPresent()) { - prnfbNotificationBuilder.withInjectionUrl(find(adminFormValues, - predicate(AdminFormValues.FIELDS.injection_url.name())).get(VALUE)); + if (tryFind(adminFormValues, predicate(injection_url.name())).isPresent()) { + prnfbNotificationBuilder.withInjectionUrl(find(adminFormValues, predicate(injection_url.name())).get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.injection_url_regexp.name())).isPresent()) { - prnfbNotificationBuilder.withInjectionUrlRegexp(find(adminFormValues, - predicate(AdminFormValues.FIELDS.injection_url_regexp.name())).get(VALUE)); + if (tryFind(adminFormValues, predicate(injection_url_regexp.name())).isPresent()) { + prnfbNotificationBuilder.withInjectionUrlRegexp(find(adminFormValues, predicate(injection_url_regexp.name())).get( + VALUE)); + } + if (tryFind(adminFormValues, predicate(trigger_if_isconflicting.name())).isPresent()) { + prnfbNotificationBuilder.withTriggerIfCanMerge(find(adminFormValues, predicate(trigger_if_isconflicting.name())) + .get(VALUE)); } - if (tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.trigger_if_isconflicting.name())).isPresent()) { - prnfbNotificationBuilder.withTriggerIfCanMerge(find(adminFormValues, - predicate(AdminFormValues.FIELDS.trigger_if_isconflicting.name())).get(VALUE)); + for (final Map event : filter(adminFormValues, predicate(trigger_ignore_state.name()))) { + prnfbNotificationBuilder.withTriggerIgnoreState(PullRequestState.valueOf(event.get(VALUE))); } return prnfbNotificationBuilder.build(); } @@ -179,7 +195,7 @@ public static void checkFieldsRecognized(AdminFormValues adminFormValues) throws for (final Map m : adminFormValues) { for (final Entry entry : m.entrySet()) { if (entry.getKey().equals(NAME)) { - if (AdminFormValues.FIELDS.valueOf(entry.getValue()) == null) { + if (valueOf(entry.getValue()) == null) { throw new ValidationException(entry.getValue(), "Field not recognized!"); } } else if (!entry.getKey().equals(VALUE)) { @@ -192,15 +208,13 @@ public static void checkFieldsRecognized(AdminFormValues adminFormValues) throws public static PrnfbSettings getPrnfbSettings(PluginSettings pluginSettings) throws ValidationException { final PrnfbSettingsBuilder prnfbSettingsBuilder = prnfbSettingsBuilder(); for (final AdminFormValues adminFormValues : getSettingsAsFormValues(pluginSettings)) { - if (isOfType(adminFormValues, AdminFormValues.FORM_TYPE.TRIGGER_CONFIG_FORM)) { + if (isOfType(adminFormValues, TRIGGER_CONFIG_FORM)) { prnfbSettingsBuilder.withNotification(getPrnfbNotification(adminFormValues)); } else { prnfbSettingsBuilder.withButton(getPrnfbButton(adminFormValues)); } - prnfbSettingsBuilder - .withUsersAllowed(tryFind(adminFormValues, predicate(AdminFormValues.FIELDS.user_allowed.name())).isPresent()); - prnfbSettingsBuilder.withAdminsAllowed(tryFind(adminFormValues, - predicate(AdminFormValues.FIELDS.admin_allowed.name())).isPresent()); + prnfbSettingsBuilder.withUsersAllowed(tryFind(adminFormValues, predicate(user_allowed.name())).isPresent()); + prnfbSettingsBuilder.withAdminsAllowed(tryFind(adminFormValues, predicate(admin_allowed.name())).isPresent()); } return prnfbSettingsBuilder.build(); } @@ -227,13 +241,11 @@ public static List getSettingsAsFormValues(PluginSettings setti * compatible. */ private static AdminFormValues injectConfigurationName(AdminFormValues adminFormValues) { - final Optional> nameMapOpt = tryFind(adminFormValues, - predicate(AdminFormValues.FIELDS.name.name())); + final Optional> nameMapOpt = tryFind(adminFormValues, predicate(name.name())); if (nameMapOpt.isPresent()) { return adminFormValues; } - adminFormValues.add(ImmutableMap. builder().put(NAME, AdminFormValues.FIELDS.name.name()) - .put(VALUE, DEFAULT_NAME).build()); + adminFormValues.add(ImmutableMap. builder().put(NAME, name.name()).put(VALUE, DEFAULT_NAME).build()); return adminFormValues; } @@ -246,10 +258,9 @@ private static void storeNotificationsMap(PluginSettings pluginSettings, Map allNotificationsMap) throws ValidationException { final List toStore = newArrayList(); for (final AdminFormValues adminFormValues : allNotificationsMap.values()) { - final Optional> formIdOpt = tryFind(adminFormValues, - predicate(AdminFormValues.FIELDS.FORM_IDENTIFIER.name())); + final Optional> formIdOpt = tryFind(adminFormValues, predicate(FORM_IDENTIFIER.name())); if (!formIdOpt.isPresent() || formIdOpt.get().get(VALUE).trim().isEmpty()) { - throw new ValidationException(AdminFormValues.FIELDS.FORM_IDENTIFIER.name(), "Not set!"); + throw new ValidationException(FORM_IDENTIFIER.name(), "Not set!"); } toStore.add(new Gson().toJson(adminFormValues)); } @@ -260,17 +271,16 @@ public static void storeSettings(PluginSettings pluginSettings, final AdminFormV throws ValidationException { injectFormIdentifierIfNotSet(config); final Map allNotificationsMap = getNotificationsMap(pluginSettings); - allNotificationsMap.put(find(config, predicate(AdminFormValues.FIELDS.FORM_IDENTIFIER.name())).get(VALUE), config); + allNotificationsMap.put(find(config, predicate(FORM_IDENTIFIER.name())).get(VALUE), config); storeNotificationsMap(pluginSettings, allNotificationsMap); } public static void injectFormIdentifierIfNotSet(final AdminFormValues config) { - final Optional> formIdOpt = tryFind(config, - predicate(AdminFormValues.FIELDS.FORM_IDENTIFIER.name())); + final Optional> formIdOpt = tryFind(config, predicate(FORM_IDENTIFIER.name())); if (!formIdOpt.isPresent() || formIdOpt.get().get(VALUE).trim().isEmpty()) { final String generatedIdentifier = formIdentifierGnerator(); - removeIf(config, predicate(AdminFormValues.FIELDS.FORM_IDENTIFIER.name())); - config.add(new ImmutableMap.Builder().put(NAME, AdminFormValues.FIELDS.FORM_IDENTIFIER.name()) + removeIf(config, predicate(FORM_IDENTIFIER.name())); + config.add(new ImmutableMap.Builder().put(NAME, FORM_IDENTIFIER.name()) .put(VALUE, generatedIdentifier).build()); } } diff --git a/src/main/resources/admin.vm b/src/main/resources/admin.vm index b3c1847..f8d6eec 100644 --- a/src/main/resources/admin.vm +++ b/src/main/resources/admin.vm @@ -153,6 +153,13 @@
+
+
+ Triggers do not apply to +
+
+
+
Triggers diff --git a/src/test/java/se/bjurr/prnbs/admin/utils/PullRequestEventBuilder.java b/src/test/java/se/bjurr/prnbs/admin/utils/PullRequestEventBuilder.java index 9810ec7..372e60f 100644 --- a/src/test/java/se/bjurr/prnbs/admin/utils/PullRequestEventBuilder.java +++ b/src/test/java/se/bjurr/prnbs/admin/utils/PullRequestEventBuilder.java @@ -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"; @@ -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; @@ -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); @@ -120,4 +123,9 @@ public PullRequestEventBuilder withPullRequestId(Long id) { this.pullRequestId = id; return this; } + + public PullRequestEventBuilder withPullRequestInState(PullRequestState pullRequestState) { + this.pullRequestState = pullRequestState; + return this; + } } diff --git a/src/test/java/se/bjurr/prnfb/admin/PrnfbPullRequestEventListenerTest.java b/src/test/java/se/bjurr/prnfb/admin/PrnfbPullRequestEventListenerTest.java index e2d9074..0bf3a4b 100644 --- a/src/test/java/se/bjurr/prnfb/admin/PrnfbPullRequestEventListenerTest.java +++ b/src/test/java/se/bjurr/prnfb/admin/PrnfbPullRequestEventListenerTest.java @@ -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; @@ -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; @@ -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 { @@ -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 from = newArrayList();