Skip to content

Commit

Permalink
#1323 Fixing regex for extracting version from branch names
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Jul 10, 2024
1 parent ad1f5a5 commit 6ef05b7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ object VersionUtils {
/**
* Regex used to get a version from the end of a name.
*/
val semVerSuffixRegex = ".*(\\d+(\\.\\d+)+)$".toRegex()
val semVerSuffixRegex = "(\\d+(\\.\\d+)+)\$".toRegex()

fun getVersionText(regex: Regex, path: String): String? {
val matcher = regex.matchEntire(path)
val matcher = regex.find(path)
return if (matcher != null) {
// There is at least one capturing group, we can use it
if (matcher.groupValues.size >= 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ class VersionUtilsTest {
assertNull(VersionUtils.getVersion("master".toRegex(), "master"))
}

@Test
fun `Terminal sem ver`() {
assertEquals("99999.51.8", VersionUtils.getVersionText(VersionUtils.semVerSuffixRegex, "release/99999.51.8"))
assertEquals("99999.51", VersionUtils.getVersionText(VersionUtils.semVerSuffixRegex, "release/99999.51"))
assertEquals("1.51", VersionUtils.getVersionText(VersionUtils.semVerSuffixRegex, "release/1.51"))
assertEquals("1.51.89", VersionUtils.getVersionText(VersionUtils.semVerSuffixRegex, "release/1.51.89"))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ class BranchVersionTemplatingSourceTest {
assertEquals("1.8", text)
}

@Test
fun `Getting the branch version with default parameters and match using branch name with several digits`() {
val branch = BranchFixtures.testBranch()
every {
branchDisplayNameService.getBranchDisplayName(branch, BranchNamePolicy.DISPLAY_NAME_OR_NAME)
} returns "release/99999.51"
val text = source.render(
entity = branch,
configMap = emptyMap(),
renderer = PlainEventRenderer.INSTANCE,
)
assertEquals("99999.51", text)
}

@Test
fun `Getting the branch version with default parameters and no match`() {
val branch = BranchFixtures.testBranch()
Expand Down

0 comments on commit 6ef05b7

Please sign in to comment.