Skip to content

Commit

Permalink
fix: adding default changelog template jenkinsci/git-changelog-plugin#58
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Jun 11, 2021
1 parent ecc3815 commit 051d61c
Show file tree
Hide file tree
Showing 22 changed files with 92 additions and 47 deletions.
30 changes: 3 additions & 27 deletions src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
32 changes: 32 additions & 0 deletions src/main/resources/changelog.mustache
Original file line number Diff line number Diff line change
@@ -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}}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Extended variable: {{customVariable}}
* {{authorName}}
{{/authors}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Changelog of Git Changelog.
{{/commits}}
{{/issues}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Extended variable: {{customVariable}}
* {{authorName}}
{{/authors}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Changelog of Git Changelog.
{{/commits}}
{{/issues}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Changelog of Git Changelog.
{{/commits}}
{{/issues}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Changelog of Git Changelog.

{{/commits}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Extended variable: {{customVariable}}
* {{authorName}}
{{/authors}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Changelog of Git Changelog.

{{/authors}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Changelog of Git Changelog.

{{/commits}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Changelog of Git Changelog.

{{/commits}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ template:
{{/issues}}
{{/tags}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ template:
{{/hasLinkedIssues}}
{{/issues}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ template:
{{name}} {{title}}
{{/issues}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ isNoIssue: {{isNoIssue}}
{{/issues}}
{{/tags}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Changelog of Git Changelog.
{{/commits}}
{{/issues}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Changelog of Git Changelog.
{{/commits}}
{{/tags}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ template:
{{/merge}}
{{/commits}}


---------------------------------------------

settings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ Has paragraphs: {{hash}}




---------------------------------------------

settings:
Expand Down Expand Up @@ -653,7 +652,6 @@ This reverts commit 1edc0d71eccce51abfb5f62fdddfbe73913785f5.




---------------------------------------------

context:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ template:

{{/commits}}


---------------------------------------------

settings:
Expand Down

0 comments on commit 051d61c

Please sign in to comment.