Skip to content
Snippets Groups Projects
Commit 6673592b authored by Nicholas Blair's avatar Nicholas Blair
Browse files

Merge branch 'handle-maven-release-tags' into 'master'

fix: handle maven release plugin generated tags

Look like 'my-awesome-project-0.1.0'; need to use a Pattern to slice out the semver triplet.

This pull request fixes a bug found while converting a project that previously used the maven-release-plugin; the tags generated by that plugin start with the project name, and can't be parsed by the Java SemVer library as is.

See https://git.doit.wisc.edu/adi-ia/bucky-backup-app/merge_requests/5

cc @paul.erickson 

See merge request !4
parents 1298289b 078885f4
No related branches found
No related tags found
1 merge request!4fix: handle maven release plugin generated tags
group 'edu.wisc.doit.gradle' group 'edu.wisc.doit.gradle'
version '0.2.1' version '0.2.2'
apply plugin: 'groovy' apply plugin: 'groovy'
apply plugin: 'idea' apply plugin: 'idea'
......
...@@ -3,6 +3,9 @@ package edu.wisc.doit.gradle ...@@ -3,6 +3,9 @@ package edu.wisc.doit.gradle
import com.github.zafarkhaja.semver.Version import com.github.zafarkhaja.semver.Version
import org.gradle.api.GradleException import org.gradle.api.GradleException
import java.util.regex.Matcher
import java.util.regex.Pattern
/** /**
* Encapsulates logic for comparing version strings. * Encapsulates logic for comparing version strings.
* *
...@@ -10,6 +13,7 @@ import org.gradle.api.GradleException ...@@ -10,6 +13,7 @@ import org.gradle.api.GradleException
*/ */
class CompareVersions { class CompareVersions {
static final Pattern PATTERN = Pattern.compile(".*(\\d+\\.\\d+\\.\\d+).*")
/** /**
* *
* @param fromGitDescribe the version string from git (e.g. 'git describe') * @param fromGitDescribe the version string from git (e.g. 'git describe')
...@@ -22,12 +26,7 @@ class CompareVersions { ...@@ -22,12 +26,7 @@ class CompareVersions {
println "git describe is empty - there are no releases yet. You must have at least one tag on the repository to use this task." println "git describe is empty - there are no releases yet. You must have at least one tag on the repository to use this task."
return return
} }
def lastVersion = Version.valueOf(fromGitDescribe) def lastVersion = parseVersionFromGitDescribe(fromGitDescribe)
if(!''.equals(lastVersion.getPreReleaseVersion())) {
// git describe produced something like '0.20.7-1-gfbe61fd'
// split on '-' and recreate last as the first token
lastVersion = Version.valueOf(fromGitDescribe.split('-')[0])
}
// read 'version' field from build.gradle // read 'version' field from build.gradle
def currentVersion = Version.valueOf(inBuildGradle); def currentVersion = Version.valueOf(inBuildGradle);
...@@ -38,4 +37,20 @@ class CompareVersions { ...@@ -38,4 +37,20 @@ class CompareVersions {
throw new GradleException("version field in build.gradle with current value " + currentVersion + " must be incremented past " + lastVersion) throw new GradleException("version field in build.gradle with current value " + currentVersion + " must be incremented past " + lastVersion)
} }
} }
/**
*
* @param gitDescribe the version string from git
* @return a {@link Version}
* @throws IllegalArgumentException if a version could not be extracted
*/
static Version parseVersionFromGitDescribe(String gitDescribe) {
Matcher m = PATTERN.matcher(gitDescribe);
if(m.matches()) {
String substring = m.group(1);
println "Extracted ${substring} from git describe output of ${gitDescribe}"
return Version.valueOf(substring);
}
throw new IllegalArgumentException("cannot parse version from " + gitDescribe)
}
} }
package edu.wisc.doit.gradle package edu.wisc.doit.gradle
import com.github.zafarkhaja.semver.Version
import org.gradle.api.GradleException import org.gradle.api.GradleException
import org.junit.Test import org.junit.Test
import static org.junit.Assert.assertEquals
/** /**
* Unit tests for {@link CompareVersions}. * Unit tests for {@link CompareVersions}.
* *
...@@ -30,4 +33,22 @@ class CompareVersionsTest { ...@@ -30,4 +33,22 @@ class CompareVersionsTest {
public void describe_includes_hash_same_version_expects_failure() { public void describe_includes_hash_same_version_expects_failure() {
CompareVersions.compare("0.20.7-1-gfbe61fd", "0.20.7") CompareVersions.compare("0.20.7-1-gfbe61fd", "0.20.7")
} }
@Test
public void describe_maven_release_plugin_style_tags() {
CompareVersions.compare("my-awesome-project-0.2.0-13-g98171dc", "0.2.1")
}
@Test
void "parseVersionFromGitDescribe control"() {
assertEquals(Version.valueOf("0.0.1"), CompareVersions.parseVersionFromGitDescribe("0.0.1"))
}
@Test
void "parseVersionFromGitDescribe with preReleaseVersion"() {
assertEquals(Version.valueOf("0.20.7"), CompareVersions.parseVersionFromGitDescribe("0.20.7-1-gfbe61fd"))
}
@Test
void "parseVersionFromGitDescribe maven release plugin formatted tag"() {
assertEquals(Version.valueOf("0.2.0"), CompareVersions.parseVersionFromGitDescribe("my-awesome-project-0.2.0-13-g98171dc"))
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment