Skip to content
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 @@ -5,18 +5,25 @@ import pl.allegro.tech.build.axion.release.domain.scm.ScmPosition

enum PredefinedVersionCreator {

SIMPLE('simple', { String versionFromTag, ScmPosition position ->
SIMPLE('simple', { String versionFromTag, ScmPosition position, VersionContext versionContext ->
return versionFromTag
}),

VERSION_WITH_BRANCH('versionWithBranch', { String versionFromTag, ScmPosition position ->
VERSION_WITH_BRANCH('versionWithBranch', { String versionFromTag, ScmPosition position, VersionContext versionContext ->
if ((position.branch != 'master' && position.branch != 'main') && position.branch != 'HEAD') {
return "$versionFromTag-$position.branch".toString()
}
return versionFromTag
}),

VERSION_WITH_COMMIT_HASH('versionWithCommitHash', { String versionFromTag, ScmPosition position ->
VERSION_WITH_BRANCH_WHEN_SNAPSHOT('versionWithBranchWhenSnapshot', { String versionFromTag, ScmPosition position, VersionContext versionContext ->
if ((position.branch != 'master' && position.branch != 'main') && position.branch != 'HEAD' && versionContext.isSnapshot()) {
return "$versionFromTag-$position.branch".toString()
}
return versionFromTag
}),

VERSION_WITH_COMMIT_HASH('versionWithCommitHash', { String versionFromTag, ScmPosition position, VersionContext versionContext ->
if ((position.branch != 'master' && position.branch != 'main') && position.branch != 'HEAD') {
return "$versionFromTag-$position.shortRevision".toString()
}
Expand All @@ -25,14 +32,14 @@ enum PredefinedVersionCreator {

private final String type

final VersionProperties.Creator versionCreator
final VersionProperties.VersionCreator versionCreator

private PredefinedVersionCreator(String type, Closure c) {
this.type = type
this.versionCreator = c
}

static VersionProperties.Creator versionCreatorFor(String type) {
static VersionProperties.VersionCreator versionCreatorFor(String type) {
PredefinedVersionCreator creator = values().find { it.type == type }
if (creator == null) {
throw new IllegalArgumentException("There is no predefined version creator with $type type. " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ abstract class VersionConfig extends BaseExtension {
abstract Property<Boolean> getCreateReleaseCommit()

@Internal
abstract Property<VersionProperties.Creator> getVersionCreator()
abstract Property<VersionProperties.VersionCreator> getVersionCreator()

@Internal
abstract Property<VersionProperties.Creator> getSnapshotCreator()
Expand Down Expand Up @@ -195,7 +195,7 @@ abstract class VersionConfig extends BaseExtension {
this.createReleaseCommit.set(createReleaseCommit)
}

void versionCreator(VersionProperties.Creator versionCreator) {
void versionCreator(VersionProperties.VersionCreator versionCreator) {
this.versionCreator.set(versionCreator)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class VersionPropertiesFactory {
)
}

private static VersionProperties.Creator findVersionCreator(VersionConfig config, String currentBranch) {
private static VersionProperties.VersionCreator findVersionCreator(VersionConfig config, String currentBranch) {
if (config.versionCreatorType().isPresent()) {
return PredefinedVersionCreator.versionCreatorFor(config.versionCreatorType().get())
}
Expand Down Expand Up @@ -75,14 +75,15 @@ class VersionPropertiesFactory {
}

private
static VersionProperties.Creator find(String currentBranch, Map<String, Object> collection, VersionProperties.Creator defaultValue, Closure<VersionProperties.Creator> converter) {
static VersionProperties.VersionCreator find(String currentBranch, Map<String, Object> collection, VersionProperties.VersionCreator defaultValue,
Closure<VersionProperties.VersionCreator> converter) {
Object value = collection?.findResult { pattern, value ->
Pattern.matches(pattern, currentBranch) ? value : null
}

if (value == null) {
return defaultValue
} else if ((value instanceof Closure) || (value instanceof VersionProperties.Creator)) {
} else if ((value instanceof Closure) || (value instanceof VersionProperties.VersionCreator)) {
return value
} else {
return converter.call(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public VersionContext currentVersion(VersionProperties versionRules, TagProperti

public DecoratedVersion currentDecoratedVersion(VersionProperties versionProperties, TagProperties tagRules, NextVersionProperties nextVersionRules) {
VersionContext versionContext = versionResolver.resolveVersion(versionProperties, tagRules, nextVersionRules);
String version = versionProperties.getVersionCreator().apply(versionContext.getVersion().toString(), versionContext.getPosition());
String version = versionProperties.getVersionCreator().apply(versionContext.getVersion().toString(), versionContext.getPosition(), versionContext);

if (versionProperties.isSanitizeVersion()) {
version = sanitizer.sanitize(version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.zafarkhaja.semver.Version;
import pl.allegro.tech.build.axion.release.domain.MonorepoConfig;
import pl.allegro.tech.build.axion.release.domain.VersionContext;
import pl.allegro.tech.build.axion.release.domain.VersionIncrementerContext;
import pl.allegro.tech.build.axion.release.domain.scm.ScmPosition;

Expand All @@ -11,14 +12,18 @@ public interface Creator {
String apply(String versionFromTag, ScmPosition position);
}

public interface VersionCreator {
String apply(String versionFromTag, ScmPosition position, VersionContext versionContext);
}

public interface Incrementer {
Version apply(VersionIncrementerContext versionIncrementerContext);
}

private final String forcedVersion;
private final boolean forceSnapshot;
private final boolean ignoreUncommittedChanges;
private final Creator versionCreator;
private final VersionCreator versionCreator;
private final Creator snapshotCreator;
private final Incrementer versionIncrementer;
private final boolean sanitizeVersion;
Expand All @@ -29,7 +34,7 @@ public VersionProperties(
String forcedVersion,
boolean forceSnapshot,
boolean ignoreUncommittedChanges,
Creator versionCreator,
VersionCreator versionCreator,
Creator snapshotCreator,
Incrementer versionIncrementer,
boolean sanitizeVersion,
Expand Down Expand Up @@ -63,7 +68,7 @@ public final boolean isIgnoreUncommittedChanges() {
return ignoreUncommittedChanges;
}

public final Creator getVersionCreator() {
public final VersionCreator getVersionCreator() {
return versionCreator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,56 @@ package pl.allegro.tech.build.axion.release.domain
import spock.lang.Specification

import static pl.allegro.tech.build.axion.release.domain.scm.ScmPositionBuilder.scmPosition
import static pl.allegro.tech.build.axion.release.domain.VersionContextBuilder.versionContext

class PredefinedVersionCreatorTest extends Specification {

def "default version creator should just return version string"() {
expect:
PredefinedVersionCreator.SIMPLE.versionCreator.apply('version',scmPosition('master')) == 'version'
PredefinedVersionCreator.SIMPLE.versionCreator.apply('version',scmPosition('master'), null) == 'version'
}

def "versionWithBranch version creator should return simple version when on master"() {
expect:
PredefinedVersionCreator.VERSION_WITH_BRANCH.versionCreator.apply('version', scmPosition('master')) == 'version'
PredefinedVersionCreator.VERSION_WITH_BRANCH.versionCreator.apply('version', scmPosition('master'), null) == 'version'
}

def "versionWithBranch version creator should return version with appended branch name when not on master"() {
expect:
PredefinedVersionCreator.VERSION_WITH_BRANCH.versionCreator.apply('version', scmPosition('branch')) == 'version-branch'
PredefinedVersionCreator.VERSION_WITH_BRANCH.versionCreator.apply('version', scmPosition('branch'), null) == 'version-branch'
}

def "versionWithBranchWhenSnapshot version creator should return version when on master"() {
expect:
PredefinedVersionCreator.VERSION_WITH_BRANCH_WHEN_SNAPSHOT.versionCreator.apply('version', scmPosition('master'),
versionContext(scmPosition('master'), false)) == 'version'
}

def "versionWithBranchWhenSnapshot version creator should return version when on not on master and no snapshot"() {
expect:
PredefinedVersionCreator.VERSION_WITH_BRANCH_WHEN_SNAPSHOT.versionCreator.apply('version', scmPosition('branch'),
versionContext(scmPosition('branch'), false)) == 'version'
}

def "versionWithBranchWhenSnapshot version creator should return version with appended branch name when on not on master and snapshot"() {
expect:
PredefinedVersionCreator.VERSION_WITH_BRANCH_WHEN_SNAPSHOT.versionCreator.apply('version', scmPosition('branch'),
versionContext(scmPosition('branch'), true)) == 'version-branch'
}

def "versionWithCommitHash version creator should return simple version when on main"() {
expect:
PredefinedVersionCreator.VERSION_WITH_COMMIT_HASH.versionCreator.apply('version', scmPosition('main')) == 'version'
PredefinedVersionCreator.VERSION_WITH_COMMIT_HASH.versionCreator.apply('version', scmPosition('main'), null) == 'version'
}

def "versionWithCommitHash version creator should return version with appended short SHA-1 hash when not on main"() {
expect:
PredefinedVersionCreator.VERSION_WITH_COMMIT_HASH.versionCreator.apply('version', scmPosition('branch')) == 'version-c143976'
PredefinedVersionCreator.VERSION_WITH_COMMIT_HASH.versionCreator.apply('version', scmPosition('branch'), null) == 'version-c143976'
}

def "should return version creator of given type"() {
expect:
PredefinedVersionCreator.versionCreatorFor('simple').apply('version', null) == 'version'
PredefinedVersionCreator.versionCreatorFor('simple').apply('version', null, null) == 'version'
}

def "should throw exception when trying to obtain undefined version creator"() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package pl.allegro.tech.build.axion.release.domain

import pl.allegro.tech.build.axion.release.domain.scm.ScmPosition
import com.github.zafarkhaja.semver.Version

import static pl.allegro.tech.build.axion.release.domain.scm.ScmPositionBuilder.scmPosition

class VersionContextBuilder {
private Version version = Version.parse('1.0.0')

private boolean snapshot = false

private Version previousVersion = Version.parse('1.0.0')

private ScmPosition position = scmPosition('master')

private VersionContextBuilder() {
}

static VersionContextBuilder scmPosition() {
return new VersionContextBuilder()
}

static VersionContext versionContext(ScmPosition position, boolean snapshot) {
return new VersionContextBuilder().withPosition(position).withSnapshot(snapshot).build()
}

VersionContext build() {
return new VersionContext(version, snapshot, previousVersion, position)
}

VersionContextBuilder withVersion(Version version) {
this.version = version
return this
}

VersionContextBuilder withSnapshot(boolean snapshot) {
this.snapshot = snapshot
return this
}

VersionContextBuilder withPreviousVersion(Version previousVersion) {
this.previousVersion = previousVersion
return this
}

VersionContextBuilder withPosition(ScmPosition position) {
this.position = position
return this
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class VersionServiceTest extends Specification {

def "should return version information"() {
given:
VersionProperties properties = versionProperties().withVersionCreator({ v, t -> v }).build()
VersionProperties properties = versionProperties().withVersionCreator({ v, t, c -> v }).build()
resolver.resolveVersion(properties, tagProperties, nextVersionProperties) >> new VersionContext(
Version.valueOf("1.0.1"),
true,
Expand All @@ -125,7 +125,7 @@ class VersionServiceTest extends Specification {

def "should sanitize version if flag is set to true"() {
given:
VersionProperties properties = versionProperties().withVersionCreator({ v, t -> return v + '-feature/hello' }).build()
VersionProperties properties = versionProperties().withVersionCreator({ v, t, c -> return v + '-feature/hello' }).build()

resolver.resolveVersion(properties, tagProperties, nextVersionProperties) >> new VersionContext(
Version.valueOf("1.0.1"),
Expand All @@ -145,7 +145,7 @@ class VersionServiceTest extends Specification {
given:
VersionProperties properties = versionProperties()
.dontSanitizeVersion()
.withVersionCreator({ v, t -> return v + '-feature/hello' })
.withVersionCreator({ v, t, c -> return v + '-feature/hello' })
.build()

resolver.resolveVersion(properties, tagProperties, nextVersionProperties) >> new VersionContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class VersionPropertiesBuilder {

private MonorepoConfig monorepoConfig = Fixtures.monorepoConfig()

private VersionProperties.Creator versionCreator = PredefinedVersionCreator.SIMPLE.versionCreator
private VersionProperties.VersionCreator versionCreator = PredefinedVersionCreator.SIMPLE.versionCreator

private VersionProperties.Creator snapshotCreator = PredefinedSnapshotCreator.SIMPLE.snapshotCreator

Expand Down