Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rc incrementors #142

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,42 @@ enum PredefinedVersionIncrementer {
}),

INCREMENT_PRERELEASE('incrementPrerelease', { VersionIncrementerContext context, Map config ->
Version version
if (context.currentVersion.preReleaseVersion) {
Matcher matcher = context.currentVersion.preReleaseVersion =~ /^(.*?)(\d+)$/
if (matcher.matches()) {
long nextNumber = Long.parseLong(matcher.group(2)) + 1
String nextNumberPadded = format("%0" + matcher.group(2).length() + "d", nextNumber);
String nextPreReleaseVersion = matcher.group(1) + nextNumberPadded

return new Version.Builder()
.setNormalVersion(context.currentVersion.normalVersion)
.setPreReleaseVersion(nextPreReleaseVersion)
.build();
}
version = incrementPrereleaseAllowingLeadingZeros(context)
}
return context.currentVersion.incrementPatchVersion()
return version ? version : context.currentVersion.incrementPatchVersion()
}),

INCREMENT_PRERELEASE_OR_MINOR('incrementPrereleaseOrMinor', { VersionIncrementerContext context, Map config ->
Version version
if (context.currentVersion.preReleaseVersion) {
version = incrementPrereleaseAllowingLeadingZeros(context)
}
return version ? version : context.currentVersion.incrementMinorVersion()
}),

CREATE_MAJOR_RC('createMajorRC', { VersionIncrementerContext context, Map config ->
throwErrorIfPrerelease(context)
return new Version.Builder()
.setNormalVersion(context.currentVersion.incrementMajorVersion().toString())
.setPreReleaseVersion("RC1")
.build();
}),

CREATE_MINOR_RC('createMinorRC', { VersionIncrementerContext context, Map config ->
throwErrorIfPrerelease(context)
return new Version.Builder()
.setNormalVersion(context.currentVersion.incrementMinorVersion().toString())
.setPreReleaseVersion("RC1")
.build();
}),

CREATE_FINAL('createFinal', { VersionIncrementerContext context, Map config ->
throwErrorIfNotPrerelease(context)
return new Version.Builder()
.setNormalVersion(context.currentVersion.normalVersion)
.build();
}),

BRANCH_SPECIFIC('branchSpecific', { VersionIncrementerContext context, Map config ->
Expand All @@ -55,6 +77,33 @@ enum PredefinedVersionIncrementer {

final Closure<Version> versionIncrementer

private static incrementPrereleaseAllowingLeadingZeros(VersionIncrementerContext context) {
Matcher matcher = context.currentVersion.preReleaseVersion =~ /^(.*?)(\d+)$/
if (matcher.matches()) {
long nextNumber = Long.parseLong(matcher.group(2)) + 1
String nextNumberPadded = format("%0" + matcher.group(2).length() + "d", nextNumber);
String nextPreReleaseVersion = matcher.group(1) + nextNumberPadded

return new Version.Builder()
.setNormalVersion(context.currentVersion.normalVersion)
.setPreReleaseVersion(nextPreReleaseVersion)
.build();
}
return null
}

private static throwErrorIfPrerelease(VersionIncrementerContext context) {
if (context.currentVersion.getPreReleaseVersion()) {
throw new IllegalArgumentException("Already on a prerelease, use incrementPrerelease")
}
}

private static throwErrorIfNotPrerelease(VersionIncrementerContext context) {
if (!context.currentVersion.getPreReleaseVersion()) {
throw new IllegalArgumentException("Not on a prerelease, use normal release instead")
}
}

private PredefinedVersionIncrementer(String name, Closure<Version> c) {
this.name = name
this.versionIncrementer = c
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package pl.allegro.tech.build.axion.release.domain

import com.github.zafarkhaja.semver.Version
import pl.allegro.tech.build.axion.release.domain.scm.ScmPosition
import spock.lang.Specification
import static pl.allegro.tech.build.axion.release.domain.PredefinedVersionIncrementer.*

import static pl.allegro.tech.build.axion.release.domain.PredefinedVersionIncrementer.versionIncrementerFor
import com.github.zafarkhaja.semver.Version

import spock.lang.Ignore
import spock.lang.Specification
import spock.lang.Unroll

class PredefinedVersionIncrementerTest extends Specification {

VersionIncrementerContext context = new VersionIncrementerContext(Version.valueOf('0.1.0'), ScmPosition.defaultPosition())

def "should increment patch when incrementPatch rule used"() {
expect:
versionIncrementerFor('incrementPatch')(context) == Version.valueOf('0.1.1')
Expand All @@ -19,7 +22,7 @@ class PredefinedVersionIncrementerTest extends Specification {
expect:
versionIncrementerFor('incrementMinor')(context) == Version.valueOf('0.2.0')
}

def "should increment major when incrementMajor rule used"() {
expect:
versionIncrementerFor('incrementMajor')(context) == Version.valueOf('1.0.0')
Expand All @@ -34,16 +37,16 @@ class PredefinedVersionIncrementerTest extends Specification {
expect:
versionIncrementerFor('incrementMinorIfNotOnRelease', [releaseBranchPattern: 'master'])(context) == Version.valueOf('0.1.1')
}

def "should delegate to first matching incrementer when branchSpecific rule used"() {
expect:
versionIncrementerFor('branchSpecific', ['release.*': 'incrementPatch', 'master': 'incrementMinor'])(context) == Version.valueOf('0.2.0')
}

def "should increment prerelease version when incrementPrerelease rule used"() {
given:
VersionIncrementerContext context = new VersionIncrementerContext(Version.valueOf('0.1.0-rc1'), ScmPosition.defaultPosition())

expect:
versionIncrementerFor('incrementPrerelease')(context) == Version.valueOf('0.1.0-rc2')
}
Expand All @@ -56,10 +59,103 @@ class PredefinedVersionIncrementerTest extends Specification {
versionIncrementerFor('incrementPrerelease')(context) == Version.valueOf('0.1.0-rc02')
}

def "should create prerelease for next major version when createMajorRC rule used"() {
given:
VersionIncrementerContext context = new VersionIncrementerContext(Version.valueOf('0.1.0'), ScmPosition.defaultPosition())

expect:
versionIncrementerFor('createMajorRC')(context) == Version.valueOf('1.0.0-RC1')
}

def "should throw exception if already an RC version when createMajorRC rule used"() {
given:
VersionIncrementerContext context = new VersionIncrementerContext(Version.valueOf('0.1.0-rc1'), ScmPosition.defaultPosition())

when:
versionIncrementerFor('createMajorRC')(context)

then:
IllegalArgumentException e = thrown(IllegalArgumentException)
e.message == "Already on a prerelease, use incrementPrerelease"
}

def "should create prerelease for next minor version when createMinorRC rule used"() {
given:
VersionIncrementerContext context = new VersionIncrementerContext(Version.valueOf('0.1.0'), ScmPosition.defaultPosition())

expect:
versionIncrementerFor('createMinorRC')(context) == Version.valueOf('0.2.0-RC1')
}

def "should throw exception if already an RC version when createMinorRC rule used"() {
given:
VersionIncrementerContext context = new VersionIncrementerContext(Version.valueOf('0.1.0-rc1'), ScmPosition.defaultPosition())

when:
versionIncrementerFor('createMinorRC')(context)

then:
IllegalArgumentException e = thrown(IllegalArgumentException)
e.message == "Already on a prerelease, use incrementPrerelease"
}

@Unroll
def "should create #finalVersion for #rcVersion when createFinal rule used"(String rcVersion, String finalVersion) {
given:
VersionIncrementerContext context = new VersionIncrementerContext(Version.valueOf(rcVersion), ScmPosition.defaultPosition())

expect:
versionIncrementerFor('createFinal')(context) == Version.valueOf(finalVersion)

where:
rcVersion << ['0.1.0-rc1', '1.0.0-rc1', '1.1.0-rc2']
finalVersion << ['0.1.0', '1.0.0', '1.1.0']
}

def "should throw error if not on rc when createFinal rule used"() {
given:
VersionIncrementerContext context = new VersionIncrementerContext(Version.valueOf('0.1.0'), ScmPosition.defaultPosition())

when:
versionIncrementerFor('createFinal')(context)

then:
IllegalArgumentException e = thrown(IllegalArgumentException)
e.message == "Not on a prerelease, use normal release instead"
}

def "should throw exception if already an RC version when createFinal rule used"() {
given:
VersionIncrementerContext context = new VersionIncrementerContext(Version.valueOf('0.1.0-rc1'), ScmPosition.defaultPosition())

when:
versionIncrementerFor('createMinorRC')(context)

then:
IllegalArgumentException e = thrown(IllegalArgumentException)
e.message == "Already on a prerelease, use incrementPrerelease"
}

def "should increment prerelease when incrementPrereleaseOrMinor rule used"() {
given:
VersionIncrementerContext context = new VersionIncrementerContext(Version.valueOf('0.1.0-rc2'), ScmPosition.defaultPosition())

expect:
versionIncrementerFor('incrementPrereleaseOrMinor')(context) == Version.valueOf('0.1.0-rc3')
}

def "should increment minor when not on prerelease when incrementPrereleaseOrMinor rule used"() {
given:
VersionIncrementerContext context = new VersionIncrementerContext(Version.valueOf('0.1.0'), ScmPosition.defaultPosition())

expect:
versionIncrementerFor('incrementPrereleaseOrMinor')(context) == Version.valueOf('0.2.0')
}

def "should throw exception when unknown incrementer used"() {
when:
versionIncrementerFor('unknown')(context)

then:
thrown(IllegalArgumentException)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import pl.allegro.tech.build.axion.release.RepositoryBasedTest
import pl.allegro.tech.build.axion.release.domain.properties.NextVersionProperties
import pl.allegro.tech.build.axion.release.domain.properties.TagProperties
import pl.allegro.tech.build.axion.release.domain.properties.VersionProperties

import static pl.allegro.tech.build.axion.release.domain.properties.NextVersionPropertiesBuilder.nextVersionProperties
import static pl.allegro.tech.build.axion.release.domain.properties.TagPropertiesBuilder.tagProperties
import static pl.allegro.tech.build.axion.release.domain.properties.VersionPropertiesBuilder.versionProperties

import spock.lang.Ignore

class VersionResolverTest extends RepositoryBasedTest {

VersionResolver resolver

TagProperties tagRules = tagProperties().build()
Expand All @@ -22,17 +23,17 @@ class VersionResolverTest extends RepositoryBasedTest {
def setup() {
resolver = new VersionResolver(repository, context.versionFactory())
}

def "should return default previous and current version when no tag in repository"() {
when:
VersionWithPosition version = resolver.resolveVersion(defaultVersionRules, tagRules, nextVersionRules)

then:
version.previousVersion.toString() == '0.1.0'
version.version.toString() == '0.1.0'
version.position.tagless()
}

def "should return same previous and current version when on release tag"() {
given:
repository.tag('release-1.1.0')
Expand All @@ -44,7 +45,21 @@ class VersionResolverTest extends RepositoryBasedTest {
version.previousVersion.toString() == '1.1.0'
version.version.toString() == '1.1.0'
}


@Ignore("Couldn't make the version resolver to generate a new version, use forceSnapshot for now")
def "should return incremented current version when previous is rc and not the current"() {
given:
repository.tag('release-1.1.0-rc1')
VersionProperties versionRules = versionProperties().withVersionIncrementer('createFinal').build()

when:
VersionWithPosition version = resolver.resolveVersion(versionRules, tagRules, nextVersionRules)

then:
version.previousVersion.toString() == '1.1.0-rc1'
version.version.toString() == '1.1.0'
}

def "should return unmodified previous and incremented current version when not on tag"() {
given:
repository.tag('release-1.1.0')
Expand All @@ -57,7 +72,7 @@ class VersionResolverTest extends RepositoryBasedTest {
version.previousVersion.toString() == '1.1.0'
version.version.toString() == '1.1.1'
}

def "should return previous version from last release tag and current from alpha when on alpha tag"() {
given:
repository.tag('release-1.1.0')
Expand All @@ -72,7 +87,7 @@ class VersionResolverTest extends RepositoryBasedTest {
version.version.toString() == '2.0.0'
!version.position.onTag
}

def "should return previous version from last release and current from forced version when forcing version"() {
given:
repository.tag('release-1.1.0')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package pl.allegro.tech.build.axion.release.domain.properties

import com.github.zafarkhaja.semver.Version;

import groovy.lang.Closure;
import pl.allegro.tech.build.axion.release.domain.PredefinedVersionCreator
import pl.allegro.tech.build.axion.release.domain.PredefinedVersionIncrementer

class VersionPropertiesBuilder {

private String forcedVersion

private Closure<Version> myVersionIncrementer = PredefinedVersionIncrementer.versionIncrementerFor('incrementPatch')

private boolean forceSnapshot = false

private boolean ignoreUncommittedChanges = true
Expand All @@ -24,10 +29,15 @@ class VersionPropertiesBuilder {
forceSnapshot: forceSnapshot,
ignoreUncommittedChanges: ignoreUncommittedChanges,
versionCreator: PredefinedVersionCreator.SIMPLE.versionCreator,
versionIncrementer: PredefinedVersionIncrementer.versionIncrementerFor('incrementPatch'),
versionIncrementer: myVersionIncrementer,
sanitizeVersion: true)
}

VersionPropertiesBuilder withVersionIncrementer(String incrementer) {
this.myVersionIncrementer = PredefinedVersionIncrementer.versionIncrementerFor(incrementer)
return this
}

VersionPropertiesBuilder forceVersion(String version) {
this.forcedVersion = version
return this
Expand Down