-
Notifications
You must be signed in to change notification settings - Fork 11
Changelog #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
200Puls
wants to merge
7
commits into
foo4u:master
Choose a base branch
from
olpapchenko:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changelog #12
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e7f0c06
feat: produce a meaningful message, if the current directory is not a…
olpapchenko a86038d
feat: produce a meaningful message, if the current directory is not a…
olpapchenko ad23843
feat: add changelog file generation feature (#1)
olpapchenko fc98a5d
Update README.md
olpapchenko f1681dd
Changelog (#2)
olpapchenko a38b6a5
Update README.md
olpapchenko 51ca227
Add spot bugs (#3)
olpapchenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,6 @@ public interface CommitAdapter<T> | |
| String getShortMessage(); | ||
|
|
||
| T getCommit(); | ||
|
|
||
| String getCommitHash(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...rc/main/java/com/smartling/cc4j/semantic/release/common/changelog/ChangelogExtractor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.smartling.cc4j.semantic.release.common.changelog; | ||
|
|
||
| import com.smartling.cc4j.semantic.release.common.Commit; | ||
| import com.smartling.cc4j.semantic.release.common.ConventionalCommitType; | ||
| import com.smartling.cc4j.semantic.release.common.scm.ScmApiException; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| public interface ChangelogExtractor { | ||
| /** | ||
| * Extracts and groups commits by their commit types | ||
| * @return - commits grouped by commit type | ||
| */ | ||
| Map<ConventionalCommitType, Set<Commit>> getGroupedCommitsByCommitTypes() throws ScmApiException; | ||
| } |
118 changes: 118 additions & 0 deletions
118
...rc/main/java/com/smartling/cc4j/semantic/release/common/changelog/ChangelogGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package com.smartling.cc4j.semantic.release.common.changelog; | ||
|
|
||
| import com.smartling.cc4j.semantic.release.common.Commit; | ||
| import com.smartling.cc4j.semantic.release.common.ConventionalCommitType; | ||
| import com.smartling.cc4j.semantic.release.common.LogHandler; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ChangelogGenerator { | ||
| public static final int COMMIT_HASH_DISPLAYED_LENGTH = 10; | ||
| private final Logger logger = LoggerFactory.getLogger(LogHandler.class); | ||
|
|
||
| private static final String CHANGELOG_FORMAT = "## %s (%s)" + | ||
| "%s"; | ||
| private static final String MD_LINK_FORMAT = "[%s](%s)"; | ||
| private static final String BUG_FIXES_HEADER = "Bug fixes"; | ||
| private static final String FEATURE_HEADER = "Feature"; | ||
| private static final String BREAKING_HEADER = "Breaking changes"; | ||
| private static final String DOCS_HEADER = "Docs"; | ||
| private static final String CI_HEADER = "CI"; | ||
| private static final String BUILD_HEADER = "Build"; | ||
|
|
||
| private final String repoUrlFormat; | ||
| private final String trackingSystemUrlFormat; | ||
|
|
||
| public ChangelogGenerator(String repoUrlFormat, String trackingSystemUrlFormat) { | ||
| this.repoUrlFormat = repoUrlFormat; | ||
| this.trackingSystemUrlFormat = trackingSystemUrlFormat; | ||
| } | ||
|
|
||
| public String generate(String nextVersion, Map<ConventionalCommitType, Set<Commit>> commitsByCommitType) { | ||
| Objects.requireNonNull(nextVersion, "next version can not be null"); | ||
| Objects.requireNonNull(commitsByCommitType, "commits by type can not be null"); | ||
|
|
||
| List<String> sections = new ArrayList<>(); | ||
|
|
||
| if (commitsByCommitType.get(ConventionalCommitType.BREAKING_CHANGE) != null) { | ||
| getSection(BREAKING_HEADER, commitsByCommitType.get(ConventionalCommitType.BREAKING_CHANGE)).ifPresent(sections::add); | ||
| } | ||
|
|
||
| if (commitsByCommitType.get(ConventionalCommitType.FIX) != null) { | ||
| getSection(BUG_FIXES_HEADER, commitsByCommitType.get(ConventionalCommitType.FIX)).ifPresent(sections::add); | ||
| } | ||
|
|
||
| if (commitsByCommitType.get(ConventionalCommitType.FEAT) != null) { | ||
| getSection(FEATURE_HEADER, commitsByCommitType.get(ConventionalCommitType.FEAT)).ifPresent(sections::add); | ||
| } | ||
|
|
||
| if (commitsByCommitType.get(ConventionalCommitType.DOCS) != null) { | ||
| getSection(DOCS_HEADER, commitsByCommitType.get(ConventionalCommitType.DOCS)).ifPresent(sections::add); | ||
| } | ||
|
|
||
| if (commitsByCommitType.get(ConventionalCommitType.CI) != null) { | ||
| getSection(CI_HEADER, commitsByCommitType.get(ConventionalCommitType.CI)).ifPresent(sections::add); | ||
| } | ||
|
|
||
| if (commitsByCommitType.get(ConventionalCommitType.BUILD) != null) { | ||
| getSection(BUILD_HEADER, commitsByCommitType.get(ConventionalCommitType.BUILD)).ifPresent(sections::add); | ||
| } | ||
|
|
||
| sections = sections.stream().filter(Objects::nonNull).collect(Collectors.toList()); | ||
|
|
||
| return String.format(CHANGELOG_FORMAT, nextVersion, LocalDate.now(), "\n" + String.join("\n", sections)); | ||
| } | ||
|
|
||
| private Optional<String> getSection(String header, Set<Commit> commits) { | ||
| String sectionEntries = getSectionEntries(commits); | ||
| if (sectionEntries != null && !sectionEntries.trim().equals("")) { | ||
| return Optional.of("###" + header + "\n" + sectionEntries); | ||
| } | ||
|
|
||
| return Optional.empty(); | ||
| } | ||
|
|
||
| private String getSectionEntries(Set<Commit> commits) { | ||
| Set<String> uniqueMessages = new HashSet<>(); | ||
| return commits.stream() | ||
| .filter(commit -> commit.getCommitMessageDescription().isPresent() && uniqueMessages.add(commit.getCommitMessageDescription().get())) | ||
| .map(this::getChangeLogEntry) | ||
| .filter(Optional::isPresent) | ||
| .map(Optional::get) | ||
| .sorted() | ||
| .collect(Collectors.joining("\n")); | ||
| } | ||
|
|
||
| private Optional<String> getChangeLogEntry(Commit commit) { | ||
| if (!commit.getCommitMessageDescription().isPresent()) { | ||
| logger.warn("Skipping message for commit: {}", commit.getCommitHash()); | ||
| } | ||
| return commit.getCommitMessageDescription().map(message -> { | ||
| if (commit.getCommitMessageDescription().get().trim().equals("")) { | ||
| logger.warn("Skipping message for commit: {}", commit.getCommitHash()); | ||
| return null; | ||
| } | ||
| return "* " + commit.getCommitMessageDescription().get() + getTrackingSystemLink(commit) + getCommitHashLink(commit); | ||
| }); | ||
| } | ||
|
|
||
| private String getCommitHashLink(Commit commit) { | ||
| if (this.repoUrlFormat == null) { | ||
| return " (" + commit.getCommitHash().substring(0, COMMIT_HASH_DISPLAYED_LENGTH) + ")"; | ||
| } else { | ||
| return " " + String.format(MD_LINK_FORMAT, "(" + commit.getCommitHash().substring(0, COMMIT_HASH_DISPLAYED_LENGTH) + ")", String.format(this.repoUrlFormat, commit.getCommitHash())); | ||
| } | ||
| } | ||
|
|
||
| private String getTrackingSystemLink(Commit commit) { | ||
| if (this.trackingSystemUrlFormat == null || !commit.getTrackingSystemId().isPresent()) { | ||
| return commit.getTrackingSystemId().map(s -> " (" + s + ")").orElse(""); | ||
| } else { | ||
| return " " + String.format(MD_LINK_FORMAT, "(" + commit.getTrackingSystemId().get() + ")", String.format(this.trackingSystemUrlFormat, commit.getTrackingSystemId().get())); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I detect that this code is problematic. According to the Bad practice (BAD_PRACTICE), FS: Format string should use %n rather than n (VA_FORMAT_STRING_USES_NEWLINE).
This format string includes a newline character (\n). In format strings, it is generally preferable to use %n, which will produce the platform-specific line separator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @lillieMaiBauer, i added a spot bugs plugin to the project.
%n - looks the right thing to do. But i realized this case:
I am not sure if this is the correct behavior? I can see in many sources that %n should be used, however - is the case described above ok - so we really want to use platform specific separator in generated changelog file?