Skip to content

Commit b7e5960

Browse files
committed
chore: Tracer version increments patch when on "release/v* branch
1 parent 12d677e commit b7e5960

File tree

2 files changed

+69
-36
lines changed

2 files changed

+69
-36
lines changed

buildSrc/src/main/kotlin/datadog/gradle/plugin/version/GitDescribeValueSource.kt renamed to buildSrc/src/main/kotlin/datadog/gradle/plugin/version/GitCommandValueSource.kt

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,39 @@ package datadog.gradle.plugin.version
22

33
import org.gradle.api.GradleException
44
import org.gradle.api.file.DirectoryProperty
5-
import org.gradle.api.provider.Property
5+
import org.gradle.api.provider.ListProperty
66
import org.gradle.api.provider.ValueSource
77
import org.gradle.api.provider.ValueSourceParameters
88
import org.gradle.process.ExecOperations
9-
import org.gradle.process.ExecResult
109
import java.io.ByteArrayOutputStream
1110
import java.nio.charset.Charset
1211
import javax.inject.Inject
1312

14-
abstract class GitDescribeValueSource @Inject constructor(
13+
abstract class GitCommandValueSource @Inject constructor(
1514
private val execOperations: ExecOperations
16-
) : ValueSource<String, GitDescribeValueSource.Parameters> {
15+
) : ValueSource<String, GitCommandValueSource.Parameters> {
1716
override fun obtain(): String {
1817
val workDir = parameters.workingDirectory.get()
19-
val tagPrefix = parameters.tagVersionPrefix.get()
20-
21-
val commandsArray = mutableListOf(
22-
"git",
23-
"describe",
24-
"--abbrev=8",
25-
"--tags",
26-
"--first-parent",
27-
"--match=$tagPrefix[0-9].[0-9]*.[0-9]",
28-
)
29-
if (parameters.showDirty.get()) {
30-
commandsArray.add("--dirty")
31-
}
18+
val commands = parameters.gitCommand.get()
3219

3320
val outputStream = ByteArrayOutputStream()
3421
val result = try {
3522
execOperations.exec {
36-
commandLine(commandsArray)
23+
commandLine(commands)
3724
workingDir(workDir)
3825
standardOutput = outputStream
3926
errorOutput = outputStream
4027
}
4128
} catch (e: Exception) {
42-
throw GradleException("Failed to run: ${commandsArray.joinToString(" ")}", e)
29+
throw GradleException("Failed to run: ${commands.joinToString(" ")}", e)
4330
}
4431

4532
val output = outputStream.toString(Charset.defaultCharset().name())
4633
result.exitValue.let { exitValue ->
4734
if (exitValue != 0) {
4835
throw GradleException(
4936
"""
50-
Failed to run: ${commandsArray.joinToString(" ")}
37+
Failed to run: ${commands.joinToString(" ")}
5138
(exit code: $exitValue)
5239
Output:
5340
$output
@@ -56,12 +43,11 @@ abstract class GitDescribeValueSource @Inject constructor(
5643
}
5744
}
5845

59-
return output
46+
return output.trim()
6047
}
6148

6249
interface Parameters : ValueSourceParameters {
63-
val tagVersionPrefix: Property<String>
64-
val showDirty: Property<Boolean>
6550
val workingDirectory: DirectoryProperty
51+
val gitCommand: ListProperty<String>
6652
}
6753
}

buildSrc/src/main/kotlin/datadog/gradle/plugin/version/TracerVersionPlugin.kt

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.gradle.api.logging.Logging
77
import org.gradle.api.model.ObjectFactory
88
import org.gradle.api.provider.ProviderFactory
99
import org.gradle.kotlin.dsl.property
10+
import java.io.File
1011
import javax.inject.Inject
1112

1213
class TracerVersionPlugin @Inject constructor(
@@ -18,10 +19,10 @@ class TracerVersionPlugin @Inject constructor(
1819
if (targetProject.rootProject != targetProject) {
1920
throw IllegalStateException("Only root project can apply plugin")
2021
}
22+
targetProject.extensions.create("tracerVersion", TracerVersionExtension::class.java)
23+
val extension = targetProject.extensions.getByType(TracerVersionExtension::class.java)
2124

22-
val extension = targetProject.extensions.create("tracerVersion", TracerVersionExtension::class.java)
2325
val versionProvider = versionProvider(targetProject, extension)
24-
2526
targetProject.allprojects {
2627
version = versionProvider
2728
}
@@ -31,27 +32,73 @@ class TracerVersionPlugin @Inject constructor(
3132
targetProject: Project,
3233
extension: TracerVersionExtension
3334
): String {
34-
val workingDirectory = targetProject.projectDir
35+
val repoWorkingDirectory = targetProject.rootDir
3536

36-
val buildVersion: String = if (!workingDirectory.resolve(".git").exists()) {
37+
val buildVersion: String = if (!repoWorkingDirectory.resolve(".git").exists()) {
38+
// Not a git repository
3739
extension.defaultVersion.get()
3840
} else {
39-
providerFactory.of(GitDescribeValueSource::class.java) {
40-
parameters {
41-
this.tagVersionPrefix.set(extension.tagVersionPrefix)
42-
this.showDirty.set(extension.detectDirty)
43-
this.workingDirectory.set(workingDirectory)
41+
providerFactory.zip(
42+
gitDescribeProvider(extension, repoWorkingDirectory),
43+
gitCurrentBranchProvider(repoWorkingDirectory)
44+
) { describeString, currentBranch ->
45+
toTracerVersion(describeString, extension) {
46+
when {
47+
currentBranch.startsWith("release/v") -> {
48+
logger.info("Incrementing patch because release branch : $currentBranch")
49+
nextPatchVersion()
50+
}
51+
else -> {
52+
logger.info("Incrementing patch")
53+
nextMinorVersion()
54+
}
55+
}
4456
}
45-
}.map {
46-
toTracerVersion(it.trim(), extension)
4757
}.get()
4858
}
4959

5060
logger.lifecycle("Tracer build version: {}", buildVersion)
5161
return buildVersion
5262
}
5363

54-
private fun toTracerVersion(describeString: String, extension: TracerVersionExtension): String {
64+
private fun gitCurrentBranchProvider(
65+
repoWorkingDirectory: File
66+
) = providerFactory.of(GitCommandValueSource::class.java) {
67+
parameters {
68+
gitCommand.addAll(
69+
"git",
70+
"rev-parse",
71+
"--abbrev-ref",
72+
"HEAD",
73+
)
74+
workingDirectory.set(repoWorkingDirectory)
75+
}
76+
}
77+
78+
private fun gitDescribeProvider(
79+
extension: TracerVersionExtension,
80+
repoWorkingDirectory: File
81+
) = providerFactory.of(GitCommandValueSource::class.java) {
82+
parameters {
83+
val tagPrefix = extension.tagVersionPrefix.get()
84+
gitCommand.addAll(
85+
"git",
86+
"describe",
87+
"--abbrev=8",
88+
"--tags",
89+
"--first-parent",
90+
"--match=$tagPrefix[0-9].[0-9]*.[0-9]",
91+
)
92+
93+
if (extension.detectDirty.get()) {
94+
gitCommand.add("--dirty")
95+
}
96+
97+
workingDirectory.set(repoWorkingDirectory)
98+
}
99+
}
100+
101+
private fun toTracerVersion(describeString: String, extension: TracerVersionExtension, nextVersion: Version.() -> Version): String {
55102
logger.info("Git describe output: {}", describeString)
56103

57104
val tagPrefix = extension.tagVersionPrefix.get()
@@ -63,7 +110,7 @@ class TracerVersionPlugin @Inject constructor(
63110
val hasLaterCommits = describeTrailer.isNotBlank()
64111
val version = Version.parse(lastTagVersion).let {
65112
if (hasLaterCommits) {
66-
it.nextMinorVersion()
113+
it.nextVersion()
67114
} else {
68115
it
69116
}

0 commit comments

Comments
 (0)