Skip to content

Commit

Permalink
SONAR-6015 Produce Json report
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed Jan 13, 2015
1 parent a54ad96 commit 1ea2fd3
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 56 deletions.
Expand Up @@ -25,7 +25,6 @@
import org.sonar.api.Property;
import org.sonar.api.PropertyType;
import org.sonar.api.SonarPlugin;
import org.sonar.api.checks.NoSonarFilter;
import org.sonar.core.timemachine.Periods;
import org.sonar.plugins.core.charts.DistributionAreaChart;
import org.sonar.plugins.core.charts.DistributionBarChart;
Expand Down Expand Up @@ -373,7 +372,6 @@ public List getExtensions() {
CoverageMeasurementFilter.class,
ApplyProjectRolesDecorator.class,
CommentDensityDecorator.class,
NoSonarFilter.class,
DirectoriesDecorator.class,
FilesDecorator.class,
ManualMeasureDecorator.class,
Expand Down
Expand Up @@ -134,8 +134,10 @@ public Map<PluginMetadata, Plugin> getPluginsByMetadata() {
static class PluginFilter {
private static final String PROPERTY_IS_DEPRECATED_MSG = "Property {0} is deprecated. Please use {1} instead.";
Set<String> whites = newHashSet(), blacks = newHashSet();
private AnalysisMode mode;

PluginFilter(Settings settings, AnalysisMode mode) {
this.mode = mode;
if (settings.hasKey(CoreProperties.BATCH_INCLUDE_PLUGINS)) {
whites.addAll(Arrays.asList(settings.getStringArray(CoreProperties.BATCH_INCLUDE_PLUGINS)));
}
Expand Down Expand Up @@ -177,7 +179,7 @@ static List<String> propertyValues(Settings settings, String key, String default

boolean accepts(String pluginKey) {
if (CORE_PLUGIN.equals(pluginKey)) {
return true;
return !mode.isSensorMode();
}

List<String> mergeList = newArrayList(blacks);
Expand Down
Expand Up @@ -83,6 +83,7 @@ protected void doBeforeStart() {
addDatabaseComponents();
addCoreComponents();
}

}

private void addBootstrapComponents() {
Expand Down
Expand Up @@ -32,6 +32,7 @@
import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
import org.sonar.batch.scan.filesystem.FileSystemLogger;
import org.sonar.batch.scan.maven.MavenPluginsConfigurator;
import org.sonar.batch.scan.report.JsonReport;

public final class PreviewPhaseExecutor implements PhaseExecutor {

Expand All @@ -50,12 +51,13 @@ public final class PreviewPhaseExecutor implements PhaseExecutor {
private final QProfileVerifier profileVerifier;
private final IssueExclusionsLoader issueExclusionsLoader;
private final AnalysisMode analysisMode;
private final JsonReport jsonReport;

public PreviewPhaseExecutor(Phases phases,
MavenPluginsConfigurator mavenPluginsConfigurator, InitializersExecutor initializersExecutor,
SensorsExecutor sensorsExecutor,
SensorContext sensorContext, DefaultIndex index,
EventBus eventBus, ProjectInitializer pi, FileSystemLogger fsLogger, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier,
EventBus eventBus, ProjectInitializer pi, FileSystemLogger fsLogger, JsonReport jsonReport, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier,
IssueExclusionsLoader issueExclusionsLoader, AnalysisMode analysisMode) {
this.phases = phases;
this.mavenPluginsConfigurator = mavenPluginsConfigurator;
Expand All @@ -66,6 +68,7 @@ public PreviewPhaseExecutor(Phases phases,
this.eventBus = eventBus;
this.pi = pi;
this.fsLogger = fsLogger;
this.jsonReport = jsonReport;
this.fs = fs;
this.profileVerifier = profileVerifier;
this.issueExclusionsLoader = issueExclusionsLoader;
Expand Down Expand Up @@ -98,6 +101,10 @@ public void execute(Project module) {
sensorsExecutor.execute(sensorContext);
}

if (module.isRoot()) {
jsonReport.execute();
}

cleanMemory();
eventBus.fireEvent(new ProjectAnalysisEvent(module, false));
}
Expand Down
Expand Up @@ -71,6 +71,13 @@ public DefaultProjectReferentialsLoader(DatabaseSession session, ServerClient se
this.dao = dao;
}

public DefaultProjectReferentialsLoader(ServerClient serverClient, AnalysisMode analysisMode) {
this.session = null;
this.serverClient = serverClient;
this.analysisMode = analysisMode;
this.dao = null;
}

@Override
public ProjectReferentials load(ProjectReactor reactor, TaskProperties taskProperties) {
String projectKey = reactor.getRoot().getKeyWithBranch();
Expand All @@ -83,29 +90,31 @@ public ProjectReferentials load(ProjectReactor reactor, TaskProperties taskPrope
url += "&preview=" + analysisMode.isPreview();
ProjectReferentials ref = ProjectReferentials.fromJson(serverClient.request(url));

for (ProjectDefinition module : reactor.getProjects()) {

for (Map.Entry<String, String> hashByPaths : hashByRelativePath(module.getKeyWithBranch()).entrySet()) {
String path = hashByPaths.getKey();
String hash = hashByPaths.getValue();
String lastCommits = null;
String revisions = null;
String authors = null;
List<Object[]> measuresByKey = query(projectKey + ":" + path, CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE_KEY, CoreMetrics.SCM_REVISIONS_BY_LINE_KEY,
CoreMetrics.SCM_AUTHORS_BY_LINE_KEY);
for (Object[] measureByKey : measuresByKey) {
if (measureByKey[0].equals(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE_KEY)) {
lastCommits = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE);
} else if (measureByKey[0].equals(CoreMetrics.SCM_REVISIONS_BY_LINE_KEY)) {
revisions = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_REVISIONS_BY_LINE);
} else if (measureByKey[0].equals(CoreMetrics.SCM_AUTHORS_BY_LINE_KEY)) {
authors = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_AUTHORS_BY_LINE);
if (session != null) {
for (ProjectDefinition module : reactor.getProjects()) {

for (Map.Entry<String, String> hashByPaths : hashByRelativePath(module.getKeyWithBranch()).entrySet()) {
String path = hashByPaths.getKey();
String hash = hashByPaths.getValue();
String lastCommits = null;
String revisions = null;
String authors = null;
List<Object[]> measuresByKey = query(projectKey + ":" + path, CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE_KEY, CoreMetrics.SCM_REVISIONS_BY_LINE_KEY,
CoreMetrics.SCM_AUTHORS_BY_LINE_KEY);
for (Object[] measureByKey : measuresByKey) {
if (measureByKey[0].equals(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE_KEY)) {
lastCommits = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE);
} else if (measureByKey[0].equals(CoreMetrics.SCM_REVISIONS_BY_LINE_KEY)) {
revisions = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_REVISIONS_BY_LINE);
} else if (measureByKey[0].equals(CoreMetrics.SCM_AUTHORS_BY_LINE_KEY)) {
authors = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_AUTHORS_BY_LINE);
}
}
ref.addFileData(module.getKeyWithBranch(), path, new FileData(hash, lastCommits, revisions, authors));
}
ref.addFileData(module.getKeyWithBranch(), path, new FileData(hash, lastCommits, revisions, authors));
}
ref.setLastAnalysisDate(lastSnapshotCreationDate(projectKey));
}
ref.setLastAnalysisDate(lastSnapshotCreationDate(projectKey));
return ref;
}

Expand Down
@@ -0,0 +1,65 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.batch.rule;

import org.sonar.api.batch.rule.ActiveRule;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RuleQuery;

import java.util.Collection;

public class RuleFinderCompatibility implements RuleFinder {

private final ActiveRules activeRules;

public RuleFinderCompatibility(ActiveRules activeRules) {
this.activeRules = activeRules;
}

@Override
public Rule findById(int ruleId) {
throw new UnsupportedOperationException("Unable to find rule by id");
}

@Override
public Rule findByKey(String repositoryKey, String key) {
return findByKey(RuleKey.of(repositoryKey, key));
}

@Override
public Rule findByKey(RuleKey key) {
ActiveRule ar = activeRules.find(key);
return ar == null ? null : Rule.create(key.repository(), key.rule());
}

@Override
public Rule find(RuleQuery query) {
throw new UnsupportedOperationException("Unable to find rule by query");
}

@Override
public Collection<Rule> findAll(RuleQuery query) {
throw new UnsupportedOperationException("Unable to find rule by query");
}

}
Expand Up @@ -26,6 +26,7 @@
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.rule.CheckFactory;
import org.sonar.api.checks.NoSonarFilter;
import org.sonar.api.platform.ComponentContainer;
import org.sonar.api.resources.Project;
import org.sonar.api.scan.filesystem.FileExclusions;
Expand Down Expand Up @@ -78,6 +79,7 @@
import org.sonar.batch.rule.QProfileEventsDecorator;
import org.sonar.batch.rule.QProfileSensor;
import org.sonar.batch.rule.QProfileVerifier;
import org.sonar.batch.rule.RuleFinderCompatibility;
import org.sonar.batch.rule.RulesProfileProvider;
import org.sonar.batch.scan.filesystem.ComponentIndexer;
import org.sonar.batch.scan.filesystem.DefaultModuleFileSystem;
Expand Down Expand Up @@ -133,7 +135,8 @@ private void addCoreComponents() {
if (!sensorMode) {
add(DefaultPhaseExecutor.class);
} else {
add(PreviewPhaseExecutor.class);
add(RuleFinderCompatibility.class,
PreviewPhaseExecutor.class);
}

add(
Expand Down Expand Up @@ -201,6 +204,7 @@ private void addCoreComponents() {
IssueExclusionsLoader.class,
EnforceIssuesFilter.class,
IgnoreIssuesFilter.class,
NoSonarFilter.class,

ScanPerspectives.class);
}
Expand Down
Expand Up @@ -147,7 +147,6 @@ private void addBatchComponents() {
ResourceCache.class,
ComponentDataCache.class,
FileHashesPersister.class,
DefaultUserFinder.class,

// file system
InputPathCache.class,
Expand Down Expand Up @@ -176,9 +175,6 @@ private void addBatchComponents() {
HighlightableBuilder.class,
SymbolizableBuilder.class,

// technical debt
DefaultTechnicalDebtModel.class,

// Differential periods
PeriodsDefinition.class,

Expand All @@ -205,10 +201,15 @@ private void addDataBaseComponents() {
SourcePersister.class,
ResourceKeyMigration.class,

DefaultUserFinder.class,

// Rules
new RulesProvider(),
new DebtModelProvider(),

// technical debt
DefaultTechnicalDebtModel.class,

ProjectLock.class);
}

Expand Down
Expand Up @@ -43,6 +43,11 @@ public MeasureCache(Caches caches, MetricFinder metricFinder, TechnicalDebtModel
cache = caches.createCache("measures");
}

public MeasureCache(Caches caches, MetricFinder metricFinder) {
caches.registerValueCoder(Measure.class, new MeasureValueCoder(metricFinder, null));
cache = caches.createCache("measures");
}

public Iterable<Entry<Measure>> entries() {
return cache.entries();
}
Expand Down

0 comments on commit 1ea2fd3

Please sign in to comment.