From 043c278c2844bd09ce9bd5add89d67eb7311a21f Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 23 Sep 2014 10:51:34 +0200 Subject: SONAR-5644, SONAR-5473 Create new SCM extension point and fetch SCM data using WS --- .../src/main/java/org/sonar/xoo/XooPlugin.java | 6 +- .../java/org/sonar/xoo/lang/ScmActivitySensor.java | 112 --------------------- .../java/org/sonar/xoo/lang/XooScmProvider.java | 102 +++++++++++++++++++ 3 files changed, 106 insertions(+), 114 deletions(-) delete mode 100644 plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/ScmActivitySensor.java create mode 100644 plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooScmProvider.java (limited to 'plugins/sonar-xoo-plugin/src') diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java index c7e43d9e0ab..e1988ac37ab 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java @@ -22,10 +22,10 @@ package org.sonar.xoo; import org.sonar.api.SonarPlugin; import org.sonar.xoo.lang.CoveragePerTestSensor; import org.sonar.xoo.lang.MeasureSensor; -import org.sonar.xoo.lang.ScmActivitySensor; import org.sonar.xoo.lang.SymbolReferencesSensor; import org.sonar.xoo.lang.SyntaxHighlightingSensor; import org.sonar.xoo.lang.TestCaseSensor; +import org.sonar.xoo.lang.XooScmProvider; import org.sonar.xoo.lang.XooTokenizerSensor; import org.sonar.xoo.rule.CreateIssueByInternalKeySensor; import org.sonar.xoo.rule.OneIssueOnDirPerFileSensor; @@ -51,9 +51,11 @@ public class XooPlugin extends SonarPlugin { XooRulesDefinition.class, XooQualityProfile.class, + // SCM + XooScmProvider.class, + // sensors MeasureSensor.class, - ScmActivitySensor.class, SyntaxHighlightingSensor.class, SymbolReferencesSensor.class, XooTokenizerSensor.class, diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/ScmActivitySensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/ScmActivitySensor.java deleted file mode 100644 index 12663b0b697..00000000000 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/ScmActivitySensor.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.xoo.lang; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Charsets; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.batch.fs.FileSystem; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.sensor.Sensor; -import org.sonar.api.batch.sensor.SensorContext; -import org.sonar.api.batch.sensor.SensorDescriptor; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.FileLinesContext; -import org.sonar.api.measures.FileLinesContextFactory; -import org.sonar.api.utils.DateUtils; -import org.sonar.xoo.Xoo; - -import java.io.File; -import java.io.IOException; -import java.util.Date; -import java.util.List; - -public class ScmActivitySensor implements Sensor { - - private static final Logger LOG = LoggerFactory.getLogger(ScmActivitySensor.class); - - private static final String SCM_EXTENSION = ".scm"; - - private final FileSystem fs; - private final FileLinesContextFactory fileLinesContextFactory; - - public ScmActivitySensor(FileLinesContextFactory fileLinesContextFactory, FileSystem fileSystem) { - this.fs = fileSystem; - this.fileLinesContextFactory = fileLinesContextFactory; - } - - @Override - public void describe(SensorDescriptor descriptor) { - descriptor - .name(this.getClass().getSimpleName()) - .provides(CoreMetrics.SCM_AUTHORS_BY_LINE, - CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE, - CoreMetrics.SCM_REVISIONS_BY_LINE) - .workOnLanguages(Xoo.KEY); - } - - @Override - public void execute(SensorContext context) { - for (InputFile inputFile : fs.inputFiles(fs.predicates().hasLanguage(Xoo.KEY))) { - processFile(inputFile); - } - - } - - @VisibleForTesting - protected void processFile(InputFile inputFile) { - File ioFile = inputFile.file(); - File scmDataFile = new java.io.File(ioFile.getParentFile(), ioFile.getName() + SCM_EXTENSION); - if (!scmDataFile.exists()) { - LOG.debug("Skipping SCM data injection for " + inputFile.relativePath()); - return; - } - - FileLinesContext fileLinesContext = fileLinesContextFactory.createFor(inputFile); - try { - List lines = FileUtils.readLines(scmDataFile, Charsets.UTF_8.name()); - int lineNumber = 0; - for (String line : lines) { - lineNumber++; - if (StringUtils.isNotBlank(line)) { - // revision,author,dateTime - String[] fields = StringUtils.split(line, ','); - if (fields.length < 3) { - throw new IllegalStateException("Not enough fields on line " + lineNumber); - } - String revision = fields[0]; - String author = fields[1]; - // Will throw an exception, when date is not in format "yyyy-MM-dd" - Date date = DateUtils.parseDate(fields[2]); - - fileLinesContext.setStringValue(CoreMetrics.SCM_REVISIONS_BY_LINE_KEY, lineNumber, revision); - fileLinesContext.setStringValue(CoreMetrics.SCM_AUTHORS_BY_LINE_KEY, lineNumber, author); - fileLinesContext.setStringValue(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE_KEY, lineNumber, DateUtils.formatDateTime(date)); - } - } - } catch (IOException e) { - throw new IllegalStateException(e); - } - fileLinesContext.save(); - } -} diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooScmProvider.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooScmProvider.java new file mode 100644 index 00000000000..71582efc3ef --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooScmProvider.java @@ -0,0 +1,102 @@ +/* + * 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.xoo.lang; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Charsets; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.scm.BlameLine; +import org.sonar.api.batch.scm.ScmProvider; +import org.sonar.api.config.Settings; +import org.sonar.api.utils.DateUtils; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class XooScmProvider implements ScmProvider { + + private static final Logger LOG = LoggerFactory.getLogger(XooScmProvider.class); + + private static final String SCM_EXTENSION = ".scm"; + + private final Settings settings; + + public XooScmProvider(Settings settings) { + this.settings = settings; + } + + @Override + public String key() { + return "xoo"; + } + + @Override + public boolean supports(File baseDir) { + return false; + } + + @Override + public void blame(Iterable files, BlameResultHandler handler) { + for (InputFile inputFile : files) { + processFile(inputFile, handler); + } + } + + @VisibleForTesting + protected void processFile(InputFile inputFile, BlameResultHandler handler) { + File ioFile = inputFile.file(); + File scmDataFile = new java.io.File(ioFile.getParentFile(), ioFile.getName() + SCM_EXTENSION); + if (!scmDataFile.exists()) { + throw new IllegalStateException("Missing file " + scmDataFile); + } + + try { + List lines = FileUtils.readLines(scmDataFile, Charsets.UTF_8.name()); + List blame = new ArrayList(lines.size()); + int lineNumber = 0; + for (String line : lines) { + lineNumber++; + if (StringUtils.isNotBlank(line)) { + // revision,author,dateTime + String[] fields = StringUtils.split(line, ','); + if (fields.length < 3) { + throw new IllegalStateException("Not enough fields on line " + lineNumber); + } + String revision = fields[0]; + String author = fields[1]; + // Will throw an exception, when date is not in format "yyyy-MM-dd" + Date date = DateUtils.parseDate(fields[2]); + + blame.add(new BlameLine(date, revision, author)); + } + } + handler.handle(inputFile, blame); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } +} -- cgit v1.2.3