Skip to content

The Unified and Autonomous Development Platform

feat: conventional commits: Regex improvements for summary (#1401)

Robin Shen63af2f1a6c3e
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.java
index d051dab228..99701e0dab 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
@@ -41,7 +41,7 @@ public class BranchProtection implements Serializable {
private static final long serialVersionUID = 1L;
private static final Pattern CONVENTIONAL_COMMIT_SUBJECT = Pattern.compile(
- "^(([\\w\\-.]+)(\\(([\\w\\-./]+)\\))?!?: [^ ].+)|^(revert \".*\")",
+ "^(([a-zA-Z]+)(\\(([a-zA-Z]+([\\-/ ][a-zA-Z]+)*)\\))?!?: [^ ].+)|^(revert \".*\")",
UNICODE_CHARACTER_CLASS | CASE_INSENSITIVE);
private boolean enabled = true;
@@ -411,17 +411,9 @@ public class BranchProtection implements Serializable {
var type = matcher.group(2);
if (!commitTypes.isEmpty() && !commitTypes.contains(type))
return "Line 1: Unexpected type '" + type + "': Should be one of [" + Joiner.on(',').join(commitTypes) + "]";
- var scopes = matcher.group(4);
- if (scopes != null) {
- var scopeFound = false;
- for (var scope : StringUtils.split(scopes, " /,")) {
- if (!commitScopes.isEmpty() && !commitScopes.contains(scope))
- return "Line 1: Unexpected scope '" + scope + "': Should be one of [" + Joiner.on(',').join(commitScopes) + "]";
- scopeFound = true;
- }
- if (!scopeFound)
- return "Line 1: Scope not specified";
- }
+ 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) + "]";
}
} else {
return "Line 1: Subject is expected of either a git revert message, or format: <type>[optional (scope)][!]: <description>";
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.java
index 355b9bb3ac..6e27e6576b 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
@@ -12,7 +12,7 @@ public class BranchProtectionTest {
var branchProtection = new BranchProtection();
branchProtection.setEnforceConventionalCommits(true);
branchProtection.setCommitTypes(Lists.newArrayList("feat", "bug"));
- branchProtection.setCommitScopes(Lists.newArrayList("docs", "api"));
+ branchProtection.setCommitScopes(Lists.newArrayList("docs", "api", "docs/api", "docs api"));
branchProtection.setMaxCommitMessageLineLength(80);
assertNull(branchProtection.checkCommitMessage("Revert \"hello world\"", false));
assertNotNull(branchProtection.checkCommitMessage("revert \"hello world\"", false));
@@ -21,6 +21,8 @@ public class BranchProtectionTest {
assertNull(branchProtection.checkCommitMessage("feat(docs): hello world", false));
assertNull(branchProtection.checkCommitMessage("feat(docs)!: hello world", false));
assertNull(branchProtection.checkCommitMessage("feat(docs/api)!: hello world", false));
+ assertNull(branchProtection.checkCommitMessage("feat(docs api)!: hello world", false));
+ assertNotNull(branchProtection.checkCommitMessage("feat(docs api)!: hello world", false));
assertNotNull(branchProtection.checkCommitMessage("feat(docs2)!: hello world", false));
assertNotNull(branchProtection.checkCommitMessage("feat(docs)!: hello world", false));
assertNotNull(branchProtection.checkCommitMessage("feat:hello world", false));