From f02eeba4ef9908da5d6e5ea969e91a22c6c443cd Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 1 Mar 2019 08:17:32 -0600 Subject: [PATCH] SONAR-11561 Allow more special characters in branch names --- .../component/DefaultBranchImpl.java | 2 +- .../sonar/server/component/ComponentUpdater.java | 6 +++--- .../org/sonar/core/component/ComponentKeys.java | 2 +- .../sonar/core/component/ComponentKeysTest.java | 16 ++++++++-------- .../scanner/scan/ProjectReactorValidator.java | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/DefaultBranchImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/DefaultBranchImpl.java index 09c83904ac0..ae137aa4743 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/DefaultBranchImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/DefaultBranchImpl.java @@ -47,7 +47,7 @@ public class DefaultBranchImpl implements Branch { public DefaultBranchImpl(@Nullable String name) { this.isLegacyBranch = (name != null); this.branchName = (name == null) ? BranchDto.DEFAULT_MAIN_BRANCH_NAME : name; - if (!ComponentKeys.isValidBranch(branchName)) { + if (!ComponentKeys.isValidLegacyBranch(branchName)) { throw MessageException.of(format("\"%s\" is not a valid branch name. " + "Allowed characters are alphanumeric, '-', '_', '.' and '/'.", branchName)); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentUpdater.java index 8c784b3ad2b..0eb1efecd36 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentUpdater.java @@ -100,7 +100,7 @@ public class ComponentUpdater { } private ComponentDto createRootComponent(DbSession session, NewComponent newComponent) { - checkBranchFormat(newComponent.qualifier(), newComponent.deprecatedBranch()); + checkLegacyBranchFormat(newComponent.qualifier(), newComponent.deprecatedBranch()); String keyWithBranch = ComponentKeys.createKey(newComponent.key(), newComponent.deprecatedBranch()); checkRequest(!dbClient.componentDao().selectByKey(session, keyWithBranch).isPresent(), "Could not create %s, key already exists: %s", getQualifierToDisplay(newComponent.qualifier()), keyWithBranch); @@ -169,8 +169,8 @@ public class ComponentUpdater { "Malformed key for %s: %s. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", getQualifierToDisplay(qualifier), key); } - private void checkBranchFormat(String qualifier, @Nullable String branch) { - checkRequest(branch == null || ComponentKeys.isValidBranch(branch), + private void checkLegacyBranchFormat(String qualifier, @Nullable String branch) { + checkRequest(branch == null || ComponentKeys.isValidLegacyBranch(branch), "Malformed branch for %s: %s. Allowed characters are alphanumeric, '-', '_', '.' and '/', with at least one non-digit.", getQualifierToDisplay(qualifier), branch); } diff --git a/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java b/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java index 93cbbe0439f..a3bb4fb1453 100644 --- a/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java +++ b/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java @@ -111,7 +111,7 @@ public final class ComponentKeys { * * @return true if branchCandidate can be used for a project */ - public static boolean isValidBranch(String branchCandidate) { + public static boolean isValidLegacyBranch(String branchCandidate) { return branchCandidate.matches(VALID_BRANCH_REGEXP); } diff --git a/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java b/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java index d4f45a69769..a328a0c586b 100644 --- a/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java +++ b/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java @@ -68,14 +68,14 @@ public class ComponentKeysTest { @Test public void isValidBranchKey() { - assertThat(ComponentKeys.isValidBranch("")).isTrue(); - assertThat(ComponentKeys.isValidBranch("abc")).isTrue(); - assertThat(ComponentKeys.isValidBranch("0123")).isTrue(); - assertThat(ComponentKeys.isValidBranch("ab 12")).isFalse(); - assertThat(ComponentKeys.isValidBranch("ab_12")).isTrue(); - assertThat(ComponentKeys.isValidBranch("ab/12")).isTrue(); - assertThat(ComponentKeys.isValidBranch("ab\\12")).isFalse(); - assertThat(ComponentKeys.isValidBranch("ab\n")).isFalse(); + assertThat(ComponentKeys.isValidLegacyBranch("")).isTrue(); + assertThat(ComponentKeys.isValidLegacyBranch("abc")).isTrue(); + assertThat(ComponentKeys.isValidLegacyBranch("0123")).isTrue(); + assertThat(ComponentKeys.isValidLegacyBranch("ab 12")).isFalse(); + assertThat(ComponentKeys.isValidLegacyBranch("ab_12")).isTrue(); + assertThat(ComponentKeys.isValidLegacyBranch("ab/12")).isTrue(); + assertThat(ComponentKeys.isValidLegacyBranch("ab\\12")).isFalse(); + assertThat(ComponentKeys.isValidLegacyBranch("ab\n")).isFalse(); } @Test diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java index fb67d7dc0fa..c49d685c9c3 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java @@ -85,7 +85,7 @@ public class ProjectReactorValidator { validatePullRequestParamsWhenPluginAbsent(validationMessages); } - validateBranch(validationMessages, deprecatedBranchName); + validateLegacyBranch(validationMessages, deprecatedBranchName); if (!validationMessages.isEmpty()) { throw MessageException.of("Validation of project reactor failed:\n o " + Joiner.on("\n o ").join(validationMessages)); @@ -122,8 +122,8 @@ public class ProjectReactorValidator { } } - private static void validateBranch(List validationMessages, @Nullable String branch) { - if (isNotEmpty(branch) && !ComponentKeys.isValidBranch(branch)) { + private static void validateLegacyBranch(List validationMessages, @Nullable String branch) { + if (isNotEmpty(branch) && !ComponentKeys.isValidLegacyBranch(branch)) { validationMessages.add(format("\"%s\" is not a valid branch name. " + "Allowed characters are alphanumeric, '-', '_', '.' and '/'.", branch)); } -- 2.39.5