Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ or by setting corresponding system property.
| analyzer | specification-name | | `Conventional` | [Conventional|Gitmoji] | Commit message analyzer specification |
| changelog | renderer-name | | `Markup` | [Markup|Html] | Changelog renderer |
| | template | | `changelog` | | Changelog template |
| | renderIssueLinks | | `true` | [true|false] | Generate issue links |
| | renderCommitLinks | | `true` | [true|false] | Generate commit links |
| | renderReleaseLinks | | `true` | [true|false] | Generate release links |
| | renderBody | | `false` | [true|false] | Include commit message body |
| release | publisher-name | | `GitHub` | [Github|Gitlab] | Release publisher |
| | publish | | `false` | [true|false] | publish release to remote |
| | username | | | | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private AnalyzedCommit analyzeCommit(@NonNull Commit commit, @NonNull Remote rem
body,
footer,
type,
getCategory(type),
getCategory(type, scope),
scope,
description,
breaking.isPresent() || footer.contains("BREAKING CHANGE") ? SemVerChangeLevel.MAJOR : getLevel(type),
Expand All @@ -114,7 +114,11 @@ private AnalyzedCommit analyzeCommit(@NonNull Commit commit, @NonNull Remote rem
);
}

private ChangeCategory getCategory(String type) {
private ChangeCategory getCategory(String type, String scope) {
if ("security".equalsIgnoreCase(scope)) {
return ChangeCategory.SECURITY;
}

if (type != null) {
return configuration.getItems().stream()
.filter(v -> v.type().equals(type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,43 @@ void shouldFindBreakingChanges() {
List.of())
);
}

@Test
void shouldSecurityFindFix() {
var actual = uut.analyzeCommits(List.of(
new Commit("42", Instant.EPOCH, "JUnit",
"""
fix(security): use string encryption

* replaced weak encryption

refs #42
""")
),
Remote.of("git@github.com:Sam42R/semver-maven-plugin.git"),
new ProviderSpec("%s://%s/%s/%s/%s", "%s://%s/%s/%s/%s", "%s://%s/%s/%s/%s...%s")
);

assertThat(actual).containsExactly(
new AnalyzedCommit(
new Commit("42", Instant.EPOCH, "JUnit",
"""
fix(security): use string encryption

* replaced weak encryption

refs #42
"""),
"https://github.com/Sam42R/semver-maven-plugin/42",
"fix(security): use string encryption",
"* replaced weak encryption",
"refs #42",
"fix",
ChangeCategory.SECURITY,
"security",
"use string encryption",
SemVerChangeLevel.PATCH,
List.of(new Issue("42", "https://github.com/Sam42R/semver-maven-plugin/42")))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@

public interface ChangelogRendererFactory<R extends ChangelogRenderer> {

@NonNull R getInstance(@NonNull String template);
@NonNull R getInstance(
@NonNull String template,
boolean renderIssueLinks,
boolean renderCommitLinks,
boolean renderReleaseLinks,
boolean renderBody
);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.sam42r.semver.changelog;

import io.github.sam42r.semver.model.changelog.VersionInfo;
import io.github.sam42r.semver.model.analyze.AnalyzedCommit;
import io.github.sam42r.semver.model.changelog.VersionInfo;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
Expand All @@ -19,6 +19,10 @@
public class HtmlRenderer implements ChangelogRenderer {

private final String template;
private final boolean renderIssueLinks;
private final boolean renderCommitLinks;
private final boolean renderReleaseLinks;
private final boolean renderBody;

@Override
public @NonNull InputStream renderChangelog(
Expand All @@ -44,6 +48,11 @@ public class HtmlRenderer implements ChangelogRenderer {
) {
var context = new Context();

context.setVariable("renderIssueLinks", renderIssueLinks);
context.setVariable("renderCommitLinks", renderCommitLinks);
context.setVariable("renderReleaseLinks", renderReleaseLinks);
context.setVariable("renderBody", renderBody);

context.setVariable("release", versionInfo);
context.setVariable("commits", analyzedCommits);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
public class HtmlRendererFactory implements ChangelogRendererFactory<HtmlRenderer> {

@Override
public @NonNull HtmlRenderer getInstance(@NonNull String template) {
return new HtmlRenderer(template);
public @NonNull HtmlRenderer getInstance(
@NonNull String template,
boolean renderIssueLinks,
boolean renderCommitLinks,
boolean renderReleaseLinks,
boolean renderBody
) {
return new HtmlRenderer(template, renderIssueLinks, renderCommitLinks, renderReleaseLinks, renderBody);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ <h1>Changelog</h1>
<section class="version -planned">
<div class="header">
<h2 class="version-num">
<a th:text="${release.version}" th:href="${release.url}">v0.0.1</a>
<a th:if="${renderReleaseLinks}" th:text="${release.version}" th:href="${release.url}">v0.0.1</a>
<span th:unless="${renderReleaseLinks}" th:text="${release.version}">v0.0.1</span>
</h2>
<time class="datetime"
th:datetime="${release.date}"
Expand All @@ -47,15 +48,18 @@ <h2 class="version-num">
</th:block>
<ul>
<li>
<span th:text="${commit.header}">Lorem ipsum</span>
<span th:text="|${commit.type}${commit.scope != null and !#strings.isEmpty(commit.scope) ? '(' + commit.scope + ')' : ''}: ${commit.subject}|">Lorem ipsum</span>
<th:block th:each="issue : ${commit.issues}">
&nbsp;(<a th:href="${issue.url}" th:text="|#${issue.id}|"></a>)
&nbsp;<a th:if="${renderIssueLinks}" th:href="${issue.url}" th:text="|(#${issue.id})|"></a>
</th:block>
<th:block th:if="${commit.url}">
<th:block th:if="${renderCommitLinks && commit.url}">
&nbsp;<a th:text="${#strings.length(commit.commit.id) > 7 ? #strings.substring(commit.commit.id, 0, 7) : commit.commit.id}"
th:href="${commit.url}"></a>
</th:block>
</li>
<li th:if="${renderBody && commit.body}">
<span style="padding-left: 16px; white-space: pre-wrap;" th:text="${commit.body}">Lorem ipsum</span>
</li>
</ul>
</section>
</th:block>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ <h1>Changelog</h1>
<tr>
<td class="label-container"></td>
<td class="changelog-date">
<a th:text="${release.version}" th:href="${release.url}">v0.0.1</a>
<a th:if="${renderReleaseLinks}" th:text="${release.version}" th:href="${release.url}">v0.0.1</a>
<span th:unless="${renderReleaseLinks}" th:text="${release.version}">v0.0.1</span>
<span class="app-version"
th:text="${'(' + release.date + ')'}">
(1970-01-01)
Expand Down Expand Up @@ -61,16 +62,21 @@ <h1>Changelog</h1>
<table role="presentation">
<tr>
<td class="changelog-text">
<span th:text="${commit.header}">Lorem ipsum</span>
<span th:text="|${commit.type}${commit.scope != null and !#strings.isEmpty(commit.scope) ? '(' + commit.scope + ')' : ''}: ${commit.subject}|">Lorem ipsum</span>
<th:block th:each="issue : ${commit.issues}">
(<a th:href="${issue.url}" th:text="|#${issue.id}|"></a>)
<a th:if="${renderIssueLinks}" th:href="${issue.url}" th:text="|(#${issue.id})|"></a>
</th:block>
<th:block th:if="${commit.url}">
<th:block th:if="${renderCommitLinks && commit.url}">
<a th:text="${#strings.length(commit.commit.id) > 7 ? #strings.substring(commit.commit.id, 0, 7) : commit.commit.id}"
th:href="${commit.url}"></a>
</th:block>
</td>
</tr>
<tr th:if="${renderBody && commit.body}">
<td class="changelog-text" style="padding-left: 16px;">
<span style="white-space: pre-wrap;" th:text="${commit.body}">Lorem ipsum</span>
</td>
</tr>
</table>
</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ <h3 class="flex items-center w-full mb-3 space-x-3 md:w-1/3">
<span class="z-10 hidden block w-5 h-5 bg-white border-4 border-gray-300 rounded-full md:block"></span>

<div class="text-xl font-semibold tracking-tight text-gray-800">
<a th:text="${release.version}" th:href="${release.url}">v0.0.1</a>
<a th:if="${renderReleaseLinks}" th:text="${release.version}" th:href="${release.url}">v0.0.1</a>
<span th:unless="${renderReleaseLinks}" th:text="${release.version}">v0.0.1</span>
</div>
<time class="text-xl font-semibold tracking-tight text-gray-800"
th:datetime="${release.date}"
Expand Down Expand Up @@ -54,15 +55,18 @@ <h3 class="flex items-center w-full mb-3 space-x-3 md:w-1/3">
</p>
</th:block>
<p class="text-gray-700">
<span th:text="${commit.header}">Lorem ipsum</span>
<span th:text="|${commit.type}${commit.scope != null and !#strings.isEmpty(commit.scope) ? '(' + commit.scope + ')' : ''}: ${commit.subject}|">Lorem ipsum</span>
<th:block th:each="issue : ${commit.issues}">
(<a th:href="${issue.url}" th:text="|#${issue.id}|"></a>)
<a th:if="${renderIssueLinks}" th:href="${issue.url}" th:text="|(#${issue.id})|"></a>
</th:block>
<th:block th:if="${commit.url}">
<th:block th:if="${renderCommitLinks && commit.url}">
&nbsp;<a th:text="${#strings.length(commit.commit.id) > 7 ? #strings.substring(commit.commit.id, 0, 7) : commit.commit.id}"
th:href="${commit.url}"></a>
</th:block>
</p>
<p th:if="${renderBody && commit.body}" style="padding-left: 16px;">
<span style="white-space: pre-wrap;" th:text="${commit.body}">Lorem ipsum</span>
</p>
</th:block>
</div>
</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.sam42r.semver.changelog;

import io.github.sam42r.semver.model.analyze.Issue;
import io.github.sam42r.semver.model.changelog.VersionInfo;
import io.github.sam42r.semver.model.analyze.AnalyzedCommit;
import io.github.sam42r.semver.model.analyze.ChangeCategory;
import io.github.sam42r.semver.model.analyze.Issue;
import io.github.sam42r.semver.model.analyze.SemVerChangeLevel;
import io.github.sam42r.semver.model.changelog.VersionInfo;
import io.github.sam42r.semver.model.scm.Commit;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.jupiter.api.io.TempDir;
Expand All @@ -30,7 +30,7 @@ class HtmlRendererTest {
@ParameterizedTest
@CsvSource({"default", "missive", "spinal"})
void shouldCreateChangelogFull(String template) throws IOException {
var uut = new HtmlRenderer(template);
var uut = new HtmlRendererFactory().getInstance(template, true, true, true, true);
var changelog = tempDir.resolve("Changelog.html");

try (var inputStream = uut.renderChangelog(changelog, release("v1.0.0"), analyzedCommits())) {
Expand All @@ -47,7 +47,7 @@ void shouldCreateChangelogFull(String template) throws IOException {
@ParameterizedTest
@CsvSource({"default", "missive", "spinal"})
void shouldUpdateChangelog(String template) throws IOException {
var uut = new HtmlRenderer(template);
var uut = new HtmlRendererFactory().getInstance(template, false, false, false, false);
var changelog = tempDir.resolve("Changelog.html");

try (var inputStream = uut.renderChangelog(changelog, release("v1.0.0"), analyzedCommits())) {
Expand Down Expand Up @@ -83,13 +83,17 @@ private List<AnalyzedCommit> analyzedCommits() {
"""
feat(scm): Lorem ipsum

* Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
* Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam

refs #42
"""),
null,
"feat(scm): Lorem ipsum",
"* Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam",
"""
* Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
* Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
""",
"refs #42",
"feat",
ChangeCategory.ADDED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -26,6 +27,10 @@ public class MarkupRenderer implements ChangelogRenderer {
private static final String CHANGELOG_TEMPLATE = "%s.mustache";

private final String template;
private final boolean renderIssueLinks;
private final boolean renderCommitLinks;
private final boolean renderReleaseLinks;
private final boolean renderBody;

@Override
public @NonNull InputStream renderChangelog(
Expand All @@ -37,17 +42,6 @@ public class MarkupRenderer implements ChangelogRenderer {

var alreadyExists = Files.exists(path);

// TODO
// Changelog -> docs(changelog): ...

// Added -> feat
// Changed -> refactor
// Deprecated -> DEPRECATED footer
// Removed -> ???
// Fixed -> fix
// Security -> fix(security): ... OR feat(security): ...
// Other -> all others

var mustacheFactory = new DefaultMustacheFactory("io/github/sam42r/semver/changelog");
try (
var reader = mustacheFactory.getReader(CHANGELOG_TEMPLATE.formatted(template));
Expand All @@ -65,6 +59,8 @@ public class MarkupRenderer implements ChangelogRenderer {

var mustache = mustacheFactory.compile(reader, CHANGELOG_TEMPLATE.formatted(template));
var context = new HashMap<String, Object>();

context.put("renderReleaseLink", renderReleaseLinks);
context.put("release", versionInfo);

context.put("hasAdded", categorizedCommits.containsKey(ChangeCategory.ADDED));
Expand Down Expand Up @@ -125,12 +121,19 @@ public class MarkupRenderer implements ChangelogRenderer {
* @return Mustache compliant {@link RenderedCommit}
*/
private RenderedCommit renderedCommit(AnalyzedCommit analyzedCommit) {
var messageBuilder = new StringBuilder(analyzedCommit.type());
if (analyzedCommit.scope() != null && !analyzedCommit.scope().isBlank()) {
messageBuilder.append("(%s)".formatted(analyzedCommit.scope()));
}
messageBuilder.append(": %s".formatted(analyzedCommit.subject()));

return new RenderedCommit(
analyzedCommit.header(),
analyzedCommit.url() == null || analyzedCommit.url().isBlank() ?
messageBuilder.toString(),
!renderBody ? null : Arrays.asList(analyzedCommit.body().trim().split("\n")),
!renderCommitLinks || analyzedCommit.url() == null || analyzedCommit.url().isBlank() ?
null :
new Link(trimToLength(analyzedCommit.commit().id(), 7), analyzedCommit.url()),
analyzedCommit.issues() == null || analyzedCommit.issues().isEmpty() ?
!renderIssueLinks || analyzedCommit.issues() == null || analyzedCommit.issues().isEmpty() ?
Collections.emptyList() :
analyzedCommit.issues().stream()
.filter(v -> v.url() != null && !v.url().isBlank())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
public class MarkupRendererFactory implements ChangelogRendererFactory<MarkupRenderer> {

@Override
public @NonNull MarkupRenderer getInstance(@NonNull String template) {
return new MarkupRenderer(template);
public @NonNull MarkupRenderer getInstance(
@NonNull String template,
boolean renderIssueLinks,
boolean renderCommitLinks,
boolean renderReleaseLinks,
boolean renderBody
) {
return new MarkupRenderer(template, renderIssueLinks, renderCommitLinks, renderReleaseLinks, renderBody);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import java.util.List;

public record RenderedCommit(String message, Link reference, List<Link> issues) {
public record RenderedCommit(String message, List<String> body, Link reference, List<Link> issues) {
}
Loading