Skip to content

Commit

Permalink
Merge pull request #47 from ADI10HERO/multiline-commits
Browse files Browse the repository at this point in the history
fix: update breaking change footer
  • Loading branch information
garethjevans authored Jul 4, 2021
2 parents 21221d1 + 06da65a commit 0524959
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package io.jenkins.plugins.conventionalcommits;

import com.github.zafarkhaja.semver.Version;
import io.jenkins.plugins.conventionalcommits.utils.LogUtils;

import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;

public class ConventionalCommits {

LogUtils logger = new LogUtils();

private List<String> filterMergeCommits(List<String> commits) {
return commits.stream().filter(s -> !s.startsWith("Merge")).collect(Collectors.toList());
}

public Version nextVersion(Version in, List<String> commits) {
List<String> filtered = filterMergeCommits(commits);
List<String> breaking = filtered.stream().filter(s -> s.contains("!:") || breakingChangeFooter(s) ).collect(Collectors.toList());
List<String> breaking = filtered.stream().filter(s -> s.contains("!:") || breakingChangeFooter(s)).collect(Collectors.toList());
List<String> features = filtered.stream().filter(s -> s.startsWith("feat")).collect(Collectors.toList());

if (!breaking.isEmpty()) {
Expand All @@ -26,14 +31,26 @@ public Version nextVersion(Version in, List<String> commits) {
return in.incrementPatchVersion();
}

private boolean breakingChangeFooter(String commit){
int startIndex = commit.lastIndexOf("\n");

String result = commit;

if(startIndex!=-1 && startIndex!= commit.length()){
result = commit.substring(startIndex+1);
private boolean breakingChangeFooter(String commit) {

boolean result = false;
String[] lines = commit.split("[\\r\\n]+");

for (String line : lines) {
if (line.startsWith("BREAKING CHANGE:") || line.startsWith("BREAKING-CHANGE:")) {
result = true;
break;
} else if (line.toLowerCase().startsWith("breaking change:") || line.toLowerCase().startsWith("breaking-change:")) {
String keyword = line.substring(0, 16);
logger.log(
Level.INFO, Level.INFO, Level.FINE, Level.FINE, true,
"'" + keyword + "' detected which is not compliant with Conventional Commits Guidelines " +
"(https://www.conventionalcommits.org/en/v1.0.0/#summary)"
);
}
}
return result.startsWith("BREAKING CHANGE");

return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.jenkins.plugins.conventionalcommits.utils;


import io.jenkins.plugins.conventionalcommits.ConventionalCommits;

import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LogUtils {

private static final Logger LOGGER = Logger.getLogger(ConventionalCommits.class.getName());
private static Handler consoleHandler = new ConsoleHandler();

private void beforeLogging(Level loggerLevel, Level consoleLevel){
LOGGER.setLevel(loggerLevel);
consoleHandler.setLevel(consoleLevel);
LOGGER.addHandler(consoleHandler);
}

private void afterLogging(Level loggerLevel, Level consoleLevel){
LOGGER.removeHandler(consoleHandler);
LOGGER.setLevel(loggerLevel);
consoleHandler.setLevel(consoleLevel);
}

public void log(Level initialLogLevel, Level initialConsoleLevel, Level requiredLogLevel,
Level requiredConsoleLevel, boolean revertAfterLogging, String message) {
/*
revertAfterLogging (boolean): revert to initial log and console level after logging the current log string
*/

beforeLogging(requiredLogLevel, requiredConsoleLevel);
LOGGER.log(requiredLogLevel, message);
if (revertAfterLogging)
afterLogging(initialLogLevel, initialConsoleLevel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ public void willBumpMajorVersion_FooterMultipleLineCommit() {
}

@Test
public void willNotBumpMajorVersion_ExclamationMultipleLineCommitNotFooter() {
public void willBumpMajorVersion_ExclamationMultipleLineCommitNotFooter() {
ConventionalCommits cc = new ConventionalCommits();

Version out = cc.nextVersion(Version.valueOf("0.0.1"), Collections.singletonList(
"chore: new major version \nBREAKING CHANGE: new breaking change \nstupid footer"
"chore: new major version \nBREAKING CHANGE: new breaking change \nextra footer"
));
assertThat(out, is(notNullValue()));
assertThat(out.toString(), is("0.0.2"));
assertThat(out.toString(), is("1.0.0"));
}

@Test
Expand All @@ -138,4 +138,17 @@ public void willBumpMajorVersion_MultipleCommitsMultipleLineFooter() {
assertThat(out, is(notNullValue()));
assertThat(out.toString(), is("1.0.0"));
}

@Test
public void willNotBumpMajorVersion_BreakingChangeCaseSensitivity() {
ConventionalCommits cc = new ConventionalCommits();
Version out = cc.nextVersion(Version.valueOf("0.0.1"), Arrays.asList(
"feat: add new feature",
"fix: bug fix \nBreaking Change: breaking change",
"fix: bug fix \nBREAKING change: breaking change"
));

assertThat(out, is(notNullValue()));
assertThat(out.toString(), is("0.1.0"));
}
}

0 comments on commit 0524959

Please sign in to comment.