From 6fb9c8232144206a6326d5989466782ec88834d9 Mon Sep 17 00:00:00 2001 From: Sandro Heinzelmann Date: Fri, 27 Apr 2018 15:38:33 +0200 Subject: [PATCH] Use a subset of default notification events (#1) Only use broken, failed, and fixed by default. This default can be changed in the plugin config. --- .../ConfigurableNotificationListener.java | 27 +++++++++++---- .../gong/notifier/PluginSettingsBase.java | 19 +++++++++++ .../gong/notifier/go/api/GoServerApi.java | 2 +- .../plugin-settings-base.template.html | 5 +++ .../ConfigurableNotificationListenerTest.java | 34 +++++++++++-------- .../email/EmailNotificationListener.java | 9 +++-- 6 files changed, 70 insertions(+), 26 deletions(-) diff --git a/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/ConfigurableNotificationListener.java b/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/ConfigurableNotificationListener.java index 3eb30a6..f5632a4 100644 --- a/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/ConfigurableNotificationListener.java +++ b/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/ConfigurableNotificationListener.java @@ -28,13 +28,15 @@ public abstract class ConfigurableNotificationListener implements NotificationLi private String targetEnvVariableSuffix; private String eventsEnvVariableSuffix; private SessionCache, Integer> routingConfigs; + private Set defaultEvents; public ConfigurableNotificationListener(PipelineInfoProvider pipelineInfo, String envVariableBase, - String targetEnvVariableSuffix, String eventsEnvVariableSuffix) { + String targetEnvVariableSuffix, String eventsEnvVariableSuffix, Set defaultEvents) { this.pipelineInfo = pipelineInfo; this.envVariableBase = envVariableBase; this.targetEnvVariableSuffix = targetEnvVariableSuffix; this.eventsEnvVariableSuffix = eventsEnvVariableSuffix; + this.defaultEvents = defaultEvents; this.routingConfigs = new SessionCache<>(5, TimeUnit.MINUTES, 1000, this::fetchPipelineTargetConfig); } @@ -137,7 +139,7 @@ else if (v.name.endsWith(targetEnvVariableSuffix)) { } - private static List parseRoutingRules(String ruleStr) { + private List parseRoutingRules(String ruleStr) { List result = new LinkedList<>(); String[] rules = ruleStr.toLowerCase().split("\\s*,\\s*"); for (String rule: rules) { @@ -157,13 +159,13 @@ private static List parseRoutingRules(String ruleStr) { return result; } - private static class TargetConfig { + private class TargetConfig { Set targets; List routingRules; boolean applies(String stage, String event) { if (routingRules == null || routingRules.isEmpty()) { - return true; + return matchesDefaultEvent(event); } for (RoutingRule rule: routingRules) { @@ -175,7 +177,7 @@ boolean applies(String stage, String event) { } } - private static class RoutingRule { + private class RoutingRule { String requiredEvent; String requiredStage; @@ -185,9 +187,20 @@ private static class RoutingRule { } boolean applies(String stage, String event) { - return ("all".equals(requiredEvent) || event.equals(requiredEvent)) && - ("all".equals(requiredStage) || stage.equals(requiredStage)); + return appliesToEvent(event) && appliesToStage(stage); } + + boolean appliesToEvent(String event) { + return ("all".equals(requiredEvent) && matchesDefaultEvent(event)) || event.equals(requiredEvent); + } + + boolean appliesToStage(String stage) { + return "all".equals(requiredStage) || stage.equals(requiredStage); + } + } + + private boolean matchesDefaultEvent(String event) { + return event != null && (defaultEvents == null || defaultEvents.isEmpty() || defaultEvents.contains(event)); } public enum Event { diff --git a/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/PluginSettingsBase.java b/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/PluginSettingsBase.java index bc9216a..8cddde7 100644 --- a/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/PluginSettingsBase.java +++ b/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/PluginSettingsBase.java @@ -1,7 +1,10 @@ package ch.adnovum.gong.notifier; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import ch.adnovum.gong.notifier.go.api.SettingsField; @@ -11,6 +14,7 @@ public class PluginSettingsBase { private static final String DEFAULT_SERVER_DISPLAY_URL = "https://localhost:8154/go"; private static final String DEFAULT_REST_USER = null; private static final String DEFAULT_REST_PASSWORD = null; + private static final String DEFAULT_EVENTS = "broken, fixed, failed"; public static final Map BASE_FIELD_CONFIG = new HashMap<>(); static { @@ -18,12 +22,15 @@ public class PluginSettingsBase { BASE_FIELD_CONFIG.put("serverDisplayUrl", new SettingsField("Server Display URL", DEFAULT_SERVER_DISPLAY_URL, false, false, 0)); BASE_FIELD_CONFIG.put("restUser", new SettingsField("Rest User", DEFAULT_REST_USER, false, false, 0)); BASE_FIELD_CONFIG.put("restPassword", new SettingsField("Rest Password", DEFAULT_REST_PASSWORD, false, true, 0)); + BASE_FIELD_CONFIG.put("defaultEvents", new SettingsField("Default notification events", DEFAULT_EVENTS, false, false, + 0)); } private String serverUrl = DEFAULT_SERVER_URL; private String serverDisplayUrl = DEFAULT_SERVER_DISPLAY_URL; private String restUser = DEFAULT_REST_USER; private String restPassword = DEFAULT_REST_PASSWORD; + private String defaultEvents = DEFAULT_EVENTS; public String getServerDisplayUrl() { return valueOrDefault(serverDisplayUrl, DEFAULT_SERVER_DISPLAY_URL); @@ -57,6 +64,18 @@ public void setRestPassword(String restPassword) { this.restPassword = restPassword; } + public String getDefaultEvents() { + return valueOrDefault(defaultEvents, DEFAULT_EVENTS); + } + + public Set getDefaultEventsSet() { + return new HashSet<>(Arrays.asList(getDefaultEvents().split("\\s*,\\s*"))); + } + + public void setDefaultEvents(String defaultEvents) { + this.defaultEvents = defaultEvents; + } + private static String valueOrDefault(String value, String defaultValue) { return value == null || value.isEmpty() ? defaultValue : value; } diff --git a/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/go/api/GoServerApi.java b/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/go/api/GoServerApi.java index 5dec8ec..6ac8576 100644 --- a/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/go/api/GoServerApi.java +++ b/gong-notifier-base/src/main/java/ch/adnovum/gong/notifier/go/api/GoServerApi.java @@ -55,7 +55,7 @@ private static Optional fetch(String url, Class clazz, String apiVer, return Optional.of(t); } } - catch (IOException e) { + catch (Exception e) { LOGGER.error("Error fetching " + url + ":", e); return Optional.empty(); } diff --git a/gong-notifier-base/src/main/resources/plugin-settings-base.template.html b/gong-notifier-base/src/main/resources/plugin-settings-base.template.html index 647a143..8d7b33d 100644 --- a/gong-notifier-base/src/main/resources/plugin-settings-base.template.html +++ b/gong-notifier-base/src/main/resources/plugin-settings-base.template.html @@ -22,6 +22,11 @@ {{ GOINPUTNAME[restPassword].$error.server }} +
+ + + {{ GOINPUTNAME[defaultEvents].$error.server }} +