-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: Support appending to message attachments instead of replacing #74
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,29 +5,24 @@ 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.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 appendAttachments: Boolean, | ||
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 +40,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 +94,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 +121,54 @@ class SlackClient( | |
messageId: String? = null, | ||
previousAttachments: List<SlackMessage.Attachment>? = null, | ||
): SlackMessage { | ||
val attachments = mutableMapOf<JobType, SlackMessage.Attachment>() | ||
val attachments = if (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<JobType, SlackMessage.Attachment>() | ||
|
||
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, | ||
messageId = messageId, | ||
attachments = attachments.values.toList() | ||
attachments = attachments | ||
) | ||
} | ||
|
||
private suspend fun getSlackMessageById( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions are moved to SlackHttpClient. |
||
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 +178,7 @@ class SlackClient( | |
) | ||
|
||
@Serializable | ||
private data class MessageResponse( | ||
data class MessageResponse( | ||
@SerialName("ok") | ||
val ok: Boolean, // true | ||
@SerialName("messages") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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.util.JsonUtil | ||
import com.monta.slack.notifier.util.client | ||
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.* | ||
Check warning on line 14 in src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt GitHub Actions / detekt[detekt] src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt#L14 <detekt.NoWildcardImports>
Raw output
Check warning on line 14 in src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt GitHub Actions / detekt[detekt] src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt#L14 <detekt.WildcardImport>
Raw output
|
||
import io.ktor.utils.io.charsets.* | ||
Check warning on line 15 in src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt GitHub Actions / detekt[detekt] src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt#L15 <detekt.NoWildcardImports>
Raw output
Check warning on line 15 in src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt GitHub Actions / detekt[detekt] src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt#L15 <detekt.WildcardImport>
Raw output
|
||
|
||
open class SlackHttpClient( | ||
private val slackToken: String, | ||
private val slackChannelId: String, | ||
) { | ||
|
||
open suspend fun getSlackMessageById( | ||
messageId: String, | ||
): MessageResponse? { | ||
val response = client.get { | ||
header("Authorization", "Bearer ${slackToken}") | ||
Check warning on line 26 in src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt GitHub Actions / detekt[detekt] src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt#L26 <detekt.StringTemplate>
Raw output
|
||
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 | ||
} | ||
} | ||
|
||
open suspend fun makeSlackRequest(url: String, message: SlackMessage): Response? { | ||
val response = client.post(url) { | ||
header("Authorization", "Bearer ${slackToken}") | ||
Check warning on line 49 in src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt GitHub Actions / detekt[detekt] src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt#L49 <detekt.StringTemplate>
Raw output
|
||
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 | ||
} | ||
} | ||
|
||
Check warning on line 64 in src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt GitHub Actions / detekt[detekt] src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt#L64 <detekt.NoBlankLineBeforeRbrace>
Raw output
|
||
|
||
Check warning on line 65 in src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt GitHub Actions / detekt[detekt] src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt#L65 <detekt.NoConsecutiveBlankLines>
Raw output
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the primary change of the PR, adding support for appending attachments instead of replacing them, depending on the input property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would break the updating of status in the notifier?
Specifically where it goes from
In Progress
toSuccess
or am I wrong?