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(cli): Attempt at patching the existing event with a new file reader to support trunk based flows #63

Merged
merged 4 commits into from
Apr 26, 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
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
}
Loading