Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Sonar SCM Activity Plugin
* Copyright (C) 2010 SonarSource
* [email protected]
*
* This program 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.
*
* This program 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 02
*/
package org.sonar.plugins.scmactivity;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.*;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.PersistenceMode;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.plugins.scmactivity.ProjectStatus.FileStatus;
import java.util.Arrays;
import java.util.List;
/**
* @author Evgeny Mandrikov
*/
public final class ScmActivitySensor implements Sensor {
private static final Logger LOG = LoggerFactory.getLogger(ScmActivitySensor.class);
private TimeMachine timeMachine;
private ScmConfiguration conf;
private LocalModificationChecker checkLocalModifications;
private Changelog changelog;
private Blame blameSensor;
public ScmActivitySensor(ScmConfiguration conf, LocalModificationChecker checkLocalModifications, Changelog changelog, Blame blameSensor, TimeMachine timeMachine) {
this.conf = conf;
this.checkLocalModifications = checkLocalModifications;
this.changelog = changelog;
this.blameSensor = blameSensor;
this.timeMachine = timeMachine;
}
@DependedUpon
public final List generatesMetrics() {
return Arrays.asList(
CoreMetrics.SCM_REVISION,
CoreMetrics.SCM_LAST_COMMIT_DATE,
CoreMetrics.SCM_COMMITS,
CoreMetrics.SCM_AUTHORS_BY_LINE,
CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE,
CoreMetrics.SCM_REVISIONS_BY_LINE);
}
public boolean shouldExecuteOnProject(Project project) {
return conf.isEnabled();
}
public void analyse(Project project, SensorContext context) {
checkLocalModifications.check();
ProjectStatus projectStatus = new ProjectStatus(project);
changelog.load(projectStatus, getPreviousRevision(project));
TimeProfiler profiler = new TimeProfiler().start("Retrieve files SCM info");
LOG.debug(String.format("%d files touched in changelog", projectStatus.getFileStatuses().size()));
for (FileStatus fileStatus : projectStatus.getFileStatuses()) {
inspectFile(context, fileStatus);
}
profiler.stop();
inspectProject(context, project, projectStatus);
}
private void inspectFile(SensorContext context, FileStatus fileStatus) {
Resource resource = toResource(context, fileStatus);
if (resource == null) {
LOG.debug("File not found in Sonar index: " + fileStatus.getFile());
return;
}
if (fileStatus.isModified()) {
blameSensor.analyse(fileStatus, resource, context);
} else {
LOG.debug("File not changed since previous analysis: " + fileStatus.getFile());
copyPreviousFileMeasures(resource, context);
}
}
private void copyPreviousFileMeasures(Resource resource, SensorContext context) {
List previousMeasures = getPreviousMeasures(resource, CoreMetrics.SCM_REVISION, CoreMetrics.SCM_LAST_COMMIT_DATE,
CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE, CoreMetrics.SCM_REVISIONS_BY_LINE, CoreMetrics.SCM_AUTHORS_BY_LINE);
for (Measure previousMeasure : previousMeasures) {
if (StringUtils.isNotBlank( previousMeasure.getData())) {
PersistenceMode persistence = previousMeasure.getMetric().isDataType() ? PersistenceMode.DATABASE : PersistenceMode.FULL;
context.saveMeasure(resource, new Measure(previousMeasure.getMetric(), previousMeasure.getData())).setPersistenceMode(persistence);
}
}
}
private Resource toResource(SensorContext context, FileStatus fileStatus) {
Resource resource = null;
if (conf.isJavaProject()) {
if (Java.isJavaFile(fileStatus.getFile())) {
resource = JavaFile.fromRelativePath(fileStatus.getRelativePath(), false);
}
} else {
resource = new org.sonar.api.resources.File(fileStatus.getRelativePath());
}
if (resource != null) {
return context.getResource(resource);
}
return null;
}
private void inspectProject(SensorContext context, Project project, ProjectStatus projectStatus) {
final String revision;
final String date;
if (projectStatus.isModified()) {
revision = projectStatus.getRevision();
date = ScmUtils.formatLastCommitDate(projectStatus.getDate());
} else {
revision = getPreviousMeasure(project, CoreMetrics.SCM_REVISION);
date = getPreviousMeasure(project, CoreMetrics.SCM_LAST_COMMIT_DATE);
}
Double commits = getPreviousMeasureValue(project, CoreMetrics.SCM_COMMITS);
if (commits == null) {
commits = 0d;
}
commits += projectStatus.getChanges();
context.saveMeasure(new Measure(CoreMetrics.SCM_REVISION, revision));
context.saveMeasure(new Measure(CoreMetrics.SCM_LAST_COMMIT_DATE, date));
context.saveMeasure(new Measure(CoreMetrics.SCM_COMMITS, commits));
}
private Double getPreviousMeasureValue(Resource resource, Metric metric) {
TimeMachineQuery query = new TimeMachineQuery(resource)
.setOnlyLastAnalysis(true)
.setMetrics(metric);
List