Skip to content

Commit

Permalink
Do not fail if nothing to commit in shutdown hook - just warn
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszwawrzyk committed May 12, 2023
1 parent e81caee commit 65b0611
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ data class PullRequestManager(
if (hook.commands.isNotEmpty()) {
hook.commands.forEach {
Thread.sleep(1000)
CommandRunner.run(listOf("sh", "-c", it), workspaceRoot)
CommandRunner.runForOutput(listOf("sh", "-c", it), workspaceRoot)
}
git.commitSelectedFiles(hook.filesToCommit, hook.commitMessage)
if (hook.runFor == HookRunFor.Commit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,35 @@ import java.nio.file.Path
private val logger = KotlinLogging.logger {}

class CommandRunner {
data class Result(val exitCode: Int, val stdout: String, val stderr: String) {
val isSuccess: Boolean
get() = exitCode == 0
}

companion object {
suspend fun run(directory: Path, vararg command: String): String {
return run(command.toList(), directory)
suspend fun runForOutput(directory: Path, vararg command: String): String {
return runForOutput(command.toList(), directory)
}

suspend fun runForOutput(command: List<String>, directory: Path): String {
val result = run(command, directory)
if (result.isSuccess) {
return result.stdout
} else {
val message = "${command.joinToString(" ")}\n${result.stdout}\n${result.stderr}"
throw RuntimeException(message)
}
}

suspend fun run(command: List<String>, directory: Path): String {
suspend fun run(command: List<String>, directory: Path): Result {
logger.info { command.joinToString(" ") { if (it.contains(" ")) """"$it"""" else it } }
return withContext(Dispatchers.IO) {
val process = ProcessBuilder(command).directory(directory.toFile()).start()
.onExit().await()
val stdout = process.inputStream.bufferedReader().use { it.readText() }
val stderr = process.errorStream.bufferedReader().use { it.readText() }

if (process.exitValue() == 0) {
stdout
} else {
throw RuntimeException(
"${command.joinToString(" ")}\n$stdout\n$stderr",
)
}
Result(process.exitValue(), stdout, stderr)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.nio.file.Path

class GitClient(private val repositoryRoot: Path) {
private val quiet = "--quiet"
private val git = runBlocking { CommandRunner.run(listOf("sh", "-c", "which git"), repositoryRoot).trim() }
private val git = runBlocking { CommandRunner.runForOutput(listOf("sh", "-c", "which git"), repositoryRoot).trim() }

suspend fun checkout(target: String, newBranch: Boolean = false) {
val b = if (newBranch) "-b" else null
Expand Down Expand Up @@ -76,7 +76,11 @@ class GitClient(private val repositoryRoot: Path) {
suspend fun run(vararg gitArgs: String?): String = run(gitArgs.toList())

suspend fun run(gitArgs: List<String?>): String {
return CommandRunner.run(listOf(git) + gitArgs.filterNotNull(), repositoryRoot)
return CommandRunner.runForOutput(listOf(git) + gitArgs.filterNotNull(), repositoryRoot)
}

suspend fun runForResult(vararg args: String): CommandRunner.Result {
return CommandRunner.run(listOf(git) + args, repositoryRoot)
}

data class GitAuthor(val name: String, val email: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ class GitOperations(workspaceRoot: Path, private val baseBranch: String) {

suspend fun commitSelectedFiles(filesToCommit: List<String>, commitMessage: String) {
git.add(filesToCommit)
git.commit(commitMessage)
val noChanges = git.runForResult("git", "diff", "--quiet", "--exit-code", "--cached").isSuccess
if (noChanges) {
logger.warn { "No changes to commit" }
} else {
git.commit(commitMessage)
}
}

suspend fun squashLastTwoCommits() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PostUpdateHookTest : E2EBase() {
val git = GitClient(localRoot)
runBlocking {
git.checkout("bazel-steward/io.arrow-kt/arrow-core/1.1.5")
val validationCommandOutput = CommandRunner.run(
val validationCommandOutput = CommandRunner.runForOutput(
localRoot,
"sh",
"-c",
Expand Down Expand Up @@ -52,7 +52,7 @@ class PostUpdateHookTest : E2EBase() {
val git = GitClient(localRoot)
runBlocking {
git.checkout("bazel-steward/io.arrow-kt/arrow-core/1.1.5")
val validationCommandOutput = CommandRunner.run(
val validationCommandOutput = CommandRunner.runForOutput(
localRoot,
"sh",
"-c",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ class BazelRulesExtractor {
""".trimMargin(),
)
// solution from https://github.com/bazelbuild/bazel/issues/6377#issuecomment-1237791008
CommandRunner.run(workspaceRoot, "bazel", "build", "@all_external_repositories//:result.json")
CommandRunner.runForOutput(workspaceRoot, "bazel", "build", "@all_external_repositories//:result.json")
workspaceFilePath.writeText(originalContent)
deleteFile(tempFileForBzl)
val bazelPath = CommandRunner.run(workspaceRoot, "bazel", "info", "output_base").trim()
val bazelPath = CommandRunner.runForOutput(workspaceRoot, "bazel", "info", "output_base").trim()
val resultFilePath = Path(bazelPath).resolve("external/all_external_repositories/result.json")
if (!resultFilePath.exists()) {
throw RuntimeException("Failed to find a file: $resultFilePath")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MavenDataExtractor(private val workspaceRoot: Path) {
}

private suspend fun extractFromFile(fileName: String): List<String> = withContext(Dispatchers.IO) {
val xml = CommandRunner.run(
val xml = CommandRunner.runForOutput(
workspaceRoot,
"bazel",
"query",
Expand Down

0 comments on commit 65b0611

Please sign in to comment.