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

feat(everything): Refactoring the way events are read to enable multiple event types #64

Merged
merged 5 commits into from
May 3, 2024
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
81 changes: 71 additions & 10 deletions src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.monta.slack.notifier

import com.monta.slack.notifier.model.GithubPushContext
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.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.*
Expand All @@ -21,14 +23,14 @@ class SlackClient(
) {

suspend fun create(
githubPushContext: GithubPushContext,
githubEvent: GithubEvent,
jobType: JobType,
jobStatus: JobStatus,
): String {
val response = makeSlackRequest(
url = "https://slack.com/api/chat.postMessage",
message = generateMessage(
githubPushContext = githubPushContext,
message = generateMessageFromGithubEvent(
githubEvent = githubEvent,
jobType = jobType,
jobStatus = jobStatus
)
Expand All @@ -39,16 +41,16 @@ class SlackClient(

suspend fun update(
messageId: String,
githubPushContext: GithubPushContext,
githubEvent: GithubEvent,
jobType: JobType,
jobStatus: JobStatus,
): String {
val previousMessage = getSlackMessageById(messageId)

val response = makeSlackRequest(
url = "https://slack.com/api/chat.update",
message = generateMessage(
githubPushContext = githubPushContext,
message = generateMessageFromGithubEvent(
githubEvent = githubEvent,
jobType = jobType,
jobStatus = jobStatus,
messageId = messageId,
Expand All @@ -59,8 +61,66 @@ class SlackClient(
return requireNotNull(response?.ts)
}

private fun generateMessage(
githubPushContext: GithubPushContext,
private fun generateSlackMessageFromEvent(
githubEvent: GithubEvent,
serviceName: String?,
serviceEmoji: String?,
slackChannelId: String,
messageId: String?,
attachments: List<SlackMessage.Attachment>?,
): SlackMessage {
val title = buildTitle(githubEvent.repository, githubEvent.workflow, serviceName, serviceEmoji)

return SlackMessage(
channel = slackChannelId,
ts = messageId,
text = title,
blocks = listOf(
SlackBlock(
type = "header",
text = SlackBlock.Text(
type = "plain_text",
text = title
)
),
SlackBlock(
type = "divider"
),
SlackBlock(
type = "section",
fields = listOf(
SlackBlock.Text(
type = "mrkdwn",
text = " \n*Branch:*\n${githubEvent.refName}"
),
SlackBlock.Text(
type = "mrkdwn",
text = " \n*Run:*\n<${githubEvent.getRunUrl()}|${githubEvent.runId}>"
),
SlackBlock.Text(
type = "mrkdwn",
text = " \n*Comitter:*\n${githubEvent.displayName}"
),
SlackBlock.Text(
type = "mrkdwn",
text = " \n*Message:*\n<${githubEvent.getCommitUrl()}|${githubEvent.getCommitMessage()}>"
),
SlackBlock.Text(
type = "mrkdwn",
text = " \n*SHA:*\n<${githubEvent.getCommitUrl()}|${githubEvent.commitSHA}>"
)
)
),
SlackBlock(
type = "divider"
)
),
attachments = attachments
)
}

private fun generateMessageFromGithubEvent(
githubEvent: GithubEvent,
jobType: JobType,
jobStatus: JobStatus,
messageId: String? = null,
Expand All @@ -86,7 +146,8 @@ class SlackClient(
)
)

return githubPushContext.toMessage(
return generateSlackMessageFromEvent(
githubEvent = githubEvent,
serviceName = serviceName,
serviceEmoji = serviceEmoji,
slackChannelId = slackChannelId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ 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.model.GithubPushContext
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.PublishSlackService
import com.monta.slack.notifier.util.JsonUtil
import com.monta.slack.notifier.util.populateEventFromTrunkBasedEvent
import com.monta.slack.notifier.util.populateEventFromJson
import com.monta.slack.notifier.util.readStringFromFile
import kotlinx.coroutines.runBlocking

Expand Down Expand Up @@ -75,36 +75,38 @@ class PublishSlackCommand : CliktCommand() {

override fun run() {
runBlocking {
val githubPushContext = getGithubPushContext()
val githubEvent = getGithubEvent()
PublishSlackService(
serviceName = serviceName.valueOrNull(),
serviceEmoji = serviceEmoji.valueOrNull(),
slackToken = slackToken,
slackChannelId = slackChannelId
).publish(
githubPushContext = githubPushContext,
githubEvent = githubEvent,
jobType = JobType.fromString(jobType),
jobStatus = JobStatus.fromString(jobStatus),
slackMessageId = slackMessageId.valueOrNull()
)
}
}

private fun getGithubPushContext(): GithubPushContext {
private fun getGithubEvent(): GithubEvent {
val baseGithubContext: BaseGithubContext

val eventJson = readStringFromFile(githubEventPath)
var event = JsonUtil.instance.decodeFromString<GithubPushContext.Event>(eventJson)

// In builds from trunk based workflows, the json event is different
// We use this hack to populate the original event with new info
if (event.headCommit == null) {
event = populateEventFromTrunkBasedEvent(eventJson, event)
}
return GithubPushContext(
// The Github events can take many shapes, therefore we
// sort them and transfer them into a simpler object
baseGithubContext = populateEventFromJson(eventJson)

return GithubEvent(
repository = githubRepository,
refName = githubRefName,
runId = githubRunId,
workflow = githubWorkflow,
event = event,
refName = githubRefName
displayName = baseGithubContext.displayName,
commitSHA = baseGithubContext.sha,
commitMessage = baseGithubContext.message,
workflow = githubWorkflow
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.monta.slack.notifier.model

class GithubEvent(
val repository: String,
val refName: String,
var runId: String,
val displayName: String?,
val commitSHA: String?,
val commitMessage: String?,
val workflow: String?,
) {
fun getRunUrl(): String {
return "https://github.com/$repository/actions/runs/$runId"
}
fun getCommitUrl(): String {
return "https://github.com/$repository/commit/$commitSHA"
}
fun getCommitMessage(): String? {
return commitMessage
?.replace("\n", " ")
?.replace("\r", " ")
?.take(120)
}
}

This file was deleted.

Loading
Loading