Skip to content

Commit c575959

Browse files
authored
feat(everything): Refactoring the way events are read to enable multiple event types (#64)
* feat(everything): Refactoring the way events are read to enable multiple event types * Change exception codes and kt lint * Delete event.yaml * Remove annotations * Fixed detekt warning
1 parent 98a307c commit c575959

12 files changed

+312
-240
lines changed

src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt

+71-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.monta.slack.notifier
22

3-
import com.monta.slack.notifier.model.GithubPushContext
3+
import com.monta.slack.notifier.model.GithubEvent
44
import com.monta.slack.notifier.model.JobStatus
55
import com.monta.slack.notifier.model.JobType
6+
import com.monta.slack.notifier.model.SlackBlock
67
import com.monta.slack.notifier.model.SlackMessage
78
import com.monta.slack.notifier.util.JsonUtil
9+
import com.monta.slack.notifier.util.buildTitle
810
import com.monta.slack.notifier.util.client
911
import io.ktor.client.request.*
1012
import io.ktor.client.statement.*
@@ -21,14 +23,14 @@ class SlackClient(
2123
) {
2224

2325
suspend fun create(
24-
githubPushContext: GithubPushContext,
26+
githubEvent: GithubEvent,
2527
jobType: JobType,
2628
jobStatus: JobStatus,
2729
): String {
2830
val response = makeSlackRequest(
2931
url = "https://slack.com/api/chat.postMessage",
30-
message = generateMessage(
31-
githubPushContext = githubPushContext,
32+
message = generateMessageFromGithubEvent(
33+
githubEvent = githubEvent,
3234
jobType = jobType,
3335
jobStatus = jobStatus
3436
)
@@ -39,16 +41,16 @@ class SlackClient(
3941

4042
suspend fun update(
4143
messageId: String,
42-
githubPushContext: GithubPushContext,
44+
githubEvent: GithubEvent,
4345
jobType: JobType,
4446
jobStatus: JobStatus,
4547
): String {
4648
val previousMessage = getSlackMessageById(messageId)
4749

4850
val response = makeSlackRequest(
4951
url = "https://slack.com/api/chat.update",
50-
message = generateMessage(
51-
githubPushContext = githubPushContext,
52+
message = generateMessageFromGithubEvent(
53+
githubEvent = githubEvent,
5254
jobType = jobType,
5355
jobStatus = jobStatus,
5456
messageId = messageId,
@@ -59,8 +61,66 @@ class SlackClient(
5961
return requireNotNull(response?.ts)
6062
}
6163

62-
private fun generateMessage(
63-
githubPushContext: GithubPushContext,
64+
private fun generateSlackMessageFromEvent(
65+
githubEvent: GithubEvent,
66+
serviceName: String?,
67+
serviceEmoji: String?,
68+
slackChannelId: String,
69+
messageId: String?,
70+
attachments: List<SlackMessage.Attachment>?,
71+
): SlackMessage {
72+
val title = buildTitle(githubEvent.repository, githubEvent.workflow, serviceName, serviceEmoji)
73+
74+
return SlackMessage(
75+
channel = slackChannelId,
76+
ts = messageId,
77+
text = title,
78+
blocks = listOf(
79+
SlackBlock(
80+
type = "header",
81+
text = SlackBlock.Text(
82+
type = "plain_text",
83+
text = title
84+
)
85+
),
86+
SlackBlock(
87+
type = "divider"
88+
),
89+
SlackBlock(
90+
type = "section",
91+
fields = listOf(
92+
SlackBlock.Text(
93+
type = "mrkdwn",
94+
text = " \n*Branch:*\n${githubEvent.refName}"
95+
),
96+
SlackBlock.Text(
97+
type = "mrkdwn",
98+
text = " \n*Run:*\n<${githubEvent.getRunUrl()}|${githubEvent.runId}>"
99+
),
100+
SlackBlock.Text(
101+
type = "mrkdwn",
102+
text = " \n*Comitter:*\n${githubEvent.displayName}"
103+
),
104+
SlackBlock.Text(
105+
type = "mrkdwn",
106+
text = " \n*Message:*\n<${githubEvent.getCommitUrl()}|${githubEvent.getCommitMessage()}>"
107+
),
108+
SlackBlock.Text(
109+
type = "mrkdwn",
110+
text = " \n*SHA:*\n<${githubEvent.getCommitUrl()}|${githubEvent.commitSHA}>"
111+
)
112+
)
113+
),
114+
SlackBlock(
115+
type = "divider"
116+
)
117+
),
118+
attachments = attachments
119+
)
120+
}
121+
122+
private fun generateMessageFromGithubEvent(
123+
githubEvent: GithubEvent,
64124
jobType: JobType,
65125
jobStatus: JobStatus,
66126
messageId: String? = null,
@@ -86,7 +146,8 @@ class SlackClient(
86146
)
87147
)
88148

89-
return githubPushContext.toMessage(
149+
return generateSlackMessageFromEvent(
150+
githubEvent = githubEvent,
90151
serviceName = serviceName,
91152
serviceEmoji = serviceEmoji,
92153
slackChannelId = slackChannelId,

src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt

+18-16
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package com.monta.slack.notifier.command
33
import com.github.ajalt.clikt.core.CliktCommand
44
import com.github.ajalt.clikt.parameters.options.option
55
import com.github.ajalt.clikt.parameters.options.required
6-
import com.monta.slack.notifier.model.GithubPushContext
6+
import com.monta.slack.notifier.model.GithubEvent
77
import com.monta.slack.notifier.model.JobStatus
88
import com.monta.slack.notifier.model.JobType
9+
import com.monta.slack.notifier.model.serializers.BaseGithubContext
910
import com.monta.slack.notifier.service.PublishSlackService
10-
import com.monta.slack.notifier.util.JsonUtil
11-
import com.monta.slack.notifier.util.populateEventFromTrunkBasedEvent
11+
import com.monta.slack.notifier.util.populateEventFromJson
1212
import com.monta.slack.notifier.util.readStringFromFile
1313
import kotlinx.coroutines.runBlocking
1414

@@ -75,36 +75,38 @@ class PublishSlackCommand : CliktCommand() {
7575

7676
override fun run() {
7777
runBlocking {
78-
val githubPushContext = getGithubPushContext()
78+
val githubEvent = getGithubEvent()
7979
PublishSlackService(
8080
serviceName = serviceName.valueOrNull(),
8181
serviceEmoji = serviceEmoji.valueOrNull(),
8282
slackToken = slackToken,
8383
slackChannelId = slackChannelId
8484
).publish(
85-
githubPushContext = githubPushContext,
85+
githubEvent = githubEvent,
8686
jobType = JobType.fromString(jobType),
8787
jobStatus = JobStatus.fromString(jobStatus),
8888
slackMessageId = slackMessageId.valueOrNull()
8989
)
9090
}
9191
}
9292

93-
private fun getGithubPushContext(): GithubPushContext {
93+
private fun getGithubEvent(): GithubEvent {
94+
val baseGithubContext: BaseGithubContext
95+
9496
val eventJson = readStringFromFile(githubEventPath)
95-
var event = JsonUtil.instance.decodeFromString<GithubPushContext.Event>(eventJson)
9697

97-
// In builds from trunk based workflows, the json event is different
98-
// We use this hack to populate the original event with new info
99-
if (event.headCommit == null) {
100-
event = populateEventFromTrunkBasedEvent(eventJson, event)
101-
}
102-
return GithubPushContext(
98+
// The Github events can take many shapes, therefore we
99+
// sort them and transfer them into a simpler object
100+
baseGithubContext = populateEventFromJson(eventJson)
101+
102+
return GithubEvent(
103103
repository = githubRepository,
104+
refName = githubRefName,
104105
runId = githubRunId,
105-
workflow = githubWorkflow,
106-
event = event,
107-
refName = githubRefName
106+
displayName = baseGithubContext.displayName,
107+
commitSHA = baseGithubContext.sha,
108+
commitMessage = baseGithubContext.message,
109+
workflow = githubWorkflow
108110
)
109111
}
110112

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.monta.slack.notifier.model
2+
3+
class GithubEvent(
4+
val repository: String,
5+
val refName: String,
6+
var runId: String,
7+
val displayName: String?,
8+
val commitSHA: String?,
9+
val commitMessage: String?,
10+
val workflow: String?,
11+
) {
12+
fun getRunUrl(): String {
13+
return "https://github.com/$repository/actions/runs/$runId"
14+
}
15+
fun getCommitUrl(): String {
16+
return "https://github.com/$repository/commit/$commitSHA"
17+
}
18+
fun getCommitMessage(): String? {
19+
return commitMessage
20+
?.replace("\n", " ")
21+
?.replace("\r", " ")
22+
?.take(120)
23+
}
24+
}

src/commonMain/kotlin/com/monta/slack/notifier/model/GithubPushContext.kt

-149
This file was deleted.

0 commit comments

Comments
 (0)