From 0a2910dba2780875d4eede7bdb2dbfd2156868fd Mon Sep 17 00:00:00 2001 From: David Gofferje Date: Thu, 25 Sep 2025 15:53:15 +0200 Subject: [PATCH 1/3] Extra interface voor VersionCreator. Let op: testen werken niet (zijn niet aangepast nog) --- .../domain/PredefinedVersionCreator.groovy | 18 +++++++++++++----- .../axion/release/domain/VersionConfig.groovy | 4 ++-- .../config/VersionPropertiesFactory.groovy | 7 ++++--- .../axion/release/domain/VersionService.java | 2 +- .../domain/properties/VersionProperties.java | 11 ++++++++--- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/main/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreator.groovy b/src/main/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreator.groovy index 4bcd6223..96d964b0 100644 --- a/src/main/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreator.groovy +++ b/src/main/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreator.groovy @@ -5,18 +5,26 @@ 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 ((versionContext.position.branch != 'master' && versionContext.position.branch != 'main') && versionContext.position.branch != 'HEAD' && + versionContext.isSnapshot()) { + return "$versionFromTag-$versionContext.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() } @@ -25,14 +33,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. " + diff --git a/src/main/groovy/pl/allegro/tech/build/axion/release/domain/VersionConfig.groovy b/src/main/groovy/pl/allegro/tech/build/axion/release/domain/VersionConfig.groovy index ab5077ae..fb793e9e 100644 --- a/src/main/groovy/pl/allegro/tech/build/axion/release/domain/VersionConfig.groovy +++ b/src/main/groovy/pl/allegro/tech/build/axion/release/domain/VersionConfig.groovy @@ -108,7 +108,7 @@ abstract class VersionConfig extends BaseExtension { abstract Property getCreateReleaseCommit() @Internal - abstract Property getVersionCreator() + abstract Property getVersionCreator() @Internal abstract Property getSnapshotCreator() @@ -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) } diff --git a/src/main/groovy/pl/allegro/tech/build/axion/release/infrastructure/config/VersionPropertiesFactory.groovy b/src/main/groovy/pl/allegro/tech/build/axion/release/infrastructure/config/VersionPropertiesFactory.groovy index c0d6fc17..43410de2 100644 --- a/src/main/groovy/pl/allegro/tech/build/axion/release/infrastructure/config/VersionPropertiesFactory.groovy +++ b/src/main/groovy/pl/allegro/tech/build/axion/release/infrastructure/config/VersionPropertiesFactory.groovy @@ -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()) } @@ -75,14 +75,15 @@ class VersionPropertiesFactory { } private - static VersionProperties.Creator find(String currentBranch, Map collection, VersionProperties.Creator defaultValue, Closure converter) { + static VersionProperties.VersionCreator find(String currentBranch, Map collection, VersionProperties.VersionCreator defaultValue, + Closure 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) diff --git a/src/main/java/pl/allegro/tech/build/axion/release/domain/VersionService.java b/src/main/java/pl/allegro/tech/build/axion/release/domain/VersionService.java index 517a7ef5..309e7914 100644 --- a/src/main/java/pl/allegro/tech/build/axion/release/domain/VersionService.java +++ b/src/main/java/pl/allegro/tech/build/axion/release/domain/VersionService.java @@ -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); diff --git a/src/main/java/pl/allegro/tech/build/axion/release/domain/properties/VersionProperties.java b/src/main/java/pl/allegro/tech/build/axion/release/domain/properties/VersionProperties.java index a8e323cd..a224271e 100644 --- a/src/main/java/pl/allegro/tech/build/axion/release/domain/properties/VersionProperties.java +++ b/src/main/java/pl/allegro/tech/build/axion/release/domain/properties/VersionProperties.java @@ -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; @@ -11,6 +12,10 @@ 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); } @@ -18,7 +23,7 @@ public interface Incrementer { 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; @@ -29,7 +34,7 @@ public VersionProperties( String forcedVersion, boolean forceSnapshot, boolean ignoreUncommittedChanges, - Creator versionCreator, + VersionCreator versionCreator, Creator snapshotCreator, Incrementer versionIncrementer, boolean sanitizeVersion, @@ -63,7 +68,7 @@ public final boolean isIgnoreUncommittedChanges() { return ignoreUncommittedChanges; } - public final Creator getVersionCreator() { + public final VersionCreator getVersionCreator() { return versionCreator; } From c53fe475d0e90bbe59547826c764bdd2b67afc99 Mon Sep 17 00:00:00 2001 From: David Gofferje Date: Fri, 26 Sep 2025 09:51:25 +0200 Subject: [PATCH 2/3] Bestaande testen weer werkend gemaakt (falen er nog steeds 2, maar dit is in main al zo) --- .../domain/PredefinedVersionCreatorTest.groovy | 12 ++++++------ .../axion/release/domain/VersionServiceTest.groovy | 6 +++--- .../properties/VersionPropertiesBuilder.groovy | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreatorTest.groovy b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreatorTest.groovy index f1d76c35..85e49bce 100644 --- a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreatorTest.groovy +++ b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreatorTest.groovy @@ -9,32 +9,32 @@ 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 "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"() { diff --git a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/VersionServiceTest.groovy b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/VersionServiceTest.groovy index d434d4af..c1174d6d 100644 --- a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/VersionServiceTest.groovy +++ b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/VersionServiceTest.groovy @@ -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, @@ -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"), @@ -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( diff --git a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/properties/VersionPropertiesBuilder.groovy b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/properties/VersionPropertiesBuilder.groovy index f8034dfb..c93e21ac 100644 --- a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/properties/VersionPropertiesBuilder.groovy +++ b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/properties/VersionPropertiesBuilder.groovy @@ -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 From b4086ba119aaf4e32b570778bb9676e33ee2a5ad Mon Sep 17 00:00:00 2001 From: David Gofferje Date: Fri, 26 Sep 2025 14:45:26 +0200 Subject: [PATCH 3/3] Tests added for new functionality (versionWithBranchWhenSnapshot) --- .../domain/PredefinedVersionCreator.groovy | 5 +- .../PredefinedVersionCreatorTest.groovy | 19 +++++++ .../domain/VersionContextBuilder.groovy | 53 +++++++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 src/test/groovy/pl/allegro/tech/build/axion/release/domain/VersionContextBuilder.groovy diff --git a/src/main/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreator.groovy b/src/main/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreator.groovy index 96d964b0..4eb1e9c4 100644 --- a/src/main/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreator.groovy +++ b/src/main/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreator.groovy @@ -17,9 +17,8 @@ enum PredefinedVersionCreator { }), VERSION_WITH_BRANCH_WHEN_SNAPSHOT('versionWithBranchWhenSnapshot', { String versionFromTag, ScmPosition position, VersionContext versionContext -> - if ((versionContext.position.branch != 'master' && versionContext.position.branch != 'main') && versionContext.position.branch != 'HEAD' && - versionContext.isSnapshot()) { - return "$versionFromTag-$versionContext.position.branch".toString() + if ((position.branch != 'master' && position.branch != 'main') && position.branch != 'HEAD' && versionContext.isSnapshot()) { + return "$versionFromTag-$position.branch".toString() } return versionFromTag }), diff --git a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreatorTest.groovy b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreatorTest.groovy index 85e49bce..50b1bed3 100644 --- a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreatorTest.groovy +++ b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreatorTest.groovy @@ -4,6 +4,7 @@ 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 { @@ -22,6 +23,24 @@ class PredefinedVersionCreatorTest extends Specification { 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'), null) == 'version' diff --git a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/VersionContextBuilder.groovy b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/VersionContextBuilder.groovy new file mode 100644 index 00000000..cdff208c --- /dev/null +++ b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/VersionContextBuilder.groovy @@ -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 + } + + +}