Skip to content

Commit

Permalink
fix: semantic version stepping
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Jun 9, 2021
1 parent caabfcf commit 3c0cf44
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
11 changes: 10 additions & 1 deletion src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,23 @@ public void prependToFile(final File file) throws GitChangelogRepositoryExceptio
* been configured.
*/
public SemanticVersion getNextSemanticVersion() throws GitChangelogRepositoryException {
final boolean fromGiven =
this.settings.getFromRef().isPresent() || this.settings.getFromCommit().isPresent();
final SemanticVersion highestSemanticVersion = this.getHighestSemanticVersion();
if (!fromGiven) {
final Optional<String> tag = highestSemanticVersion.findTag();
if (tag.isPresent()) {
this.withFromRef(tag.get());
}
}
final Changelog changelog = this.getChangelog(false);
final List<String> tags = this.getTagsAsStrings(changelog);
final List<String> commits = this.getCommitMessages(changelog);
final String majorVersionPattern = this.settings.getSemanticMajorPattern().orElse(null);
final String minorVersionPattern = this.settings.getSemanticMinorPattern().orElse(null);
final SemanticVersioning semanticVersioning =
new SemanticVersioning(tags, commits, majorVersionPattern, minorVersionPattern);
return semanticVersioning.getNextVersion();
return semanticVersioning.getNextVersion(highestSemanticVersion);
}

public SemanticVersion getHighestSemanticVersion() throws GitChangelogRepositoryException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@ public enum VERSION_STEP {
private final List<String> commits;
private final Pattern majorPattern;
private final Pattern minorPattern;
private final List<String> tags;

public SemanticVersioning(
final List<String> tags,
final List<String> commits,
final String majorPattern,
final String minorPattern) {
this.tags = tags;
this.commits = commits;
this.majorPattern = Pattern.compile(this.notNull(majorPattern, "majorPattern"));
this.minorPattern = Pattern.compile(this.notNull(minorPattern, "minorPattern"));
}

public SemanticVersion getNextVersion() {
final SemanticVersion highestVersion = SemanticVersioning.getHighestVersion(this.tags);
public SemanticVersion getNextVersion(final SemanticVersion highestVersion) {
final VERSION_STEP versionStep = this.getVersionStep();
if (versionStep == VERSION_STEP.MAJOR) {
return new SemanticVersion(highestVersion.getMajor() + 1, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.Before;
import org.junit.Test;
import se.bjurr.gitchangelog.api.GitChangelogApi;
import se.bjurr.gitchangelog.internal.semantic.SemanticVersion;
import se.bjurr.gitchangelog.internal.semantic.SemanticVersioning;

public class SemanticVersioningTest {
Expand All @@ -20,35 +21,50 @@ public class SemanticVersioningTest {

@Before
public void before() {
this.tags = new ArrayList<String>();
this.commits = new ArrayList<String>();
this.tags = new ArrayList<>();
this.commits = new ArrayList<>();
this.majorPattern = "[Bb]reaking:.*";
this.minorPattern = "[Uu]pdate:.*";
this.sut =
new SemanticVersioning(this.tags, this.commits, this.majorPattern, this.minorPattern);
}

@Test
public void smokeTest() throws Throwable {
final GitChangelogApi builder =
public void smokeTest1() throws Throwable {
final GitChangelogApi builder1 =
gitChangelogApiBuilder() //
.withToCommit("7c1c366") //
.withSemanticMajorVersionPattern("breaking:.*") //
.withSemanticMinorVersionPattern("update:.*");

assertThat(builder.getNextSemanticVersion().toString()).isEqualTo("1.144.5");
assertThat(builder.getHighestSemanticVersion().toString()).isEqualTo("1.144.4");
assertThat(builder1.getNextSemanticVersion().toString()).isEqualTo("1.144.5");

final GitChangelogApi builder2 =
gitChangelogApiBuilder() //
.withToCommit("7c1c366") //
.withSemanticMajorVersionPattern("breaking:.*") //
.withSemanticMinorVersionPattern("update:.*");
assertThat(builder2.getHighestSemanticVersion().toString()).isEqualTo("1.144.4");
}

@Test
public void smokeTest2() throws Throwable {
final SemanticVersion nextSemanticVersion =
gitChangelogApiBuilder()
.withToRef("1.155.0") //
.withSemanticMajorVersionPattern("^[Bb]reaking.*")
.withSemanticMinorVersionPattern("^[Ff]eat.*")
.getNextSemanticVersion();
assertThat(nextSemanticVersion.toString()).isEqualTo("1.155.1");
}

@Test
public void testMajorStep() throws Throwable {
this.tags.add("v1.0.0");
this.commits.add("breaking: whatever");

assertThat(
SemanticVersioning.getHighestVersion(this.tags)
+ " -> "
+ this.sut.getNextVersion()) //
final SemanticVersion highestVersion = SemanticVersioning.getHighestVersion(this.tags);
assertThat(highestVersion + " -> " + this.sut.getNextVersion(highestVersion)) //
.isEqualTo("1.0.0 -> 2.0.0");
}

Expand All @@ -57,10 +73,8 @@ public void testMinorStep() throws Throwable {
this.tags.add("v1.0.0");
this.commits.add("update: whatever");

assertThat(
SemanticVersioning.getHighestVersion(this.tags)
+ " -> "
+ this.sut.getNextVersion()) //
final SemanticVersion highestVersion = SemanticVersioning.getHighestVersion(this.tags);
assertThat(highestVersion + " -> " + this.sut.getNextVersion(highestVersion)) //
.isEqualTo("1.0.0 -> 1.1.0");
}

Expand All @@ -69,10 +83,8 @@ public void testPatchStep() throws Throwable {
this.tags.add("v1.0.0");
this.commits.add("chore: whatever");

assertThat(
SemanticVersioning.getHighestVersion(this.tags)
+ " -> "
+ this.sut.getNextVersion()) //
final SemanticVersion highestVersion = SemanticVersioning.getHighestVersion(this.tags);
assertThat(highestVersion + " -> " + this.sut.getNextVersion(highestVersion)) //
.isEqualTo("1.0.0 -> 1.0.1");
}
}

0 comments on commit 3c0cf44

Please # to comment.