Skip to content

Commit

Permalink
Use a subset of default notification events (#1)
Browse files Browse the repository at this point in the history
Only use broken, failed, and fixed by default.
This default can be changed in the plugin config.
  • Loading branch information
Sandro Heinzelmann committed Apr 27, 2018
1 parent dcf234b commit 6fb9c82
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public abstract class ConfigurableNotificationListener implements NotificationLi
private String targetEnvVariableSuffix;
private String eventsEnvVariableSuffix;
private SessionCache<String, Collection<TargetConfig>, Integer> routingConfigs;
private Set<String> defaultEvents;

public ConfigurableNotificationListener(PipelineInfoProvider pipelineInfo, String envVariableBase,
String targetEnvVariableSuffix, String eventsEnvVariableSuffix) {
String targetEnvVariableSuffix, String eventsEnvVariableSuffix, Set<String> 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);
}

Expand Down Expand Up @@ -137,7 +139,7 @@ else if (v.name.endsWith(targetEnvVariableSuffix)) {
}


private static List<RoutingRule> parseRoutingRules(String ruleStr) {
private List<RoutingRule> parseRoutingRules(String ruleStr) {
List<RoutingRule> result = new LinkedList<>();
String[] rules = ruleStr.toLowerCase().split("\\s*,\\s*");
for (String rule: rules) {
Expand All @@ -157,13 +159,13 @@ private static List<RoutingRule> parseRoutingRules(String ruleStr) {
return result;
}

private static class TargetConfig {
private class TargetConfig {
Set<String> targets;
List<RoutingRule> routingRules;

boolean applies(String stage, String event) {
if (routingRules == null || routingRules.isEmpty()) {
return true;
return matchesDefaultEvent(event);
}

for (RoutingRule rule: routingRules) {
Expand All @@ -175,7 +177,7 @@ boolean applies(String stage, String event) {
}
}

private static class RoutingRule {
private class RoutingRule {
String requiredEvent;
String requiredStage;

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,19 +14,23 @@ 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<String, SettingsField> BASE_FIELD_CONFIG = new HashMap<>();
static {
BASE_FIELD_CONFIG.put("serverUrl", new SettingsField("Server URL", DEFAULT_SERVER_URL, false, false, 0));
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);
Expand Down Expand Up @@ -57,6 +64,18 @@ public void setRestPassword(String restPassword) {
this.restPassword = restPassword;
}

public String getDefaultEvents() {
return valueOrDefault(defaultEvents, DEFAULT_EVENTS);
}

public Set<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static <T> Optional<T> fetch(String url, Class<T> clazz, String apiVer,
return Optional.of(t);
}
}
catch (IOException e) {
catch (Exception e) {
LOGGER.error("Error fetching " + url + ":", e);
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<input id="gong-rest-pwd" type="password" ng-model="restPassword"/>
<span class="form_error" ng-show="GOINPUTNAME[restPassword].$error.server">{{ GOINPUTNAME[restPassword].$error.server }}</span>
</div>
<div class="form_item_block">
<label>Default events to notify:</label>
<input id="default-events" type="text" ng-model="defaultEvents" placeholder="fixed, broken, failed"/>
<span class="form_error" ng-show="GOINPUTNAME[defaultEvents].$error.server">{{ GOINPUTNAME[defaultEvents].$error.server }}</span>
</div>
<!-- Not really useful because the ajax request always includes the user's session cookie (no way not to) so can't verify authorization -->
<!--
<button class="check_connection submit button MB_focusable"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
@RunWith(MockitoJUnitRunner.class)
public class ConfigurableNotificationListenerTest {

private static final Set<String> DEFAULT_EVENTS = new HashSet<>(Arrays.asList(
"broken", "fixed", "failed"
));

private static final List<BiConsumer<ConfigurableNotificationListener, StageStateChange>> ALL_HANDLERS = Arrays.asList(
ConfigurableNotificationListener::handleBroken,
ConfigurableNotificationListener::handlePassed,
Expand Down Expand Up @@ -54,29 +58,29 @@ public void shouldRouteToSingleTarget() throws Exception {
addMockEnvVariables("pipeline1",
"GONG_TEST_TARGET", "[email protected]");

listener.handlePassed(new StageStateChange("pipeline1",
listener.handleBroken(new StageStateChange("pipeline1",
10,
"stage1",
"passed"));
"broken"));

listener.assertTargets("passed", "[email protected]");
listener.assertTargets("broken", "[email protected]");
}

@Test
public void shouldRouteToMultipleTargetsInSameVar() throws Exception {
addMockEnvVariables("pipeline1",
"GONG_TEST_TARGET", "[email protected], [email protected]");

listener.handlePassed(new StageStateChange("pipeline1",
listener.handleBroken(new StageStateChange("pipeline1",
10,
"stage1",
"passed"));
"broken"));

listener.assertTargets("passed", "[email protected]", "[email protected]");
listener.assertTargets("broken", "[email protected]", "[email protected]");
}

@Test
public void shouldRouteAllEventsToSingleTarget() throws Exception {
public void shouldRouteAllDefaultEventsToSingleTarget() throws Exception {
addMockEnvVariables("pipeline1",
"GONG_TEST_TARGET", "[email protected]");

Expand All @@ -86,13 +90,10 @@ public void shouldRouteAllEventsToSingleTarget() throws Exception {
"dummy");
ALL_HANDLERS.forEach(h -> h.accept(listener, change));

assertEquals(6, listener.targets.size());
listener.assertTargets("broken", "[email protected]");
listener.assertTargets("passed", "[email protected]");
assertEquals(3, listener.targets.size());
listener.assertTargets("fixed", "[email protected]");
listener.assertTargets("broken", "[email protected]");
listener.assertTargets("failed", "[email protected]");
listener.assertTargets("cancelled", "[email protected]");
listener.assertTargets("building", "[email protected]");
}

@Test
Expand Down Expand Up @@ -190,7 +191,7 @@ public void shouldRouteForSpecificStagesAndEvents() throws Exception {
}

@Test
public void shouldRouteForSpecificStagesAndAllEvents() throws Exception {
public void shouldRouteForSpecificStagesAndAllDefaultEvents() throws Exception {
addMockEnvVariables("pipeline1",
"GONG_TEST_TARGET", "[email protected]",
"GONG_TEST_EVENTS", "stage2.broken, stage1.all");
Expand All @@ -202,7 +203,10 @@ public void shouldRouteForSpecificStagesAndAllEvents() throws Exception {

ALL_HANDLERS.forEach(h -> h.accept(listener, change1));

assertEquals(ALL_HANDLERS.size(), listener.targets.size());
assertEquals(3, listener.targets.size());
listener.assertTargetsForStage("stage1", "fixed", "[email protected]");
listener.assertTargetsForStage("stage1", "broken", "[email protected]");
listener.assertTargetsForStage("stage1", "failed", "[email protected]");
}

@Test
Expand Down Expand Up @@ -315,7 +319,7 @@ private class Target {

public TestConfigurableNotificationListener(PipelineInfoProvider pipelineInfo, String envVariableBase,
String targetEnvVariableSuffix, String eventsEnvVariableSuffix) {
super(pipelineInfo, envVariableBase, targetEnvVariableSuffix, eventsEnvVariableSuffix);
super(pipelineInfo, envVariableBase, targetEnvVariableSuffix, eventsEnvVariableSuffix, DEFAULT_EVENTS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import ch.adnovum.gong.notifier.ConfigurableNotificationListener;
import ch.adnovum.gong.notifier.PipelineInfoProvider;
Expand All @@ -30,8 +31,9 @@ public class EmailNotificationListener extends ConfigurableNotificationListener
private ModificationListGenerator modListGenerator;

public EmailNotificationListener(PipelineInfoProvider pipelineInfo, EmailSender emailSender, String senderEmail,
String subjectTemplate, String bodyTemplate, String serverDisplayUrl, ModificationListGenerator modListGenerator) {
super(pipelineInfo, EMAIL_ENV_VARIABLE, TARGET_SUFFIX, EVENTS_SUFFIX);
String subjectTemplate, String bodyTemplate, String serverDisplayUrl, Set<String> defaultEvents,
ModificationListGenerator modListGenerator) {
super(pipelineInfo, EMAIL_ENV_VARIABLE, TARGET_SUFFIX, EVENTS_SUFFIX, defaultEvents);

this.emailSender = emailSender;
this.senderEmail = senderEmail;
Expand All @@ -44,7 +46,8 @@ public EmailNotificationListener(PipelineInfoProvider pipelineInfo, EmailSender
public EmailNotificationListener(PipelineInfoProvider pipelineInfo, EmailSender emailSender,
PluginSettings settings) {
this(pipelineInfo, emailSender, settings.getSenderEmail(), settings.getSubjectTemplate(), settings.getBodyTemplate(),
settings.getServerDisplayUrl(), new ModificationListGenerator(settings.getTimezone(), true));
settings.getServerDisplayUrl(), settings.getDefaultEventsSet(),
new ModificationListGenerator(settings.getTimezone(), true));
}

@Override
Expand Down

0 comments on commit 6fb9c82

Please sign in to comment.