The Unified and Autonomous Development Platform
feat: improve conventional commit message check to validate validate footer (#1461)
Robin Shend71ee167ca72
diff --git a/server-core/src/main/java/io/onedev/server/migration/DataMigrator.java b/server-core/src/main/java/io/onedev/server/migration/DataMigrator.javaindex 37bd09a483..b6a3a9387e 100644--- a/server-core/src/main/java/io/onedev/server/migration/DataMigrator.java+++ b/server-core/src/main/java/io/onedev/server/migration/DataMigrator.java@@ -5511,5 +5511,20 @@ public class DataMigrator {private void migrate129(File dataDir, Stack<Integer> versions) {}++ private void migrate130(File dataDir, Stack<Integer> versions) {+ for (File file: dataDir.listFiles()) {+ if (file.getName().startsWith("Projects.xml")) {+ VersionedXmlDoc dom = VersionedXmlDoc.fromFile(file);+ for (Element element: dom.getRootElement().elements()) {+ for (Element branchProtectionElement : element.element("branchProtections").elements()) {+ branchProtectionElement.addElement("checkCommitMessageFooter").setText("false");+ branchProtectionElement.addElement("commitTypesForFooterCheck");+ }+ }+ dom.writeToFile(file, false);+ }+ }+ }}diff --git a/server-core/src/main/java/io/onedev/server/model/support/code/BranchProtection.java b/server-core/src/main/java/io/onedev/server/model/support/code/BranchProtection.javaindex f95bb1afa6..0ee579372c 100644--- a/server-core/src/main/java/io/onedev/server/model/support/code/BranchProtection.java+++ b/server-core/src/main/java/io/onedev/server/model/support/code/BranchProtection.java@@ -3,7 +3,6 @@ package io.onedev.server.model.support.code;import com.google.common.base.Joiner;import com.google.common.base.Splitter;import io.onedev.commons.codeassist.InputSuggestion;-import io.onedev.commons.utils.StringUtils;import io.onedev.server.OneDev;import io.onedev.server.annotation.*;import io.onedev.server.entitymanager.BuildManager;@@ -59,11 +58,17 @@ public class BranchProtection implements Serializable {private boolean commitSignatureRequired = false;private boolean enforceConventionalCommits;-+private List<String> commitTypes = new ArrayList<>();private List<String> commitScopes = new ArrayList<>();+ private boolean checkCommitMessageFooter;++ private String commitMessageFooterPattern;++ private List<String> commitTypesForFooterCheck = new ArrayList<>();+private Integer maxCommitMessageLineLength;private String reviewRequirement;@@ -183,6 +188,44 @@ public class BranchProtection implements Serializable {this.commitScopes = commitScopes;}+ @Editable(order=391)+ @ShowCondition("isEnforceConventionalCommitsEnabled")+ public boolean isCheckCommitMessageFooter() {+ return checkCommitMessageFooter;+ }++ public void setCheckCommitMessageFooter(boolean checkCommitMessageFooter) {+ this.checkCommitMessageFooter = checkCommitMessageFooter;+ }++ private static boolean isCheckCommitMessageFooterEnabled() {+ return (boolean) EditContext.get().getInputValue("checkCommitMessageFooter");+ }++ @Editable(order=393, description = "A " ++ "<a href='https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html'>Java regular expression</a> " ++ "to validate commit message footer")+ @ShowCondition("isCheckCommitMessageFooterEnabled")+ @NotEmpty+ public String getCommitMessageFooterPattern() {+ return commitMessageFooterPattern;+ }++ public void setCommitMessageFooterPattern(String commitMessageFooterPattern) {+ this.commitMessageFooterPattern = commitMessageFooterPattern;+ }++ @Editable(order=394, description = "Optionally specify applicable commit types for commit message footer check (hit ENTER to add value). " ++ "Leave empty to all types")+ @ShowCondition("isCheckCommitMessageFooterEnabled")+ public List<String> getCommitTypesForFooterCheck() {+ return commitTypesForFooterCheck;+ }++ public void setCommitTypesForFooterCheck(List<String> commitTypesForFooterCheck) {+ this.commitTypesForFooterCheck = commitTypesForFooterCheck;+ }+@Editable(order=395, placeholder = "No limit")@Min(40)public Integer getMaxCommitMessageLineLength() {@@ -414,6 +457,15 @@ public class BranchProtection implements Serializable {var scope = matcher.group(4);if (scope != null && !commitScopes.isEmpty() && !commitScopes.contains(scope))return "Line 1: Unexpected scope '" + scope + "': Should be one of [" + Joiner.on(',').join(commitScopes) + "]";+ if (checkCommitMessageFooter && (commitTypesForFooterCheck.isEmpty() || commitTypesForFooterCheck.contains(type))) {+ var size = lines.size();+ if (size < 3 || lines.get(size-1).length() == 0+ || lines.get(size-2).length() != 0 || lines.get(size-3).length() == 0) {+ return "A footer is expected as last line and exactly one blank line should precede the footer";+ } else if (!lines.get(size-1).matches(commitMessageFooterPattern)) {+ return "Unexpected footer format: Should match pattern '" + commitMessageFooterPattern + "'";+ }+ }}} else {return "Line 1: Subject is expected of either a git revert message, or format: <type>[optional (scope)][!]: <description>";@@ -428,6 +480,7 @@ public class BranchProtection implements Serializable {break;}}+if (maxCommitMessageLineLength != null) {for (int i=0; i<lines.size(); i++) {var line = lines.get(i);diff --git a/server-core/src/test/java/io/onedev/server/model/support/code/BranchProtectionTest.java b/server-core/src/test/java/io/onedev/server/model/support/code/BranchProtectionTest.javaindex 6e27e6576b..e0c52f4f60 100644--- a/server-core/src/test/java/io/onedev/server/model/support/code/BranchProtectionTest.java+++ b/server-core/src/test/java/io/onedev/server/model/support/code/BranchProtectionTest.java@@ -41,6 +41,20 @@ public class BranchProtectionTest {assertNotNull(branchProtection.checkCommitMessage("feat:hello world\nbody", false));assertNotNull(branchProtection.checkCommitMessage("feat:hello world\n\n\nbody", false));assertNotNull(branchProtection.checkCommitMessage("hello world hello world hello world hello world hello world hello world hello world hello world hello world ", true));++ branchProtection.setCheckCommitMessageFooter(true);+ branchProtection.setCommitMessageFooterPattern("Signed By: .+");+ assertNull(branchProtection.checkCommitMessage("hello world", true));+ assertNotNull(branchProtection.checkCommitMessage("feat: a feature", false));+ assertNotNull(branchProtection.checkCommitMessage("feat: a feature\n", false));+ assertNotNull(branchProtection.checkCommitMessage("feat: a feature\nSigned By: robin", false));+ assertNotNull(branchProtection.checkCommitMessage("feat: a feature\n\na footer", false));+ assertNull(branchProtection.checkCommitMessage("feat: a feature\n\nSigned By: robin", false));++ branchProtection.setCommitTypesForFooterCheck(Lists.newArrayList("fix"));+ assertNull(branchProtection.checkCommitMessage("feat: a feature", false));+ assertNotNull(branchProtection.checkCommitMessage("fix: a bug", false));+ assertNotNull(branchProtection.checkCommitMessage("fix: a bug\n\nSigned By: Robin Shen", false));}}\ No newline at end of file