diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt b/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt index c900b06..20d263e 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt @@ -3,16 +3,38 @@ 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.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.readStringFromFile import kotlinx.coroutines.runBlocking class PublishSlackCommand : CliktCommand() { + val githubEventPath: String by option( + help = "Github Context event json file path", + envvar = "GITHUB_EVENT_PATH" + ).required() + + val githubRepository: String by option( + help = "Github Context repository", + envvar = "GITHUB_REPOSITORY" + ).required() + + val githubRunId: String by option( + help = "Github Context run id", + envvar = "GITHUB_RUN_ID" + ).required() - private val githubContext: String by option( - help = "Github Context", - envvar = "PUBLISH_SLACK_GITHUB_CONTEXT" + val githubWorkflow: String by option( + help = "Github Context workflow", + envvar = "GITHUB_WORKFLOW" + ).required() + + val githubRefName: String by option( + help = "Github Context ref name", + envvar = "GITHUB_REF_NAME" ).required() private val serviceName: String? by option( @@ -52,13 +74,14 @@ class PublishSlackCommand : CliktCommand() { override fun run() { runBlocking { + val githubPushContext = getGithubPushContext() PublishSlackService( serviceName = serviceName.valueOrNull(), serviceEmoji = serviceEmoji.valueOrNull(), slackToken = slackToken, slackChannelId = slackChannelId ).publish( - githubContext = githubContext, + githubPushContext = githubPushContext, jobType = JobType.fromString(jobType), jobStatus = JobStatus.fromString(jobStatus), slackMessageId = slackMessageId.valueOrNull() @@ -66,6 +89,18 @@ class PublishSlackCommand : CliktCommand() { } } + private fun getGithubPushContext(): GithubPushContext { + val eventJson = readStringFromFile(githubEventPath) + val event = JsonUtil.instance.decodeFromString(eventJson) + return GithubPushContext( + repository = githubRepository, + runId = githubRunId, + workflow = githubWorkflow, + event = event, + refName = githubRefName + ) + } + /** * Needed for optional parameters as the return the empty string instead of null * if set via ENV variables (as we do from our GitHub Actions) diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt b/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt index ac725a9..51510e0 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/service/PublishSlackService.kt @@ -4,7 +4,6 @@ import com.monta.slack.notifier.SlackClient import com.monta.slack.notifier.model.GithubPushContext import com.monta.slack.notifier.model.JobStatus import com.monta.slack.notifier.model.JobType -import com.monta.slack.notifier.util.JsonUtil import com.monta.slack.notifier.util.writeToOutput class PublishSlackService( @@ -22,13 +21,11 @@ class PublishSlackService( ) suspend fun publish( - githubContext: String, + githubPushContext: GithubPushContext, jobType: JobType, jobStatus: JobStatus, slackMessageId: String? ): String { - val githubPushContext = JsonUtil.instance.decodeFromString(githubContext) - val messageId = if (slackMessageId.isNullOrBlank()) { slackClient.create( githubPushContext = githubPushContext, diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt b/src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt new file mode 100644 index 0000000..7439826 --- /dev/null +++ b/src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt @@ -0,0 +1,29 @@ +package com.monta.slack.notifier.util + +import kotlinx.cinterop.ByteVar +import kotlinx.cinterop.allocArray +import kotlinx.cinterop.memScoped +import kotlinx.cinterop.toKString +import platform.posix.fclose +import platform.posix.fgets +import platform.posix.fopen + +fun readStringFromFile(filePath: String): String { + val returnBuffer = StringBuilder() + val file = fopen(filePath, "r") + ?: throw IllegalArgumentException("Cannot open file $filePath for reading") + try { + memScoped { + val readBufferLength = 64 * 1024 + val buffer = allocArray(readBufferLength) + var line = fgets(buffer, readBufferLength, file)?.toKString() + while (line != null) { + returnBuffer.append(line) + line = fgets(buffer, readBufferLength, file)?.toKString() + } + } + } finally { + fclose(file) + } + return returnBuffer.toString() +}