From 3e069bb2b57757927591167e9930fc6612944575 Mon Sep 17 00:00:00 2001 From: RoB Murdock Date: Thu, 12 Sep 2024 08:41:08 -0400 Subject: [PATCH] [patch] adding config file support for tag options --- .../tools/digger/core/AlwaysLeftParent.kt | 13 ++++ .../tools/digger/core/FindTrunkPath.kt | 31 --------- .../com/zegreatrob/tools/tagger/cli/Tag.kt | 6 ++ .../tools/tagger/cli/TaggerConfig.kt | 3 + .../CalculateVersionCommandConfigFileTest.kt | 2 +- .../tagger/cli/TagCommandConfigFileTest.kt | 63 +++++++++++++++++++ 6 files changed, 86 insertions(+), 32 deletions(-) create mode 100644 tools/digger-core/src/commonMain/kotlin/com/zegreatrob/tools/digger/core/AlwaysLeftParent.kt delete mode 100644 tools/digger-core/src/commonMain/kotlin/com/zegreatrob/tools/digger/core/FindTrunkPath.kt create mode 100644 tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/TagCommandConfigFileTest.kt diff --git a/tools/digger-core/src/commonMain/kotlin/com/zegreatrob/tools/digger/core/AlwaysLeftParent.kt b/tools/digger-core/src/commonMain/kotlin/com/zegreatrob/tools/digger/core/AlwaysLeftParent.kt new file mode 100644 index 00000000..fe9b59c0 --- /dev/null +++ b/tools/digger-core/src/commonMain/kotlin/com/zegreatrob/tools/digger/core/AlwaysLeftParent.kt @@ -0,0 +1,13 @@ +package com.zegreatrob.tools.digger.core + +import com.zegreatrob.tools.adapter.git.CommitRef + +fun List.alwaysLeftParent() = fold(emptyList()) { acc, commit -> + if (acc.isEmpty()) { + acc + commit + } else if (commit.id == acc.last().parents.firstOrNull()) { + acc + commit + } else { + acc + } +} diff --git a/tools/digger-core/src/commonMain/kotlin/com/zegreatrob/tools/digger/core/FindTrunkPath.kt b/tools/digger-core/src/commonMain/kotlin/com/zegreatrob/tools/digger/core/FindTrunkPath.kt deleted file mode 100644 index 17aae608..00000000 --- a/tools/digger-core/src/commonMain/kotlin/com/zegreatrob/tools/digger/core/FindTrunkPath.kt +++ /dev/null @@ -1,31 +0,0 @@ -package com.zegreatrob.tools.digger.core - -import com.zegreatrob.tools.adapter.git.CommitRef -import com.zegreatrob.tools.adapter.git.GitAdapter -import com.zegreatrob.tools.adapter.git.TagRef - -fun GitAdapter.findTrunkPath(tagRefs: List, log: List): List = allPaths( - log = log, - firstTagCommit = log.first(), - preferredCommitIds = tagRefs.map { it.commitId }.toSet(), -) - .shortestPathWithMostTags(taggedCommitIds = tagRefs.map { it.commitId }.toSet()) - ?: log.alwaysLeftParent() - -private fun MutableList>.shortestPathWithMostTags(taggedCommitIds: Set): List? { - val pathTagCountGroups = groupBy { path -> - path.count { taggedCommitIds.contains(it.id) } - } - return pathTagCountGroups[pathTagCountGroups.keys.max()] - ?.minBy { it.size } -} - -fun List.alwaysLeftParent() = fold(emptyList()) { acc, commit -> - if (acc.isEmpty()) { - acc + commit - } else if (commit.id == acc.last().parents.firstOrNull()) { - acc + commit - } else { - acc - } -} diff --git a/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tag.kt b/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tag.kt index 9518990a..c863be84 100644 --- a/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tag.kt +++ b/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/Tag.kt @@ -2,6 +2,7 @@ package com.zegreatrob.tools.tagger.cli import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.core.CliktError +import com.github.ajalt.clikt.core.context import com.github.ajalt.clikt.parameters.arguments.argument import com.github.ajalt.clikt.parameters.options.default import com.github.ajalt.clikt.parameters.options.option @@ -13,6 +14,11 @@ import com.zegreatrob.tools.tagger.core.TaggerCore import com.zegreatrob.tools.tagger.core.tag class Tag : CliktCommand() { + + init { + context { valueSources(ConfigFileSource(envvarReader)) } + } + private val dir by argument("git-repo") private val releaseBranch by option().required() private val version: String by option().required() diff --git a/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/TaggerConfig.kt b/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/TaggerConfig.kt index e10a307d..4225e75e 100644 --- a/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/TaggerConfig.kt +++ b/tools/tagger-cli/src/commonMain/kotlin/com/zegreatrob/tools/tagger/cli/TaggerConfig.kt @@ -11,4 +11,7 @@ data class TaggerConfig( val patchRegex: String? = null, val versionRegex: String? = null, val noneRegex: String? = null, + val userName: String? = null, + val userEmail: String? = null, + val warningsAsErrors: Boolean? = null, ) diff --git a/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersionCommandConfigFileTest.kt b/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersionCommandConfigFileTest.kt index 434a7a55..bf3d9bba 100644 --- a/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersionCommandConfigFileTest.kt +++ b/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/CalculateVersionCommandConfigFileTest.kt @@ -25,8 +25,8 @@ class CalculateVersionCommandConfigFileTest : CalculateVersionTestSpec { } override fun configureWithDefaults() { - val config = TaggerConfig(releaseBranch = "master") arguments += projectDir.absolutePath + val config = TaggerConfig(releaseBranch = "master") Json.encodeToStream(config, File(projectDir, ".tagger").outputStream()) } diff --git a/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/TagCommandConfigFileTest.kt b/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/TagCommandConfigFileTest.kt new file mode 100644 index 00000000..242768a7 --- /dev/null +++ b/tools/tagger-cli/src/jvmTest/kotlin/com/zegreatrob/tools/tagger/cli/TagCommandConfigFileTest.kt @@ -0,0 +1,63 @@ +@file:OptIn(ExperimentalSerializationApi::class) + +package com.zegreatrob.tools.tagger.cli + +import com.github.ajalt.clikt.testing.test +import com.zegreatrob.tools.tagger.TagTestSpec +import com.zegreatrob.tools.tagger.TestResult +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.encodeToStream +import org.junit.jupiter.api.io.TempDir +import java.io.File +import kotlin.test.BeforeTest + +class TagCommandConfigFileTest : TagTestSpec { + + @field:TempDir + override lateinit var projectDir: File + + override val addFileNames: Set = emptySet() + private lateinit var arguments: List + + @BeforeTest + fun setup() { + arguments = listOf("-q", "tag") + } + + override fun configureWithDefaults() { + val config = TaggerConfig(releaseBranch = "master") + Json.encodeToStream(config, File(projectDir, ".tagger").outputStream()) + arguments += projectDir.absolutePath + } + + override fun configureWithOverrides( + releaseBranch: String?, + userName: String?, + userEmail: String?, + warningsAsErrors: Boolean?, + ) { + var config = TaggerConfig() + releaseBranch?.let { config = config.copy(releaseBranch = releaseBranch) } + userName?.let { config = config.copy(userName = userName) } + userEmail?.let { config = config.copy(userEmail = userEmail) } + warningsAsErrors?.let { config = config.copy(warningsAsErrors = warningsAsErrors) } + Json.encodeToStream(config, File(projectDir, ".tagger").outputStream()) + + arguments += projectDir.absolutePath + } + + override fun execute(version: String): TestResult { + arguments += "--version=$version" + val test = cli() + .test(arguments, envvars = mapOf("PWD" to projectDir.absolutePath)) + return if (test.statusCode == 0) { + test + .output + .trim() + .let { TestResult.Success(it) } + } else { + TestResult.Failure(test.output.trim()) + } + } +}