From 09810fb02729a8b3424c6dad4d16e2ff3d985bf2 Mon Sep 17 00:00:00 2001 From: Anders Nickelsen Date: Fri, 27 Sep 2024 15:39:38 +0200 Subject: [PATCH 1/4] Add support for appending to attachments instead of overwriting the status --- build.gradle.kts | 1 + .../com/monta/slack/notifier/SlackClient.kt | 120 +++++--------- .../monta/slack/notifier/SlackHttpClient.kt | 67 ++++++++ .../notifier/command/PublishSlackCommand.kt | 17 +- .../notifier/service/PublishSlackService.kt | 24 +-- .../service/PublishSlackServiceTest.kt | 154 ++++++++++++++++++ 6 files changed, 294 insertions(+), 89 deletions(-) create mode 100644 src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt create mode 100644 src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index b461071..6ec3bbd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -62,6 +62,7 @@ kotlin { implementation(kotlin("test-annotations-common")) implementation("io.kotest:kotest-framework-engine:$kotestVersion") implementation("io.kotest:kotest-assertions-core:$kotestVersion") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1") } } } diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt b/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt index 5540afb..67a9703 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt @@ -5,29 +5,22 @@ import com.monta.slack.notifier.model.JobStatus import com.monta.slack.notifier.model.JobType import com.monta.slack.notifier.model.SlackBlock import com.monta.slack.notifier.model.SlackMessage -import com.monta.slack.notifier.util.JsonUtil +import com.monta.slack.notifier.service.Input import com.monta.slack.notifier.util.buildTitle -import com.monta.slack.notifier.util.client -import io.ktor.client.request.* -import io.ktor.client.statement.* -import io.ktor.http.* -import io.ktor.utils.io.charsets.* +import kotlinx.datetime.LocalDateTime import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable class SlackClient( - private val serviceName: String?, - private val serviceEmoji: String?, - private val slackToken: String, - private val slackChannelId: String, + private val input: Input, + private val slackHttpClient: SlackHttpClient, ) { - suspend fun create( githubEvent: GithubEvent, jobType: JobType, jobStatus: JobStatus, ): String { - val response = makeSlackRequest( + val response = slackHttpClient.makeSlackRequest( url = "https://slack.com/api/chat.postMessage", message = generateMessageFromGithubEvent( githubEvent = githubEvent, @@ -45,9 +38,9 @@ class SlackClient( jobType: JobType, jobStatus: JobStatus, ): String { - val previousMessage = getSlackMessageById(messageId) + val previousMessage = slackHttpClient.getSlackMessageById(messageId) - val response = makeSlackRequest( + val response = slackHttpClient.makeSlackRequest( url = "https://slack.com/api/chat.update", message = generateMessageFromGithubEvent( githubEvent = githubEvent, @@ -99,7 +92,7 @@ class SlackClient( ), SlackBlock.Text( type = "mrkdwn", - text = " \n*Comitter:*\n${githubEvent.displayName}" + text = " \n*Committer:*\n${githubEvent.displayName}" ), SlackBlock.Text( type = "mrkdwn", @@ -126,81 +119,54 @@ class SlackClient( messageId: String? = null, previousAttachments: List? = null, ): SlackMessage { - val attachments = mutableMapOf() + val attachments = if (input.appendAttachments) { + previousAttachments.orEmpty() + SlackMessage.Attachment( + color = jobStatus.color, + fields = listOf( + SlackMessage.Attachment.Field( + title = jobType.label + " ($LocalDateTime)", + short = false, + value = jobStatus.message + ) + ) + ) + } else { + val attachments = mutableMapOf() - previousAttachments?.forEach { previousAttachment -> - if (previousAttachment.jobType == null) { - return@forEach + previousAttachments?.forEach { previousAttachment -> + if (previousAttachment.jobType == null) { + return@forEach + } + attachments[previousAttachment.jobType] = previousAttachment } - attachments[previousAttachment.jobType] = previousAttachment - } - attachments[jobType] = SlackMessage.Attachment( - color = jobStatus.color, - fields = listOf( - SlackMessage.Attachment.Field( - title = jobType.label, - short = false, - value = jobStatus.message + attachments[jobType] = SlackMessage.Attachment( + color = jobStatus.color, + fields = listOf( + SlackMessage.Attachment.Field( + title = jobType.label, + short = false, + value = jobStatus.message + ) ) ) - ) + + attachments.values.toList() + } return generateSlackMessageFromEvent( githubEvent = githubEvent, - serviceName = serviceName, - serviceEmoji = serviceEmoji, - slackChannelId = slackChannelId, + serviceName = input.serviceName, + serviceEmoji = input.serviceEmoji, + slackChannelId = input.slackChannelId, messageId = messageId, - attachments = attachments.values.toList() + attachments = attachments ) } - private suspend fun getSlackMessageById( - messageId: String, - ): MessageResponse? { - val response = client.get { - header("Authorization", "Bearer $slackToken") - url { - url("https://slack.com/api/conversations.history") - parameters.append("channel", slackChannelId) - parameters.append("oldest", messageId) - parameters.append("inclusive", "true") - parameters.append("limit", "1") - } - } - - val bodyString = response.bodyAsText() - - return if (response.status.value in 200..299) { - println("successfully got message bodyString=$bodyString") - JsonUtil.instance.decodeFromString(bodyString) - } else { - println("failed to get message $bodyString") - null - } - } - - private suspend fun makeSlackRequest(url: String, message: SlackMessage): Response? { - val response = client.post(url) { - header("Authorization", "Bearer $slackToken") - contentType(ContentType.Application.Json.withParameter("charset", Charsets.UTF_8.name)) - setBody(message) - } - - val bodyString = response.bodyAsText() - - return if (response.status.value in 200..299) { - println("successfully posted message bodyString=$bodyString") - JsonUtil.instance.decodeFromString(bodyString) - } else { - println("failed to post message $bodyString") - null - } - } @Serializable - private data class Response( + data class Response( @SerialName("ok") val ok: Boolean, // true @SerialName("channel") @@ -210,7 +176,7 @@ class SlackClient( ) @Serializable - private data class MessageResponse( + data class MessageResponse( @SerialName("ok") val ok: Boolean, // true @SerialName("messages") diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt b/src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt new file mode 100644 index 0000000..224f935 --- /dev/null +++ b/src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt @@ -0,0 +1,67 @@ +package com.monta.slack.notifier + +import com.monta.slack.notifier.SlackClient.MessageResponse +import com.monta.slack.notifier.SlackClient.Response +import com.monta.slack.notifier.model.SlackMessage +import com.monta.slack.notifier.service.Input +import com.monta.slack.notifier.util.JsonUtil +import com.monta.slack.notifier.util.client +import io.ktor.client.HttpClient +import io.ktor.client.request.get +import io.ktor.client.request.header +import io.ktor.client.request.post +import io.ktor.client.request.setBody +import io.ktor.client.request.url +import io.ktor.client.statement.bodyAsText +import io.ktor.http.* +import io.ktor.utils.io.charsets.* + +open class SlackHttpClient( + private val input: Input, +) { + + open suspend fun getSlackMessageById( + messageId: String, + ): MessageResponse? { + val response = client.get { + header("Authorization", "Bearer ${input.slackToken}") + url { + url("https://slack.com/api/conversations.history") + parameters.append("channel", input.slackChannelId) + parameters.append("oldest", messageId) + parameters.append("inclusive", "true") + parameters.append("limit", "1") + } + } + + val bodyString = response.bodyAsText() + + return if (response.status.value in 200..299) { + println("successfully got message bodyString=$bodyString") + JsonUtil.instance.decodeFromString(bodyString) + } else { + println("failed to get message $bodyString") + null + } + } + + open suspend fun makeSlackRequest(url: String, message: SlackMessage): Response? { + val response = client.post(url) { + header("Authorization", "Bearer ${input.slackToken}") + contentType(ContentType.Application.Json.withParameter("charset", Charsets.UTF_8.name)) + setBody(message) + } + + val bodyString = response.bodyAsText() + + return if (response.status.value in 200..299) { + println("successfully posted message bodyString=$bodyString") + JsonUtil.instance.decodeFromString(bodyString) + } else { + println("failed to post message $bodyString") + null + } + } + + +} diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt b/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt index 075d77c..e5ee93c 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt @@ -3,11 +3,14 @@ package com.monta.slack.notifier.command import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.options.required +import com.monta.slack.notifier.SlackHttpClient import com.monta.slack.notifier.model.GithubEvent import com.monta.slack.notifier.model.JobStatus import com.monta.slack.notifier.model.JobType import com.monta.slack.notifier.model.serializers.BaseGithubContext +import com.monta.slack.notifier.service.Input import com.monta.slack.notifier.service.PublishSlackService +import com.monta.slack.notifier.util.client import com.monta.slack.notifier.util.populateEventFromJson import com.monta.slack.notifier.util.readStringFromFile import kotlinx.coroutines.runBlocking @@ -73,14 +76,24 @@ class PublishSlackCommand : CliktCommand() { envvar = "SLACK_MESSAGE_ID" ) + private val appendStatusUpdates: String? by option( + help = "Append status updates or change to latest status", + envvar = "APPEND_STATUS_UPDATES" + ) + override fun run() { runBlocking { val githubEvent = getGithubEvent() - PublishSlackService( + val input = Input( serviceName = serviceName.valueOrNull(), serviceEmoji = serviceEmoji.valueOrNull(), slackToken = slackToken, - slackChannelId = slackChannelId + slackChannelId = slackChannelId, + appendAttachments = appendStatusUpdates.toBoolean() + ) + PublishSlackService( + input = input, + slackHttpClient = SlackHttpClient(input) ).publish( githubEvent = githubEvent, jobType = JobType.fromString(jobType), diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt b/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt index 0696d36..c13eaf1 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt @@ -1,23 +1,27 @@ package com.monta.slack.notifier.service import com.monta.slack.notifier.SlackClient +import com.monta.slack.notifier.SlackHttpClient import com.monta.slack.notifier.model.GithubEvent import com.monta.slack.notifier.model.JobStatus import com.monta.slack.notifier.model.JobType import com.monta.slack.notifier.util.writeToOutput +data class Input( + val serviceName: String?, + val serviceEmoji: String?, + val slackToken: String, + val slackChannelId: String, + val appendAttachments: Boolean = false, +) + class PublishSlackService( - serviceName: String?, - serviceEmoji: String?, - slackToken: String, - slackChannelId: String, + input: Input, + slackHttpClient: SlackHttpClient, ) { - private val slackClient = SlackClient( - serviceName = serviceName, - serviceEmoji = serviceEmoji, - slackToken = slackToken, - slackChannelId = slackChannelId + input = input, + slackHttpClient = slackHttpClient ) suspend fun publish( @@ -37,7 +41,7 @@ class PublishSlackService( messageId = slackMessageId, githubEvent = githubEvent, jobType = jobType, - jobStatus = jobStatus + jobStatus = jobStatus, ) } diff --git a/src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt b/src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt new file mode 100644 index 0000000..049f824 --- /dev/null +++ b/src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt @@ -0,0 +1,154 @@ +package com.monta.slack.notifier.service + +import com.monta.slack.notifier.SlackClient.MessageResponse +import com.monta.slack.notifier.SlackClient.Response +import com.monta.slack.notifier.SlackHttpClient +import com.monta.slack.notifier.model.GithubEvent +import com.monta.slack.notifier.model.JobStatus +import com.monta.slack.notifier.model.JobType +import com.monta.slack.notifier.model.SlackMessage +import io.kotest.matchers.shouldBe +import kotlinx.coroutines.test.runTest +import kotlin.test.Test + +class TestSlackHttpClient(input: Input) : SlackHttpClient(input) { + + val sentMessages = mutableListOf() + + override suspend fun getSlackMessageById( + messageId: String, + ): MessageResponse? { + return MessageResponse(true, sentMessages.toList()) + } + + override suspend fun makeSlackRequest(url: String, message: SlackMessage): Response? { + sentMessages.add(message) + return Response(true, "channel", "00:00:00") + } + + fun listMessages(): List { + return sentMessages.toList() + } +} + +class PublishSlackServiceTest { + @Test + fun test_replacing_attachments() { + val input = Input( + serviceName = "gineau pig", + serviceEmoji = "🐷", + slackToken = "token", + slackChannelId = "#anni-test", + appendAttachments = false + ) + val testSlackHttpClient = TestSlackHttpClient(input) + val service = PublishSlackService( + input = input, + testSlackHttpClient + ) + runTest { + // given one message is published + service.publish( + githubEvent = GithubEvent( + "repository", + "master", + "1", + "nickelsen", + "a1b2c3d4", + "message", + "workflow", + "url" + ), + jobType = JobType.Test, + jobStatus = JobStatus.Progress, + slackMessageId = null + ) + val slackMessagesBefore = testSlackHttpClient.listMessages() + slackMessagesBefore.size shouldBe 1 + slackMessagesBefore.first().attachments?.size shouldBe 1 + + // when a second message is published + service.publish( + githubEvent = GithubEvent( + "repository", + "master", + "1", + "nickelsen", + "a1b2c3d4", + "message", + "workflow", + "url" + ), + jobType = JobType.Test, + jobStatus = JobStatus.Success, + slackMessageId = "00:00:00" + ) + + // then the attachment of the second message is replaced + val slackMessagesAfter = testSlackHttpClient.listMessages() + slackMessagesAfter.size shouldBe 2 + slackMessagesAfter.last().attachments?.size shouldBe 1 + + } + + } + + @Test + fun test_appending_attachments() { + val input = Input( + serviceName = "gineau pig", + serviceEmoji = "🐷", + slackToken = "token", + slackChannelId = "#anni-test", + appendAttachments = true + ) + val testSlackHttpClient = TestSlackHttpClient(input) + val service = PublishSlackService( + input = input, + testSlackHttpClient + ) + runTest { + // given one message is published + service.publish( + githubEvent = GithubEvent( + "repository", + "master", + "1", + "nickelsen", + "a1b2c3d4", + "message", + "workflow", + "url" + ), + jobType = JobType.Test, + jobStatus = JobStatus.Progress, + slackMessageId = null + ) + val slackMessagesBefore = testSlackHttpClient.listMessages() + slackMessagesBefore.size shouldBe 1 + slackMessagesBefore.first().attachments?.size shouldBe 1 + + // when a second message is published + service.publish( + githubEvent = GithubEvent( + "repository", + "master", + "1", + "nickelsen", + "a1b2c3d4", + "message", + "workflow", + "url" + ), + jobType = JobType.Test, + jobStatus = JobStatus.Success, + slackMessageId = "00:00:00" + ) + + // then an additional attachment is added to the second message + val slackMessagesAfter = testSlackHttpClient.listMessages() + slackMessagesAfter.size shouldBe 2 + slackMessagesAfter.last().attachments?.size shouldBe 2 + } + } +} From 11a936fc7a08e4865fbd02e84dac682d1406410d Mon Sep 17 00:00:00 2001 From: Anders Nickelsen Date: Mon, 30 Sep 2024 12:41:17 +0200 Subject: [PATCH 2/4] Remove Input --- .../com/monta/slack/notifier/SlackClient.kt | 14 ++++---- .../monta/slack/notifier/SlackHttpClient.kt | 11 +++--- .../notifier/command/PublishSlackCommand.kt | 12 ++----- .../notifier/service/PublishSlackService.kt | 20 +++++------ .../service/PublishSlackServiceTest.kt | 34 +++++++------------ 5 files changed, 38 insertions(+), 53 deletions(-) diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt b/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt index 67a9703..8c522d7 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt @@ -5,14 +5,16 @@ import com.monta.slack.notifier.model.JobStatus import com.monta.slack.notifier.model.JobType import com.monta.slack.notifier.model.SlackBlock import com.monta.slack.notifier.model.SlackMessage -import com.monta.slack.notifier.service.Input import com.monta.slack.notifier.util.buildTitle import kotlinx.datetime.LocalDateTime import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable class SlackClient( - private val input: Input, + private val serviceName: String?, + private val serviceEmoji: String?, + private val slackChannelId: String, + private val appendAttachments: Boolean, private val slackHttpClient: SlackHttpClient, ) { suspend fun create( @@ -119,7 +121,7 @@ class SlackClient( messageId: String? = null, previousAttachments: List? = null, ): SlackMessage { - val attachments = if (input.appendAttachments) { + val attachments = if (appendAttachments) { previousAttachments.orEmpty() + SlackMessage.Attachment( color = jobStatus.color, fields = listOf( @@ -156,9 +158,9 @@ class SlackClient( return generateSlackMessageFromEvent( githubEvent = githubEvent, - serviceName = input.serviceName, - serviceEmoji = input.serviceEmoji, - slackChannelId = input.slackChannelId, + serviceName = serviceName, + serviceEmoji = serviceEmoji, + slackChannelId = slackChannelId, messageId = messageId, attachments = attachments ) diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt b/src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt index 224f935..5fa36be 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt @@ -3,10 +3,8 @@ package com.monta.slack.notifier import com.monta.slack.notifier.SlackClient.MessageResponse import com.monta.slack.notifier.SlackClient.Response import com.monta.slack.notifier.model.SlackMessage -import com.monta.slack.notifier.service.Input import com.monta.slack.notifier.util.JsonUtil import com.monta.slack.notifier.util.client -import io.ktor.client.HttpClient import io.ktor.client.request.get import io.ktor.client.request.header import io.ktor.client.request.post @@ -17,17 +15,18 @@ import io.ktor.http.* import io.ktor.utils.io.charsets.* open class SlackHttpClient( - private val input: Input, + private val slackToken: String, + private val slackChannelId: String, ) { open suspend fun getSlackMessageById( messageId: String, ): MessageResponse? { val response = client.get { - header("Authorization", "Bearer ${input.slackToken}") + header("Authorization", "Bearer ${slackToken}") url { url("https://slack.com/api/conversations.history") - parameters.append("channel", input.slackChannelId) + parameters.append("channel", slackChannelId) parameters.append("oldest", messageId) parameters.append("inclusive", "true") parameters.append("limit", "1") @@ -47,7 +46,7 @@ open class SlackHttpClient( open suspend fun makeSlackRequest(url: String, message: SlackMessage): Response? { val response = client.post(url) { - header("Authorization", "Bearer ${input.slackToken}") + header("Authorization", "Bearer ${slackToken}") contentType(ContentType.Application.Json.withParameter("charset", Charsets.UTF_8.name)) setBody(message) } diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt b/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt index e5ee93c..0695e4a 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt @@ -8,9 +8,7 @@ import com.monta.slack.notifier.model.GithubEvent import com.monta.slack.notifier.model.JobStatus import com.monta.slack.notifier.model.JobType import com.monta.slack.notifier.model.serializers.BaseGithubContext -import com.monta.slack.notifier.service.Input import com.monta.slack.notifier.service.PublishSlackService -import com.monta.slack.notifier.util.client import com.monta.slack.notifier.util.populateEventFromJson import com.monta.slack.notifier.util.readStringFromFile import kotlinx.coroutines.runBlocking @@ -84,16 +82,12 @@ class PublishSlackCommand : CliktCommand() { override fun run() { runBlocking { val githubEvent = getGithubEvent() - val input = Input( + PublishSlackService( serviceName = serviceName.valueOrNull(), serviceEmoji = serviceEmoji.valueOrNull(), - slackToken = slackToken, slackChannelId = slackChannelId, - appendAttachments = appendStatusUpdates.toBoolean() - ) - PublishSlackService( - input = input, - slackHttpClient = SlackHttpClient(input) + appendAttachments = appendStatusUpdates.toBoolean(), + slackHttpClient = SlackHttpClient(slackToken, slackChannelId) ).publish( githubEvent = githubEvent, jobType = JobType.fromString(jobType), diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt b/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt index c13eaf1..52e09ba 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt @@ -7,20 +7,18 @@ import com.monta.slack.notifier.model.JobStatus import com.monta.slack.notifier.model.JobType import com.monta.slack.notifier.util.writeToOutput -data class Input( - val serviceName: String?, - val serviceEmoji: String?, - val slackToken: String, - val slackChannelId: String, - val appendAttachments: Boolean = false, -) - class PublishSlackService( - input: Input, - slackHttpClient: SlackHttpClient, + private val serviceName: String?, + private val serviceEmoji: String?, + private val slackChannelId: String, + private val appendAttachments: Boolean = false, + private val slackHttpClient: SlackHttpClient, ) { private val slackClient = SlackClient( - input = input, + serviceName = serviceName, + serviceEmoji = serviceEmoji, + slackChannelId = slackChannelId, + appendAttachments = appendAttachments, slackHttpClient = slackHttpClient ) diff --git a/src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt b/src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt index 049f824..947645d 100644 --- a/src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt +++ b/src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt @@ -11,7 +11,7 @@ import io.kotest.matchers.shouldBe import kotlinx.coroutines.test.runTest import kotlin.test.Test -class TestSlackHttpClient(input: Input) : SlackHttpClient(input) { +class TestSlackHttpClient(slackToken: String, slackChannelId: String) : SlackHttpClient(slackToken, slackChannelId) { val sentMessages = mutableListOf() @@ -34,16 +34,12 @@ class TestSlackHttpClient(input: Input) : SlackHttpClient(input) { class PublishSlackServiceTest { @Test fun test_replacing_attachments() { - val input = Input( + val testSlackHttpClient = TestSlackHttpClient("token", "#monta") + val service = PublishSlackService( serviceName = "gineau pig", serviceEmoji = "🐷", - slackToken = "token", - slackChannelId = "#anni-test", - appendAttachments = false - ) - val testSlackHttpClient = TestSlackHttpClient(input) - val service = PublishSlackService( - input = input, + slackChannelId = "#monta", + appendAttachments = false, testSlackHttpClient ) runTest { @@ -53,7 +49,7 @@ class PublishSlackServiceTest { "repository", "master", "1", - "nickelsen", + "monta", "a1b2c3d4", "message", "workflow", @@ -73,7 +69,7 @@ class PublishSlackServiceTest { "repository", "master", "1", - "nickelsen", + "monta", "a1b2c3d4", "message", "workflow", @@ -95,16 +91,12 @@ class PublishSlackServiceTest { @Test fun test_appending_attachments() { - val input = Input( + val testSlackHttpClient = TestSlackHttpClient("token", "#monta") + val service = PublishSlackService( serviceName = "gineau pig", serviceEmoji = "🐷", - slackToken = "token", - slackChannelId = "#anni-test", - appendAttachments = true - ) - val testSlackHttpClient = TestSlackHttpClient(input) - val service = PublishSlackService( - input = input, + slackChannelId = "#monta", + appendAttachments = true, testSlackHttpClient ) runTest { @@ -114,7 +106,7 @@ class PublishSlackServiceTest { "repository", "master", "1", - "nickelsen", + "monta", "a1b2c3d4", "message", "workflow", @@ -134,7 +126,7 @@ class PublishSlackServiceTest { "repository", "master", "1", - "nickelsen", + "monta", "a1b2c3d4", "message", "workflow", From ebbc8a6260a1ad8713dafbaa4e2d053e653ebd6f Mon Sep 17 00:00:00 2001 From: Anders Nickelsen Date: Mon, 30 Sep 2024 13:38:06 +0200 Subject: [PATCH 3/4] Fix detekt --- .../kotlin/com/monta/slack/notifier/SlackClient.kt | 1 - .../com/monta/slack/notifier/SlackHttpClient.kt | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt b/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt index 8c522d7..7ed0a61 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt @@ -166,7 +166,6 @@ class SlackClient( ) } - @Serializable data class Response( @SerialName("ok") diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt b/src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt index 5fa36be..e40fb47 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt @@ -11,8 +11,10 @@ import io.ktor.client.request.post import io.ktor.client.request.setBody import io.ktor.client.request.url import io.ktor.client.statement.bodyAsText -import io.ktor.http.* -import io.ktor.utils.io.charsets.* +import io.ktor.http.ContentType +import io.ktor.http.contentType +import io.ktor.utils.io.charsets.Charsets +import io.ktor.utils.io.charsets.name open class SlackHttpClient( private val slackToken: String, @@ -23,7 +25,7 @@ open class SlackHttpClient( messageId: String, ): MessageResponse? { val response = client.get { - header("Authorization", "Bearer ${slackToken}") + header("Authorization", "Bearer $slackToken") url { url("https://slack.com/api/conversations.history") parameters.append("channel", slackChannelId) @@ -46,7 +48,7 @@ open class SlackHttpClient( open suspend fun makeSlackRequest(url: String, message: SlackMessage): Response? { val response = client.post(url) { - header("Authorization", "Bearer ${slackToken}") + header("Authorization", "Bearer $slackToken") contentType(ContentType.Application.Json.withParameter("charset", Charsets.UTF_8.name)) setBody(message) } @@ -61,6 +63,4 @@ open class SlackHttpClient( null } } - - } From 60e78b945163394a3c82ba49c7b9016b546e3966 Mon Sep 17 00:00:00 2001 From: Anders Nickelsen Date: Mon, 30 Sep 2024 13:38:41 +0200 Subject: [PATCH 4/4] More fix detekt --- .../com/monta/slack/notifier/service/PublishSlackServiceTest.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt b/src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt index 947645d..86d6616 100644 --- a/src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt +++ b/src/commonTest/kotlin/com/monta/slack/notifier/service/PublishSlackServiceTest.kt @@ -84,9 +84,7 @@ class PublishSlackServiceTest { val slackMessagesAfter = testSlackHttpClient.listMessages() slackMessagesAfter.size shouldBe 2 slackMessagesAfter.last().attachments?.size shouldBe 1 - } - } @Test