diff options
| author | Janos Gyerik | 2018-09-24 16:48:41 +0200 |
|---|---|---|
| committer | Janos Gyerik | 2018-10-24 10:31:30 +0200 |
| commit | b889b9dd017a923f1504c9bd59e55662d1618ec6 (patch) | |
| tree | ade9171302303972bcf483f9e13e7e73cb8730db /sonar-css-plugin/src/main/java | |
| parent | 8065b6f95ba00e7828371a3232afb9fb32cb0edc (diff) | |
| download | sonar-css-b889b9dd017a923f1504c9bd59e55662d1618ec6.tar.bz2 | |
Register warning when CSS files were not analyzed because of Node.js configuration issues
Diffstat (limited to 'sonar-css-plugin/src/main/java')
3 files changed, 85 insertions, 5 deletions
diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/AnalysisWarningsWrapper.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/AnalysisWarningsWrapper.java new file mode 100644 index 0000000..9abc96e --- /dev/null +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/AnalysisWarningsWrapper.java @@ -0,0 +1,43 @@ +/* + * SonarCSS + * Copyright (C) 2018-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * 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 02110-1301, USA. + */ +package org.sonar.css.plugin; + +import org.sonar.api.batch.InstantiationStrategy; +import org.sonar.api.batch.ScannerSide; +import org.sonar.api.notifications.AnalysisWarnings; + +/** + * Wrap an AnalysisWarnings instance, available since SQ API 7.4. + * Do not load this class on older runtimes. + * Drop this class when the minimum supported version of SonarQube API reaches 7.4. + */ +@ScannerSide +@InstantiationStrategy("PER_BATCH") +public class AnalysisWarningsWrapper { + private final AnalysisWarnings analysisWarnings; + + public AnalysisWarningsWrapper(AnalysisWarnings analysisWarnings) { + this.analysisWarnings = analysisWarnings; + } + + public void addUnique(String text) { + this.analysisWarnings.addUnique(text); + } +} diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssPlugin.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssPlugin.java index 323f628..93cba33 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssPlugin.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssPlugin.java @@ -20,6 +20,8 @@ package org.sonar.css.plugin; import org.sonar.api.Plugin; +import org.sonar.api.SonarProduct; +import org.sonar.api.SonarRuntime; import org.sonar.api.config.PropertyDefinition; import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.Version; @@ -27,6 +29,8 @@ import org.sonar.css.plugin.bundle.CssBundleHandler; public class CssPlugin implements Plugin { + private static final Version ANALYSIS_WARNINGS_MIN_SUPPORTED_SQ_VERSION = Version.create(7, 4); + static final String FILE_SUFFIXES_KEY = "sonar.css.file.suffixes"; public static final String FILE_SUFFIXES_DEFVALUE = ".css,.less,.scss"; @@ -86,5 +90,17 @@ public class CssPlugin implements Plugin { .multiValues(true) .build()); } + + if (isAnalysisWarningsSupported(context.getRuntime())) { + context.addExtension(AnalysisWarningsWrapper.class); + } + } + + /** + * Drop this and related when the minimum supported version of SonarQube API reaches 7.4. + */ + private static boolean isAnalysisWarningsSupported(SonarRuntime runtime) { + return runtime.getApiVersion().isGreaterThanOrEqual(ANALYSIS_WARNINGS_MIN_SUPPORTED_SQ_VERSION) + && runtime.getProduct() != SonarProduct.SONARLINT; } } diff --git a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRuleSensor.java b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRuleSensor.java index 0e4f5fc..fa078fb 100644 --- a/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRuleSensor.java +++ b/sonar-css-plugin/src/main/java/org/sonar/css/plugin/CssRuleSensor.java @@ -30,6 +30,7 @@ import java.nio.file.Paths; import java.util.Collections; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.annotation.Nullable; import org.apache.commons.io.IOUtils; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; @@ -51,18 +52,29 @@ public class CssRuleSensor implements Sensor { private static final Logger LOG = Loggers.get(CssRuleSensor.class); private static final int MIN_NODE_VERSION = 6; + private static final String WARNING_PREFIX = "CSS files were not analyzed. "; private final BundleHandler bundleHandler; private final CssRules cssRules; private final LinterCommandProvider linterCommandProvider; + @Nullable + private final AnalysisWarningsWrapper analysisWarnings; private final ExternalProcessStreamConsumer externalProcessStreamConsumer = new ExternalProcessStreamConsumer(); public CssRuleSensor(BundleHandler bundleHandler, CheckFactory checkFactory, - LinterCommandProvider linterCommandProvider) { + LinterCommandProvider linterCommandProvider, + @Nullable AnalysisWarningsWrapper analysisWarnings) { this.bundleHandler = bundleHandler; this.linterCommandProvider = linterCommandProvider; this.cssRules = new CssRules(checkFactory); + this.analysisWarnings = analysisWarnings; + } + + public CssRuleSensor(BundleHandler bundleHandler, + CheckFactory checkFactory, + LinterCommandProvider linterCommandProvider) { + this(bundleHandler, checkFactory, linterCommandProvider, null); } @Override @@ -127,6 +139,9 @@ public class CssRuleSensor implements Sensor { version = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8).trim(); } catch (Exception e) { LOG.error("Failed to get Node.js version. " + messageSuffix, e); + if (analysisWarnings != null) { + analysisWarnings.addUnique(WARNING_PREFIX + "Node.js version could not be detected using command: " + nodeExecutable + " -v"); + } return false; } @@ -135,13 +150,19 @@ public class CssRuleSensor implements Sensor { if (versionMatcher.matches()) { int major = Integer.parseInt(versionMatcher.group(1)); if (major < MIN_NODE_VERSION) { - String message = String.format("Only Node.js v%s or later is supported, got %s. %s", MIN_NODE_VERSION, version, messageSuffix); - LOG.error(message); + String message = String.format("Only Node.js v%s or later is supported, got %s.", MIN_NODE_VERSION, version); + LOG.error(message + ' ' + messageSuffix); + if (analysisWarnings != null) { + analysisWarnings.addUnique(WARNING_PREFIX + message); + } return false; } } else { - String message = String.format("Failed to parse Node.js version, got '%s'. %s", version, messageSuffix); - LOG.error(message); + String message = String.format("Failed to parse Node.js version, got '%s'.", version); + LOG.error(message + ' ' + messageSuffix); + if (analysisWarnings != null) { + analysisWarnings.addUnique(WARNING_PREFIX + message); + } return false; } |
