3 * Copyright (C) 2009-2022 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.pushevent;
22 import com.google.gson.Gson;
23 import com.google.gson.GsonBuilder;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Objects;
27 import java.util.Optional;
28 import org.jetbrains.annotations.NotNull;
29 import org.sonar.api.ce.ComputeEngineSide;
30 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
31 import org.sonar.ce.task.projectanalysis.component.Component;
32 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
33 import org.sonar.core.issue.DefaultIssue;
34 import org.sonar.db.protobuf.DbCommons;
35 import org.sonar.db.protobuf.DbIssues;
36 import org.sonar.db.pushevent.PushEventDto;
37 import org.sonar.server.issue.TaintChecker;
39 import static java.nio.charset.StandardCharsets.UTF_8;
40 import static java.util.Objects.requireNonNull;
41 import static java.util.Objects.requireNonNullElse;
44 public class PushEventFactory {
45 private static final Gson GSON = new GsonBuilder().create();
47 private final TreeRootHolder treeRootHolder;
48 private final AnalysisMetadataHolder analysisMetadataHolder;
49 private final TaintChecker taintChecker;
51 public PushEventFactory(TreeRootHolder treeRootHolder,
52 AnalysisMetadataHolder analysisMetadataHolder, TaintChecker taintChecker) {
53 this.treeRootHolder = treeRootHolder;
54 this.analysisMetadataHolder = analysisMetadataHolder;
55 this.taintChecker = taintChecker;
58 public Optional<PushEventDto> raiseEventOnIssue(DefaultIssue currentIssue) {
59 var currentIssueComponentUuid = currentIssue.componentUuid();
60 if (!taintChecker.isTaintVulnerability(currentIssue) || currentIssueComponentUuid == null) {
61 return Optional.empty();
64 var component = treeRootHolder.getComponentByUuid(Objects.requireNonNull(currentIssue.componentUuid()));
65 if (currentIssue.isNew() || currentIssue.isCopied() || isReopened(currentIssue)) {
66 return Optional.of(raiseTaintVulnerabilityRaisedEvent(component, currentIssue));
68 if (currentIssue.isBeingClosed()) {
69 return Optional.of(raiseTaintVulnerabilityClosedEvent(currentIssue));
71 return Optional.empty();
74 private static boolean isReopened(DefaultIssue currentIssue) {
75 var currentChange = currentIssue.currentChange();
76 if (currentChange == null) {
79 var status = currentChange.get("status");
80 return status != null && status.toString().equals("CLOSED|OPEN");
83 private PushEventDto raiseTaintVulnerabilityRaisedEvent(Component component, DefaultIssue issue) {
84 TaintVulnerabilityRaised event = prepareEvent(component, issue);
85 return new PushEventDto()
86 .setName("TaintVulnerabilityRaised")
87 .setProjectUuid(treeRootHolder.getRoot().getUuid())
88 .setPayload(serializeEvent(event));
91 private TaintVulnerabilityRaised prepareEvent(Component component, DefaultIssue issue) {
92 TaintVulnerabilityRaised event = new TaintVulnerabilityRaised();
93 event.setProjectKey(issue.projectKey());
94 event.setCreationDate(issue.creationDate().getTime());
95 event.setKey(issue.key());
96 event.setSeverity(issue.severity());
97 event.setRuleKey(issue.getRuleKey().toString());
98 event.setType(issue.type().name());
100 event.setBranch(analysisMetadataHolder.getBranch().getName());
102 DbIssues.Locations issueLocations = requireNonNull(issue.getLocations());
104 Location mainLocation = new Location();
105 mainLocation.setMessage(issue.getMessage());
107 mainLocation.setFilePath(component.getName());
109 TextRange mainLocationTextRange = getTextRange(issueLocations.getTextRange(), issueLocations.getChecksum());
110 mainLocation.setTextRange(mainLocationTextRange);
111 event.setMainLocation(mainLocation);
113 List<Flow> flows = new LinkedList<>();
114 for (DbIssues.Flow sourceFlow : issueLocations.getFlowList()) {
115 Flow flow = new Flow();
116 List<Location> locations = new LinkedList<>();
117 for (DbIssues.Location sourceLocation : sourceFlow.getLocationList()) {
118 Location location = new Location();
119 var locationComponent = treeRootHolder.getComponentByUuid(sourceLocation.getComponentId());
120 location.setFilePath(requireNonNullElse(locationComponent, component).getName());
121 location.setMessage(sourceLocation.getMsg());
123 TextRange textRange = getTextRange(sourceLocation.getTextRange(), sourceLocation.getChecksum());
124 location.setTextRange(textRange);
126 locations.add(location);
128 flow.setLocations(locations);
131 event.setFlows(flows);
135 private PushEventDto raiseTaintVulnerabilityClosedEvent(DefaultIssue issue) {
136 TaintVulnerabilityClosed event = new TaintVulnerabilityClosed(issue.key(), issue.projectKey());
137 return new PushEventDto()
138 .setName("TaintVulnerabilityClosed")
139 .setProjectUuid(treeRootHolder.getRoot().getUuid())
140 .setPayload(serializeEvent(event));
143 private static byte[] serializeEvent(Object event) {
144 return GSON.toJson(event).getBytes(UTF_8);
148 private static TextRange getTextRange(DbCommons.TextRange source, String checksum) {
149 TextRange textRange = new TextRange();
150 textRange.setStartLine(source.getStartLine());
151 textRange.setStartLineOffset(source.getStartOffset());
152 textRange.setEndLine(source.getEndLine());
153 textRange.setEndLineOffset(source.getEndOffset());
154 textRange.setHash(checksum);