From d2559996e381b4d997c12e0b02e4738e7f13d506 Mon Sep 17 00:00:00 2001 From: Marcel Kesselring Date: Mon, 26 Oct 2020 12:51:27 +0100 Subject: [PATCH] feat: use short git commit hashes - by default Old approach using the full commit hashes can be enabled using the new useShortCommitHashes property. --- README.md | 1 + .../octopusdeploy/OctopusDeployExtension.kt | 7 ++++++ .../octopusdeploy/OctopusDeployPlugin.kt | 1 + .../task/CommitsSinceLastTagTask.kt | 22 ++++++++++++++++--- .../task/CommitsSinceLastTagTaskTest.kt | 20 ++++++++++++++--- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 49bb7ed..801def1 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ httpLogLevel | configures the http logging while using the Octopus API | `HttpLo issueTrackerName | When parsing issues the target issue tracker name is needed. Currently only `Jira` supported | **optional/none** parseCommitsForJiraIssues | Enable Jira Issue parsing. This needs the changelog generation enabled to parse the commits there. | **optional/none** jiraBaseBrowseUrl | For proper Jira URLs we need the base URL, something like `https://testric.atlassian.net/browse/`. | **optional/none** +useShortCommitHashes | Use short (7 char) commit hashes. | true `generateChangelogSinceLastTag` extracts all commits between the HEAD and the last tag. If no tag is found, the first commit in the history tree is used instead. diff --git a/src/main/kotlin/com/liftric/octopusdeploy/OctopusDeployExtension.kt b/src/main/kotlin/com/liftric/octopusdeploy/OctopusDeployExtension.kt index cfa02aa..0af4d26 100644 --- a/src/main/kotlin/com/liftric/octopusdeploy/OctopusDeployExtension.kt +++ b/src/main/kotlin/com/liftric/octopusdeploy/OctopusDeployExtension.kt @@ -106,6 +106,13 @@ open class OctopusDeployExtension(project: Project) { @Optional val jiraBaseBrowseUrl: Property = project.objects.property() + /** + * Use short (7 char) commit hashes. Default is [true] + */ + @Input + @Optional + val useShortCommitHashes: Property = project.objects.property() + /** * Default `buildInformationAddition` implementation adding context from the CI environment for Gitlab CI. Also sets `commitLinkBaseUrl`. */ diff --git a/src/main/kotlin/com/liftric/octopusdeploy/OctopusDeployPlugin.kt b/src/main/kotlin/com/liftric/octopusdeploy/OctopusDeployPlugin.kt index 120ea9e..a8178f8 100644 --- a/src/main/kotlin/com/liftric/octopusdeploy/OctopusDeployPlugin.kt +++ b/src/main/kotlin/com/liftric/octopusdeploy/OctopusDeployPlugin.kt @@ -30,6 +30,7 @@ class OctopusDeployPlugin : Plugin { project.tasks.create("commitsSinceLastTag", CommitsSinceLastTagTask::class.java).apply { dependsOn(getFirstCommitHashTask, getPreviousTagTask) project.afterEvaluate { + useShortCommitHashes.set(extension.useShortCommitHashes) workingDir = extension.gitRoot commitLinkBaseUrl = extension.commitLinkBaseUrl } diff --git a/src/main/kotlin/com/liftric/octopusdeploy/task/CommitsSinceLastTagTask.kt b/src/main/kotlin/com/liftric/octopusdeploy/task/CommitsSinceLastTagTask.kt index b40b481..d0d46dd 100644 --- a/src/main/kotlin/com/liftric/octopusdeploy/task/CommitsSinceLastTagTask.kt +++ b/src/main/kotlin/com/liftric/octopusdeploy/task/CommitsSinceLastTagTask.kt @@ -3,13 +3,16 @@ package com.liftric.octopusdeploy.task import com.liftric.octopusdeploy.api.CommitCli import com.liftric.octopusdeploy.shell import org.gradle.api.DefaultTask +import org.gradle.api.provider.Property import org.gradle.api.tasks.* +import org.gradle.kotlin.dsl.property import java.io.File open class CommitsSinceLastTagTask : DefaultTask() { init { group = "octopus" - description = "Calls git log to receive all commits since the previous tag or the first commit of the current history." + description = + "Calls git log to receive all commits since the previous tag or the first commit of the current history." outputs.upToDateWhen { false } } @@ -22,15 +25,22 @@ open class CommitsSinceLastTagTask : DefaultTask() { @InputFile @Optional var firstCommitFile: File? = null + @InputFile @Optional var previousTagFile: File? = null + @Input + @Optional + val useShortCommitHashes: Property = project.objects.property() + @Input var commits: List = emptyList() @TaskAction fun execute() { + val useShortHash = useShortCommitHashes.getOrElse(true) + val previousTag: String? = previousTagFile?.readText() if (previousTag == null) { logger.info("couldn't get previous tag, will use the first commit instead.") @@ -49,7 +59,7 @@ open class CommitsSinceLastTagTask : DefaultTask() { }.filter { it.size >= 2 } .map { CommitCli( - Id = it[0], + Id = it[0].shorten(useShortHash), Comment = it.subList(1, it.size).joinToString(" ").replace("\\", ""), LinkUrl = "${commitLinkBaseUrl.removeSuffix("/")}/${it[0]}" ) @@ -65,4 +75,10 @@ open class CommitsSinceLastTagTask : DefaultTask() { throw IllegalStateException("git describe exitCode: $exitCode") } } -} \ No newline at end of file +} + +private fun String.shorten(useShortHash: Boolean): String = if (useShortHash && length > 6) { + substring(0, 7) +} else { + this +} diff --git a/src/test/kotlin/com/liftric/octopusdeploy/task/CommitsSinceLastTagTaskTest.kt b/src/test/kotlin/com/liftric/octopusdeploy/task/CommitsSinceLastTagTaskTest.kt index e9857df..6b74d1a 100644 --- a/src/test/kotlin/com/liftric/octopusdeploy/task/CommitsSinceLastTagTaskTest.kt +++ b/src/test/kotlin/com/liftric/octopusdeploy/task/CommitsSinceLastTagTaskTest.kt @@ -1,6 +1,5 @@ package com.liftric.octopusdeploy.task -import junit.framework.TestCase import junit.framework.TestCase.assertEquals import org.gradle.testkit.runner.GradleRunner import org.gradle.testkit.runner.TaskOutcome @@ -14,8 +13,17 @@ class CommitsSinceLastTagTaskTest { @Test fun testExecute() { + testCommitsSinceLastTag(expectedHashLength = 7, useLongHashes = false) + } + + @Test + fun testLongHashExecute() { + testCommitsSinceLastTag(expectedHashLength = 40, useLongHashes = true) + } + + private fun testCommitsSinceLastTag(expectedHashLength: Int, useLongHashes: Boolean) { println(testProjectDir.root.absolutePath) - setupBuild() + setupBuild(useLongHashes) val result = GradleRunner.create() .forwardOutput() .withProjectDir(testProjectDir.root) @@ -24,9 +32,14 @@ class CommitsSinceLastTagTaskTest { .build() println(result.output) assertEquals(TaskOutcome.SUCCESS, result.task(":commitsSinceLastTag")?.outcome) + result.output + .split("\n") + .filter { it.startsWith("CommitCli") } + .firstOrNull { it.matches(Regex("CommitCli\\(Id=\\w{$expectedHashLength}?, LinkUrl=http.+?, Comment=.+?\\)")) } + ?: error("didn't find $expectedHashLength char commit hash!") } - fun setupBuild() { + fun setupBuild(useLongHashes: Boolean) { testProjectDir.newFile("build.gradle.kts").apply { writeText( """ @@ -38,6 +51,7 @@ octopus { version.set("whatever") packageName.set("whatever") serverUrl.set("whatever") + ${if (useLongHashes) "useShortCommitHashes.set(false)" else ""} }""" ) }