]> source.dussan.org Git - sonarqube.git/commitdiff
Revert "SONAR-6589 Fix project and module key validation"
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 1 Jun 2015 10:42:38 +0000 (12:42 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 1 Jun 2015 10:49:06 +0000 (12:49 +0200)
This reverts commit df0e388a524ebf11978e9961e9b6f25a184d9dba.

server/sonar-server/src/main/java/org/sonar/server/computation/step/ValidateProjectStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/ValidateProjectStepTest.java

index 92409fc2acf75eb52212b7d2e12c430b1a533411..120b507a6de7e75448d52e5c8c36aecf9bdecb6b 100644 (file)
@@ -31,7 +31,6 @@ import javax.annotation.Nonnull;
 import org.sonar.api.CoreProperties;
 import org.sonar.api.config.Settings;
 import org.sonar.batch.protocol.output.BatchReport;
-import org.sonar.batch.protocol.output.BatchReportReader;
 import org.sonar.core.component.ComponentDto;
 import org.sonar.core.component.ComponentKeys;
 import org.sonar.core.persistence.DbSession;
@@ -75,7 +74,7 @@ public class ValidateProjectStep implements ComputationStep {
         }
       });
       ValidateProjectsVisitor visitor = new ValidateProjectsVisitor(session, dbClient.componentDao(), context.getReportMetadata(),
-        context.getReportReader(), settings.getBoolean(CoreProperties.CORE_PREVENT_AUTOMATIC_PROJECT_CREATION), modulesByKey);
+        settings.getBoolean(CoreProperties.CORE_PREVENT_AUTOMATIC_PROJECT_CREATION), modulesByKey);
       visitor.visit(context.getRoot());
 
       if (!visitor.validationMessages.isEmpty()) {
@@ -95,21 +94,18 @@ public class ValidateProjectStep implements ComputationStep {
     private final DbSession session;
     private final ComponentDao componentDao;
     private final BatchReport.Metadata reportMetadata;
-    private final BatchReportReader batchReportReader;
     private final boolean preventAutomaticProjectCreation;
     private final Map<String, ComponentDto> modulesByKey;
     private final List<String> validationMessages = new ArrayList<>();
 
     private Component root;
 
-    public ValidateProjectsVisitor(DbSession session, ComponentDao componentDao, BatchReport.Metadata reportMetadata, BatchReportReader batchReportReader,
-      boolean preventAutomaticProjectCreation, Map<String, ComponentDto> modulesByKey) {
+    public ValidateProjectsVisitor(DbSession session, ComponentDao componentDao, BatchReport.Metadata reportMetadata, boolean preventAutomaticProjectCreation,
+      Map<String, ComponentDto> modulesByKey) {
       super(Component.Type.MODULE, Order.PRE_ORDER);
       this.session = session;
       this.componentDao = componentDao;
       this.reportMetadata = reportMetadata;
-      this.batchReportReader = batchReportReader;
-
       this.preventAutomaticProjectCreation = preventAutomaticProjectCreation;
       this.modulesByKey = modulesByKey;
     }
@@ -118,7 +114,6 @@ public class ValidateProjectStep implements ComputationStep {
     public void visitProject(Component project) {
       this.root = project;
       validateBranch();
-      validateBatchKey(project);
 
       String projectKey = project.getKey();
       ComponentDto projectDto = loadComponent(projectKey);
@@ -133,13 +128,14 @@ public class ValidateProjectStep implements ComputationStep {
           + "If you really want to stop directly analysing project \"%s\", please first delete it from SonarQube and then relaunch the analysis of project \"%s\".",
           projectKey, anotherProject.key(), anotherProject.key(), projectKey));
       }
+      validateKey(projectKey);
     }
 
     @Override
     public void visitModule(Component module) {
-      String projectKey = root.getKey();
       String moduleKey = module.getKey();
-      validateBatchKey(module);
+      String projectKey = root.getKey();
+      validateKey(moduleKey);
 
       ComponentDto moduleDto = loadComponent(moduleKey);
       if (moduleDto == null) {
@@ -156,11 +152,10 @@ public class ValidateProjectStep implements ComputationStep {
       }
     }
 
-    private void validateBatchKey(Component component) {
-      String batchKey = batchReportReader.readComponent(component.getRef()).getKey();
-      if (!ComponentKeys.isValidModuleKey(batchKey)) {
+    private void validateKey(String moduleKey) {
+      if (!ComponentKeys.isValidModuleKey(moduleKey)) {
         validationMessages.add(String.format("\"%s\" is not a valid project or module key. "
-          + "Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", batchKey));
+          + "Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", moduleKey));
       }
     }
 
index 8bfcf5a067aab150b242ee0397a70de778650fc2..434c3dfca867b694d22f866ac79892589643af14 100644 (file)
@@ -21,6 +21,7 @@
 package org.sonar.server.computation.step;
 
 import java.io.File;
+import java.io.IOException;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.ClassRule;
@@ -30,7 +31,6 @@ import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 import org.sonar.api.CoreProperties;
 import org.sonar.api.config.Settings;
-import org.sonar.batch.protocol.Constants;
 import org.sonar.batch.protocol.output.BatchReport;
 import org.sonar.batch.protocol.output.BatchReportReader;
 import org.sonar.batch.protocol.output.BatchReportWriter;
@@ -65,9 +65,6 @@ public class ValidateProjectStepTest {
 
   Settings settings;
 
-  File reportDir;
-  BatchReportWriter writer;
-
   ValidateProjectStep sut;
 
   @Before
@@ -76,8 +73,6 @@ public class ValidateProjectStepTest {
     dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), new ComponentDao());
     dbSession = dbClient.openSession(false);
     settings = new Settings();
-    reportDir = temp.newFolder();
-    writer = new BatchReportWriter(reportDir);
 
     sut = new ValidateProjectStep(dbClient, settings);
   }
@@ -89,18 +84,11 @@ public class ValidateProjectStepTest {
 
   @Test
   public void not_fail_if_provisioning_enforced_and_project_exists() throws Exception {
-    writer.writeMetadata(BatchReport.Metadata.newBuilder().build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(1)
-      .setType(Constants.ComponentType.PROJECT)
-      .setKey(PROJECT_KEY)
-      .build());
-
     settings.appendProperty(CoreProperties.CORE_PREVENT_AUTOMATIC_PROJECT_CREATION, "true");
     dbClient.componentDao().insert(dbSession, ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY));
     dbSession.commit();
 
-    sut.execute(new ComputationContext(new BatchReportReader(reportDir), null, null, null,
+    sut.execute(new ComputationContext(createBasicBatchReportReader(), null, null, null,
       ComponentTreeBuilders.from(new DumbComponent(Component.Type.PROJECT, 1, "ABCD", PROJECT_KEY)), null));
   }
 
@@ -109,66 +97,46 @@ public class ValidateProjectStepTest {
     thrown.expect(IllegalArgumentException.class);
     thrown.expectMessage("Unable to scan non-existing project '" + PROJECT_KEY + "'");
 
-    writer.writeMetadata(BatchReport.Metadata.newBuilder().build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(1)
-      .setType(Constants.ComponentType.PROJECT)
-      .setKey(PROJECT_KEY)
-      .build());
-
     settings.appendProperty(CoreProperties.CORE_PREVENT_AUTOMATIC_PROJECT_CREATION, "true");
 
-    sut.execute(new ComputationContext(new BatchReportReader(reportDir), null, null, null,
+    sut.execute(new ComputationContext(createBasicBatchReportReader(), null, null, null,
       ComponentTreeBuilders.from(new DumbComponent(Component.Type.PROJECT, 1, "ABCD", PROJECT_KEY)), null));
   }
 
   @Test
   public void fail_if_provisioning_not_enforced_and_project_does_not_exists() throws Exception {
-    writer.writeMetadata(BatchReport.Metadata.newBuilder().build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(1)
-      .setType(Constants.ComponentType.PROJECT)
-      .setKey(PROJECT_KEY)
-      .build());
-
     settings.appendProperty(CoreProperties.CORE_PREVENT_AUTOMATIC_PROJECT_CREATION, "false");
 
-    sut.execute(new ComputationContext(new BatchReportReader(reportDir), null, null, null,
+    sut.execute(new ComputationContext(createBasicBatchReportReader(), null, null, null,
       ComponentTreeBuilders.from(new DumbComponent(Component.Type.PROJECT, 1, "ABCD", PROJECT_KEY)), null));
   }
 
   @Test
   public void not_fail_on_valid_branch() throws Exception {
+    File reportDir = temp.newFolder();
+    BatchReportWriter writer = new BatchReportWriter(reportDir);
     writer.writeMetadata(BatchReport.Metadata.newBuilder()
       .setBranch("origin/master")
       .build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(1)
-      .setType(Constants.ComponentType.PROJECT)
-      .setKey(PROJECT_KEY)
-      .build());
 
     sut.execute(new ComputationContext(new BatchReportReader(reportDir), null, null, null,
-      ComponentTreeBuilders.from(new DumbComponent(Component.Type.PROJECT, 1, "ABCD", PROJECT_KEY + ":origin/master")), null));
+      ComponentTreeBuilders.from(new DumbComponent(Component.Type.PROJECT, 1, "ABCD", PROJECT_KEY)), null));
   }
 
   @Test
   public void fail_on_invalid_branch() throws Exception {
+    File reportDir = temp.newFolder();
     thrown.expect(IllegalArgumentException.class);
     thrown.expectMessage("Validation of project failed:\n" +
       "  o \"bran#ch\" is not a valid branch name. Allowed characters are alphanumeric, '-', '_', '.' and '/'.");
 
+    BatchReportWriter writer = new BatchReportWriter(reportDir);
     writer.writeMetadata(BatchReport.Metadata.newBuilder()
       .setBranch("bran#ch")
       .build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(1)
-      .setType(Constants.ComponentType.PROJECT)
-      .setKey(PROJECT_KEY)
-      .build());
 
     sut.execute(new ComputationContext(new BatchReportReader(reportDir), null, null, null,
-      ComponentTreeBuilders.from(new DumbComponent(Component.Type.PROJECT, 1, "ABCD", PROJECT_KEY + ":bran#ch")), null));
+      ComponentTreeBuilders.from(new DumbComponent(Component.Type.PROJECT, 1, "ABCD", PROJECT_KEY)), null));
   }
 
   @Test
@@ -180,22 +148,9 @@ public class ValidateProjectStepTest {
       "  o \"Project\\Key\" is not a valid project or module key. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.\n" +
       "  o \"Module$Key\" is not a valid project or module key. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit");
 
-    writer.writeMetadata(BatchReport.Metadata.newBuilder().build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(1)
-      .setType(Constants.ComponentType.PROJECT)
-      .setKey(invalidProjectKey)
-      .addChildRef(2)
-      .build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(2)
-      .setType(Constants.ComponentType.MODULE)
-      .setKey("Module$Key")
-      .build());
-
     DumbComponent root = new DumbComponent(Component.Type.PROJECT, 1, "ABCD", invalidProjectKey,
       new DumbComponent(Component.Type.MODULE, 2, "BCDE", "Module$Key"));
-    sut.execute(new ComputationContext(new BatchReportReader(reportDir), null, null, null, ComponentTreeBuilders.from(root), null));
+    sut.execute(new ComputationContext(createBasicBatchReportReader(), null, null, null, ComponentTreeBuilders.from(root), null));
   }
 
   @Test
@@ -206,26 +161,13 @@ public class ValidateProjectStepTest {
       "If you really want to stop directly analysing project \"" + MODULE_KEY + "\", please first delete it from SonarQube and then relaunch the analysis of project \""
       + PROJECT_KEY + "\".");
 
-    writer.writeMetadata(BatchReport.Metadata.newBuilder().build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(1)
-      .setType(Constants.ComponentType.PROJECT)
-      .setKey(PROJECT_KEY)
-      .addChildRef(2)
-      .build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(2)
-      .setType(Constants.ComponentType.MODULE)
-      .setKey(MODULE_KEY)
-      .build());
-
     ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(MODULE_KEY);
     dbClient.componentDao().insert(dbSession, project);
     dbSession.commit();
 
     DumbComponent root = new DumbComponent(Component.Type.PROJECT, 1, "ABCD", PROJECT_KEY,
       new DumbComponent(Component.Type.MODULE, 2, "BCDE", MODULE_KEY));
-    sut.execute(new ComputationContext(new BatchReportReader(reportDir), null, null, null,
+    sut.execute(new ComputationContext(createBasicBatchReportReader(), null, null, null,
       ComponentTreeBuilders.from(root), null));
   }
 
@@ -236,19 +178,6 @@ public class ValidateProjectStepTest {
     thrown.expectMessage("Validation of project failed:\n" +
       "  o Module \"" + MODULE_KEY + "\" is already part of project \"" + anotherProjectKey + "\"");
 
-    writer.writeMetadata(BatchReport.Metadata.newBuilder().build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(1)
-      .setType(Constants.ComponentType.PROJECT)
-      .setKey(PROJECT_KEY)
-      .addChildRef(2)
-      .build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(2)
-      .setType(Constants.ComponentType.MODULE)
-      .setKey(MODULE_KEY)
-      .build());
-
     ComponentDto project = ComponentTesting.newProjectDto("ABCD").setKey(PROJECT_KEY);
     ComponentDto anotherProject = ComponentTesting.newProjectDto().setKey(anotherProjectKey);
     dbClient.componentDao().insert(dbSession, project, anotherProject);
@@ -258,7 +187,7 @@ public class ValidateProjectStepTest {
 
     DumbComponent root = new DumbComponent(Component.Type.PROJECT, 1, "ABCD", PROJECT_KEY,
       new DumbComponent(Component.Type.MODULE, 2, "BCDE", MODULE_KEY));
-    sut.execute(new ComputationContext(new BatchReportReader(reportDir), null, null, null,
+    sut.execute(new ComputationContext(createBasicBatchReportReader(), null, null, null,
       ComponentTreeBuilders.from(root), null));
   }
 
@@ -271,19 +200,6 @@ public class ValidateProjectStepTest {
       "If you really want to stop directly analysing project \"" + anotherProjectKey + "\", please first delete it from SonarQube and then relaunch the analysis of project \""
       + PROJECT_KEY + "\".");
 
-    writer.writeMetadata(BatchReport.Metadata.newBuilder().build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(1)
-      .setType(Constants.ComponentType.PROJECT)
-      .setKey(PROJECT_KEY)
-      .addChildRef(2)
-      .build());
-    writer.writeComponent(BatchReport.Component.newBuilder()
-      .setRef(2)
-      .setType(Constants.ComponentType.MODULE)
-      .setKey(MODULE_KEY)
-      .build());
-
     ComponentDto anotherProject = ComponentTesting.newProjectDto().setKey(anotherProjectKey);
     dbClient.componentDao().insert(dbSession, anotherProject);
     ComponentDto module = ComponentTesting.newModuleDto("ABCD", anotherProject).setKey(PROJECT_KEY);
@@ -292,8 +208,15 @@ public class ValidateProjectStepTest {
 
     DumbComponent root = new DumbComponent(Component.Type.PROJECT, 1, "ABCD", PROJECT_KEY,
       new DumbComponent(Component.Type.MODULE, 2, "BCDE", MODULE_KEY));
-    sut.execute(new ComputationContext(new BatchReportReader(reportDir), null, null, null,
+    sut.execute(new ComputationContext(createBasicBatchReportReader(), null, null, null,
       ComponentTreeBuilders.from(root), null));
   }
 
+  private BatchReportReader createBasicBatchReportReader() throws IOException {
+    File reportDir = temp.newFolder();
+    BatchReportWriter writer = new BatchReportWriter(reportDir);
+    writer.writeMetadata(BatchReport.Metadata.newBuilder()
+      .build());
+    return new BatchReportReader(reportDir);
+  }
 }