From 130c32df8b536a4ea4d44e146275027c7d405355 Mon Sep 17 00:00:00 2001 From: Malik Diarra Date: Tue, 16 Apr 2024 12:01:15 -0700 Subject: [PATCH] Use slack block for automatic disabling of notifications (#11903) --- .../notification/SlackNotificationClient.java | 45 ++++++++++--------- .../SlackNotificationClientTest.java | 6 +-- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/airbyte-notification/src/main/java/io/airbyte/notification/SlackNotificationClient.java b/airbyte-notification/src/main/java/io/airbyte/notification/SlackNotificationClient.java index 55a275c780f..70b6a8ac38a 100644 --- a/airbyte-notification/src/main/java/io/airbyte/notification/SlackNotificationClient.java +++ b/airbyte-notification/src/main/java/io/airbyte/notification/SlackNotificationClient.java @@ -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; @@ -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 @@ -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 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:*"); @@ -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 diff --git a/airbyte-notification/src/test/java/io/airbyte/notification/SlackNotificationClientTest.java b/airbyte-notification/src/test/java/io/airbyte/notification/SlackNotificationClientTest.java index 83834349bc3..5e1116d4f02 100644 --- a/airbyte-notification/src/test/java/io/airbyte/notification/SlackNotificationClientTest.java +++ b/airbyte-notification/src/test/java/io/airbyte/notification/SlackNotificationClientTest.java @@ -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, "")); @@ -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, "")); @@ -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();