Skip to content

Commit

Permalink
Exposed actual previous tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
Szczepan Faber committed Apr 9, 2021
1 parent b02d6ba commit d64c698
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ScmVersionExposedApiIntegrationTest extends BaseIntegrationTest {
task outputDecorated { doLast {
println "Version: \${scmVersion.version}"
println "Previous: \${scmVersion.previousVersion}" //'previousVersion' property smoke test
println "Previous tag: \${scmVersion.previousTag}" //'previousVersion' property smoke test
} }
""")

Expand All @@ -20,6 +21,7 @@ class ScmVersionExposedApiIntegrationTest extends BaseIntegrationTest {
then:
result.output.contains('Version: 0.1.0-SNAPSHOT')
result.output.contains('Previous: 0.1.0')
result.output.contains('Previous tag: null')
result.task(":outputDecorated").outcome == TaskOutcome.SUCCESS
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package pl.allegro.tech.build.axion.release.domain
import org.gradle.api.Project
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.Optional
import pl.allegro.tech.build.axion.release.ReleasePlugin
import pl.allegro.tech.build.axion.release.TagPrefixConf
import pl.allegro.tech.build.axion.release.domain.hooks.HooksConfig
import pl.allegro.tech.build.axion.release.domain.properties.Properties
import pl.allegro.tech.build.axion.release.infrastructure.di.Context
Expand Down Expand Up @@ -155,12 +155,29 @@ class VersionConfig {
return resolvedVersion.decoratedVersion
}

/**
* Previous version in the context of the 'next version marker' mechanism.
* Caveat: if 'next version marker' tag was not found then {@link #getPreviousVersion()} will be equal to {@link #getVersion()}
* and potentially <em>different</em> than {@link #getPreviousTag()}.
*/
@Input
@Optional
String getPreviousVersion() {
ensureVersionExists()
return resolvedVersion.previousVersion
}

/**
* Previous release tag. Simplified example assuming a typical configuration:
* given tags are 'v1.2.0', 'v1.1.0', 'v1.0.0', and we checked out 'v1.2.0', previous tag will be 'v1.1.0'.
*/
@Input
@Optional
String getPreviousTag() {
ensureVersionExists()
return resolvedVersion.previousTag
}

@Input
String getUndecoratedVersion() {
ensureVersionExists()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
public class VersionContext {

private final Version version;
private final String previousTag;
private final boolean snapshot;
private final Version previousVersion;
private final ScmPosition position;

public VersionContext(Version version, boolean snapshot, Version previousVersion, ScmPosition position) {
public VersionContext(Version version, String previousTag, boolean snapshot, Version previousVersion, ScmPosition position) {
this.version = version;
this.previousTag = previousTag;
this.snapshot = snapshot;
this.previousVersion = previousVersion;
this.position = position;
Expand All @@ -33,6 +35,10 @@ public final ScmPosition getPosition() {
return position;
}

public String getPreviousTag() {
return previousTag;
}

@Override
public String toString() {
return "VersionContext{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import pl.allegro.tech.build.axion.release.domain.scm.ScmRepository;
import pl.allegro.tech.build.axion.release.domain.scm.TaggedCommits;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -60,7 +62,7 @@ public VersionContext resolveVersion(VersionProperties versionProperties, TagPro

VersionFactory.FinalVersion finalVersion = versionFactory.createFinalVersion(scmState, versions.current);

return new VersionContext(finalVersion.version, finalVersion.snapshot, versions.previous, latestChangePosition);
return new VersionContext(finalVersion.version, versions.previousTag, finalVersion.snapshot, versions.previous, latestChangePosition);
}

private VersionInfo readVersions(
Expand All @@ -80,7 +82,8 @@ private VersionInfo readVersions(
Pattern nextVersionTagPattern = Pattern.compile(".*" + nextVersionProperties.getSuffix() + "$");
boolean forceSnapshot = versionProperties.isForceSnapshot();

TaggedCommits latestTaggedCommit = TaggedCommits.fromLatestCommit(repository, releaseTagPattern, latestChangePosition);
List<String> previousTag = new LinkedList<>();
TaggedCommits latestTaggedCommit = TaggedCommits.fromLatestCommit(repository, releaseTagPattern, latestChangePosition, previousTag);
VersionSorter.Result currentVersionInfo = versionFromTaggedCommits(
latestTaggedCommit, false,
nextVersionTagPattern,
Expand All @@ -99,6 +102,7 @@ private VersionInfo readVersions(

return new VersionInfo(
currentVersion,
(!previousTag.isEmpty())? previousTag.iterator().next() : null,
previousVersion,
(onCommitWithLatestChange && !currentVersionInfo.isNextVersion),
currentVersionInfo.isNextVersion,
Expand Down Expand Up @@ -130,6 +134,7 @@ private VersionInfo readVersionsByHighestVersion(

return new VersionInfo(
currentVersion,
null,
previousVersion,
(onCommitWithLatestChange && !currentVersionInfo.isNextVersion),
currentVersionInfo.isNextVersion,
Expand All @@ -143,13 +148,23 @@ private VersionSorter.Result versionFromTaggedCommits(TaggedCommits taggedCommit

private static final class VersionInfo {
final Version current;

//previous tag inferred from the current tag
//if current is '1.2.3', previous tag could be '1.2.2'
final String previousTag;

//previous version in the context of the 'next version marker' mechanism
//Caveat: if 'next version marker' tag was not found then 'previous' will be equal to 'current' *and*
// 'previous' will not be the same as 'previousTag'.
final Version previous;

final boolean onReleaseTag;
final boolean onNextVersionTag;
final boolean noTagsFound;

VersionInfo(Version current, Version previous, boolean onReleaseTag, boolean onNextVersionTag, boolean noTagsFound) {
VersionInfo(Version current, String previousTag, Version previous, boolean onReleaseTag, boolean onNextVersionTag, boolean noTagsFound) {
this.current = current;
this.previousTag = previousTag;
this.previous = previous;
this.onReleaseTag = onReleaseTag;
this.onNextVersionTag = onNextVersionTag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
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;
Expand Down Expand Up @@ -37,9 +38,8 @@ public DecoratedVersion currentDecoratedVersion(VersionProperties versionPropert
finalVersion = finalVersion + "-" + SNAPSHOT;
}


return new DecoratedVersion(versionContext.getVersion().toString(), finalVersion, versionContext.getPosition(),
versionContext.getPreviousVersion().toString());
versionContext.getPreviousVersion().toString(), versionContext.getPreviousTag());
}

public static class DecoratedVersion {
Expand All @@ -48,13 +48,15 @@ public static class DecoratedVersion {
private final String decoratedVersion;
private final ScmPosition position;
private final String previousVersion;
private final String previousTag;

public DecoratedVersion(String undecoratedVersion, String decoratedVersion, ScmPosition position,
String previousVersion) {
String previousVersion, String previousTag) {
this.undecoratedVersion = undecoratedVersion;
this.decoratedVersion = decoratedVersion;
this.position = position;
this.previousVersion = previousVersion;
this.previousTag = previousTag;
}

@Input
Expand All @@ -73,8 +75,15 @@ public final ScmPosition getPosition() {
}

@Input
@Optional
public final String getPreviousVersion() {
return previousVersion;
}

@Input
@Optional
public final String getPreviousTag() {
return previousTag;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public static TaggedCommits fromListOfCommits(ScmPosition latestTagPosition, Lis
return new TaggedCommits(latestTagPosition, taggedCommits);
}

public static TaggedCommits fromLatestCommit(ScmRepository repository, Pattern tagPattern, ScmPosition latestTagPosition) {
TagsOnCommit latestTags = repository.latestTags(tagPattern);
return new TaggedCommits(latestTagPosition, Arrays.asList(latestTags));
public static TaggedCommits fromLatestCommit(ScmRepository repository, Pattern tagPattern, ScmPosition latestTagPosition, List<String> previousTag) {
List<TagsOnCommit> tagsOnCommits = repository.taggedCommits(tagPattern);
TagsOnCommit latest = tagsOnCommits.isEmpty() ? TagsOnCommit.empty() : tagsOnCommits.iterator().next();
if (tagsOnCommits.size() > 1) {
previousTag.add(tagsOnCommits.get(1).getTags().iterator().next());
}
return new TaggedCommits(latestTagPosition, Arrays.asList(latest));
}

public static TaggedCommits fromAllCommits(ScmRepository repository, Pattern tagPattern, ScmPosition latestTagPosition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ class VersionResolverTest extends RepositoryBasedTest {
then:
version.previousVersion.toString() == '1.2.0'
version.version.toString() == '1.2.0'
version.previousTag == 'v1.5.0'
!version.snapshot
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class VersionServiceTest extends Specification {
VersionProperties properties = versionProperties().build()
resolver.resolveVersion(properties, tagProperties, nextVersionProperties) >> new VersionContext(
Version.valueOf('1.0.0'),
null,
false,
Version.valueOf('1.0.0'),
new ScmPosition('', '', 'master')
Expand All @@ -55,6 +56,7 @@ class VersionServiceTest extends Specification {
VersionProperties properties = versionProperties().forceSnapshot().build()
resolver.resolveVersion(properties, tagProperties, nextVersionProperties) >> new VersionContext(
Version.valueOf('1.0.1'),
null,
true,
Version.valueOf('1.0.1'),
new ScmPosition('', '', 'master')
Expand All @@ -73,6 +75,7 @@ class VersionServiceTest extends Specification {
VersionProperties properties = versionProperties().build()
resolver.resolveVersion(properties, tagProperties, nextVersionProperties) >> new VersionContext(
Version.valueOf("1.0.1"),
null,
true,
Version.valueOf("1.0.1"),
new ScmPosition('', '', 'master')
Expand All @@ -91,6 +94,7 @@ class VersionServiceTest extends Specification {
VersionProperties properties = versionProperties().dontIgnoreUncommittedChanges().build()
resolver.resolveVersion(properties, tagProperties, nextVersionProperties) >> new VersionContext(
Version.valueOf("1.0.1"),
null,
true,
Version.valueOf("1.0.1"),
new ScmPosition('', '', 'master')
Expand All @@ -109,6 +113,7 @@ class VersionServiceTest extends Specification {
VersionProperties properties = versionProperties().withVersionCreator({ v, t -> v }).build()
resolver.resolveVersion(properties, tagProperties, nextVersionProperties) >> new VersionContext(
Version.valueOf("1.0.1"),
"v1.0.0",
true,
Version.valueOf("1.0.0"),
new ScmPosition('', '', 'master')
Expand All @@ -121,6 +126,7 @@ class VersionServiceTest extends Specification {
version.undecoratedVersion == '1.0.1'
version.decoratedVersion == '1.0.1-SNAPSHOT'
version.previousVersion == '1.0.0'
version.previousTag == 'v1.0.0'
}

def "should sanitize version if flag is set to true"() {
Expand All @@ -129,6 +135,7 @@ class VersionServiceTest extends Specification {

resolver.resolveVersion(properties, tagProperties, nextVersionProperties) >> new VersionContext(
Version.valueOf("1.0.1"),
null,
true,
Version.valueOf("1.0.1"),
new ScmPosition('', '', 'master')
Expand All @@ -150,6 +157,7 @@ class VersionServiceTest extends Specification {

resolver.resolveVersion(properties, tagProperties, nextVersionProperties) >> new VersionContext(
Version.valueOf("1.0.1"),
null,
true,
Version.valueOf("1.0.1"),
new ScmPosition('', '', 'master')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package pl.allegro.tech.build.axion.release.domain.hooks
import com.github.zafarkhaja.semver.Version
import pl.allegro.tech.build.axion.release.domain.VersionContext
import pl.allegro.tech.build.axion.release.domain.properties.HooksProperties
import pl.allegro.tech.build.axion.release.domain.scm.ScmPosition
import pl.allegro.tech.build.axion.release.domain.scm.ScmPositionBuilder
import spock.lang.Specification

import static pl.allegro.tech.build.axion.release.domain.scm.ScmPositionBuilder.scmPosition
Expand All @@ -15,6 +13,7 @@ class ReleaseHooksRunnerTest extends Specification {

private VersionContext version = new VersionContext(
new Version.Builder().setNormalVersion('2.0.0-SNAPSHOT').build(),
null,
true,
new Version.Builder().setNormalVersion('1.0.0').build(),
scmPosition('master')
Expand Down

0 comments on commit d64c698

Please sign in to comment.