From 337fa7efca5083587d6c01192ecd9875c17d85f6 Mon Sep 17 00:00:00 2001 From: see Date: Thu, 23 Nov 2023 04:49:56 +0100 Subject: [PATCH] feat: allow using regex in commit message check (#26) --- README.adoc | 16 +++++++- .../versioner/core/git/JGitVersioner.java | 14 +++++-- .../core/params/VersionKeywords.java | 16 +++++++- .../versioner/core/git/JGitVersionerTest.java | 38 +++++++++++++++++++ .../plugin/mojo/params/VersionConfigTest.java | 16 ++++++-- .../mojo/params/VersionKeywordsTest.java | 11 +++++- .../extension/GitVersionerModelProcessor.java | 4 +- .../mojo/params/VersionKeywordsParam.java | 14 +++++-- .../mojo/params/VersionKeywordsParamTest.java | 8 ++-- 9 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/core/git/JGitVersionerTest.java diff --git a/README.adoc b/README.adoc index b90cfe0..bec7707 100644 --- a/README.adoc +++ b/README.adoc @@ -31,7 +31,7 @@ values such as Git hash, branch name etc. See https://github.com/manikmagar/git-versioner-maven-extension-examples[manikmagar/git-versioner-maven-extension-examples] for examples of using this extension. - + [#versionKeywords] == What are version keywords? *Version keywords* are the reserved words that describes which milestone of the release is this. @@ -202,6 +202,20 @@ gv.keywords.minorKey=[SMALL] gv.keywords.patchKey=[FIX] ---- +=== Use regex for version keywords +You can also use regex to match version keywords. +This is useful when you want to be sure that the version keyword will only be matched when it is the first word in the commit message. +So if for example you have a merge commit message which contains the messages of the merged commits, you can use a regex to match only the first commit message. + +To use regex for version keywords, you can add following properties to `.mvn/git-versioner.extensions.properties` file - + +.Example configuration for regex version keywords +---- +gv.keywords.useRegex=true +gv.keywords.majorKey=^\\[major\\].* +gv.keywords.minorKey=^\\[minor\\].* +gv.keywords.patchKey=^\\[patch\\].* +---- == How to access generated version properties? This extension adds all version properties to *Maven properties* during build cycle - diff --git a/git-versioner-maven-core/src/main/java/com/github/manikmagar/maven/versioner/core/git/JGitVersioner.java b/git-versioner-maven-core/src/main/java/com/github/manikmagar/maven/versioner/core/git/JGitVersioner.java index f0a3658..868996e 100644 --- a/git-versioner-maven-core/src/main/java/com/github/manikmagar/maven/versioner/core/git/JGitVersioner.java +++ b/git-versioner-maven-core/src/main/java/com/github/manikmagar/maven/versioner/core/git/JGitVersioner.java @@ -35,11 +35,11 @@ public VersionStrategy version() { .collect(Collectors.toList()); Collections.reverse(revCommits); for (RevCommit commit : revCommits) { - if (commit.getFullMessage().contains(versionConfig.getKeywords().getMajorKey())) { + if (hasValue(commit.getFullMessage(), versionConfig.getKeywords().getMajorKey())) { versionStrategy.increment(VersionComponentType.MAJOR, hash); - } else if (commit.getFullMessage().contains(versionConfig.getKeywords().getMinorKey())) { + } else if (hasValue(commit.getFullMessage(), versionConfig.getKeywords().getMinorKey())) { versionStrategy.increment(VersionComponentType.MINOR, hash); - } else if (commit.getFullMessage().contains(versionConfig.getKeywords().getPatchKey())) { + } else if (hasValue(commit.getFullMessage(),versionConfig.getKeywords().getPatchKey())) { versionStrategy.increment(VersionComponentType.PATCH, hash); } else { versionStrategy.increment(VersionComponentType.COMMIT, hash); @@ -48,4 +48,12 @@ public VersionStrategy version() { return versionStrategy; }); } + + boolean hasValue(String commitMessage, String keyword) { + if(versionConfig.getKeywords().isUseRegex()){ + return commitMessage.matches(keyword); + }else { + return commitMessage.contains(keyword); + } + } } diff --git a/git-versioner-maven-core/src/main/java/com/github/manikmagar/maven/versioner/core/params/VersionKeywords.java b/git-versioner-maven-core/src/main/java/com/github/manikmagar/maven/versioner/core/params/VersionKeywords.java index 6b19b61..65dee7e 100644 --- a/git-versioner-maven-core/src/main/java/com/github/manikmagar/maven/versioner/core/params/VersionKeywords.java +++ b/git-versioner-maven-core/src/main/java/com/github/manikmagar/maven/versioner/core/params/VersionKeywords.java @@ -13,6 +13,7 @@ public final class VersionKeywords { public static final String GV_KEYWORDS_MAJOR_KEY = "gv.keywords.majorKey"; public static final String GV_KEYWORDS_MINOR_KEY = "gv.keywords.minorKey"; public static final String GV_KEYWORDS_PATCH_KEY = "gv.keywords.patchKey"; + public static final String GV_KEYWORDS_KEY_USEREGEX = "gv.keywords.useRegex"; /** * The keyword for calculating major version of the SemVer. @@ -26,11 +27,16 @@ public final class VersionKeywords { * The keyword for calculating patch version of the SemVer. */ private String patchKey = KEY_PATCH; + /** + * Whether to use regex for matching keywords. + */ + private boolean useRegex = false; - public VersionKeywords(String majorKey, String minorKey, String patchKey) { + public VersionKeywords(String majorKey, String minorKey, String patchKey, boolean useRegex) { setMajorKey(majorKey); setMinorKey(minorKey); setPatchKey(patchKey); + setUseRegex(useRegex); } public VersionKeywords() { @@ -72,6 +78,14 @@ public void setPatchKey(String patchKey) { } } + public void setUseRegex(boolean useRegex) { + this.useRegex = useRegex; + } + + public boolean isUseRegex() { + return useRegex; + } + @Override public boolean equals(Object o) { if (this == o) diff --git a/git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/core/git/JGitVersionerTest.java b/git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/core/git/JGitVersionerTest.java new file mode 100644 index 0000000..be99f52 --- /dev/null +++ b/git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/core/git/JGitVersionerTest.java @@ -0,0 +1,38 @@ +package com.github.manikmagar.maven.versioner.core.git; + +import com.github.manikmagar.maven.versioner.core.params.VersionConfig; +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(JUnitParamsRunner.class) +public class JGitVersionerTest { + private VersionConfig versionConfig = new VersionConfig();; + private JGitVersioner jGitVersioner = new JGitVersioner(versionConfig); + + @Test + @Parameters(value = { + "Fix: [minor] corrected typo, .*\\[minor\\].*, true", + "[minor] Fix: corrected typo, ^\\[minor\\].*, true", + "Fix: [minor] corrected typo, ^\\[minor\\].*, false", + "Update: improved performance, [minor], false"}) + public void testHasValueWithRegex(String commitMessage, String keyword, boolean expected) { + versionConfig.getKeywords().setUseRegex(true); + + assertThat(jGitVersioner.hasValue(commitMessage, keyword)).isEqualTo(expected); + } + + @Test + public void testHasValueWithoutRegex() { + versionConfig.getKeywords().setUseRegex(false); + + String commitMessage = "Fix: [minor] corrected typo"; + assertThat(jGitVersioner.hasValue(commitMessage, "[minor]")).isTrue(); + + commitMessage = "Update: improved performance"; + assertThat(jGitVersioner.hasValue(commitMessage, "[minor]")).isFalse(); + } +} diff --git a/git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionConfigTest.java b/git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionConfigTest.java index badfc98..8e69636 100644 --- a/git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionConfigTest.java +++ b/git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionConfigTest.java @@ -36,7 +36,17 @@ public void setKeywords() { VersionKeywords versionKeywords = new VersionKeywords(); versionKeywords.setMajorKey("[TEST]"); versionConfig.setKeywords(versionKeywords); - assertThat(versionConfig.getKeywords()).extracting("majorKey", "minorKey", "patchKey").containsExactly("[TEST]", - KEY_MINOR, KEY_PATCH); + assertThat(versionConfig.getKeywords()).extracting("majorKey", "minorKey", "patchKey", "useRegex").containsExactly("[TEST]", + KEY_MINOR, KEY_PATCH, false); } -} \ No newline at end of file + + @Test + public void setUseRegEx() { + VersionConfig versionConfig = new VersionConfig(); + VersionKeywords versionKeywords = new VersionKeywords(); + versionKeywords.setUseRegex(true); + versionConfig.setKeywords(versionKeywords); + assertThat(versionConfig.getKeywords()).extracting("majorKey", "minorKey", "patchKey", "useRegex").containsExactly(KEY_MAJOR, + KEY_MINOR, KEY_PATCH, true); + } +} diff --git a/git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsTest.java b/git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsTest.java index 9417ac8..a0e6357 100644 --- a/git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsTest.java +++ b/git-versioner-maven-core/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsTest.java @@ -15,8 +15,8 @@ public class VersionKeywordsTest { @Test public void defaultKeywords() { - assertThat(new VersionKeywords()).extracting("majorKey", "minorKey", "patchKey").containsExactly(KEY_MAJOR, - KEY_MINOR, KEY_PATCH); + assertThat(new VersionKeywords()).extracting("majorKey", "minorKey", "patchKey","useRegex").containsExactly(KEY_MAJOR, + KEY_MINOR, KEY_PATCH, false); } @Test @@ -43,4 +43,11 @@ public void setPatchKey(String key, String expected, String keyType) { assertThat(versionKeywords.getPatchKey()).as(keyType).isEqualTo(expected); } + @Test + @Parameters(value = {"true", "false"}) + public void setUseRegex(boolean useRegex) { + var versionKeywords = new VersionKeywords(); + versionKeywords.setUseRegex(useRegex); + assertThat(versionKeywords.isUseRegex()).isEqualTo(useRegex); + } } diff --git a/git-versioner-maven-extension/src/main/java/com/github/manikmagar/maven/versioner/extension/GitVersionerModelProcessor.java b/git-versioner-maven-extension/src/main/java/com/github/manikmagar/maven/versioner/extension/GitVersionerModelProcessor.java index 4239099..9ffac37 100644 --- a/git-versioner-maven-extension/src/main/java/com/github/manikmagar/maven/versioner/extension/GitVersionerModelProcessor.java +++ b/git-versioner-maven-extension/src/main/java/com/github/manikmagar/maven/versioner/extension/GitVersionerModelProcessor.java @@ -187,9 +187,10 @@ private void addPluginConfiguration(Plugin plugin) { String config = String.format( " %s" + " %s %s" + + " %s" + " ", versionConfig.getKeywords().getMajorKey(), versionConfig.getKeywords().getMinorKey(), - versionConfig.getKeywords().getPatchKey()); + versionConfig.getKeywords().getPatchKey(), versionConfig.getKeywords().isUseRegex()); try { Xpp3Dom configDom = Xpp3DomBuilder.build(new StringReader(config)); plugin.setConfiguration(configDom); @@ -211,6 +212,7 @@ private VersionConfig loadConfig() { keywords.setMajorKey(properties.getProperty(VersionKeywords.GV_KEYWORDS_MAJOR_KEY)); keywords.setMinorKey(properties.getProperty(VersionKeywords.GV_KEYWORDS_MINOR_KEY)); keywords.setPatchKey(properties.getProperty(VersionKeywords.GV_KEYWORDS_PATCH_KEY)); + keywords.setUseRegex(properties.getProperty(VersionKeywords.GV_KEYWORDS_KEY_USEREGEX,"false").equals("true")); coreVersionConfig.setKeywords(keywords); VersionPattern versionPattern = new VersionPattern(); diff --git a/git-versioner-maven-plugin/src/main/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsParam.java b/git-versioner-maven-plugin/src/main/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsParam.java index fcf018f..ab8972b 100644 --- a/git-versioner-maven-plugin/src/main/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsParam.java +++ b/git-versioner-maven-plugin/src/main/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsParam.java @@ -29,10 +29,14 @@ public final class VersionKeywordsParam { @Parameter(name = "patchKey", defaultValue = KEY_PATCH, property = GV_KEYWORDS_PATCH_KEY) private String patchKey = KEY_PATCH; - public VersionKeywordsParam(String majorKey, String minorKey, String patchKey) { + @Parameter(name = "useRegex", defaultValue = "false", property = GV_KEYWORDS_KEY_USEREGEX) + private boolean useRegex = false; + + public VersionKeywordsParam(String majorKey, String minorKey, String patchKey, boolean useRegex) { this.majorKey = majorKey; this.minorKey = minorKey; this.patchKey = patchKey; + this.useRegex = useRegex; } public VersionKeywordsParam() { @@ -51,8 +55,12 @@ public String getPatchKey() { return patchKey; } + public boolean isUseRegex() { + return useRegex; + } + public VersionKeywords toVersionKeywords() { - return new VersionKeywords(getMajorKey(), getMinorKey(), getPatchKey()); + return new VersionKeywords(getMajorKey(), getMinorKey(), getPatchKey(), isUseRegex()); } @Override public boolean equals(Object o) { @@ -62,7 +70,7 @@ public boolean equals(Object o) { return false; VersionKeywordsParam that = (VersionKeywordsParam) o; return getMajorKey().equals(that.getMajorKey()) && getMinorKey().equals(that.getMinorKey()) - && getPatchKey().equals(that.getPatchKey()); + && getPatchKey().equals(that.getPatchKey()) && isUseRegex() == that.isUseRegex(); } @Override diff --git a/git-versioner-maven-plugin/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsParamTest.java b/git-versioner-maven-plugin/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsParamTest.java index 6803801..8cf73c5 100644 --- a/git-versioner-maven-plugin/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsParamTest.java +++ b/git-versioner-maven-plugin/src/test/java/com/github/manikmagar/maven/versioner/plugin/mojo/params/VersionKeywordsParamTest.java @@ -9,7 +9,7 @@ public class VersionKeywordsParamTest { @Test public void newVersionKeywords() { - assertThat(new VersionKeywords("[big]", "[medium]", "[small]")).extracting("majorKey", "minorKey", "patchKey") + assertThat(new VersionKeywords("[big]", "[medium]", "[small]", false)).extracting("majorKey", "minorKey", "patchKey") .containsExactly("[big]", "[medium]", "[small]"); } @@ -20,8 +20,8 @@ public void toDefaultVersionKeywords() { } @Test public void toCustomVersionKeywords() { - assertThat(new VersionKeywordsParam("[big]", "[medium]", "[small]")) + assertThat(new VersionKeywordsParam("[big]", "[medium]", "[small]", false)) .extracting(VersionKeywordsParam::toVersionKeywords) - .isEqualTo(new VersionKeywords("[big]", "[medium]", "[small]")); + .isEqualTo(new VersionKeywords("[big]", "[medium]", "[small]", false)); } -} \ No newline at end of file +}