3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.issue;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Optional;
28 import javax.annotation.Nullable;
29 import org.jetbrains.annotations.NotNull;
30 import org.slf4j.LoggerFactory;
31 import org.sonar.api.issue.impact.SoftwareQuality;
32 import org.sonar.api.rule.RuleKey;
33 import org.sonar.api.rules.RuleType;
34 import org.sonar.api.server.rule.internal.ImpactMapper;
35 import org.sonar.api.utils.Duration;
36 import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
37 import org.sonar.ce.task.projectanalysis.component.Component;
38 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
39 import org.sonar.ce.task.projectanalysis.issue.filter.IssueFilter;
40 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolder;
41 import org.sonar.ce.task.projectanalysis.source.SourceLinesHashRepository;
42 import org.sonar.core.issue.DefaultIssue;
43 import org.sonar.core.issue.tracking.Input;
44 import org.sonar.core.issue.tracking.LazyInput;
45 import org.sonar.core.issue.tracking.LineHashSequence;
46 import org.sonar.core.util.CloseableIterator;
47 import org.sonar.db.protobuf.DbCommons;
48 import org.sonar.db.protobuf.DbIssues;
49 import org.sonar.scanner.protocol.Constants;
50 import org.sonar.scanner.protocol.Constants.Severity;
51 import org.sonar.scanner.protocol.output.ScannerReport;
52 import org.sonar.scanner.protocol.output.ScannerReport.Impact;
53 import org.sonar.scanner.protocol.output.ScannerReport.IssueType;
54 import org.sonar.server.rule.CommonRuleKeys;
56 import static java.util.stream.Collectors.toMap;
57 import static org.apache.commons.lang3.StringUtils.isNotEmpty;
58 import static org.sonar.api.issue.Issue.STATUS_OPEN;
59 import static org.sonar.api.issue.Issue.STATUS_TO_REVIEW;
61 public class TrackerRawInputFactory {
62 private static final long DEFAULT_EXTERNAL_ISSUE_EFFORT = 0L;
63 private final TreeRootHolder treeRootHolder;
64 private final BatchReportReader reportReader;
65 private final IssueFilter issueFilter;
66 private final SourceLinesHashRepository sourceLinesHash;
67 private final RuleRepository ruleRepository;
68 private final ActiveRulesHolder activeRulesHolder;
70 public TrackerRawInputFactory(TreeRootHolder treeRootHolder, BatchReportReader reportReader, SourceLinesHashRepository sourceLinesHash,
71 IssueFilter issueFilter, RuleRepository ruleRepository, ActiveRulesHolder activeRulesHolder) {
72 this.treeRootHolder = treeRootHolder;
73 this.reportReader = reportReader;
74 this.sourceLinesHash = sourceLinesHash;
75 this.issueFilter = issueFilter;
76 this.ruleRepository = ruleRepository;
77 this.activeRulesHolder = activeRulesHolder;
80 public Input<DefaultIssue> create(Component component) {
81 return new RawLazyInput(component);
84 private class RawLazyInput extends LazyInput<DefaultIssue> {
85 private final Component component;
87 private RawLazyInput(Component component) {
88 this.component = component;
92 protected LineHashSequence loadLineHashSequence() {
93 if (component.getType() == Component.Type.FILE) {
94 return new LineHashSequence(sourceLinesHash.getLineHashesMatchingDBVersion(component));
96 return new LineHashSequence(Collections.emptyList());
101 protected List<DefaultIssue> loadIssues() {
102 List<DefaultIssue> result = new ArrayList<>();
104 if (component.getReportAttributes().getRef() == null) {
108 try (CloseableIterator<ScannerReport.Issue> reportIssues = reportReader.readComponentIssues(component.getReportAttributes().getRef())) {
109 // optimization - do not load line hashes if there are no issues -> getLineHashSequence() is executed
110 // as late as possible
111 while (reportIssues.hasNext()) {
112 ScannerReport.Issue reportIssue = reportIssues.next();
113 if (isOnInactiveRule(reportIssue)) {
116 if (!isIssueOnUnsupportedCommonRule(reportIssue)) {
117 LoggerFactory.getLogger(getClass()).debug("Ignored issue from analysis report on rule {}:{}", reportIssue.getRuleRepository(), reportIssue.getRuleKey());
120 DefaultIssue issue = toIssue(getLineHashSequence(), reportIssue);
121 if (issueFilter.accept(issue, component)) {
127 Map<RuleKey, ScannerReport.AdHocRule> adHocRuleMap = new HashMap<>();
128 try (CloseableIterator<ScannerReport.AdHocRule> reportAdHocRule = reportReader.readAdHocRules()) {
129 while (reportAdHocRule.hasNext()) {
130 ScannerReport.AdHocRule adHocRule = reportAdHocRule.next();
131 adHocRuleMap.put(RuleKey.of(RuleKey.EXTERNAL_RULE_REPO_PREFIX + adHocRule.getEngineId(), adHocRule.getRuleId()), adHocRule);
135 try (CloseableIterator<ScannerReport.ExternalIssue> reportExternalIssues = reportReader.readComponentExternalIssues(component.getReportAttributes().getRef())) {
136 // optimization - do not load line hashes if there are no issues -> getLineHashSequence() is executed
137 // as late as possible
138 while (reportExternalIssues.hasNext()) {
139 ScannerReport.ExternalIssue reportExternalIssue = reportExternalIssues.next();
140 result.add(toExternalIssue(getLineHashSequence(), reportExternalIssue, adHocRuleMap));
147 private boolean isOnInactiveRule(ScannerReport.Issue reportIssue) {
148 RuleKey ruleKey = RuleKey.of(reportIssue.getRuleRepository(), reportIssue.getRuleKey());
149 return !activeRulesHolder.get(ruleKey).isPresent();
152 private boolean isIssueOnUnsupportedCommonRule(ScannerReport.Issue issue) {
153 // issues on batch common rules are ignored. This feature
154 // is natively supported by compute engine since 5.2.
155 return !issue.getRuleRepository().startsWith(CommonRuleKeys.REPOSITORY_PREFIX);
158 private DefaultIssue toIssue(LineHashSequence lineHashSeq, ScannerReport.Issue reportIssue) {
159 DefaultIssue issue = new DefaultIssue();
160 init(issue, STATUS_OPEN);
161 RuleKey ruleKey = RuleKey.of(reportIssue.getRuleRepository(), reportIssue.getRuleKey());
162 issue.setRuleKey(ruleKey);
163 if (reportIssue.hasTextRange()) {
164 int startLine = reportIssue.getTextRange().getStartLine();
165 issue.setLine(startLine);
166 issue.setChecksum(lineHashSeq.getHashForLine(startLine));
168 issue.setChecksum("");
170 if (isNotEmpty(reportIssue.getMsg())) {
171 issue.setMessage(reportIssue.getMsg());
172 if (!reportIssue.getMsgFormattingList().isEmpty()) {
173 issue.setMessageFormattings(convertMessageFormattings(reportIssue.getMsgFormattingList()));
176 Rule rule = ruleRepository.getByKey(ruleKey);
177 issue.setMessage(rule.getName());
179 if (reportIssue.getSeverity() != Severity.UNSET_SEVERITY) {
180 issue.setSeverity(reportIssue.getSeverity().name());
182 if (Double.compare(reportIssue.getGap(), 0D) != 0) {
183 issue.setGap(reportIssue.getGap());
185 DbIssues.Locations.Builder dbLocationsBuilder = DbIssues.Locations.newBuilder();
186 if (reportIssue.hasTextRange()) {
187 dbLocationsBuilder.setTextRange(convertTextRange(reportIssue.getTextRange()));
189 for (ScannerReport.Flow flow : reportIssue.getFlowList()) {
190 if (flow.getLocationCount() > 0) {
191 DbIssues.Flow.Builder dbFlowBuilder = convertLocations(flow);
192 dbLocationsBuilder.addFlow(dbFlowBuilder);
195 issue.setIsFromExternalRuleEngine(false);
196 issue.setLocations(dbLocationsBuilder.build());
197 issue.setQuickFixAvailable(reportIssue.getQuickFixAvailable());
198 issue.setRuleDescriptionContextKey(reportIssue.hasRuleDescriptionContextKey() ? reportIssue.getRuleDescriptionContextKey() : null);
199 issue.setCodeVariants(reportIssue.getCodeVariantsList());
201 issue.replaceImpacts(replaceDefaultWithOverridenImpacts(issue.ruleKey(), reportIssue.getOverridenImpactsList()));
205 private Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> replaceDefaultWithOverridenImpacts(RuleKey ruleKey, List<Impact> overridenImpactsList) {
206 Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> overridenImpactMap = getOverridenImpactMap(overridenImpactsList);
207 return ruleRepository.getByKey(ruleKey).getDefaultImpacts().entrySet()
211 entry -> overridenImpactMap.containsKey(entry.getKey()) ? overridenImpactMap.get(entry.getKey()) : entry.getValue()));
214 private static Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> getOverridenImpactMap(List<Impact> overridenImpactsList) {
215 return overridenImpactsList.stream()
217 impact -> SoftwareQuality.valueOf(impact.getSoftwareQuality()),
218 impact -> org.sonar.api.issue.impact.Severity.valueOf(impact.getSeverity())));
221 private DbIssues.Flow.Builder convertLocations(ScannerReport.Flow flow) {
222 DbIssues.Flow.Builder dbFlowBuilder = DbIssues.Flow.newBuilder();
223 for (ScannerReport.IssueLocation location : flow.getLocationList()) {
224 convertLocation(location).ifPresent(dbFlowBuilder::addLocation);
226 if (isNotEmpty(flow.getDescription())) {
227 dbFlowBuilder.setDescription(flow.getDescription());
229 toFlowType(flow.getType()).ifPresent(dbFlowBuilder::setType);
230 return dbFlowBuilder;
234 private DefaultIssue toExternalIssue(LineHashSequence lineHashSeq, ScannerReport.ExternalIssue reportExternalIssue, Map<RuleKey, ScannerReport.AdHocRule> adHocRuleMap) {
235 DefaultIssue issue = new DefaultIssue();
236 RuleKey ruleKey = RuleKey.of(RuleKey.EXTERNAL_RULE_REPO_PREFIX + reportExternalIssue.getEngineId(), reportExternalIssue.getRuleId());
237 issue.setRuleKey(ruleKey);
238 ruleRepository.addOrUpdateAddHocRuleIfNeeded(ruleKey, () -> toAdHocRule(reportExternalIssue, adHocRuleMap.get(issue.ruleKey())));
240 Rule existingRule = ruleRepository.getByKey(ruleKey);
241 issue.setSeverity(determineDeprecatedSeverity(reportExternalIssue, existingRule));
242 issue.setType(determineDeprecatedType(reportExternalIssue, existingRule));
243 issue.replaceImpacts(replaceDefaultWithOverridenImpacts(issue.ruleKey(), reportExternalIssue.getImpactsList()));
245 init(issue, issue.type() == RuleType.SECURITY_HOTSPOT ? STATUS_TO_REVIEW : STATUS_OPEN);
247 if (reportExternalIssue.hasTextRange()) {
248 int startLine = reportExternalIssue.getTextRange().getStartLine();
249 issue.setLine(startLine);
250 issue.setChecksum(lineHashSeq.getHashForLine(startLine));
252 issue.setChecksum("");
254 if (isNotEmpty(reportExternalIssue.getMsg())) {
255 issue.setMessage(reportExternalIssue.getMsg());
256 if (!reportExternalIssue.getMsgFormattingList().isEmpty()) {
257 issue.setMessageFormattings(convertMessageFormattings(reportExternalIssue.getMsgFormattingList()));
260 issue.setEffort(Duration.create(reportExternalIssue.getEffort() != 0 ? reportExternalIssue.getEffort() : DEFAULT_EXTERNAL_ISSUE_EFFORT));
261 DbIssues.Locations.Builder dbLocationsBuilder = DbIssues.Locations.newBuilder();
262 if (reportExternalIssue.hasTextRange()) {
263 dbLocationsBuilder.setTextRange(convertTextRange(reportExternalIssue.getTextRange()));
265 for (ScannerReport.Flow flow : reportExternalIssue.getFlowList()) {
266 if (flow.getLocationCount() > 0) {
267 DbIssues.Flow.Builder dbFlowBuilder = convertLocations(flow);
268 dbLocationsBuilder.addFlow(dbFlowBuilder);
271 issue.setIsFromExternalRuleEngine(true);
272 issue.setLocations(dbLocationsBuilder.build());
277 private NewAdHocRule toAdHocRule(ScannerReport.ExternalIssue reportIssue, @Nullable ScannerReport.AdHocRule adHocRule) {
278 if (adHocRule != null) {
279 return new NewAdHocRule(adHocRule);
281 return new NewAdHocRule(reportIssue);
284 private Optional<DbIssues.FlowType> toFlowType(ScannerReport.FlowType flowType) {
287 return Optional.of(DbIssues.FlowType.DATA);
289 return Optional.of(DbIssues.FlowType.EXECUTION);
291 return Optional.empty();
293 throw new IllegalArgumentException("Unrecognized type: " + flowType);
297 private RuleType toRuleType(IssueType type) {
302 return RuleType.CODE_SMELL;
304 return RuleType.VULNERABILITY;
305 case SECURITY_HOTSPOT:
306 return RuleType.SECURITY_HOTSPOT;
309 throw new IllegalStateException("Invalid issue type: " + type);
313 private DefaultIssue init(DefaultIssue issue, String initialStatus) {
314 issue.setStatus(initialStatus);
315 issue.setResolution(null);
316 issue.setComponentUuid(component.getUuid());
317 issue.setComponentKey(component.getKey());
318 issue.setProjectUuid(treeRootHolder.getRoot().getUuid());
319 issue.setProjectKey(treeRootHolder.getRoot().getKey());
323 private Optional<DbIssues.Location> convertLocation(ScannerReport.IssueLocation source) {
324 DbIssues.Location.Builder target = DbIssues.Location.newBuilder();
325 if (source.getComponentRef() != 0 && source.getComponentRef() != component.getReportAttributes().getRef()) {
326 // SONAR-10781 Component might not exist because on PR, only changed components are included in the report
327 Optional<Component> optionalComponent = treeRootHolder.getOptionalComponentByRef(source.getComponentRef());
328 if (!optionalComponent.isPresent()) {
329 return Optional.empty();
331 target.setComponentId(optionalComponent.get().getUuid());
333 if (isNotEmpty(source.getMsg())) {
334 target.setMsg(source.getMsg());
335 source.getMsgFormattingList()
336 .forEach(m -> target.addMsgFormatting(convertMessageFormatting(m)));
338 if (source.hasTextRange()) {
339 ScannerReport.TextRange sourceRange = source.getTextRange();
340 DbCommons.TextRange.Builder targetRange = convertTextRange(sourceRange);
341 target.setTextRange(targetRange);
343 return Optional.of(target.build());
346 private DbCommons.TextRange.Builder convertTextRange(ScannerReport.TextRange sourceRange) {
347 DbCommons.TextRange.Builder targetRange = DbCommons.TextRange.newBuilder();
348 targetRange.setStartLine(sourceRange.getStartLine());
349 targetRange.setStartOffset(sourceRange.getStartOffset());
350 targetRange.setEndLine(sourceRange.getEndLine());
351 targetRange.setEndOffset(sourceRange.getEndOffset());
355 private RuleType determineDeprecatedType(ScannerReport.ExternalIssue reportExternalIssue, Rule rule) {
356 if (reportExternalIssue.getType() != ScannerReport.IssueType.UNSET) {
357 return toRuleType(reportExternalIssue.getType());
358 } else if (rule.getType() != null) {
359 return rule.getType();
360 } else if (!rule.getDefaultImpacts().isEmpty()) {
361 SoftwareQuality impactSoftwareQuality = ImpactMapper.getBestImpactForBackmapping(rule.getDefaultImpacts()).getKey();
362 return ImpactMapper.convertToRuleType(impactSoftwareQuality);
364 throw new IllegalArgumentException("Cannot determine the type for issue of rule %s".formatted(reportExternalIssue.getRuleId()));
368 private static String determineDeprecatedSeverity(ScannerReport.ExternalIssue reportExternalIssue, Rule rule) {
369 if (reportExternalIssue.getSeverity() != Constants.Severity.UNSET_SEVERITY) {
370 return reportExternalIssue.getSeverity().name();
371 } else if (rule.getSeverity() != null) {
372 return rule.getSeverity();
373 } else if (!rule.getDefaultImpacts().isEmpty()) {
374 org.sonar.api.issue.impact.Severity impactSeverity = ImpactMapper.getBestImpactForBackmapping(rule.getDefaultImpacts()).getValue();
375 return ImpactMapper.convertToDeprecatedSeverity(impactSeverity);
377 throw new IllegalArgumentException("Cannot determine the severity for issue of rule %s".formatted(reportExternalIssue.getRuleId()));
384 private static DbIssues.MessageFormattings convertMessageFormattings(List<ScannerReport.MessageFormatting> msgFormattings) {
385 DbIssues.MessageFormattings.Builder builder = DbIssues.MessageFormattings.newBuilder();
386 msgFormattings.stream()
387 .forEach(m -> builder.addMessageFormatting(TrackerRawInputFactory.convertMessageFormatting(m)));
388 return builder.build();
392 private static DbIssues.MessageFormatting convertMessageFormatting(ScannerReport.MessageFormatting m) {
393 DbIssues.MessageFormatting.Builder msgFormattingBuilder = DbIssues.MessageFormatting.newBuilder();
394 return msgFormattingBuilder
395 .setStart(m.getStart())
397 .setType(DbIssues.MessageFormattingType.valueOf(m.getType().name())).build();