-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,53 @@ 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 +177,7 @@ class SlackClient( | |
) | ||
|
||
@Serializable | ||
private data class MessageResponse( | ||
data class MessageResponse( | ||
@SerialName("ok") | ||
val ok: Boolean, // true | ||
@SerialName("messages") | ||
|
66 changes: 66 additions & 0 deletions
66
src/commonMain/kotlin/com/monta/slack/notifier/SlackHttpClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.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, | ||
private val slackChannelId: String, | ||
) { | ||
|
||
open 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 | ||
} | ||
} | ||
|
||
open 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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?