Skip to content

Commit

Permalink
feat(cli): Attempt at patching the existing event with a new file rea…
Browse files Browse the repository at this point in the history
…der to support trunk based flows (#63)

* Attempt at patching the existing event with a new file reader

* Remove labels

* Remove unused import

* linting
  • Loading branch information
baldm authored Apr 26, 2024
1 parent 95ae39c commit 98a307c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.monta.slack.notifier.model.JobStatus
import com.monta.slack.notifier.model.JobType
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.readStringFromFile
import kotlinx.coroutines.runBlocking

Expand Down Expand Up @@ -91,7 +92,13 @@ class PublishSlackCommand : CliktCommand() {

private fun getGithubPushContext(): GithubPushContext {
val eventJson = readStringFromFile(githubEventPath)
val event = JsonUtil.instance.decodeFromString<GithubPushContext.Event>(eventJson)
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(
repository = githubRepository,
runId = githubRunId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ data class GithubPushContext(
@Serializable
data class Event(
@SerialName("head_commit")
val headCommit: Commit? = null,
var headCommit: Commit? = null,
@SerialName("pusher")
val pusher: Committer? = null,
@SerialName("ref")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.monta.slack.notifier.model

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class GithubTrunkBasedPushContext(
@SerialName("run_id")
val runId: String? = null, // 4. Fourth var, Name of run (should be )
) {
// In this class we grab variables from a trunk based event flow
// Where each thing we need to populate the slack message are listed
@Serializable
data class PullRequestHead(
@SerialName("ref") val ref: String,
)

@Serializable
data class PullRequest(
@SerialName("head") val head: PullRequestHead,
@SerialName("title") val title: String,
@SerialName("user") val user: PullRequestUser,
)

@Serializable
data class PullRequestUser(
@SerialName("login") val login: String,
)

@Serializable
data class Repository(
@SerialName("full_name") val fullName: String,
)

@Serializable
data class Event(
@SerialName("after") val sha: String,
@SerialName("pull_request") val pullRequest: PullRequest,
@SerialName("repository") val repository: Repository,
)
}
20 changes: 20 additions & 0 deletions src/commonMain/kotlin/com/monta/slack/notifier/util/TrunkUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.monta.slack.notifier.util

import com.monta.slack.notifier.model.GithubPushContext
import com.monta.slack.notifier.model.GithubTrunkBasedPushContext

/**
* Populates the existing event type with information needed to generate
* an entire Slack notification. This is super hacky but an easy solution
* without deintegrating GithubPushContext from the rest of the code.
*/
fun populateEventFromTrunkBasedEvent(eventJson: String, event: GithubPushContext.Event): GithubPushContext.Event {
val trunkBasedEvent = JsonUtil.instance.decodeFromString<GithubTrunkBasedPushContext.Event>(eventJson)
event.headCommit = GithubPushContext.Commit(
committer = GithubPushContext.Committer(name = trunkBasedEvent.pullRequest.user.login),
id = trunkBasedEvent.sha,
url = "https://github.com/${trunkBasedEvent.repository.fullName}/compare/${trunkBasedEvent.pullRequest.head.ref}",
message = trunkBasedEvent.pullRequest.title
)
return event
}

0 comments on commit 98a307c

Please sign in to comment.