3 * Copyright (C) 2009-2023 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.server.issue.ws.pull;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
28 import java.util.stream.Collectors;
29 import org.sonar.api.server.ServerSide;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.db.issue.IssueDto;
34 import org.sonar.db.protobuf.DbIssues;
35 import org.sonar.server.user.UserSession;
36 import org.sonar.server.ws.MessageFormattingUtils;
37 import org.sonarqube.ws.Common;
38 import org.sonarqube.ws.Issues;
40 import static org.sonar.db.protobuf.DbIssues.Locations;
41 import static org.sonarqube.ws.Issues.TaintVulnerabilityLite;
42 import static org.sonarqube.ws.Issues.TaintVulnerabilityPullQueryTimestamp;
45 public class PullTaintActionProtobufObjectGenerator implements ProtobufObjectGenerator {
46 private final DbClient dbClient;
47 private final UserSession userSession;
48 private Map<String, ComponentDto> componentsMap;
50 public PullTaintActionProtobufObjectGenerator(DbClient dbClient, UserSession userSession) {
51 this.dbClient = dbClient;
52 this.userSession = userSession;
56 public TaintVulnerabilityPullQueryTimestamp generateTimestampMessage(long timestamp) {
58 TaintVulnerabilityPullQueryTimestamp.Builder responseBuilder = TaintVulnerabilityPullQueryTimestamp.newBuilder();
59 responseBuilder.setQueryTimestamp(timestamp);
60 return responseBuilder.build();
64 public TaintVulnerabilityLite generateIssueMessage(IssueDto issueDto) {
65 TaintVulnerabilityLite.Builder taintBuilder = TaintVulnerabilityLite.newBuilder();
66 Locations locations = issueDto.parseLocations();
68 if (componentsMap == null) {
72 Issues.Location.Builder locationBuilder = Issues.Location.newBuilder();
73 if (issueDto.getMessage() != null) {
74 locationBuilder.setMessage(issueDto.getMessage());
75 locationBuilder.addAllMessageFormattings(MessageFormattingUtils.dbMessageFormattingToWs(issueDto.parseMessageFormattings()));
77 if (issueDto.getFilePath() != null) {
78 locationBuilder.setFilePath(issueDto.getFilePath());
80 if (locations != null) {
81 Issues.TextRange textRange = buildTextRange(locations);
82 locationBuilder.setTextRange(textRange);
83 getFlows(taintBuilder, locations, issueDto);
86 taintBuilder.setAssignedToSubscribedUser(issueDto.getAssigneeUuid() != null &&
87 issueDto.getAssigneeUuid().equals(userSession.getUuid()));
89 taintBuilder.setKey(issueDto.getKey());
90 taintBuilder.setCreationDate(issueDto.getCreatedAt());
91 taintBuilder.setResolved(issueDto.getStatus().equals(org.sonar.api.issue.Issue.STATUS_RESOLVED));
92 taintBuilder.setRuleKey(issueDto.getRuleKey().toString());
93 if (issueDto.getSeverity() != null) {
94 taintBuilder.setSeverity(Common.Severity.valueOf(issueDto.getSeverity()));
96 taintBuilder.setType(Common.RuleType.forNumber(issueDto.getType()));
97 taintBuilder.setClosed(false);
98 taintBuilder.setMainLocation(locationBuilder.build());
99 issueDto.getOptionalRuleDescriptionContextKey().ifPresent(taintBuilder::setRuleDescriptionContextKey);
101 return taintBuilder.build();
105 public TaintVulnerabilityLite generateClosedIssueMessage(String uuid) {
106 TaintVulnerabilityLite.Builder taintBuilder = TaintVulnerabilityLite.newBuilder();
107 taintBuilder.setKey(uuid);
108 taintBuilder.setClosed(true);
109 return taintBuilder.build();
112 private void getFlows(TaintVulnerabilityLite.Builder taintBuilder, Locations locations, IssueDto issueDto) {
113 List<Issues.Flow> flows = new ArrayList<>();
115 for (DbIssues.Flow f : locations.getFlowList()) {
116 Set<String> componentUuids = new HashSet<>();
118 Issues.Flow.Builder builder = Issues.Flow.newBuilder();
119 List<Issues.Location> flowLocations = new ArrayList<>();
120 getComponentUuids(f, componentUuids);
122 for (DbIssues.Location l : f.getLocationList()) {
123 Issues.Location.Builder flowLocationBuilder = Issues.Location
125 .setMessage(l.getMsg())
126 .setTextRange(buildTextRange(l));
127 if (l.hasComponentId() && componentsMap.containsKey(l.getComponentId())) {
128 flowLocationBuilder.setFilePath(componentsMap.get(l.getComponentId()).path());
130 flowLocationBuilder.setFilePath(issueDto.getFilePath());
132 flowLocations.add(flowLocationBuilder.build());
134 builder.addAllLocations(flowLocations);
135 flows.add(builder.build());
137 taintBuilder.addAllFlows(flows);
141 private void getComponentUuids(DbIssues.Flow f, Set<String> componentUuids) {
142 for (DbIssues.Location l : f.getLocationList()) {
143 if (l.hasComponentId() && !componentsMap.containsKey(l.getComponentId())) {
144 componentUuids.add(l.getComponentId());
148 if (!componentUuids.isEmpty()) {
149 componentsMap.putAll(getLocationComponents(componentUuids));
153 private static Issues.TextRange buildTextRange(DbIssues.Location location) {
154 int startLine = location.getTextRange().getStartLine();
155 int endLine = location.getTextRange().getEndLine();
156 int startOffset = location.getTextRange().getStartOffset();
157 int endOffset = location.getTextRange().getEndOffset();
159 return Issues.TextRange.newBuilder()
160 .setHash(location.getChecksum())
161 .setStartLine(startLine)
163 .setStartLineOffset(startOffset)
164 .setEndLineOffset(endOffset).build();
167 private void refreshComponents() {
168 componentsMap = new HashMap<>();
171 private Map<String, ComponentDto> getLocationComponents(Set<String> components) {
172 try (DbSession dbSession = dbClient.openSession(false)) {
173 return dbClient.componentDao().selectByUuids(dbSession, components)
174 .stream().collect(Collectors.toMap(ComponentDto::uuid, c -> c));