Skip to content

Commit

Permalink
fix: reduce github context read from env to mitigate "argument list t…
Browse files Browse the repository at this point in the history
…oo long" bug (#43)
  • Loading branch information
dirkhas authored Aug 24, 2023
1 parent cfe1aff commit cb4c2bc
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -52,20 +74,33 @@ 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()
)
}
}

private fun getGithubPushContext(): GithubPushContext {
val eventJson = readStringFromFile(githubEventPath)
val event = JsonUtil.instance.decodeFromString<GithubPushContext.Event>(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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<GithubPushContext>(githubContext)

val messageId = if (slackMessageId.isNullOrBlank()) {
slackClient.create(
githubPushContext = githubPushContext,
Expand Down
29 changes: 29 additions & 0 deletions src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt
Original file line number Diff line number Diff line change
@@ -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<ByteVar>(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()
}

0 comments on commit cb4c2bc

Please sign in to comment.