Skip to content

Commit

Permalink
Merge pull request #1 from ichromanrd/feat/tagPrefix
Browse files Browse the repository at this point in the history
Feat: Add tagPattern extension
  • Loading branch information
denis-zhdanov authored Feb 6, 2024
2 parents 08a25ba + 0b3af6d commit 07a0241
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

.gradle
.idea
build
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 @@ -55,9 +55,10 @@ class GradleReleasePaperworkPlugin : Plugin<Project> {
val commit = commitChanges(
project = project,
newVersion = versionToRelease,
extension = extension,
changedFiles = arrayOf(releaseNotesFile, getProjectVersionFile(project, extension))
)
createTag(versionToRelease, project, commit)
createTag(versionToRelease, project, extension, commit)
}
}
}
Expand Down Expand Up @@ -241,8 +242,9 @@ class GradleReleasePaperworkPlugin : Plugin<Project> {
val log = Git.open(project.rootDir).log().call()
val result = mutableListOf<String>()
val maxChanges = getMaxChangesPerRelease(extension)
val releaseCommitRegex = getReleaseCommitRegex(extension)
for (commit in log) {
if (RELEASE_COMMIT_REGEX.matches(commit.shortMessage)) {
if (releaseCommitRegex.matches(commit.shortMessage)) {
// skip release commit
continue
}
Expand Down Expand Up @@ -337,19 +339,30 @@ class GradleReleasePaperworkPlugin : Plugin<Project> {
projectVersionFile.writeText(newContent)
}

private fun commitChanges(project: Project, newVersion: String, vararg changedFiles: File): RevCommit {
private fun commitChanges(
project: Project,
newVersion: String,
extension: GradleReleasePaperworkPluginExtension,
vararg changedFiles: File,
): RevCommit {
val git = Git.open(project.rootDir)
for (file in changedFiles) {
val pathToCommit = project.rootDir.toPath().relativize(file.toPath()).toString()
project.logger.lifecycle("committing file $pathToCommit")
git.add().addFilepattern(pathToCommit).call()
}
return git.commit().setMessage(String.format(RELEASE_COMMIT_MESSAGE_PATTERN, newVersion)).call()
val tagPattern = getTagPattern(extension)
return git.commit().setMessage(String.format(tagPattern, newVersion)).call()
}

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

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 pattern = getTagPattern(extension)
return pattern.replace("%s", "\\S+").toRegex()
}

companion object {
val DEFAULT_PROJECT_VERSION_REGEX = """version\s*=\s*['"]([^'"]+)""".toRegex()
const val DEFAULT_RELEASE_NOTES_FILE = "RELEASE_NOTES.md"
const val RELEASE_COMMIT_MESSAGE_PATTERN = "release-%s"
val RELEASE_COMMIT_REGEX = RELEASE_COMMIT_MESSAGE_PATTERN.replace("%s", "\\S+").toRegex()
const val DEFAULT_RELEASE_COMMIT_MESSAGE_PATTERN = "release-%s"
const val RELEASE_DESCRIPTION_FORMAT = "v<version> released on <date><additional-release-description>"
const val COMMIT_DESCRIPTION_FORMAT = " * <commit-hash> <commit-description>"
val COMMIT_DESCRIPTION_SUFFIX_REGEX = """\s+\*\s+""".toRegex()
val DATE_SYSTEM_PROPERTY_NAME = "paperwork.release.date"
const val DATE_SYSTEM_PROPERTY_NAME = "paperwork.release.date"
val ZONE_UTC = ZoneId.of("UTC")
private val DATE_FORMATTER: DateTimeFormatter = DateTimeFormatter.ofPattern("dd MMM yyyy z")
private const val DEFAULT_MAX_CHANGES_PER_RELEASE = 20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ interface GradleReleasePaperworkPluginExtension {
val changeDescription: Property<(String) -> String?>

val maxChangesPerRelease: Property<Int>

val tagPattern: Property<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -478,19 +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.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 release is made and no tagPattern defined then git tag is created with default pattern`() {
val version = "1.0.0"
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 {
tagPattern.set("$pattern")
}
""".trimIndent()

prepareAndRunTagPatternTest(
content,
String.format(pattern, version)
)
}
}

0 comments on commit 07a0241

Please sign in to comment.