Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting default title only when no subject has been set #750

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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 @@ -109,7 +108,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
lezzago marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down