Skip to content

Commit

Permalink
Allow channel notifications in Slack alerts by suffixing the channel …
Browse files Browse the repository at this point in the history
…name with an exclamation mark
  • Loading branch information
neilprosser committed Mar 6, 2015
1 parent 1b2d701 commit fccb2ee
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void sendNotification(Check check, Subscription subscription, List<Alert>

List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
parameters.add(new BasicNameValuePair("token", token));
parameters.add(new BasicNameValuePair("channel", channel));
parameters.add(new BasicNameValuePair("channel", StringUtils.removeEnd(channel, "!")));
parameters.add(new BasicNameValuePair("text", formatContent(emojis, check, subscription, alerts)));
parameters.add(new BasicNameValuePair("username", username));
parameters.add(new BasicNameValuePair("icon_url", iconUrl));
Expand Down Expand Up @@ -118,16 +119,19 @@ public String apply(Alert input) {
return String.format("%s = %s", input.getTarget(), input.getValue().toString());
}
}));

String channel = subscription.getTarget().contains("!") ? "@channel" : "";

final String state = check.getState().toString();

return String.format("%s%s %s [%s]\n```\n%s\n```\n#%s",
return String.format("%s%s %s [%s]\n```\n%s\n```\n#%s %s",
Iterables.get(emojis, check.getState().ordinal(), ""),
state,
check.getName(),
url,
alertsString,
state.toLowerCase(Locale.getDefault())
state.toLowerCase(Locale.getDefault()),
channel
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void after() {
}

@Test
public void notifcationServiceCanOnlyHandleFlowdockSubscription() {
public void notifcationServiceCanOnlyHandleSlackSubscription() {
assertThat(notificationService.canHandle(SubscriptionType.SLACK), is(true));
for (SubscriptionType type : SubscriptionType.values()) {
if (type == SubscriptionType.SLACK) {
Expand Down Expand Up @@ -124,6 +124,55 @@ public void basicSlackTest() {
verify(mockSeyrenConfig).getBaseUrl();
}

@Test
public void mentionChannelWhenTargetContainsExclamationTest() {
BigDecimal value = new BigDecimal("1.0");

Check check = new Check()
.withId("123")
.withEnabled(true)
.withName("test-check")
.withState(AlertType.ERROR);
Subscription subscription = new Subscription()
.withEnabled(true)
.withType(SubscriptionType.SLACK)
.withTarget("target!");
Alert alert = new Alert()
.withValue(value)
.withTimestamp(new DateTime())
.withFromType(AlertType.OK)
.withToType(AlertType.ERROR);
List<Alert> alerts = Arrays.asList(alert);

StringBodyCapture bodyCapture = new StringBodyCapture();

clientDriver.addExpectation(
onRequestTo("/api/chat.postMessage")
.withMethod(ClientDriverRequest.Method.POST)
.capturingBodyIn(bodyCapture)
.withHeader("accept", "application/json"),
giveEmptyResponse());

notificationService.sendNotification(check, subscription, alerts);

String content = bodyCapture.getContent();
System.out.println(decode(content));

assertThat(content, Matchers.containsString("token="));
assertThat(content, Matchers.containsString("&channel=target"));
assertThat(content, Matchers.containsString(encode("@channel")));
assertThat(content, Matchers.containsString(encode("ERROR test-check")));
assertThat(content, Matchers.containsString(encode("/#/checks/123")));
assertThat(content, Matchers.containsString("&username=Seyren"));
assertThat(content, Matchers.containsString("&icon_url="));

verify(mockSeyrenConfig).getSlackEmojis();
verify(mockSeyrenConfig).getSlackIconUrl();
verify(mockSeyrenConfig).getSlackToken();
verify(mockSeyrenConfig).getSlackUsername();
verify(mockSeyrenConfig).getBaseUrl();
}

String encode(String data) {
try {
return URLEncoder.encode(data, "ISO-8859-1");
Expand Down

0 comments on commit fccb2ee

Please sign in to comment.