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

Fix stderror not being captured in processed exec and max retries #26

Merged
merged 23 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
29 changes: 24 additions & 5 deletions src/main/kotlin/slack/cli/exec/ProcessedExecCli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public class ProcessedExecCli :
option("--config", envvar = "PE_CONFIGURATION_FILE")
.path(mustExist = true, canBeFile = true, canBeDir = false)

private val debug by option("--debug", "-d").flag()

@get:TestOnly
private val noExit by
option(
"--no-exit",
help = "Instructs this CLI to not exit the process with the status code. Test only!"
)
.flag()
@get:TestOnly internal val parseOnly by option("--parse-only").flag(default = false)

internal val args by argument().multiple()
Expand All @@ -76,8 +85,12 @@ public class ProcessedExecCli :

// Initial command execution
val (exitCode, logFile) = executeCommand(cmd, tmpDir)
while (exitCode != 0) {
echo("Command failed with exit code $exitCode. Running processor script...")
var attempts = 0
while (exitCode != 0 && attempts < 1) {
attempts++
echo(
"Command failed with exit code $exitCode. Running processor script (attempt $attempts)..."
)

echo("Processing CI failure")
val resultProcessor = ResultProcessor(verbose, bugsnagKey, config, ::echo)
Expand Down Expand Up @@ -114,8 +127,14 @@ public class ProcessedExecCli :

// If we got here, all is well
// Delete the tmp files
tmpDir.deleteRecursively()
exitProcess(exitCode)
if (!debug) {
tmpDir.deleteRecursively()
}

echo("Exiting with code $exitCode")
if (!noExit) {
exitProcess(exitCode)
}
}

// Function to execute command and capture output. Shorthand to the testable top-level function.
Expand Down Expand Up @@ -148,7 +167,7 @@ internal fun executeCommand(
// Pass the line through unmodified
line to ""
}
val process = command.process()
val process = command.process() forkErr { it pipe echoHandler pipe tmpFile.toFile() }
pipeline { process pipe echoHandler pipe tmpFile.toFile() }.join()
exitCode = process.process.pcb.exitCode
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/slack/cli/exec/ResultProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ internal class ResultProcessor(
val bugsnag: Bugsnag? by lazy { bugsnagKey?.let { key -> createBugsnag(key) } }

val logLinesReversed = logFile.readLines().asReversed()
echo("Checking ${config.knownIssues.size} known issues")
for (issue in config.knownIssues) {
val retrySignal = issue.check(logLinesReversed, echo)

if (retrySignal != RetrySignal.Unknown) {
// Report to bugsnag. Shared common Throwable but with different messages.
bugsnag?.apply {
bugsnag?.let {
verboseEcho("Reporting to bugsnag: $retrySignal")
notify(IssueThrowable(issue), Severity.ERROR) { report ->
it.notify(IssueThrowable(issue), Severity.ERROR) { report ->
// Group by the throwable message
report.setGroupingHash(issue.groupingHash)
report.addToTab("Run Info", "After-Retry", isAfterRetry)
Expand All @@ -70,6 +71,7 @@ internal class ResultProcessor(
}
}
}
?: run { verboseEcho("Skipping bugsnag reporting: $retrySignal") }

if (retrySignal is RetrySignal.Ack) {
echo("Recognized known issue but cannot retry: ${issue.message}")
Expand Down
18 changes: 15 additions & 3 deletions src/main/kotlin/slack/cli/exec/RetrySignal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ import kotlin.time.Duration
internal sealed interface RetrySignal {

/** Unknown issue. */
@TypeLabel("unknown") object Unknown : RetrySignal
@TypeLabel("unknown")
object Unknown : RetrySignal {
// TODO remove when we have data objects in Kotlin 1.9
override fun toString() = this::class.simpleName!!
}

/** Indicates an issue that is recognized but cannot be retried. */
@TypeLabel("ack") object Ack : RetrySignal
@TypeLabel("ack")
object Ack : RetrySignal {
// TODO remove when we have data objects in Kotlin 1.9
override fun toString() = this::class.simpleName!!
}

/** Indicates this issue should be retried immediately. */
@TypeLabel("immediate") object RetryImmediately : RetrySignal
@TypeLabel("immediate")
object RetryImmediately : RetrySignal {
// TODO remove when we have data objects in Kotlin 1.9
override fun toString() = this::class.simpleName!!
}

/** Indicates this issue should be retried after a [delay]. */
@TypeLabel("delayed")
Expand Down
32 changes: 32 additions & 0 deletions src/test/kotlin/slack/cli/exec/ResultProcessorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,38 @@ class ResultProcessorTest {
assertThat(logs.joinToString("\n").trim()).contains(expectedOutput)
}

@Test
fun testExecuteCommandWithStderr() {
val script =
"""
#!/bin/bash

>&2 echo "Error text"
"""
.trimIndent()
val scriptFile =
tmpFolder.newFile("script.sh").apply {
writeText(script)
setExecutable(true)
}
tmpFolder.newFile("test.txt")
val tmpDir = tmpFolder.newFolder("tmp/processed_exec")
val (exitCode, outputFile) =
executeCommand(tmpFolder.root.toPath(), scriptFile.absolutePath, tmpDir.toPath(), logs::add)
assertThat(exitCode).isEqualTo(0)

val expectedOutput =
"""
Error text
"""
.trimIndent()

assertThat(outputFile.readText().trim()).isEqualTo(expectedOutput)

// Note we use "contains" here because our script may output additional logs
assertThat(logs.joinToString("\n").trim()).contains(expectedOutput)
}

@Test
fun unknownIssue() {
val outputFile = tmpFolder.newFile("logs.txt")
Expand Down
Loading