Skip to content

Commit

Permalink
Fix setting default title only when no subject has been set (#750) (#788
Browse files Browse the repository at this point in the history
)

Signed-off-by: Ashish Agrawal <ashisagr@amazon.com>
(cherry picked from commit 830dff1)

Co-authored-by: Ashish Agrawal <ashisagr@amazon.com>
  • Loading branch information
opensearch-trigger-bot[bot] and lezzago authored Jun 2, 2023
1 parent fab48fb commit f9fdd57
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import org.opensearch.alerting.script.QueryLevelTriggerExecutionContext
import org.opensearch.alerting.script.TriggerExecutionContext
import org.opensearch.alerting.util.destinationmigration.NotificationActionConfigs
import org.opensearch.alerting.util.destinationmigration.NotificationApiUtils.Companion.getNotificationConfigInfo
import org.opensearch.alerting.util.destinationmigration.createMessageContent
import org.opensearch.alerting.util.destinationmigration.getTitle
import org.opensearch.alerting.util.destinationmigration.publishLegacyNotification
import org.opensearch.alerting.util.destinationmigration.sendNotification
Expand Down Expand Up @@ -110,7 +109,7 @@ abstract class MonitorRunner {
?.sendNotification(
monitorCtx.client!!,
config.channel.getTitle(subject),
config.channel.createMessageContent(subject, message)
message
) ?: actionResponseContent

actionResponseContent = config.destination
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import org.opensearch.alerting.opensearchapi.retryForNotification
import org.opensearch.alerting.opensearchapi.suspendUntil
import org.opensearch.client.Client
import org.opensearch.client.node.NodeClient
import org.opensearch.common.Strings
import org.opensearch.common.unit.TimeValue
import org.opensearch.commons.ConfigConstants
import org.opensearch.commons.destination.message.LegacyBaseMessage
Expand All @@ -27,7 +26,6 @@ import org.opensearch.commons.notifications.action.LegacyPublishNotificationRequ
import org.opensearch.commons.notifications.action.LegacyPublishNotificationResponse
import org.opensearch.commons.notifications.action.SendNotificationResponse
import org.opensearch.commons.notifications.model.ChannelMessage
import org.opensearch.commons.notifications.model.ConfigType
import org.opensearch.commons.notifications.model.EventSource
import org.opensearch.commons.notifications.model.NotificationConfigInfo
import org.opensearch.commons.notifications.model.SeverityType
Expand Down Expand Up @@ -138,33 +136,11 @@ suspend fun NotificationConfigInfo.sendNotification(client: Client, title: Strin
}

/**
* For most channel types, a placeholder Alerting title will be used but the email channel/SNS notification will
* use the subject, so it appears as the actual subject of the email/SNS notification.
* A placeholder Alerting title will be used if no subject is passed in.
*/
fun NotificationConfigInfo.getTitle(subject: String?): String {
val defaultTitle = "Alerting-Notification Action"
if (this.notificationConfig.configType == ConfigType.EMAIL || this.notificationConfig.configType == ConfigType.SNS) {
return if (subject.isNullOrEmpty()) defaultTitle else subject
}

return defaultTitle
}

fun NotificationConfigInfo.createMessageContent(subject: String?, message: String): String {
// For Email Channels, the subject is not passed in the main message since it's used as the title
if (this.notificationConfig.configType == ConfigType.EMAIL) {
return constructMessageContent("", message)
}

return constructMessageContent(subject, message)
}

/**
* Similar to Destinations, this is a generic utility method for constructing message content from
* a subject and message body when sending through Notifications since the Action definition in Monitors can have both.
*/
private fun constructMessageContent(subject: String?, message: String): String {
return if (Strings.isNullOrEmpty(subject)) message else "$subject \n\n $message"
return if (subject.isNullOrEmpty()) defaultTitle else subject
}

/**
Expand Down

0 comments on commit f9fdd57

Please sign in to comment.