]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4974 SONAR-4928 Validate project key before batch bootstrap download
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 31 Jan 2014 16:47:57 +0000 (17:47 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 31 Jan 2014 16:48:03 +0000 (17:48 +0100)
sonar-batch/src/main/java/org/sonar/batch/scan/DefaultProjectBootstrapper.java
sonar-batch/src/test/java/org/sonar/batch/scan/DefaultProjectBootstrapperTest.java
sonar-batch/src/test/resources/org/sonar/batch/scan/DefaultProjectBootstrapperTest/project-with-invalid-key/sonar-project.properties [new file with mode: 0644]

index ca2c50e546a03dbb1dad25817de021ee3d1e0140..7ac6b45ca2c24ab90f4fd6a28391fa077a485b9f 100644 (file)
@@ -35,16 +35,14 @@ import org.sonar.api.batch.bootstrap.ProjectBootstrapper;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.batch.bootstrap.BootstrapSettings;
+import org.sonar.core.component.ComponentKeys;
 
 import java.io.File;
 import java.io.FileFilter;
 import java.io.FileInputStream;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.Map.Entry;
-import java.util.Properties;
 
 /**
  * Class that creates a Sonar project definition based on a set of properties.
@@ -134,9 +132,11 @@ class DefaultProjectBootstrapper implements ProjectBootstrapper {
     } else {
       checkMandatoryProperties(properties, MANDATORY_PROPERTIES_FOR_SIMPLE_PROJECT);
     }
+    final String projectKey = properties.getProperty(CoreProperties.PROJECT_KEY_PROPERTY);
+    checkProjectKeyValid(projectKey);
     File workDir;
     if (parent == null) {
-      validateDirectories(properties, baseDir, properties.getProperty(CoreProperties.PROJECT_KEY_PROPERTY));
+      validateDirectories(properties, baseDir, projectKey);
       workDir = initRootProjectWorkDir(baseDir);
     } else {
       workDir = initModuleWorkDir(properties);
@@ -148,6 +148,14 @@ class DefaultProjectBootstrapper implements ProjectBootstrapper {
     return definition;
   }
 
+  private void checkProjectKeyValid(String projectKey) {
+    if (!ComponentKeys.isValidModuleKey(projectKey)) {
+      throw new IllegalStateException(String.format(
+        "Invalid project key '%s'.\n"
+        + "Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", projectKey));
+    }
+  }
+
   @VisibleForTesting
   protected File initRootProjectWorkDir(File baseDir) {
     String workDir = settings.property(CoreProperties.WORKING_DIRECTORY);
index caedb3c46a18471615e3c372961f1f5b169c72f6..c4a47e9d9da01ef7c36943ca13249ec48082fd66 100644 (file)
@@ -68,6 +68,13 @@ public class DefaultProjectBootstrapperTest {
       TestUtils.getResource(this.getClass(), "simple-project-with-deprecated-props/libs/lib2.txt").getAbsolutePath());
   }
 
+  @Test
+  public void shouldFailOnInvalidProjectKey() throws IOException {
+    thrown.expect(IllegalStateException.class);
+    thrown.expectMessage("Invalid project key 'A key with spaces'.\nAllowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.");
+    loadProjectDefinition("project-with-invalid-key");
+  }
+
   @Test
   public void shouldFailIfUnexistingSourceDirectory() throws IOException {
     thrown.expect(IllegalStateException.class);
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/scan/DefaultProjectBootstrapperTest/project-with-invalid-key/sonar-project.properties b/sonar-batch/src/test/resources/org/sonar/batch/scan/DefaultProjectBootstrapperTest/project-with-invalid-key/sonar-project.properties
new file mode 100644 (file)
index 0000000..d13fd3f
--- /dev/null
@@ -0,0 +1,7 @@
+sonar.projectKey=A key with spaces
+sonar.projectName=Foo Project
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.projectDescription=Description of Foo Project
+
+sonar.sources=sources
+sonar.libraries=libs/*.txt