<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>
<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>
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);
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;
addProjectPluginExtensions();
}
-
private void addProjectComponents() {
ProjectDefinition projectDefinition = container.getComponentByType(ProjectTree.class).getProjectDefinition(project);
container.addSingleton(projectDefinition);
container.addSingleton(ResourceFilters.class);
container.addSingleton(DefaultModelFinder.class);
container.addSingleton(DefaultProfileLoader.class);
+ container.addSingleton(DryRunExporter.class);
container.addPicoAdapter(new ProfileProvider());
}
installer.install(container, InstantiationStrategy.PROJECT);
}
-
private void logSettings() {
// TODO move these logs in a dedicated component
LOG.info("------------- Analyzing {}", project.getName());
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);
}
*/
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
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) {
}
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);
}
}
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;
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);
}
}
this.serverFileSystem = serverFileSystem;
}
- public byte[] createDatabaseForDryRun(int resourceId) {
+ public byte[] createDatabaseForDryRun(Integer resourceId) {
String name = serverFileSystem.getTempDir().getAbsolutePath() + "db-" + System.nanoTime();
try {
}
}
- 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) {
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
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