Skip to content

Commit

Permalink
Merge pull request #7 from Liftric/feature/git-short-hash
Browse files Browse the repository at this point in the history
feat: use short git commit hashes - by default
  • Loading branch information
benjohnde authored Oct 26, 2020
2 parents 159f86b + d255999 commit 330540e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ open class OctopusDeployExtension(project: Project) {
@Optional
val jiraBaseBrowseUrl: Property<String> = project.objects.property()

/**
* Use short (7 char) commit hashes. Default is [true]
*/
@Input
@Optional
val useShortCommitHashes: Property<Boolean> = project.objects.property()

/**
* Default `buildInformationAddition` implementation adding context from the CI environment for Gitlab CI. Also sets `commitLinkBaseUrl`.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class OctopusDeployPlugin : Plugin<Project> {
project.tasks.create("commitsSinceLastTag", CommitsSinceLastTagTask::class.java).apply {
dependsOn(getFirstCommitHashTask, getPreviousTagTask)
project.afterEvaluate {
useShortCommitHashes.set(extension.useShortCommitHashes)
workingDir = extension.gitRoot
commitLinkBaseUrl = extension.commitLinkBaseUrl
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand All @@ -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<Boolean> = project.objects.property()

@Input
var commits: List<CommitCli> = 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.")
Expand All @@ -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]}"
)
Expand All @@ -65,4 +75,10 @@ open class CommitsSinceLastTagTask : DefaultTask() {
throw IllegalStateException("git describe exitCode: $exitCode")
}
}
}
}

private fun String.shorten(useShortHash: Boolean): String = if (useShortHash && length > 6) {
substring(0, 7)
} else {
this
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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(
"""
Expand All @@ -38,6 +51,7 @@ octopus {
version.set("whatever")
packageName.set("whatever")
serverUrl.set("whatever")
${if (useLongHashes) "useShortCommitHashes.set(false)" else ""}
}"""
)
}
Expand Down

0 comments on commit 330540e

Please sign in to comment.