Skip to content

Commit

Permalink
Use slack block for automatic disabling of notifications (#11903)
Browse files Browse the repository at this point in the history
  • Loading branch information
malikdiarra committed Apr 16, 2024
1 parent e594299 commit 130c32d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.net.http.HttpResponse;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import org.apache.logging.log4j.util.Strings;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
Expand Down Expand Up @@ -66,7 +67,7 @@ public boolean notifyJobFailure(final SyncSummary summary,
summary.getErrorMessage(),
summary.getConnection().getUrl(),
String.valueOf(summary.getJobId()));
return notifyJson(buildJobCompletedNotification(summary, legacyMessage).toJsonNode());
return notifyJson(buildJobCompletedNotification(summary, "Sync failure occurred", legacyMessage, Optional.empty()).toJsonNode());
}

@Override
Expand All @@ -81,19 +82,26 @@ public boolean notifyJobSuccess(final SyncSummary summary,
summary.getErrorMessage(),
summary.getConnection().getUrl(),
String.valueOf(summary.getJobId()));
return notifyJson(buildJobCompletedNotification(summary, legacyMessage).toJsonNode());
return notifyJson(buildJobCompletedNotification(summary, "Sync completed", legacyMessage, Optional.empty()).toJsonNode());
}

@NotNull
static Notification buildJobCompletedNotification(final SyncSummary summary, final String text) {
static Notification buildJobCompletedNotification(final SyncSummary summary,
final String titleText,
final String legacyText,
final Optional<String> topContent) {
Notification notification = new Notification();
notification.setText(text);
notification.setText(legacyText);
Section title = notification.addSection();
String connectionLink = Notification.createLink(summary.getConnection().getName(), summary.getConnection().getUrl());
String titleText = summary.isSuccess() ? "Sync completed" : "Sync failure occurred";
title.setText(String.format("%s: %s", titleText, connectionLink));
Section description = notification.addSection();

if (topContent.isPresent()) {
final Section topSection = notification.addSection();
topSection.setText(topContent.get());
}

Section description = notification.addSection();
final Field sourceLabel = description.addField();
sourceLabel.setType("mrkdwn");
sourceLabel.setText("*Source:*");
Expand Down Expand Up @@ -143,38 +151,35 @@ static Notification buildJobCompletedNotification(final SyncSummary summary, fin
public boolean notifyConnectionDisabled(final SyncSummary summary,
final String receiverEmail)
throws IOException, InterruptedException {
final String message = renderTemplate(
String legacyMessage = renderTemplate(
"slack/auto_disable_slack_notification_template.txt",
summary.getSource().getName(),
summary.getDestination().getName(),
summary.getErrorMessage(),
summary.getWorkspace().getId().toString(),
summary.getConnection().getId().toString());

final String webhookUrl = config.getWebhook();
if (!Strings.isEmpty(webhookUrl)) {
return notify(message);
}
return false;
String message = """
Your connection has been repeatedly failing and has been automatically disabled.
""";
return notifyJson(buildJobCompletedNotification(summary, "Connection disabled", legacyMessage, Optional.of(message)).toJsonNode());
}

@Override
public boolean notifyConnectionDisableWarning(final SyncSummary summary,
final String receiverEmail)
throws IOException, InterruptedException {
final String message = renderTemplate(
String legacyMessage = renderTemplate(
"slack/auto_disable_warning_slack_notification_template.txt",
summary.getSource().getName(),
summary.getDestination().getName(),
summary.getErrorMessage(),
summary.getWorkspace().getId().toString(),
summary.getConnection().getId().toString());

final String webhookUrl = config.getWebhook();
if (!Strings.isEmpty(webhookUrl)) {
return notify(message);
}
return false;
String message = """
Your connection has been repeatedly failing. Please address any issues to ensure your syncs continue to run.
""";
return notifyJson(
buildJobCompletedNotification(summary, "Warning - repeated connection failures", legacyMessage, Optional.of(message)).toJsonNode());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void testNotifyConnectionDisabled() throws IOException, InterruptedException {
.workspace(WorkspaceInfo.builder().id(WORKSPACE_ID).build())
.destination(DestinationInfo.builder().name(DESTINATION_TEST).build())
.source(SourceInfo.builder().name(SOURCE_TEST).build())
.connection(ConnectionInfo.builder().id(CONNECTION_ID).build())
.connection(ConnectionInfo.builder().id(CONNECTION_ID).name(CONNECTION_NAME).url("http://connection").build())
.errorMessage("job description.")
.build();
assertTrue(client.notifyConnectionDisabled(summary, ""));
Expand All @@ -200,7 +200,7 @@ void testNotifyConnectionDisabledWarning() throws IOException, InterruptedExcept
.workspace(WorkspaceInfo.builder().id(WORKSPACE_ID).build())
.destination(DestinationInfo.builder().name(DESTINATION_TEST).build())
.source(SourceInfo.builder().name(SOURCE_TEST).build())
.connection(ConnectionInfo.builder().id(CONNECTION_ID).build())
.connection(ConnectionInfo.builder().id(CONNECTION_ID).name(CONNECTION_NAME).url("http://connection").build())
.errorMessage("job description.")
.build();
assertTrue(client.notifyConnectionDisableWarning(summary, ""));
Expand Down Expand Up @@ -359,7 +359,7 @@ public void handle(final HttpExchange t) throws IOException {
response = "No notification message or message missing `text` node";
t.sendResponseHeaders(500, response.length());
} else {
response = String.format("Wrong notification messge: %s", message.get("text").asText());
response = String.format("Wrong notification message: %s", message.get("text").asText());
t.sendResponseHeaders(500, response.length());
}
final OutputStream os = t.getResponseBody();
Expand Down

0 comments on commit 130c32d

Please sign in to comment.