Browse Source

SONAR-11561 Allow more special characters in branch names

tags/7.7
Duarte Meneses 5 years ago
parent
commit
f02eeba4ef

+ 1
- 1
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/DefaultBranchImpl.java View File

@@ -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));
}

+ 3
- 3
server/sonar-server/src/main/java/org/sonar/server/component/ComponentUpdater.java View File

@@ -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);
}


+ 1
- 1
sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java View File

@@ -111,7 +111,7 @@ public final class ComponentKeys {
*
* @return <code>true</code> if <code>branchCandidate</code> can be used for a project
*/
public static boolean isValidBranch(String branchCandidate) {
public static boolean isValidLegacyBranch(String branchCandidate) {
return branchCandidate.matches(VALID_BRANCH_REGEXP);
}


+ 8
- 8
sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java View File

@@ -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

+ 3
- 3
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectReactorValidator.java View File

@@ -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<String> validationMessages, @Nullable String branch) {
if (isNotEmpty(branch) && !ComponentKeys.isValidBranch(branch)) {
private static void validateLegacyBranch(List<String> 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));
}

Loading…
Cancel
Save