Skip to content

Commit

Permalink
[patch] adding parameters for setting username and email
Browse files Browse the repository at this point in the history
  • Loading branch information
robertfmurdock committed Sep 12, 2024
1 parent 1210a84 commit 6fb89a9
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ class GitAdapter(private val workingDirectory: String) {
workingDirectory,
).trim()

fun newAnnotatedTag(name: String, ref: String) {
runProcess(
fun newAnnotatedTag(name: String, ref: String, userName: String?, userEmail: String?) {
val command = listOf("git") + inlineConfig("user.name", userName) + inlineConfig("user.email", userEmail) +
listOf(
"git",
"tag",
"--annotate",
"--message=$name",
name,
ref,
),
)
runProcess(
command,
workingDirectory,
)
}

private fun inlineConfig(property: String, value: String?) =
if (value != null) listOf("-c", "$property=$value") else emptyList()

fun pushTags() {
runProcess(listOf("git", "push", "--tags"), workingDirectory)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ class Tag : CliktCommand() {
private val dir by argument("git-repo")
private val releaseBranch by option().required()
private val version: String by option().required()
private val userName: String? by option()
private val userEmail: String? by option()
private val warningsAsErrors by option().boolean().default(false)
override fun run() {
TaggerCore(GitAdapter(dir))
.tag(version, releaseBranch)
.tag(version, releaseBranch, userName, userEmail)
.let {
when (it) {
TagResult.Success -> echo("Success!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,21 @@ class TagCommandTest : TagTestSpec {
arguments += projectDir.absolutePath
}

override fun configureWithOverrides(releaseBranch: String?, warningsAsErrors: Boolean?) {
override fun configureWithOverrides(
releaseBranch: String?,
userName: String?,
userEmail: String?,
warningsAsErrors: Boolean?,
) {
if (releaseBranch != null) {
arguments += "--release-branch=$releaseBranch"
}
if (userName != null) {
arguments += "--user-name=$userName"
}
if (userEmail != null) {
arguments += "--user-email=$userEmail"
}
if (warningsAsErrors != null) {
arguments += "--warnings-as-errors=$warningsAsErrors"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.zegreatrob.tools.tagger.core

private fun String.isSnapshot() = contains("SNAPSHOT")

fun TaggerCore.tag(version: String, releaseBranch: String?): TagResult {
fun TaggerCore.tag(version: String, releaseBranch: String?, userName: String?, userEmail: String?): TagResult {
val isSnapshot = version.isSnapshot()
val headTag = adapter.showTag("HEAD")
val alreadyTagged = headTag != null
Expand All @@ -19,9 +19,8 @@ fun TaggerCore.tag(version: String, releaseBranch: String?): TagResult {
),
)
} else {
kotlin.runCatching { adapter.newAnnotatedTag(version, "HEAD") }
kotlin.runCatching { adapter.newAnnotatedTag(version, "HEAD", userName, userEmail) }
.map {
println("lol")
adapter.pushTags()
TagResult.Success
}.getOrElse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,21 @@ class TagFunctionalTest : TagTestSpec {
)
}

override fun configureWithOverrides(releaseBranch: String?, warningsAsErrors: Boolean?) {
override fun configureWithOverrides(
releaseBranch: String?,
userName: String?,
userEmail: String?,
warningsAsErrors: Boolean?,
) {
buildFile.writeText(
"""
plugins {
id("com.zegreatrob.tools.tagger")
}
tagger {
${if (releaseBranch != null) "releaseBranch = \"$releaseBranch\"" else ""}
${if (userName != null) "userName = \"$userName\"" else ""}
${if (userEmail != null) "userEmail = \"$userEmail\"" else ""}
${if (warningsAsErrors != null) "warningsAsErrors.set($warningsAsErrors)" else ""}
}
""".trimIndent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ open class TagVersion :
private fun isSnapshot() = version.contains("SNAPSHOT")

@TaskAction
fun execute() {
when (val result = taggerExtension.core.tag(version, taggerExtension.releaseBranch)) {
fun execute() = taggerExtension.run {
when (val result = core.tag(this@TagVersion.version, releaseBranch, userName, userEmail)) {
TagResult.Success -> {}
is TagResult.Error -> if (taggerExtension.warningsAsErrors.get()) {
is TagResult.Error -> if (warningsAsErrors.get()) {
throw GradleException(result.message)
} else {
logger.warn(result.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ open class TaggerExtension(
@Input
var releaseBranch: String? = null

@Input
var userName: String? = null

@Input
var userEmail: String? = null

@Input
var warningsAsErrors = objectFactory.property<Boolean>().convention(false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,29 @@ interface TagTestSpec {
fun configureWithDefaults()
fun configureWithOverrides(
releaseBranch: String? = null,
userName: String? = null,
userEmail: String? = null,
warningsAsErrors: Boolean? = null,
)

fun execute(version: String): TestResult

@BeforeTest
fun checkPrerequisites() {
assertEquals("/dev/null", System.getenv("GIT_CONFIG_GLOBAL"), "Ensure this is set for the test to work as intended")
assertEquals("/dev/null", System.getenv("GIT_CONFIG_SYSTEM"), "Ensure this is set for the test to work as intended")
assertEquals(
"/dev/null",
System.getenv("GIT_CONFIG_GLOBAL"),
"Ensure this is set for the test to work as intended",
)
assertEquals(
"/dev/null",
System.getenv("GIT_CONFIG_SYSTEM"),
"Ensure this is set for the test to work as intended",
)
}

@Test
fun tagWillTagAndPushSuccessfully() {
fun whenUserNameAndEmailAreConfiguredTagWillTagAndPush() {
configureWithDefaults()

val originDirectory = createTempDirectory()
Expand Down Expand Up @@ -77,6 +87,32 @@ interface TagTestSpec {
assertEquals(expectedVersion, gitAdapter.showTag("HEAD"))
}

@Test
fun whenUserNameAndEmailAreParametersTagWillTagAndPush() {
configureWithOverrides(releaseBranch = "master", userName = "RoB as Test", userEmail = "[email protected]", warningsAsErrors = true)

val originDirectory = createTempDirectory()
val originGrgit = Grgit.init(fun InitOp.() {
this.dir = originDirectory.absolutePathString()
})
disableGpgSign(originDirectory.absolutePathString())
originGrgit.commit(fun CommitOp.() {
this.message = "init"
})
val grgit = initializeGitRepo(
listOf("init", "[patch] commit 1", "[patch] commit 2"),
remoteUrl = originDirectory.absolutePathString(),
)
grgit.push()

val expectedVersion = "1.0.0"
val result = execute(expectedVersion)
assertIsNot<TestResult.Failure>(result, message = "$result")

val gitAdapter = GitAdapter(this.projectDir.absolutePath)
assertEquals(expectedVersion, gitAdapter.showTag("HEAD"))
}

@Test
fun tagWillFailWhenUserEmailAndNameAreNotConfigured() {
configureWithOverrides(releaseBranch = "master", warningsAsErrors = true)
Expand Down

0 comments on commit 6fb89a9

Please sign in to comment.