]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3895 Draft of export and can work when project doesn't exist
authorDavid Gageot <david@gageot.net>
Mon, 29 Oct 2012 09:44:42 +0000 (10:44 +0100)
committerDavid Gageot <david@gageot.net>
Mon, 29 Oct 2012 09:45:10 +0000 (10:45 +0100)
pom.xml
sonar-batch/pom.xml
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java
sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/api/synchro_controller.rb

diff --git a/pom.xml b/pom.xml
index 9905d871c284db646f2c49e7a8623a518b5bd15c..398f5f45d2f5d81c9e510b7bc17d0e6d48b4e2ee 100644 (file)
--- a/pom.xml
+++ b/pom.xml
         <artifactId>reflections</artifactId>
         <version>0.9.7.RC1</version>
       </dependency>
+      <dependency>
+        <groupId>com.google.code.gson</groupId>
+        <artifactId>gson</artifactId>
+        <version>2.2.2</version>
+      </dependency>
     </dependencies>
   </dependencyManagement>
 
index 3000f785c9449ee409b0975cd085a9af82c1e173..e693da1521ab1c1526b2dbc2eb52f3c36af30726 100644 (file)
       <groupId>commons-lang</groupId>
       <artifactId>commons-lang</artifactId>
     </dependency>
+    <dependency>
+      <groupId>com.google.code.gson</groupId>
+      <artifactId>gson</artifactId>
+    </dependency>
     <dependency>
       <!-- local database -->
       <groupId>com.h2database</groupId>
index bcb2e44d252156726392cfc770f86684df2322a6..bc6f746351f1c246b26811f1b6412022a51e07a1 100644 (file)
@@ -63,7 +63,6 @@ public class BootstrapModule extends Module {
     container.addSingleton(BatchPluginRepository.class);
     container.addSingleton(ExtensionInstaller.class);
     container.addSingleton(DryRun.class);
-    container.addSingleton(DryRunExporter.class);
     container.addSingleton(Logback.class);
     container.addSingleton(ServerClient.class);
     container.addSingleton(ServerMetadata.class);
index 8ddbf991cf5a700c1a8f38b7978600aec5b64ddc..ffb2a2f54fda121021029071584cd9f7cb68841c 100644 (file)
@@ -42,6 +42,7 @@ import org.sonar.batch.config.UnsupportedProperties;
 import org.sonar.batch.events.EventBus;
 import org.sonar.batch.index.DefaultIndex;
 import org.sonar.batch.index.DefaultResourcePersister;
+import org.sonar.batch.local.DryRunExporter;
 import org.sonar.batch.phases.Phases;
 import org.sonar.batch.phases.PhasesTimeProfiler;
 import org.sonar.core.qualitymodel.DefaultModelFinder;
@@ -64,7 +65,6 @@ public class ProjectModule extends Module {
     addProjectPluginExtensions();
   }
 
-
   private void addProjectComponents() {
     ProjectDefinition projectDefinition = container.getComponentByType(ProjectTree.class).getProjectDefinition(project);
     container.addSingleton(projectDefinition);
@@ -94,6 +94,7 @@ public class ProjectModule extends Module {
     container.addSingleton(ResourceFilters.class);
     container.addSingleton(DefaultModelFinder.class);
     container.addSingleton(DefaultProfileLoader.class);
+    container.addSingleton(DryRunExporter.class);
     container.addPicoAdapter(new ProfileProvider());
   }
 
@@ -111,7 +112,6 @@ public class ProjectModule extends Module {
     installer.install(container, InstantiationStrategy.PROJECT);
   }
 
-
   private void logSettings() {
     // TODO move these logs in a dedicated component
     LOG.info("-------------  Analyzing {}", project.getName());
@@ -124,9 +124,9 @@ public class ProjectModule extends Module {
   protected void doStart() {
     DefaultIndex index = container.getComponentByType(DefaultIndex.class);
     index.setCurrentProject(project,
-      container.getComponentByType(ResourceFilters.class),
-      container.getComponentByType(ViolationFilters.class),
-      container.getComponentByType(RulesProfile.class));
+        container.getComponentByType(ResourceFilters.class),
+        container.getComponentByType(ViolationFilters.class),
+        container.getComponentByType(RulesProfile.class));
 
     container.getComponentByType(Phases.class).execute(project);
   }
index 25d9649961d28e738f6c302bba91da526a366628..7323077518e8a4300e42397b4bd39f65a4b2f510 100644 (file)
  */
 package org.sonar.batch.local;
 
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import com.google.gson.Gson;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.BatchComponent;
 import org.sonar.api.batch.SensorContext;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.rules.Violation;
+import org.sonar.api.violations.ViolationQuery;
 import org.sonar.batch.bootstrap.DryRun;
+import org.sonar.batch.index.DefaultIndex;
+
+import java.io.Serializable;
+import java.util.List;
+import java.util.Map;
 
 /**
  * @since 3.4
@@ -32,9 +43,11 @@ public class DryRunExporter implements BatchComponent {
   private static final Logger LOG = LoggerFactory.getLogger(DryRunExporter.class);
 
   private final DryRun dryRun;
+  private final DefaultIndex sonarIndex;
 
-  public DryRunExporter(DryRun dryRun) {
+  public DryRunExporter(DryRun dryRun, DefaultIndex sonarIndex) {
     this.dryRun = dryRun;
+    this.sonarIndex = sonarIndex;
   }
 
   public void execute(SensorContext context) {
@@ -43,5 +56,21 @@ public class DryRunExporter implements BatchComponent {
     }
 
     LOG.info("Exporting dry run results");
+
+    List<Map<String, ? extends Serializable>> results = Lists.newArrayList();
+
+    for (Resource resource : sonarIndex.getResources()) {
+      List<Violation> violations = sonarIndex.getViolations(ViolationQuery.create().forResource(resource));
+      for (Violation violation : violations) {
+        results.add(ImmutableMap.of(
+            "resource", violation.getResource().getKey(),
+            "line", violation.getLineId(),
+            "message", violation.getMessage()));
+
+      }
+    }
+
+    String json = new Gson().toJson(results);
+    System.out.println(json);
   }
 }
index 0e1b25ef47b19bb90a75b2a7b2a69de2cb712ad3..bb09e9c3f18164a38cbfd5f380739215e800862b 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.batch.SensorContext;
 import org.sonar.batch.bootstrap.DryRun;
+import org.sonar.batch.index.DefaultIndex;
 
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verifyZeroInteractions;
@@ -31,17 +32,18 @@ public class DryRunExporterTest {
   DryRunExporter dryRunExporter;
 
   DryRun dryRun = mock(DryRun.class);
+  DefaultIndex sonarIndex = mock(DefaultIndex.class);
   SensorContext sensorContext = mock(SensorContext.class);
 
   @Before
   public void setUp() {
-    dryRunExporter = new DryRunExporter(dryRun);
+    dryRunExporter = new DryRunExporter(dryRun, sonarIndex);
   }
 
   @Test
   public void should_disable_if_no_dry_run() {
     dryRunExporter.execute(sensorContext);
 
-    verifyZeroInteractions(sensorContext);
+    verifyZeroInteractions(sensorContext, sonarIndex);
   }
 }
index b226869cdf76db5e5b46d126e2f4cbf073c50b61..4b53e7eeae0680893323923e63284d83a4604b75 100644 (file)
@@ -46,7 +46,7 @@ public class DryRunDatabaseFactory implements ServerComponent {
     this.serverFileSystem = serverFileSystem;
   }
 
-  public byte[] createDatabaseForDryRun(int resourceId) {
+  public byte[] createDatabaseForDryRun(Integer resourceId) {
     String name = serverFileSystem.getTempDir().getAbsolutePath() + "db-" + System.nanoTime();
 
     try {
@@ -62,21 +62,23 @@ public class DryRunDatabaseFactory implements ServerComponent {
     }
   }
 
-  private void copy(DataSource source, DataSource dest, int resourceId) {
+  private void copy(DataSource source, DataSource dest, Integer resourceId) {
+    String notSecured = "NOT (PROP_KEY LIKE '%.secured')";
+    String defaultProperty = "((USER_ID IS NULL) AND (RESOURCE_ID IS NULL))";
+    String projectProperty = (null == resourceId) ? "" : " OR (RESOURCE_ID='" + resourceId + "')";
+
     new DbTemplate()
-        .copyTable(source, dest, "PROPERTIES",
-            "SELECT * FROM PROPERTIES WHERE (((USER_ID IS NULL) AND (RESOURCE_ID IS NULL)) OR (RESOURCE_ID='" + resourceId +
-              "')) AND NOT (PROP_KEY LIKE '%.secured')")
-        .copyTable(source, dest, "RULES_PROFILES", "SELECT * FROM RULES_PROFILES")
-        .copyTable(source, dest, "RULES", "SELECT * FROM RULES")
-        .copyTable(source, dest, "RULES_PARAMETERS", "SELECT * FROM RULES_PARAMETERS")
         .copyTable(source, dest, "ACTIVE_RULES", "SELECT * FROM ACTIVE_RULES")
         .copyTable(source, dest, "ACTIVE_RULE_PARAMETERS", "SELECT * FROM ACTIVE_RULE_PARAMETERS")
-        .copyTable(source, dest, "METRICS", "SELECT * FROM METRICS")
         .copyTable(source, dest, "CHARACTERISTICS", "SELECT * FROM CHARACTERISTICS")
-        .copyTable(source, dest, "CHARACTERISTIC_PROPERTIES", "SELECT * FROM CHARACTERISTIC_PROPERTIES")
         .copyTable(source, dest, "CHARACTERISTIC_EDGES", "SELECT * FROM CHARACTERISTIC_EDGES")
-        .copyTable(source, dest, "QUALITY_MODELS", "SELECT * FROM QUALITY_MODELS");
+        .copyTable(source, dest, "CHARACTERISTIC_PROPERTIES", "SELECT * FROM CHARACTERISTIC_PROPERTIES")
+        .copyTable(source, dest, "METRICS", "SELECT * FROM METRICS")
+        .copyTable(source, dest, "PROPERTIES", "SELECT * FROM PROPERTIES WHERE " + notSecured + " AND (" + defaultProperty + projectProperty + ")")
+        .copyTable(source, dest, "QUALITY_MODELS", "SELECT * FROM QUALITY_MODELS")
+        .copyTable(source, dest, "RULES", "SELECT * FROM RULES")
+        .copyTable(source, dest, "RULES_PARAMETERS", "SELECT * FROM RULES_PARAMETERS")
+        .copyTable(source, dest, "RULES_PROFILES", "SELECT * FROM RULES_PROFILES");
   }
 
   private BasicDataSource create(String dialect, String driver, String user, String password, String url) {
index 0dc1be27f8b83cc62805fcabf5398a6ee002a4a7..e85190acfbdd9569b51a0f97a3fdc6102002f4d8 100644 (file)
@@ -27,7 +27,8 @@ class Api::SynchroController < Api::ApiController
     require_parameters :resource
     load_resource()
 
-    dbFileContent = java_facade.createDatabaseForDryRun(@resource.id)
+    resource_id = @resource.id if @resource
+    dbFileContent = java_facade.createDatabaseForDryRun(resource_id)
 
     send_data String.from_java_bytes(dbFileContent)
   end
@@ -35,10 +36,13 @@ class Api::SynchroController < Api::ApiController
   private
 
   def load_resource
-    resource_id = params[:resource]
-    @resource = Project.by_key(resource_id)
-    return not_found("Resource [#{resource_id}] not found") if @resource.nil?
-    return access_denied unless is_user?(@resource)
+    resource_key = params[:resource]
+    @resource = Project.by_key(resource_key)
+    if @resource
+      access_denied unless is_user?(@resource)
+    else
+      #access_denied unless is_user?(nil)
+    end
   end
 end