diff --git a/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java b/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java index bb53b03..e07daea 100644 --- a/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java +++ b/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java @@ -184,20 +184,23 @@ public void prependToFile(final File file) throws GitChangelogRepositoryExceptio * been configured. */ public SemanticVersion getNextSemanticVersion() throws GitChangelogRepositoryException { - final boolean fromGiven = this.settings.getFromRevision().isPresent(); - final SemanticVersion highestSemanticVersion = this.getHighestSemanticVersion(); + final GitChangelogApi api = GitChangelogApi.gitChangelogApiBuilder(); + api.settings = this.settings.copy(); + + final boolean fromGiven = api.getSettings().getFromRevision().isPresent(); + final SemanticVersion highestSemanticVersion = api.getHighestSemanticVersion(); if (!fromGiven) { final Optional tag = highestSemanticVersion.findTag(); if (tag.isPresent()) { - this.withFromRevision(tag.get()); + api.withFromRevision(tag.get()); } } - final Changelog changelog = this.getChangelog(false); - final List tags = this.getTagsAsStrings(changelog); - final List commits = this.getCommitMessages(changelog); - final String majorVersionPattern = this.settings.getSemanticMajorPattern().orElse(null); - final String minorVersionPattern = this.settings.getSemanticMinorPattern(); - final String patchVersionPattern = this.settings.getSemanticPatchPattern(); + final Changelog changelog = api.getChangelog(false); + final List tags = api.getTagsAsStrings(changelog); + final List commits = api.getCommitMessages(changelog); + final String majorVersionPattern = api.settings.getSemanticMajorPattern().orElse(null); + final String minorVersionPattern = api.settings.getSemanticMinorPattern(); + final String patchVersionPattern = api.settings.getSemanticPatchPattern(); final SemanticVersioning semanticVersioning = new SemanticVersioning( tags, commits, majorVersionPattern, minorVersionPattern, patchVersionPattern); diff --git a/src/main/java/se/bjurr/gitchangelog/internal/settings/Settings.java b/src/main/java/se/bjurr/gitchangelog/internal/settings/Settings.java index c3730dc..75dbc60 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/settings/Settings.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/settings/Settings.java @@ -30,6 +30,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; @@ -373,13 +374,25 @@ public Optional getRedmineServer() { public static Settings fromFile(final URL url) { try { - return gson.fromJson( - new String(Files.readAllBytes(Paths.get(url.toURI())), UTF_8), Settings.class); + final String json = new String(Files.readAllBytes(Paths.get(url.toURI())), UTF_8); + return fromJson(json); } catch (final Exception e) { throw new RuntimeException("Cannot read " + url, e); } } + public static Settings fromJson(final String json) { + return gson.fromJson(json, Settings.class); + } + + public String toJson() { + return gson.toJson(this); + } + + public Settings copy() { + return Settings.fromJson(this.toJson()); + } + public String getUntaggedName() { return ofNullable(this.untaggedName).orElse(DEFAULT_UNTAGGED_NAME); } @@ -710,4 +723,228 @@ public void addJiraIssueAdditionalField(final String jiraIssueAdditionalField) { } this.jiraIssueAdditionalFields.add(jiraIssueAdditionalField); } + + @Override + public int hashCode() { + return Objects.hash( + this.customIssues, + this.dateFormat, + this.encoding, + this.extendedRestHeaders, + this.extendedVariables, + this.fromRepo, + this.fromRevision, + this.fromRevisionStrategy, + this.gitHubApi, + this.gitHubEnabled, + this.gitHubIssuePattern, + this.gitHubToken, + this.gitLabEnabled, + this.gitLabIssuePattern, + this.gitLabProjectName, + this.gitLabServer, + this.gitLabToken, + this.ignoreCommitsIfMessageMatches, + this.ignoreCommitsIfOlderThan, + this.ignoreCommitsWithoutIssue, + this.ignoreTagsIfNameMatches, + this.jiraBearer, + this.jiraEnabled, + this.jiraIssueAdditionalFields, + this.jiraIssuePattern, + this.jiraPassword, + this.jiraServer, + this.jiraToken, + this.jiraUsername, + this.noIssueName, + this.readableTagName, + this.redmineEnabled, + this.redmineIssuePattern, + this.redminePassword, + this.redmineServer, + this.redmineToken, + this.redmineUsername, + this.removeIssueFromMessage, + this.semanticMajorPattern, + this.semanticMinorPattern, + this.semanticPatchPattern, + this.subDirFilter, + this.templateBaseDir, + this.templatePath, + this.templateSuffix, + this.timeZone, + this.toRevision, + this.toRevisionStrategy, + this.untaggedName, + this.useIntegrations); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (this.getClass() != obj.getClass()) { + return false; + } + final Settings other = (Settings) obj; + return Objects.equals(this.customIssues, other.customIssues) + && Objects.equals(this.dateFormat, other.dateFormat) + && Objects.equals(this.encoding, other.encoding) + && Objects.equals(this.extendedRestHeaders, other.extendedRestHeaders) + && Objects.equals(this.extendedVariables, other.extendedVariables) + && Objects.equals(this.fromRepo, other.fromRepo) + && Objects.equals(this.fromRevision, other.fromRevision) + && this.fromRevisionStrategy == other.fromRevisionStrategy + && Objects.equals(this.gitHubApi, other.gitHubApi) + && this.gitHubEnabled == other.gitHubEnabled + && Objects.equals(this.gitHubIssuePattern, other.gitHubIssuePattern) + && Objects.equals(this.gitHubToken, other.gitHubToken) + && this.gitLabEnabled == other.gitLabEnabled + && Objects.equals(this.gitLabIssuePattern, other.gitLabIssuePattern) + && Objects.equals(this.gitLabProjectName, other.gitLabProjectName) + && Objects.equals(this.gitLabServer, other.gitLabServer) + && Objects.equals(this.gitLabToken, other.gitLabToken) + && Objects.equals(this.ignoreCommitsIfMessageMatches, other.ignoreCommitsIfMessageMatches) + && Objects.equals(this.ignoreCommitsIfOlderThan, other.ignoreCommitsIfOlderThan) + && this.ignoreCommitsWithoutIssue == other.ignoreCommitsWithoutIssue + && Objects.equals(this.ignoreTagsIfNameMatches, other.ignoreTagsIfNameMatches) + && Objects.equals(this.jiraBearer, other.jiraBearer) + && this.jiraEnabled == other.jiraEnabled + && Objects.equals(this.jiraIssueAdditionalFields, other.jiraIssueAdditionalFields) + && Objects.equals(this.jiraIssuePattern, other.jiraIssuePattern) + && Objects.equals(this.jiraPassword, other.jiraPassword) + && Objects.equals(this.jiraServer, other.jiraServer) + && Objects.equals(this.jiraToken, other.jiraToken) + && Objects.equals(this.jiraUsername, other.jiraUsername) + && Objects.equals(this.noIssueName, other.noIssueName) + && Objects.equals(this.readableTagName, other.readableTagName) + && this.redmineEnabled == other.redmineEnabled + && Objects.equals(this.redmineIssuePattern, other.redmineIssuePattern) + && Objects.equals(this.redminePassword, other.redminePassword) + && Objects.equals(this.redmineServer, other.redmineServer) + && Objects.equals(this.redmineToken, other.redmineToken) + && Objects.equals(this.redmineUsername, other.redmineUsername) + && this.removeIssueFromMessage == other.removeIssueFromMessage + && Objects.equals(this.semanticMajorPattern, other.semanticMajorPattern) + && Objects.equals(this.semanticMinorPattern, other.semanticMinorPattern) + && Objects.equals(this.semanticPatchPattern, other.semanticPatchPattern) + && Objects.equals(this.subDirFilter, other.subDirFilter) + && Objects.equals(this.templateBaseDir, other.templateBaseDir) + && Objects.equals(this.templatePath, other.templatePath) + && Objects.equals(this.templateSuffix, other.templateSuffix) + && Objects.equals(this.timeZone, other.timeZone) + && Objects.equals(this.toRevision, other.toRevision) + && this.toRevisionStrategy == other.toRevisionStrategy + && Objects.equals(this.untaggedName, other.untaggedName) + && this.useIntegrations == other.useIntegrations; + } + + @Override + public String toString() { + return "Settings [fromRepo=" + + this.fromRepo + + ", fromRevision=" + + this.fromRevision + + ", fromRevisionStrategy=" + + this.fromRevisionStrategy + + ", toRevision=" + + this.toRevision + + ", toRevisionStrategy=" + + this.toRevisionStrategy + + ", ignoreTagsIfNameMatches=" + + this.ignoreTagsIfNameMatches + + ", ignoreCommitsIfMessageMatches=" + + this.ignoreCommitsIfMessageMatches + + ", ignoreCommitsIfOlderThan=" + + this.ignoreCommitsIfOlderThan + + ", untaggedName=" + + this.untaggedName + + ", templatePath=" + + this.templatePath + + ", templateBaseDir=" + + this.templateBaseDir + + ", templateSuffix=" + + this.templateSuffix + + ", readableTagName=" + + this.readableTagName + + ", dateFormat=" + + this.dateFormat + + ", noIssueName=" + + this.noIssueName + + ", timeZone=" + + this.timeZone + + ", removeIssueFromMessage=" + + this.removeIssueFromMessage + + ", jiraEnabled=" + + this.jiraEnabled + + ", jiraServer=" + + this.jiraServer + + ", jiraIssuePattern=" + + this.jiraIssuePattern + + ", jiraIssueAdditionalFields=" + + this.jiraIssueAdditionalFields + + ", jiraUsername=" + + this.jiraUsername + + ", jiraPassword=" + + this.jiraPassword + + ", jiraToken=" + + this.jiraToken + + ", jiraBearer=" + + this.jiraBearer + + ", redmineEnabled=" + + this.redmineEnabled + + ", redmineServer=" + + this.redmineServer + + ", redmineIssuePattern=" + + this.redmineIssuePattern + + ", redmineUsername=" + + this.redmineUsername + + ", redminePassword=" + + this.redminePassword + + ", redmineToken=" + + this.redmineToken + + ", gitHubEnabled=" + + this.gitHubEnabled + + ", gitHubApi=" + + this.gitHubApi + + ", gitHubToken=" + + this.gitHubToken + + ", gitHubIssuePattern=" + + this.gitHubIssuePattern + + ", customIssues=" + + this.customIssues + + ", extendedVariables=" + + this.extendedVariables + + ", extendedRestHeaders=" + + this.extendedRestHeaders + + ", ignoreCommitsWithoutIssue=" + + this.ignoreCommitsWithoutIssue + + ", gitLabEnabled=" + + this.gitLabEnabled + + ", gitLabServer=" + + this.gitLabServer + + ", gitLabToken=" + + this.gitLabToken + + ", gitLabIssuePattern=" + + this.gitLabIssuePattern + + ", gitLabProjectName=" + + this.gitLabProjectName + + ", semanticMajorPattern=" + + this.semanticMajorPattern + + ", semanticMinorPattern=" + + this.semanticMinorPattern + + ", semanticPatchPattern=" + + this.semanticPatchPattern + + ", useIntegrations=" + + this.useIntegrations + + ", subDirFilter=" + + this.subDirFilter + + ", encoding=" + + this.encoding + + "]"; + } } diff --git a/src/main/java/se/bjurr/gitchangelog/internal/settings/SettingsIssue.java b/src/main/java/se/bjurr/gitchangelog/internal/settings/SettingsIssue.java index 18ba85a..b375d95 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/settings/SettingsIssue.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/settings/SettingsIssue.java @@ -5,6 +5,7 @@ import static se.bjurr.gitchangelog.internal.util.Preconditions.checkNotNull; import java.io.Serializable; +import java.util.Objects; import java.util.Optional; import se.bjurr.gitchangelog.api.model.Issue; @@ -72,4 +73,43 @@ public String getName() { public String getPattern() { return this.pattern; } + + @Override + public int hashCode() { + return Objects.hash(this.link, this.name, this.pattern, this.title, this.type); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (this.getClass() != obj.getClass()) { + return false; + } + final SettingsIssue other = (SettingsIssue) obj; + return Objects.equals(this.link, other.link) + && Objects.equals(this.name, other.name) + && Objects.equals(this.pattern, other.pattern) + && Objects.equals(this.title, other.title) + && this.type == other.type; + } + + @Override + public String toString() { + return "SettingsIssue [type=" + + this.type + + ", name=" + + this.name + + ", title=" + + this.title + + ", pattern=" + + this.pattern + + ", link=" + + this.link + + "]"; + } } diff --git a/src/test/java/se/bjurr/gitchangelog/internal/settings/SettingsTest.java b/src/test/java/se/bjurr/gitchangelog/internal/settings/SettingsTest.java index 1c2b5d3..46485f7 100644 --- a/src/test/java/se/bjurr/gitchangelog/internal/settings/SettingsTest.java +++ b/src/test/java/se/bjurr/gitchangelog/internal/settings/SettingsTest.java @@ -11,15 +11,32 @@ public class SettingsTest { @Test public void nullFromRevisionCanBeSet() { - Settings settings = new Settings(); + final Settings settings = new Settings(); settings.setFromRevision(null); assertThat(settings.getFromRevision()).isEmpty(); } @Test public void nullToRevisionCanBeSet() { - Settings settings = new Settings(); + final Settings settings = new Settings(); settings.setToRevision(null); assertThat(settings.getToRevision()).isEmpty(); } + + @Test + public void canBeSerializedAndDeserialized() { + final Settings settings = + Settings.fromFile(Settings.class.getResource("/settings/git-changelog-test-settings.json")); + final String settingsSerialized = settings.toJson(); + final Settings settingsDeserialized = Settings.fromJson(settingsSerialized); + assertThat(settingsDeserialized).isEqualTo(settings); + } + + @Test + public void canBeCopied() { + final Settings settings = + Settings.fromFile(Settings.class.getResource("/settings/git-changelog-test-settings.json")); + final Settings settingsCopy = settings.copy(); + assertThat(settingsCopy).isEqualTo(settings); + } } diff --git a/src/test/java/se/bjurr/gitchangelog/semantic/SemanticVersioningTest.java b/src/test/java/se/bjurr/gitchangelog/semantic/SemanticVersioningTest.java index 7d754c4..490112c 100644 --- a/src/test/java/se/bjurr/gitchangelog/semantic/SemanticVersioningTest.java +++ b/src/test/java/se/bjurr/gitchangelog/semantic/SemanticVersioningTest.java @@ -42,7 +42,7 @@ public void before() { public void smokeTest1() throws Throwable { final GitChangelogApi builder1 = gitChangelogApiBuilder() // - .withToCommit("7c1c366") // + .withToRevision("7c1c366") // .withSemanticMajorVersionPattern("breaking:.*") // .withSemanticMinorVersionPattern("update:.*"); @@ -51,7 +51,7 @@ public void smokeTest1() throws Throwable { final GitChangelogApi builder2 = gitChangelogApiBuilder() // - .withToCommit("7c1c366") // + .withToRevision("7c1c366") // .withSemanticMajorVersionPattern("breaking:.*") // .withSemanticMinorVersionPattern("update:.*"); assertThat(builder2.getHighestSemanticVersion().toString()).isEqualTo("1.144.4"); @@ -62,7 +62,7 @@ public void smokeTest1() throws Throwable { public void smokeTest2() throws Throwable { final SemanticVersion nextSemanticVersion = gitChangelogApiBuilder() - .withToRef("1.155.0") // + .withToRevision("1.155.0") // .withSemanticMajorVersionPattern("^[Bb]reaking.*") .withSemanticMinorVersionPattern("^[Ff]eat.*") .getNextSemanticVersion(); @@ -70,6 +70,19 @@ public void smokeTest2() throws Throwable { assertThat(nextSemanticVersion.getVersionStep()).isEqualTo(PATCH); } + @Test + public void nextSemanticVersionShouldNotEffectHighestSemanticVersion() throws Throwable { + final GitChangelogApi api = gitChangelogApiBuilder().withToRevision("1.155.0"); + assertThat(api.getNextSemanticVersion().toString()).isEqualTo("1.155.1"); + assertThat(api.getNextSemanticVersion().toString()).isEqualTo("1.155.1"); + assertThat(api.getHighestSemanticVersion().toString()).isEqualTo("1.155.0"); + assertThat(api.getHighestSemanticVersion().toString()).isEqualTo("1.155.0"); + assertThat(api.getNextSemanticVersion().toString()).isEqualTo("1.155.1"); + assertThat(api.getNextSemanticVersion().toString()).isEqualTo("1.155.1"); + assertThat(api.getHighestSemanticVersion().toString()).isEqualTo("1.155.0"); + assertThat(api.getHighestSemanticVersion().toString()).isEqualTo("1.155.0"); + } + @Test public void testMajorStep() throws Throwable { this.tags.add("v1.0.0");