From 051d61c8116bf191198881d12f28411c14ec8a9f Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Fri, 11 Jun 2021 19:56:07 +0200 Subject: [PATCH] fix: adding default changelog template jenkinsci/git-changelog-plugin#58 --- .../gitchangelog/api/GitChangelogApi.java | 30 +--------- .../internal/util/ResourceLoader.java | 57 +++++++++++++++++++ src/main/resources/changelog.mustache | 32 +++++++++++ ....testPathFilterCanBeSpecified.approved.txt | 1 - ...ssueCanBeIgnoredIssuesCommits.approved.txt | 1 - ...tThatCustomVariablesCanBeUsed.approved.txt | 1 - ...st.testThatIssuesCanBeRemoved.approved.txt | 1 - ...testThatReadableGroupCanBeSet.approved.txt | 1 - ...ThatRevertedCommitsAreRemoved.approved.txt | 1 - ...mitsHaveBeenIgnoredAreRemoved.approved.txt | 1 - ...platesTest.testAuthorsCommits.approved.txt | 1 - .../TemplatesTest.testCommits.approved.txt | 1 - ...atesTest.testCommitsVariables.approved.txt | 1 - ...TemplatesTest.testIssueLabels.approved.txt | 1 - ...tesTest.testIssueLinkedIssues.approved.txt | 1 - ...TemplatesTest.testIssueTitles.approved.txt | 1 - .../TemplatesTest.testIssueType.approved.txt | 1 - ...mplatesTest.testIssuesCommits.approved.txt | 1 - ...TemplatesTest.testTagsCommits.approved.txt | 1 - ...CanBeEmptyToDisableTheFeature.approved.txt | 1 - ...BuiltInHelperMethodsCanBeUsed.approved.txt | 2 - ...erCanBeSuppliedWithJavascript.approved.txt | 1 - 22 files changed, 92 insertions(+), 47 deletions(-) create mode 100644 src/main/java/se/bjurr/gitchangelog/internal/util/ResourceLoader.java create mode 100644 src/main/resources/changelog.mustache diff --git a/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java b/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java index e4f3781f..a8b8a9bb 100644 --- a/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java +++ b/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java @@ -13,16 +13,11 @@ import com.github.jknack.handlebars.Helper; import com.github.jknack.handlebars.Template; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; -import java.net.URISyntaxException; import java.net.URL; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @@ -47,6 +42,7 @@ import se.bjurr.gitchangelog.internal.semantic.SemanticVersioning; import se.bjurr.gitchangelog.internal.settings.Settings; import se.bjurr.gitchangelog.internal.settings.SettingsIssue; +import se.bjurr.gitchangelog.internal.util.ResourceLoader; public class GitChangelogApi { @@ -127,28 +123,8 @@ public String getTemplateString() { if (this.templateContent != null) { return this.templateContent; } - String templateString = null; - try { - byte[] templateBytes = null; - final Path templatePath = Paths.get(this.settings.getTemplatePath()); - if (templatePath.toFile().exists()) { - templateBytes = Files.readAllBytes(templatePath); - } else { - URL templateUrl = GitChangelogApi.class.getResource(this.settings.getTemplatePath()); - if (templateUrl == null) { - templateUrl = GitChangelogApi.class.getResource("/" + this.settings.getTemplatePath()); - if (templateUrl == null) { - throw new FileNotFoundException( - "Was unable to find file, or resouce, \"" + this.settings.getTemplatePath() + "\""); - } - } - templateBytes = Files.readAllBytes(Paths.get(templateUrl.toURI())); - templateString = new String(templateBytes, StandardCharsets.UTF_8); - } - } catch (final IOException | URISyntaxException e) { - throw new RuntimeException(this.settings.getTemplatePath(), e); - } - return templateString; + final String resourceName = this.settings.getTemplatePath(); + return ResourceLoader.getResourceOrFile(resourceName); } static boolean shouldUseIntegrationIfConfigured(final String templateContent) { diff --git a/src/main/java/se/bjurr/gitchangelog/internal/util/ResourceLoader.java b/src/main/java/se/bjurr/gitchangelog/internal/util/ResourceLoader.java new file mode 100644 index 00000000..fc32c0fd --- /dev/null +++ b/src/main/java/se/bjurr/gitchangelog/internal/util/ResourceLoader.java @@ -0,0 +1,57 @@ +package se.bjurr.gitchangelog.internal.util; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.stream.Collectors; + +public final class ResourceLoader { + private ResourceLoader() {} + + public static String getResourceOrFile(final String resourceName) { + + String templateString = null; + try { + final Path templatePath = Paths.get(resourceName); + if (templatePath.toFile().exists()) { + + Files.readAllBytes(templatePath); + } else { + InputStream inputStream = + getResourceFromClassLoader(resourceName, ResourceLoader.class.getClassLoader()); + if (inputStream == null) { + inputStream = + getResourceFromClassLoader( + resourceName, Thread.currentThread().getContextClassLoader()); + } + + if (inputStream == null) { + throw new FileNotFoundException( + "Was unable to find file, or resouce, \"" + resourceName + "\""); + } + templateString = + new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)) + .lines() + .collect(Collectors.joining("\n")); + } + } catch (final IOException e) { + throw new RuntimeException(resourceName, e); + } + return templateString; + } + + private static InputStream getResourceFromClassLoader( + final String resourceName, final ClassLoader classLoader) { + InputStream inputStream = classLoader.getResourceAsStream(resourceName); + if (inputStream == null) { + inputStream = classLoader.getResourceAsStream("/" + resourceName); + } + return inputStream; + } +} diff --git a/src/main/resources/changelog.mustache b/src/main/resources/changelog.mustache new file mode 100644 index 00000000..3ec8c8fa --- /dev/null +++ b/src/main/resources/changelog.mustache @@ -0,0 +1,32 @@ +# Changelog + +Changelog for {{ownerName}} {{repoName}}. + +{{#tags}} +## {{name}} + {{#issues}} + {{#hasIssue}} + {{#hasLink}} +### {{name}} [{{issue}}]({{link}}) {{title}} {{#hasIssueType}} *{{issueType}}* {{/hasIssueType}} {{#hasLabels}} {{#labels}} *{{.}}* {{/labels}} {{/hasLabels}} + {{/hasLink}} + {{^hasLink}} +### {{name}} {{issue}} {{title}} {{#hasIssueType}} *{{issueType}}* {{/hasIssueType}} {{#hasLabels}} {{#labels}} *{{.}}* {{/labels}} {{/hasLabels}} + {{/hasLink}} + {{/hasIssue}} + {{^hasIssue}} +### {{name}} + {{/hasIssue}} + + {{#commits}} +**{{{messageTitle}}}** + +{{#messageBodyItems}} + * {{.}} +{{/messageBodyItems}} + +[{{hash}}](https://github.com/{{ownerName}}/{{repoName}}/commit/{{hash}}) {{authorName}} *{{commitTime}}* + + {{/commits}} + + {{/issues}} +{{/tags}} diff --git a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testPathFilterCanBeSpecified.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testPathFilterCanBeSpecified.approved.txt index 1c7b01d3..7fd5b434 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testPathFilterCanBeSpecified.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testPathFilterCanBeSpecified.approved.txt @@ -10,7 +10,6 @@ Extended variable: {{customVariable}} * {{authorName}} {{/authors}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatCommitsWithoutIssueCanBeIgnoredIssuesCommits.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatCommitsWithoutIssueCanBeIgnoredIssuesCommits.approved.txt index 4e7c60a8..6b553566 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatCommitsWithoutIssueCanBeIgnoredIssuesCommits.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatCommitsWithoutIssueCanBeIgnoredIssuesCommits.approved.txt @@ -21,7 +21,6 @@ Changelog of Git Changelog. {{/commits}} {{/issues}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatCustomVariablesCanBeUsed.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatCustomVariablesCanBeUsed.approved.txt index a09e4bd2..4234f563 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatCustomVariablesCanBeUsed.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatCustomVariablesCanBeUsed.approved.txt @@ -10,7 +10,6 @@ Extended variable: {{customVariable}} * {{authorName}} {{/authors}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatIssuesCanBeRemoved.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatIssuesCanBeRemoved.approved.txt index 4a87fe6f..46632a1f 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatIssuesCanBeRemoved.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatIssuesCanBeRemoved.approved.txt @@ -21,7 +21,6 @@ Changelog of Git Changelog. {{/commits}} {{/issues}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatReadableGroupCanBeSet.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatReadableGroupCanBeSet.approved.txt index 1ea99c86..8562afcc 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatReadableGroupCanBeSet.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatReadableGroupCanBeSet.approved.txt @@ -21,7 +21,6 @@ Changelog of Git Changelog. {{/commits}} {{/issues}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatRevertedCommitsAreRemoved.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatRevertedCommitsAreRemoved.approved.txt index 453d0fe6..867a05f7 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatRevertedCommitsAreRemoved.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatRevertedCommitsAreRemoved.approved.txt @@ -12,7 +12,6 @@ Changelog of Git Changelog. {{/commits}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatTagsThatAreEmptyAfterCommitsHaveBeenIgnoredAreRemoved.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatTagsThatAreEmptyAfterCommitsHaveBeenIgnoredAreRemoved.approved.txt index 441ef827..01a00f1b 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatTagsThatAreEmptyAfterCommitsHaveBeenIgnoredAreRemoved.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.testThatTagsThatAreEmptyAfterCommitsHaveBeenIgnoredAreRemoved.approved.txt @@ -10,7 +10,6 @@ Extended variable: {{customVariable}} * {{authorName}} {{/authors}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testAuthorsCommits.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testAuthorsCommits.approved.txt index d2e60edd..04f3fabc 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testAuthorsCommits.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testAuthorsCommits.approved.txt @@ -16,7 +16,6 @@ Changelog of Git Changelog. {{/authors}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testCommits.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testCommits.approved.txt index 9b617058..634f6e72 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testCommits.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testCommits.approved.txt @@ -12,7 +12,6 @@ Changelog of Git Changelog. {{/commits}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testCommitsVariables.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testCommitsVariables.approved.txt index 9ec9e221..846031e3 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testCommitsVariables.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testCommitsVariables.approved.txt @@ -19,7 +19,6 @@ Changelog of Git Changelog. {{/commits}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueLabels.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueLabels.approved.txt index 17935ce1..1bd0e1ed 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueLabels.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueLabels.approved.txt @@ -10,7 +10,6 @@ template: {{/issues}} {{/tags}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueLinkedIssues.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueLinkedIssues.approved.txt index 34b61718..ea259c77 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueLinkedIssues.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueLinkedIssues.approved.txt @@ -6,7 +6,6 @@ template: {{/hasLinkedIssues}} {{/issues}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueTitles.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueTitles.approved.txt index c1286cfe..c8085dc1 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueTitles.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueTitles.approved.txt @@ -4,7 +4,6 @@ template: {{name}} {{title}} {{/issues}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueType.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueType.approved.txt index 5c33df19..0610a419 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueType.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssueType.approved.txt @@ -15,7 +15,6 @@ isNoIssue: {{isNoIssue}} {{/issues}} {{/tags}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssuesCommits.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssuesCommits.approved.txt index 224bb45f..98572073 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssuesCommits.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testIssuesCommits.approved.txt @@ -21,7 +21,6 @@ Changelog of Git Changelog. {{/commits}} {{/issues}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testTagsCommits.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testTagsCommits.approved.txt index af5e125f..8daddb11 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testTagsCommits.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testTagsCommits.approved.txt @@ -16,7 +16,6 @@ Changelog of Git Changelog. {{/commits}} {{/tags}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testThatIgnoreCommitsIfMessageMatchesCanBeEmptyToDisableTheFeature.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testThatIgnoreCommitsIfMessageMatchesCanBeEmptyToDisableTheFeature.approved.txt index b77cea6d..c37ea279 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testThatIgnoreCommitsIfMessageMatchesCanBeEmptyToDisableTheFeature.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.testThatIgnoreCommitsIfMessageMatchesCanBeEmptyToDisableTheFeature.approved.txt @@ -9,7 +9,6 @@ template: {{/merge}} {{/commits}} - --------------------------------------------- settings: diff --git a/src/test/java/se/bjurr/gitchangelog/api/helpers/HandlebarsHelperTest.testThatBuiltInHelperMethodsCanBeUsed.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/helpers/HandlebarsHelperTest.testThatBuiltInHelperMethodsCanBeUsed.approved.txt index ac4aee44..0ad0f4da 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/helpers/HandlebarsHelperTest.testThatBuiltInHelperMethodsCanBeUsed.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/helpers/HandlebarsHelperTest.testThatBuiltInHelperMethodsCanBeUsed.approved.txt @@ -128,7 +128,6 @@ Has paragraphs: {{hash}} - --------------------------------------------- settings: @@ -653,7 +652,6 @@ This reverts commit 1edc0d71eccce51abfb5f62fdddfbe73913785f5. - --------------------------------------------- context: diff --git a/src/test/java/se/bjurr/gitchangelog/api/helpers/HandlebarsHelperTest.testThatHelperCanBeSuppliedWithJavascript.approved.txt b/src/test/java/se/bjurr/gitchangelog/api/helpers/HandlebarsHelperTest.testThatHelperCanBeSuppliedWithJavascript.approved.txt index 48eb47d7..0d0d7d2c 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/helpers/HandlebarsHelperTest.testThatHelperCanBeSuppliedWithJavascript.approved.txt +++ b/src/test/java/se/bjurr/gitchangelog/api/helpers/HandlebarsHelperTest.testThatHelperCanBeSuppliedWithJavascript.approved.txt @@ -11,7 +11,6 @@ template: {{/commits}} - --------------------------------------------- settings: