]> source.dussan.org Git - sonarqube.git/blob
beb7d2cf21b8874de2f872b9cb9b199ee7f5641a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.issue.ws.pull;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
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;
39
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;
43
44 @ServerSide
45 public class PullTaintActionProtobufObjectGenerator implements ProtobufObjectGenerator {
46   private final DbClient dbClient;
47   private final UserSession userSession;
48   private Map<String, ComponentDto> componentsMap;
49
50   public PullTaintActionProtobufObjectGenerator(DbClient dbClient, UserSession userSession) {
51     this.dbClient = dbClient;
52     this.userSession = userSession;
53   }
54
55   @Override
56   public TaintVulnerabilityPullQueryTimestamp generateTimestampMessage(long timestamp) {
57     refreshComponents();
58     TaintVulnerabilityPullQueryTimestamp.Builder responseBuilder = TaintVulnerabilityPullQueryTimestamp.newBuilder();
59     responseBuilder.setQueryTimestamp(timestamp);
60     return responseBuilder.build();
61   }
62
63   @Override
64   public TaintVulnerabilityLite generateIssueMessage(IssueDto issueDto) {
65     TaintVulnerabilityLite.Builder taintBuilder = TaintVulnerabilityLite.newBuilder();
66     Locations locations = issueDto.parseLocations();
67
68     if (componentsMap == null) {
69       refreshComponents();
70     }
71
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()));
76     }
77     if (issueDto.getFilePath() != null) {
78       locationBuilder.setFilePath(issueDto.getFilePath());
79     }
80     if (locations != null) {
81       Issues.TextRange textRange = buildTextRange(locations);
82       locationBuilder.setTextRange(textRange);
83       getFlows(taintBuilder, locations, issueDto);
84     }
85
86     taintBuilder.setAssignedToSubscribedUser(issueDto.getAssigneeUuid() != null &&
87       issueDto.getAssigneeUuid().equals(userSession.getUuid()));
88
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()));
95     }
96     taintBuilder.setType(Common.RuleType.forNumber(issueDto.getType()));
97     taintBuilder.setClosed(false);
98     taintBuilder.setMainLocation(locationBuilder.build());
99     issueDto.getOptionalRuleDescriptionContextKey().ifPresent(taintBuilder::setRuleDescriptionContextKey);
100
101     return taintBuilder.build();
102   }
103
104   @Override
105   public TaintVulnerabilityLite generateClosedIssueMessage(String uuid) {
106     TaintVulnerabilityLite.Builder taintBuilder = TaintVulnerabilityLite.newBuilder();
107     taintBuilder.setKey(uuid);
108     taintBuilder.setClosed(true);
109     return taintBuilder.build();
110   }
111
112   private void getFlows(TaintVulnerabilityLite.Builder taintBuilder, Locations locations, IssueDto issueDto) {
113     List<Issues.Flow> flows = new ArrayList<>();
114
115     for (DbIssues.Flow f : locations.getFlowList()) {
116       Set<String> componentUuids = new HashSet<>();
117
118       Issues.Flow.Builder builder = Issues.Flow.newBuilder();
119       List<Issues.Location> flowLocations = new ArrayList<>();
120       getComponentUuids(f, componentUuids);
121
122       for (DbIssues.Location l : f.getLocationList()) {
123         Issues.Location.Builder flowLocationBuilder = Issues.Location
124           .newBuilder()
125           .setMessage(l.getMsg())
126           .setTextRange(buildTextRange(l));
127         if (l.hasComponentId() && componentsMap.containsKey(l.getComponentId())) {
128           flowLocationBuilder.setFilePath(componentsMap.get(l.getComponentId()).path());
129         } else {
130           flowLocationBuilder.setFilePath(issueDto.getFilePath());
131         }
132         flowLocations.add(flowLocationBuilder.build());
133       }
134       builder.addAllLocations(flowLocations);
135       flows.add(builder.build());
136
137       taintBuilder.addAllFlows(flows);
138     }
139   }
140
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());
145       }
146     }
147
148     if (!componentUuids.isEmpty()) {
149       componentsMap.putAll(getLocationComponents(componentUuids));
150     }
151   }
152
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();
158
159     return Issues.TextRange.newBuilder()
160       .setHash(location.getChecksum())
161       .setStartLine(startLine)
162       .setEndLine(endLine)
163       .setStartLineOffset(startOffset)
164       .setEndLineOffset(endOffset).build();
165   }
166
167   private void refreshComponents() {
168     componentsMap = new HashMap<>();
169   }
170
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));
175     }
176   }
177 }