-
Notifications
You must be signed in to change notification settings - Fork 176
/
AbstractBridgeSensor.java
124 lines (112 loc) · 4.76 KB
/
AbstractBridgeSensor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2024 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.plugins.javascript.bridge;
import static org.sonar.plugins.javascript.nodejs.NodeCommandBuilderImpl.NODE_EXECUTABLE_PROPERTY;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
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.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.plugins.javascript.CancellationException;
import org.sonar.plugins.javascript.JavaScriptPlugin;
import org.sonar.plugins.javascript.bridge.cache.CacheStrategies;
import org.sonar.plugins.javascript.nodejs.NodeCommandException;
import org.sonar.plugins.javascript.utils.Exclusions;
public abstract class AbstractBridgeSensor implements Sensor {
private static final Logger LOG = Loggers.get(AbstractBridgeSensor.class);
protected final String lang;
protected final BridgeServer bridgeServer;
protected List<String> exclusions;
private final AnalysisWarningsWrapper analysisWarnings;
List<String> environments;
List<String> globals;
protected SensorContext context;
protected ContextUtils contextUtils;
protected AbstractBridgeSensor(
BridgeServer bridgeServer,
AnalysisWarningsWrapper analysisWarnings,
String lang
) {
this.bridgeServer = bridgeServer;
this.analysisWarnings = analysisWarnings;
this.lang = lang;
}
@Override
public void execute(SensorContext context) {
CacheStrategies.reset();
this.context = context;
this.exclusions = Arrays.asList(Exclusions.getExcludedPaths(context.config()));
this.contextUtils = new ContextUtils(context);
environments = Arrays.asList(context.config().getStringArray(JavaScriptPlugin.ENVIRONMENTS));
globals = Arrays.asList(context.config().getStringArray(JavaScriptPlugin.GLOBALS));
try {
List<InputFile> inputFiles = getInputFiles();
if (inputFiles.isEmpty()) {
LOG.info("No input files found for analysis");
return;
}
bridgeServer.startServerLazily(context);
analyzeFiles(inputFiles);
} catch (CancellationException e) {
// do not propagate the exception
LOG.info(e.toString());
} catch (ServerAlreadyFailedException e) {
LOG.debug(
"Skipping the start of the bridge server " +
"as it failed to start during the first analysis or it's not answering anymore"
);
LOG.debug("No rules will be executed");
} catch (NodeCommandException e) {
logErrorOrWarn(e.getMessage(), e);
throw new IllegalStateException(
"Error while running Node.js. A supported version of Node.js is required for running the analysis of " +
this.lang +
" files. Please make sure a supported version of Node.js is available in the PATH or an executable path is provided via '" +
NODE_EXECUTABLE_PROPERTY +
"' property. Alternatively, you can exclude " +
this.lang +
" files from your analysis using the 'sonar.exclusions' configuration property. " +
"See the docs for configuring the analysis environment: https://docs.sonarsource.com/sonarqube/latest/analyzing-source-code/languages/javascript-typescript-css/",
e
);
} catch (Exception e) {
LOG.error("Failure during analysis", e);
throw new IllegalStateException("Analysis of " + this.lang + " files failed", e);
} finally {
CacheStrategies.logReport();
}
}
protected void logErrorOrWarn(String msg, Throwable e) {
LOG.error(msg, e);
}
protected abstract void analyzeFiles(List<InputFile> inputFiles) throws IOException;
protected abstract List<InputFile> getInputFiles();
protected boolean shouldAnalyzeWithProgram(List<InputFile> inputFiles) {
if (contextUtils.isSonarLint()) {
LOG.debug("Will use AnalysisWithWatchProgram because we are in SonarLint context");
return false;
}
LOG.debug("Will use AnalysisWithProgram");
return true;
}
}