aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pom.xml5
-rw-r--r--sonar-batch/pom.xml4
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java1
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java10
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java31
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java6
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java24
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/api/synchro_controller.rb14
8 files changed, 70 insertions, 25 deletions
diff --git a/pom.xml b/pom.xml
index 9905d871c28..398f5f45d2f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1015,6 +1015,11 @@
<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>
diff --git a/sonar-batch/pom.xml b/sonar-batch/pom.xml
index 3000f785c94..e693da1521a 100644
--- a/sonar-batch/pom.xml
+++ b/sonar-batch/pom.xml
@@ -62,6 +62,10 @@
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ </dependency>
+ <dependency>
<!-- local database -->
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java
index bcb2e44d252..bc6f746351f 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapModule.java
@@ -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);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
index 8ddbf991cf5..ffb2a2f54fd 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
@@ -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);
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java b/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
index 25d9649961d..7323077518e 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
@@ -19,11 +19,22 @@
*/
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);
}
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java b/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java
index 0e1b25ef47b..bb09e9c3f18 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java
@@ -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);
}
}
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java b/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java
index b226869cdf7..4b53e7eeae0 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/DryRunDatabaseFactory.java
@@ -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) {
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/synchro_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/synchro_controller.rb
index 0dc1be27f8b..e85190acfbd 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/synchro_controller.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/synchro_controller.rb
@@ -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