Skip to content

Commit

Permalink
moving ServiceMessageNotifier interface into OpenAPI
Browse files Browse the repository at this point in the history
replaced "parent TeamCity server" with "this TeamCity server" in the field description
Merge-request: TC-MR-4808
  • Loading branch information
Fedor Rumyantsev authored and qodana-bot committed Apr 14, 2023
1 parent 4397f69 commit d698f3a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package jetbrains.buildServer.notification.slackNotifier.notification


import jetbrains.buildServer.notification.AdHocNotificationException
import jetbrains.buildServer.notification.ServiceMessageNotificationException
import jetbrains.buildServer.notification.NotificationRule.Event.*
import jetbrains.buildServer.notification.slackNotifier.And
import jetbrains.buildServer.notification.slackNotifier.BaseSlackTestCase
Expand Down Expand Up @@ -278,7 +278,7 @@ class SlackNotifierTest : BaseSlackTestCase() {
fun `service message notification should fail if notification limit is reached`() {
`given there is connection allowing service message notifications`(1)
`when multiple service message notifications are sent`(count = 2)
`then exception is logged in the build log`("Reached limit of 1 ad-hoc Slack notifications per build")
`then exception is logged in the build log`("Reached limit of 1 service message Slack notifications per build")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class SlackNotifierDescriptor(

fun getType(): String = notifierType

fun getAdHocNotifierType(): String = adHocNotifierType
fun getServiceMessageNotifierType(): String = adHocNotifierType

fun getDisplayName(): String {
if (displayNameClashes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SlackNotifier(
private val descriptor: SlackNotifierDescriptor,
private val notificationCountHandler: BuildPromotionNotificationCountHandler,
private val domainNameFinder: DomainNameFinder
) : NotificatorAdapter(), AdHocNotifier, PositionAware {
) : NotificatorAdapter(), ServiceMessageNotifier, PositionAware {

private val slackApi = slackApiFactory.createSlackWebApi()

Expand All @@ -66,7 +66,7 @@ class SlackNotifier(

override fun getNotificatorType(): String = descriptor.getType()

override fun getAdHocNotifierType(): String = descriptor.getAdHocNotifierType()
override fun getServiceMessageNotifierType(): String = descriptor.getServiceMessageNotifierType()

override fun notifyTestsMuted(tests: Collection<STest>, muteInfo: MuteInfo, users: Set<SUser>) {
val project = muteInfo.project ?: return
Expand Down Expand Up @@ -329,20 +329,20 @@ class SlackNotifier(
return null
}

@Throws(AdHocNotificationException::class)
@Throws(ServiceMessageNotificationException::class)
private fun sendMessage(message: MessagePayload, sendTo: String, token: String) {
if (!TeamCityProperties.getBooleanOrTrue(SlackNotifierProperties.sendNotifications)) {
throw AdHocNotificationException("Slack notifications are disabled server-wide. Not sending $message to channel with id '$sendTo'.")
throw ServiceMessageNotificationException("Slack notifications are disabled server-wide. Not sending $message to channel with id '$sendTo'.")
}

val result = slackApi.postMessage(token, message.toSlackMessage(sendTo))

if (!result.ok) {
throw AdHocNotificationException("Error sending message to $sendTo: ${result.error}")
throw ServiceMessageNotificationException("Error sending message to $sendTo: ${result.error}")
}
}

@Throws(AdHocNotificationException::class)
@Throws(ServiceMessageNotificationException::class)
override fun sendBuildRelatedNotification(
message: String, runningBuild: SRunningBuild, parameters: MutableMap<String, String>
) {
Expand All @@ -362,13 +362,13 @@ class SlackNotifier(
checkExternalDomainsInMessage(message, connectionDescriptor)

val token = getToken(project, connectionDescriptor.id)
?: throw AdHocNotificationException(
?: throw ServiceMessageNotificationException(
"No token for connection with id '${connectionDescriptor.id}'" +
" in project with external id '${project.externalId}' was found"
)

val sendTo = parameters["sendTo"]
?: throw AdHocNotificationException("'sendTo' argument was not specified for message $message, build ID ${runningBuild.buildId}")
?: throw ServiceMessageNotificationException("'sendTo' argument was not specified for message $message, build ID ${runningBuild.buildId}")

val processedMessagePayload = adHocMessageBuilder.buildRelatedNotification(
runningBuild,
Expand All @@ -378,7 +378,7 @@ class SlackNotifier(
sendMessage(processedMessagePayload, sendTo, token)
}

@Throws(AdHocNotificationException::class)
@Throws(ServiceMessageNotificationException::class)
private fun findConnectionForAdHocNotification(
project: SProject,
parameters: MutableMap<String, String>
Expand All @@ -387,7 +387,7 @@ class SlackNotifier(

if (connectionId != null) {
return oauthManager.findConnectionById(project, connectionId)
?: throw AdHocNotificationException("Could not resolve Slack connection by ID '$connectionId'")
?: throw ServiceMessageNotificationException("Could not resolve Slack connection by ID '$connectionId'")
}

val descriptors = oauthManager
Expand All @@ -399,10 +399,10 @@ class SlackNotifier(
return descriptors.first()
}
descriptors.isEmpty() -> {
throw AdHocNotificationException("Could not find any suitable Slack connection with service message notifications enabled")
throw ServiceMessageNotificationException("Could not find any suitable Slack connection with service message notifications enabled")
}
else -> {
throw AdHocNotificationException("More than one suitable Slack connection was found, please specify 'connectionId' argument to explicitly select connection")
throw ServiceMessageNotificationException("More than one suitable Slack connection was found, please specify 'connectionId' argument to explicitly select connection")
}
}
}
Expand All @@ -413,12 +413,12 @@ class SlackNotifier(
try {
return rawLimitValue.toInt()
} catch (e: NumberFormatException) {
throw AdHocNotificationException("Could not resolve notification limit from '$rawLimitValue' value", e)
throw ServiceMessageNotificationException("Could not resolve notification limit from '$rawLimitValue' value", e)
}
}

@Synchronized
@Throws(AdHocNotificationException::class)
@Throws(ServiceMessageNotificationException::class)
private fun checkAdHocNotificationLimit(
runningBuild: SRunningBuild,
connectionDescriptor: OAuthConnectionDescriptor
Expand All @@ -429,20 +429,20 @@ class SlackNotifier(

val currentCount = notificationCountHandler.getCounter(
buildPromotion,
adHocNotifierType
serviceMessageNotifierType
)

if (currentCount >= limit) {
throw AdHocNotificationException("Reached limit of $limit ad-hoc Slack notifications per build")
throw ServiceMessageNotificationException("Reached limit of $limit service message Slack notifications per build")
}

notificationCountHandler.incrementCounter(
buildPromotion,
adHocNotifierType
serviceMessageNotifierType
)
}

@Throws(AdHocNotificationException::class)
@Throws(ServiceMessageNotificationException::class)
private fun checkExternalDomainsInMessage(
message: String,
descriptor: OAuthConnectionDescriptor
Expand All @@ -458,7 +458,7 @@ class SlackNotifier(
)

if (externalDomains.isNotEmpty()) {
throw AdHocNotificationException(
throw ServiceMessageNotificationException(
"Found external domains that are not allowed by configuration: " +
"[${externalDomains.joinToString(" ,")}]"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
<td><label for="adHocAllowedDomainNames">Allowed hostnames:</label></td>
<td>
<props:textProperty name="adHocAllowedDomainNames"/>
<bs:smallNote>TeamCity notifications can display only those URLs which reference hostnames from this list. You can use asterisk (*) as a wildcard for any string (for example, *.test.co.uk).</bs:smallNote>
<bs:smallNote>For security reasons, only links to this TeamCity server are allowed in notifications. Notifications with URLs to external web resources are automatically blocked. This setting allows you to specify the list of trusted hostnames that can be referenced in notifications. Use the asterisk (*) as a wildcard for any string (for example, *.test.co.uk).</bs:smallNote>
</td>
</tr>
</l:settingsGroup>
Expand Down

0 comments on commit d698f3a

Please sign in to comment.