diff --git a/src/main/java/pl/allegro/tech/build/axion/release/domain/VersionSorter.java b/src/main/java/pl/allegro/tech/build/axion/release/domain/VersionSorter.java index d62b3947..1513082c 100644 --- a/src/main/java/pl/allegro/tech/build/axion/release/domain/VersionSorter.java +++ b/src/main/java/pl/allegro/tech/build/axion/release/domain/VersionSorter.java @@ -1,6 +1,8 @@ package pl.allegro.tech.build.axion.release.domain; import com.github.zafarkhaja.semver.Version; +import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; import pl.allegro.tech.build.axion.release.domain.scm.TaggedCommits; import pl.allegro.tech.build.axion.release.domain.scm.TagsOnCommit; @@ -21,6 +23,7 @@ * * any highest (stable or alpha) */ class VersionSorter { + private static final Logger logger = Logging.getLogger(VersionSorter.class); Result pickTaggedCommit( TaggedCommits taggedCommits, @@ -32,9 +35,11 @@ Result pickTaggedCommit( Set versions = new LinkedHashSet<>(); LinkedHashMap isVersionNextVersion = new LinkedHashMap<>(); LinkedHashMap versionToCommit = new LinkedHashMap<>(); + boolean tagsFound = false; for (TagsOnCommit tagsEntry : taggedCommits.getCommits()) { List tags = tagsEntry.getTags(); + tagsFound = tagsFound || !tags.isEmpty(); // next version should be ignored when tag is on head // and there are other, normal tags on it @@ -47,10 +52,13 @@ Result pickTaggedCommit( for (String tag : tags) { boolean isNextVersion = nextVersionTagPattern.matcher(tag).matches(); if (isNextVersion && (ignoreNextVersionTags || ignoreNextVersionOnHead)) { + logger.lifecycle("Ignoring tag: {}, because it's a next version tag and it's not forced", tag); continue; } Version version = versionFactory.versionFromTag(tag); + logger.lifecycle("Detected version: {} from tag: {}", version, tag); + boolean versionDidNotExist = versions.add(version); boolean isNormalVersion = !isNextVersion; // normal tags have precedence over nextVersion tags with same version @@ -68,16 +76,16 @@ Result pickTaggedCommit( } } - - - List versionList = new ArrayList<>(versions); - Collections.sort(versionList, Collections.reverseOrder()); - - Version version = (versionList.isEmpty() ? versionFactory.initialVersion() : versionList.get(0)); - if (version == null) { - version = versionFactory.initialVersion(); + if (!tagsFound) { + logger.lifecycle("No tags were found in git history"); } + Version version = versions.stream() + .filter(Objects::nonNull) + .max(Comparator.naturalOrder()) // find latest (max) version + .orElseGet(versionFactory::initialVersion); // if no versions found, use initial version + logger.lifecycle("Version: {}, was selected from versions: {}", version, versions); + TagsOnCommit versionCommit = versionToCommit.get(version); return new Result( diff --git a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/scm/ScmPropertiesBuilder.groovy b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/scm/ScmPropertiesBuilder.groovy index 3e496d6b..4572bbf5 100644 --- a/src/test/groovy/pl/allegro/tech/build/axion/release/domain/scm/ScmPropertiesBuilder.groovy +++ b/src/test/groovy/pl/allegro/tech/build/axion/release/domain/scm/ScmPropertiesBuilder.groovy @@ -41,7 +41,7 @@ class ScmPropertiesBuilder { true, ['main', 'master'] as Set, false, - true + true ) }