-
Notifications
You must be signed in to change notification settings - Fork 131
/
DependencyCheckSensor.java
142 lines (126 loc) · 5.63 KB
/
DependencyCheckSensor.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Dependency-Check Plugin for SonarQube
* Copyright (C) 2015-2024 dependency-check
* philipp.dallig@gmail.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.dependencycheck;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.notifications.AnalysisWarnings;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.scanner.sensor.ProjectSensor;
import org.sonar.dependencycheck.base.DependencyCheckMetrics;
import org.sonar.dependencycheck.base.DependencyCheckUtils;
import org.sonar.dependencycheck.parser.JsonReportParserHelper;
import org.sonar.dependencycheck.parser.ReportParserException;
import org.sonar.dependencycheck.parser.element.Analysis;
import org.sonar.dependencycheck.parser.element.AnalysisException;
import org.sonar.dependencycheck.reason.DependencyReasonSearcher;
import org.sonar.dependencycheck.report.HtmlReportFile;
import org.sonar.dependencycheck.report.JsonReportFile;
import edu.umd.cs.findbugs.annotations.Nullable;
public class DependencyCheckSensor implements ProjectSensor {
private static final Logger LOGGER = LoggerFactory.getLogger(DependencyCheckSensor.class);
private static final String SENSOR_NAME = "Dependency-Check";
private final FileSystem fileSystem;
private final PathResolver pathResolver;
private final AnalysisWarnings analysisWarnings;
public DependencyCheckSensor(FileSystem fileSystem, PathResolver pathResolver, @Nullable AnalysisWarnings analysisWarnings) {
this.fileSystem = fileSystem;
this.pathResolver = pathResolver;
this.analysisWarnings = analysisWarnings;
}
private Optional<Analysis> parseAnalysis(SensorContext context) {
LOGGER.info("Using JSON-Reportparser");
try {
JsonReportFile report = JsonReportFile.getJsonReport(context.config(), fileSystem, pathResolver);
return Optional.of(JsonReportParserHelper.parse(report.getInputStream()));
} catch (FileNotFoundException e) {
LOGGER.info("JSON-Analysis skipped/aborted due to missing report file");
LOGGER.debug(e.getMessage(), e);
} catch (ReportParserException e) {
LOGGER.warn("JSON-Analysis aborted");
LOGGER.debug(e.getMessage(), e);
} catch (IOException e) {
LOGGER.warn("JSON-Analysis aborted due to: IO Errors", e);
}
return Optional.empty();
}
private void uploadHTMLReport(SensorContext context) {
try {
HtmlReportFile htmlReportFile = HtmlReportFile.getHtmlReport(context.config(), fileSystem, pathResolver);
String htmlReport = htmlReportFile.getReportContent();
if (htmlReport != null) {
LOGGER.info("Upload Dependency-Check HTML-Report");
context.<String>newMeasure().forMetric(DependencyCheckMetrics.REPORT).on(context.project())
.withValue(htmlReport).save();
}
} catch (FileNotFoundException e) {
LOGGER.info(e.getMessage());
LOGGER.debug(e.getMessage(), e);
}
}
private void addWarnings(Analysis analysis) {
List<AnalysisException> exceptions = analysis.getScanInfo().getExceptions();
for (AnalysisException exception : exceptions) {
addWarnings(exception);
}
}
private void addWarnings(@Nullable Throwable cause) {
if (cause != null) {
String message = cause.getMessage();
if (StringUtils.isNotBlank(message)) {
analysisWarnings.addUnique(SENSOR_NAME + " - " + message);
}
addWarnings(cause.getCause());
}
}
@Override
public String toString() {
return SENSOR_NAME;
}
@Override
public void describe(SensorDescriptor sensorDescriptor) {
sensorDescriptor.name(SENSOR_NAME);
}
@Override
public void execute(SensorContext sensorContext) {
if (DependencyCheckUtils.skipPlugin(sensorContext.config())) {
LOGGER.info("Dependency-Check skipped");
} else {
LOGGER.info("Dependency-Check - Start");
Optional<Analysis> analysis = parseAnalysis(sensorContext);
if (analysis.isPresent()) {
DependencyReasonSearcher dependencyReasonSearcher = new DependencyReasonSearcher(sensorContext);
dependencyReasonSearcher.addDependenciesToInputComponents(analysis.get(), sensorContext);
if (analysisWarnings != null ) {
addWarnings(analysis.get());
}
}
uploadHTMLReport(sensorContext);
LOGGER.info("Dependency-Check - End");
}
}
}