diff --git a/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java b/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java index 7063a080..6cde34c9 100644 --- a/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java +++ b/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java @@ -14,8 +14,8 @@ import com.github.jknack.handlebars.io.FileTemplateLoader; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.StringWriter; import java.io.Writer; import java.net.URL; @@ -50,17 +50,17 @@ import se.bjurr.gitchangelog.internal.util.ResourceLoader; @SuppressFBWarnings("PATH_TRAVERSAL_IN") -public class GitChangelogApi { - - public static GitChangelogApi gitChangelogApiBuilder() { - return new GitChangelogApi(); - } +public final class GitChangelogApi { private Settings settings; private String templateContent; private Handlebars handlebars; private final AtomicInteger helperCounter = new AtomicInteger(); + public static GitChangelogApi gitChangelogApiBuilder() { + return new GitChangelogApi(); + } + private GitChangelogApi() { this.settings = new Settings(); this.handlebars = new Handlebars(); @@ -103,7 +103,6 @@ public Settings getSettings() { * @throws GitChangelogRepositoryException */ public void render(final Writer writer) throws GitChangelogRepositoryException { - Template template = null; final String templateString = this.getTemplateString(); if (this.settings.getTemplateBaseDir() != null) { @@ -112,6 +111,7 @@ public void render(final Writer writer) throws GitChangelogRepositoryException { this.settings.getTemplateBaseDir(), this.settings.getTemplateSuffix())); } + Template template; try { template = this.handlebars.compileInline(templateString); } catch (final IOException e) { @@ -119,11 +119,11 @@ public void render(final Writer writer) throws GitChangelogRepositoryException { } try { - final Changelog changelog = this.getChangelog(this.settings.isUseIntegrations()); final Map extendedVariables = this.settings.getExtendedVariables(); if (extendedVariables == null) { throw new IllegalStateException("extendedVariables cannot be null"); } + final Changelog changelog = this.getChangelog(this.settings.isUseIntegrations()); final Context changelogContext = Context.newContext(changelog).combine(extendedVariables); template.apply(changelogContext, writer); } catch (final IOException e) { @@ -175,7 +175,7 @@ public void prependToFile(final File file) throws GitChangelogRepositoryExceptio final byte[] bytesToPrepend = this.render().getBytes(this.settings.getEncoding()); final byte[] originalBytes = Files.readAllBytes(file.toPath()); - try (final FileOutputStream outputStream = new FileOutputStream(file)) { + try (final OutputStream outputStream = Files.newOutputStream(file.toPath())) { outputStream.write(bytesToPrepend); outputStream.write(originalBytes); } @@ -197,14 +197,14 @@ public SemanticVersion getNextSemanticVersion() throws GitChangelogRepositoryExc } } final Changelog changelog = api.getChangelog(false); - final List tags = api.getTagsAsStrings(changelog); + 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); + commits, majorVersionPattern, minorVersionPattern, patchVersionPattern); return semanticVersioning.getNextVersion(highestSemanticVersion); } @@ -290,7 +290,7 @@ public GitChangelogApi withHandlebarsHelper(final String name, final Helper h * Custom issues are added to support any kind of issue management, perhaps something that is * internal to your project. See {@link SettingsIssue}. */ - public GitChangelogApi withCustomIssue( + public GitChangelogApi withCustomIssue( // NOPMD final String name, final String pattern, final String link, final String title) { this.settings.addCustomIssue(new SettingsIssue(name, pattern, link, title)); return this; diff --git a/src/main/java/se/bjurr/gitchangelog/api/model/Commit.java b/src/main/java/se/bjurr/gitchangelog/api/model/Commit.java index 07e02ea5..c8c887b1 100644 --- a/src/main/java/se/bjurr/gitchangelog/api/model/Commit.java +++ b/src/main/java/se/bjurr/gitchangelog/api/model/Commit.java @@ -10,6 +10,15 @@ public class Commit implements Serializable { private static final long serialVersionUID = 6622555148468372816L; + private final String authorEmailAddress; + private final String authorName; + private final String commitTime; + private final Long commitTimeLong; + private final String hash; + private final String hashFull; + private final Boolean merge; + private final String message; + private static List notFirst(final List stringList) { return stringList.subList(1, stringList.size()); } @@ -57,21 +66,12 @@ static List toMessageItems(final String message) { static String toMessageTitle(final String message) { final List stringList = toNoEmptyStringsList(message); - if (stringList.size() > 0) { + if (!stringList.isEmpty()) { return stringList.get(0).trim(); } return ""; } - private final String authorEmailAddress; - private final String authorName; - private final String commitTime; - private final Long commitTimeLong; - private final String hash; - private final String hashFull; - private final Boolean merge; - private final String message; - public Commit( final String authorName, final String authorEmailAddress, diff --git a/src/main/java/se/bjurr/gitchangelog/api/model/IssueType.java b/src/main/java/se/bjurr/gitchangelog/api/model/IssueType.java index 13daf1ce..e6570bd1 100644 --- a/src/main/java/se/bjurr/gitchangelog/api/model/IssueType.java +++ b/src/main/java/se/bjurr/gitchangelog/api/model/IssueType.java @@ -104,9 +104,6 @@ public boolean equals(final Object obj) { } else if (!this.name.equals(other.name)) { return false; } - if (this.type != other.type) { - return false; - } - return true; + return this.type == other.type; } } diff --git a/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java b/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java index ce6415a4..7110fcc5 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java @@ -35,7 +35,6 @@ import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import se.bjurr.gitchangelog.api.GitChangelogApiConstants; import se.bjurr.gitchangelog.api.InclusivenessStrategy; import se.bjurr.gitchangelog.api.exceptions.GitChangelogRepositoryException; import se.bjurr.gitchangelog.internal.git.model.GitCommit; @@ -126,7 +125,7 @@ public Optional> findObjectId( /** * @param from From, but not including, this commit. Except for the {@link - * GitChangelogApiConstants#ZERO_COMMIT}, it is included. + * se.bjurr.gitchangelog.api.GitChangelogApiConstants#ZERO_COMMIT}, it is included. * @param to To and including this commit. */ public GitRepoData getGitRepoData( @@ -239,11 +238,9 @@ private void addToTags( } private RevCommit firstCommit() { - Git git = null; - try { - git = new Git(this.repository); - final AnyObjectId master = this.getRef(HEAD); - final Iterator itr = git.log().add(master).call().iterator(); + try (Git git = new Git(this.repository)) { + final AnyObjectId head = this.getRef(HEAD); + final Iterator itr = git.log().add(head).call().iterator(); RevCommit last = null; while (itr.hasNext()) { last = itr.next(); @@ -251,10 +248,6 @@ private RevCommit firstCommit() { return last; } catch (final Exception e) { throw new RuntimeException("First commit not found in " + this.repository.getDirectory(), e); - } finally { - if (git != null) { - git.close(); - } } } @@ -585,7 +578,6 @@ private void populateCommitPerTag( } } } - return; } private boolean shouldPrioritizeNewWork( @@ -673,7 +665,7 @@ public List getTags( final RevisionBoundary from = this.toRevCommit(fromObjectId); final RevisionBoundary to = this.toRevCommit(toObjectId); - final List tags = new ArrayList(); + final List tags = new ArrayList<>(); for (final Ref tagRef : this.tagsBetweenFromAndTo(from, to)) { final String commitIdOfRef = this.getPeeled(tagRef).name(); if (commitIdOfRef.equals(to.getRevision().getName())) { diff --git a/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoDataHelper.java b/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoDataHelper.java index 8e065deb..def9450d 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoDataHelper.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoDataHelper.java @@ -9,7 +9,7 @@ import se.bjurr.gitchangelog.internal.git.model.GitTag; import se.bjurr.gitchangelog.internal.model.ParsedIssue; -public class GitRepoDataHelper { +public final class GitRepoDataHelper { public static GitRepoData removeCommitsWithoutIssue( final List allParsedIssues, final GitRepoData gitRepoData) { final Set commitsWithIssues = new TreeSet<>(); diff --git a/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubServiceFactory.java b/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubServiceFactory.java index 81d2ab6a..71f6655b 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubServiceFactory.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubServiceFactory.java @@ -14,7 +14,7 @@ @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") public class GitHubServiceFactory { - static Interceptor interceptor; + static Interceptor interceptor; // NOPMD public static void setInterceptor(final Interceptor interceptor) { GitHubServiceFactory.interceptor = interceptor; @@ -27,7 +27,7 @@ public static synchronized GitHubService getGitHubService( } final File cacheDir = new File(".okhttpcache"); cacheDir.mkdir(); - final Cache cache = new Cache(cacheDir, 1024 * 1024 * 10); + final Cache cache = new Cache(cacheDir, 1024 * 1024 * 10); // NOPMD final OkHttpClient.Builder builder = new OkHttpClient.Builder().cache(cache).connectTimeout(10, SECONDS); diff --git a/src/main/java/se/bjurr/gitchangelog/internal/integrations/jira/JiraClient.java b/src/main/java/se/bjurr/gitchangelog/internal/integrations/jira/JiraClient.java index bbc770f0..d8243def 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/integrations/jira/JiraClient.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/integrations/jira/JiraClient.java @@ -59,7 +59,7 @@ protected String getEndpoint(final String issue) { } public JiraClient withIssueAdditionalFields(final List fields) { - final List newFields = new ArrayList(fields); + final List newFields = new ArrayList<>(fields); Collections.sort(newFields); this.fields = Collections.unmodifiableList(newFields); return this; diff --git a/src/main/java/se/bjurr/gitchangelog/internal/integrations/redmine/RedmineClient.java b/src/main/java/se/bjurr/gitchangelog/internal/integrations/redmine/RedmineClient.java index 12be3a33..06a13a1f 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/integrations/redmine/RedmineClient.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/integrations/redmine/RedmineClient.java @@ -22,23 +22,21 @@ public String getApi() { } protected String getEndpoint(final String issue) { - final String issueNo = getIssueNumber(issue); - final String endpoint = this.api + "/issues/" + issueNo + ".json"; - return endpoint; + final String issueNo = this.getIssueNumber(issue); + return this.api + "/issues/" + issueNo + ".json"; } protected RedmineIssue toRedmineIssue(final String issue, final String json) { - final String issueNo = getIssueNumber(issue); + final String issueNo = this.getIssueNumber(issue); final String title = read(json, "$.issue.subject"); final String description = read(json, "$.issue.description"); final String type = read(json, "$.issue.tracker.name"); final String link = this.api + "/issues/" + issueNo; - final RedmineIssue redmineIssue = new RedmineIssue(title, description, link, issue, type); - return redmineIssue; + return new RedmineIssue(title, description, link, issue, type); } - protected String getIssueNumber(String issue) { + protected String getIssueNumber(final String issue) { return issue.startsWith("#") ? issue.substring(1) : issue; } diff --git a/src/main/java/se/bjurr/gitchangelog/internal/integrations/rest/RestClient.java b/src/main/java/se/bjurr/gitchangelog/internal/integrations/rest/RestClient.java index 69749103..aff1a408 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/integrations/rest/RestClient.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/integrations/rest/RestClient.java @@ -105,11 +105,21 @@ protected HttpURLConnection openConnection(final URL url) throws Exception { protected String getResponse(final HttpURLConnection conn) throws Exception { if (mockedRestClient == null) { - final InputStream inputStream = conn.getInputStream(); - final InputStreamReader inputStreamReader = - new InputStreamReader(inputStream, StandardCharsets.UTF_8); - try (BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) { - return bufferedReader.readLine(); + try (final InputStream inputStream = conn.getInputStream()) { + try (final InputStreamReader inputStreamReader = + new InputStreamReader(inputStream, StandardCharsets.UTF_8)) { + try (BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) { + final StringBuilder sb = new StringBuilder(); + while (true) { + final String nextline = bufferedReader.readLine(); + if (nextline == null) { + break; + } + sb.append(nextline); + } + return sb.toString(); + } + } } } return mockedRestClient.getResponse(conn); diff --git a/src/main/java/se/bjurr/gitchangelog/internal/issues/IssueParser.java b/src/main/java/se/bjurr/gitchangelog/internal/issues/IssueParser.java index dda60ab3..b3735800 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/issues/IssueParser.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/issues/IssueParser.java @@ -77,7 +77,7 @@ public List parseForIssues(final boolean useIntegrations) { continue; } if (!parsedIssuePerIssue.containsKey(matchedIssue)) { - ParsedIssue parsedIssue = null; + ParsedIssue parsedIssue; if (issuePattern.getType() == GITHUB) { parsedIssue = this.createParsedIssue(gitHubHelper, issuePattern, matchedIssue); } else if (issuePattern.getType() == GITLAB) { diff --git a/src/main/java/se/bjurr/gitchangelog/internal/model/Transformer.java b/src/main/java/se/bjurr/gitchangelog/internal/model/Transformer.java index 8c067c30..1009c3e1 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/model/Transformer.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/model/Transformer.java @@ -234,7 +234,7 @@ private List filterWithCommits(final List issues) { } private String format(final Date commitTime) { - final SimpleDateFormat df = new SimpleDateFormat(this.settings.getDateFormat()); + final SimpleDateFormat df = new SimpleDateFormat(this.settings.getDateFormat(), Locale.ENGLISH); df.setTimeZone(getTimeZone(this.settings.getTimeZone())); return df.format(commitTime); } diff --git a/src/main/java/se/bjurr/gitchangelog/internal/semantic/SemanticVersion.java b/src/main/java/se/bjurr/gitchangelog/internal/semantic/SemanticVersion.java index d1ab9c00..66e4c33c 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/semantic/SemanticVersion.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/semantic/SemanticVersion.java @@ -10,19 +10,18 @@ public class SemanticVersion implements Serializable { private final int minor; private final int major; private String tag; - private VERSION_STEP versionStep; + private final VERSION_STEP versionStep; - public SemanticVersion(final int major, final int minor, final int patch) { + public SemanticVersion( + final int major, final int minor, final int patch, final VERSION_STEP versionStep) { this.major = major; this.minor = minor; this.patch = patch; - this.versionStep = VERSION_STEP.NONE; + this.versionStep = versionStep; } - public SemanticVersion( - final int major, final int minor, final int patch, final VERSION_STEP versionStep) { - this(major, minor, patch); - this.versionStep = versionStep; + public SemanticVersion(final int major, final int minor, final int patch) { + this(major, minor, patch, VERSION_STEP.NONE); } public int getMajor() { diff --git a/src/main/java/se/bjurr/gitchangelog/internal/semantic/SemanticVersioning.java b/src/main/java/se/bjurr/gitchangelog/internal/semantic/SemanticVersioning.java index 45d07752..820ac9ec 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/semantic/SemanticVersioning.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/semantic/SemanticVersioning.java @@ -21,7 +21,6 @@ public enum VERSION_STEP { private final Pattern patchPattern; public SemanticVersioning( - final List tags, final List commits, final String majorPattern, final String minorPattern, 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 8786653c..ffa3bcbc 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/settings/Settings.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/settings/Settings.java @@ -224,10 +224,10 @@ public class Settings implements Serializable { private String gitLabProjectName; /** Regular expression to use when determining next semantic version based on commits. */ - private String semanticMajorPattern = null; + private String semanticMajorPattern; /** Regular expression to use when determining next semantic version based on commits. */ - private String semanticMinorPattern = null; + private String semanticMinorPattern; /** Regular expression to use when determining next semantic version based on commits. */ private String semanticPatchPattern; @@ -645,7 +645,7 @@ private String isRegexp(final String pattern, final String string) { try { Pattern.compile(pattern); } catch (final PatternSyntaxException e) { - throw new RuntimeException(pattern + " in " + string + " is not valid regexp."); + throw new RuntimeException(pattern + " in " + string + " is not valid regexp.", e); } return pattern; } diff --git a/src/main/java/se/bjurr/gitchangelog/internal/util/ResourceLoader.java b/src/main/java/se/bjurr/gitchangelog/internal/util/ResourceLoader.java index 98139f6f..64be0d14 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/util/ResourceLoader.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/util/ResourceLoader.java @@ -16,13 +16,15 @@ private ResourceLoader() {} public static String getResourceOrFile(final String resourceName, final Charset encoding) { String templateString = null; - try { + InputStream inputStream = null; + try { // NOPMD final Path templatePath = Paths.get(resourceName); if (templatePath.toFile().exists()) { templateString = new String(Files.readAllBytes(templatePath), encoding); } else { - InputStream inputStream = - getResourceFromClassLoader(resourceName, ResourceLoader.class.getClassLoader()); + inputStream = + getResourceFromClassLoader( + resourceName, ResourceLoader.class.getClassLoader()); // NOPMD if (inputStream == null) { inputStream = getResourceFromClassLoader( @@ -39,6 +41,14 @@ public static String getResourceOrFile(final String resourceName, final Charset } } catch (final IOException e) { throw new RuntimeException(resourceName, e); + } finally { // NOPMD + if (inputStream != null) { + try { + inputStream.close(); + } catch (final IOException e) { + throw new RuntimeException(e); + } + } } return templateString; } @@ -47,7 +57,7 @@ private static InputStream getResourceFromClassLoader( final String resourceName, final ClassLoader classLoader) { InputStream inputStream = classLoader.getResourceAsStream(resourceName); if (inputStream == null) { - inputStream = classLoader.getResourceAsStream("/" + resourceName); + inputStream = classLoader.getResourceAsStream("/" + resourceName); // NOPMD } return inputStream; } diff --git a/src/test/java/se/bjurr/gitchangelog/semantic/SemanticVersioningTest.java b/src/test/java/se/bjurr/gitchangelog/semantic/SemanticVersioningTest.java index 6f3e1ac1..4951aa91 100644 --- a/src/test/java/se/bjurr/gitchangelog/semantic/SemanticVersioningTest.java +++ b/src/test/java/se/bjurr/gitchangelog/semantic/SemanticVersioningTest.java @@ -35,7 +35,7 @@ public void before() { this.patchPattern = null; this.sut = new SemanticVersioning( - this.tags, this.commits, this.majorPattern, this.minorPattern, this.patchPattern); + this.commits, this.majorPattern, this.minorPattern, this.patchPattern); } @Test @@ -156,7 +156,7 @@ public void testPatchStepWithNoNewCommitsAndPattern() throws Throwable { this.patchPattern = "fix:.*"; this.sut = new SemanticVersioning( - this.tags, this.commits, this.majorPattern, this.minorPattern, this.patchPattern); + this.commits, this.majorPattern, this.minorPattern, this.patchPattern); final SemanticVersion highestVersion = SemanticVersioning.getHighestVersion(this.tags); assertThat(highestVersion + " -> " + this.sut.getNextVersion(highestVersion)) // .isEqualTo("1.0.0 -> 1.0.0"); @@ -172,7 +172,7 @@ public void testPatchStepWithPatternNotMatching() throws Throwable { this.patchPattern = "fix:.*"; this.sut = new SemanticVersioning( - this.tags, this.commits, this.majorPattern, this.minorPattern, this.patchPattern); + this.commits, this.majorPattern, this.minorPattern, this.patchPattern); assertThat(highestVersion + " -> " + this.sut.getNextVersion(highestVersion)) // .isEqualTo("1.0.0 -> 1.0.0"); assertThat(this.sut.getNextVersion(highestVersion).getVersionStep()).isEqualTo(NONE); @@ -187,7 +187,7 @@ public void testPatchStepWithPatternMatching() throws Throwable { this.patchPattern = "fix:.*"; this.sut = new SemanticVersioning( - this.tags, this.commits, this.majorPattern, this.minorPattern, this.patchPattern); + this.commits, this.majorPattern, this.minorPattern, this.patchPattern); assertThat(highestVersion + " -> " + this.sut.getNextVersion(highestVersion)) // .isEqualTo("1.0.0 -> 1.0.1"); assertThat(this.sut.getNextVersion(highestVersion).getVersionStep()).isEqualTo(PATCH);