Skip to content

Commit

Permalink
address PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
ichromanrd committed Feb 5, 2024
1 parent 43f4457 commit 0b3af6d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 29 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Example:
* [Additional Release Info](#additional-release-info)
* [Change Description](#change-description)
* [Changes Limit](#changes-limit)
* [Define Tag Pattern](#define-tag-pattern)
## How to Use
Expand Down Expand Up @@ -176,4 +177,24 @@ There might be quite a few commits since the last released version (for example,
releasePaperwork {
maxChangesPerRelease.set(50)
}
```
```
### Define Tag Pattern
There is a specific case where we want created tag name to be customized. It can be done by set the `tagPattern` as below:
```
releasePaperwork {
tagPattern.set("v%s")
}
```
Which will create a tag with this format:
```v1.0.0```
Do remember that the character `%s` must be provided as the place for the version. If `tagPattern` does not defined, default pattern
will be `release-%s` which will create a tag with this format:
```release-1.0.0```
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ class GradleReleasePaperworkPlugin : Plugin<Project> {
project.logger.lifecycle("committing file $pathToCommit")
git.add().addFilepattern(pathToCommit).call()
}
val tagPrefix = getTagPrefix(extension)
return git.commit().setMessage(String.format(tagPrefix, newVersion)).call()
val tagPattern = getTagPattern(extension)
return git.commit().setMessage(String.format(tagPattern, newVersion)).call()
}

private fun createTag(
Expand All @@ -362,7 +362,7 @@ class GradleReleasePaperworkPlugin : Plugin<Project> {
commitId: RevCommit
) {
val git = Git.open(project.rootDir)
val tagName = String.format(getTagPrefix(extension), newVersion)
val tagName = String.format(getTagPattern(extension), newVersion)
project.logger.lifecycle("creating git tag $tagName")
git
.tag()
Expand All @@ -372,13 +372,13 @@ class GradleReleasePaperworkPlugin : Plugin<Project> {
.call()
}

private fun getTagPrefix(extension: GradleReleasePaperworkPluginExtension) =
if (extension.tagPrefix.isPresent) "${extension.tagPrefix.get()}%s"
private fun getTagPattern(extension: GradleReleasePaperworkPluginExtension) =
if (extension.tagPattern.isPresent) extension.tagPattern.get()
else DEFAULT_RELEASE_COMMIT_MESSAGE_PATTERN

private fun getReleaseCommitRegex(extension: GradleReleasePaperworkPluginExtension): Regex {
val prefix = getTagPrefix(extension)
return prefix.replace("%s", "\\S+").toRegex()
val pattern = getTagPattern(extension)
return pattern.replace("%s", "\\S+").toRegex()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ interface GradleReleasePaperworkPluginExtension {

val maxChangesPerRelease: Property<Int>

val tagPrefix: Property<String>
val tagPattern: Property<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -478,40 +478,48 @@ internal class GradleReleasePaperworkPluginTest {
""".trimIndent())
}

@Test
fun `when release is made then git tag is created`() {
val version = "1.0.0"
gradleFile.appendText("""
version = "$version"
""".trimIndent())
private fun prepareAndRunTagPatternTest(
gradleFileContent: String,
expectedResult: String,
) {
gradleFile.appendText(gradleFileContent)
runBuild()

val expected = String.format(GradleReleasePaperworkPlugin.DEFAULT_RELEASE_COMMIT_MESSAGE_PATTERN, version)
val tag = git.tagList().call().find {
val actual = it.name.substring("refs/tags/".length)
actual == expected
actual == expectedResult
}
assertThat(tag).isNotNull
}

@Test
fun `when tagPrefix is defined then tag created will be using that`() {
fun `when release is made and no tagPattern defined then git tag is created with default pattern`() {
val version = "1.0.0"
val prefix = "v"
gradleFile.appendText("""
val content = """
version = "$version"
""".trimIndent()

prepareAndRunTagPatternTest(
content,
String.format(GradleReleasePaperworkPlugin.DEFAULT_RELEASE_COMMIT_MESSAGE_PATTERN, version)
)
}

@Test
fun `when release is made and tagPattern is defined then tag is created and named using tagPattern`() {
val version = "1.0.0"
val pattern = "v%s"
val content = """
version = "$version"
releasePaperwork {
tagPrefix.set("$prefix")
tagPattern.set("$pattern")
}
""".trimIndent())
runBuild()
""".trimIndent()

val expected = "$prefix$version"
val tag = git.tagList().call().find {
val actual = it.name.substring("refs/tags/".length)
actual == expected
}
assertThat(tag).isNotNull
prepareAndRunTagPatternTest(
content,
String.format(pattern, version)
)
}
}

0 comments on commit 0b3af6d

Please sign in to comment.