]> source.dussan.org Git - sonarqube.git/blob
a31082b669336516e69ed8283449cdb9d5851121
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.rules.CleanCodeAttribute;
30 import org.sonar.api.server.ServerSide;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.component.ComponentDto;
34 import org.sonar.db.issue.IssueDto;
35 import org.sonar.db.protobuf.DbIssues;
36 import org.sonar.db.rule.RuleDto;
37 import org.sonar.server.issue.ImpactFormatter;
38 import org.sonar.server.user.UserSession;
39 import org.sonar.server.ws.MessageFormattingUtils;
40 import org.sonarqube.ws.Common;
41 import org.sonarqube.ws.Issues;
42
43 import static org.sonar.db.protobuf.DbIssues.Locations;
44 import static org.sonarqube.ws.Issues.TaintVulnerabilityLite;
45 import static org.sonarqube.ws.Issues.TaintVulnerabilityPullQueryTimestamp;
46
47 @ServerSide
48 public class PullTaintActionProtobufObjectGenerator implements ProtobufObjectGenerator {
49   private final DbClient dbClient;
50   private final UserSession userSession;
51   private Map<String, ComponentDto> componentsMap;
52
53   public PullTaintActionProtobufObjectGenerator(DbClient dbClient, UserSession userSession) {
54     this.dbClient = dbClient;
55     this.userSession = userSession;
56   }
57
58   @Override
59   public TaintVulnerabilityPullQueryTimestamp generateTimestampMessage(long timestamp) {
60     refreshComponents();
61     TaintVulnerabilityPullQueryTimestamp.Builder responseBuilder = TaintVulnerabilityPullQueryTimestamp.newBuilder();
62     responseBuilder.setQueryTimestamp(timestamp);
63     return responseBuilder.build();
64   }
65
66   @Override
67   public TaintVulnerabilityLite generateIssueMessage(IssueDto issueDto, RuleDto ruleDto) {
68     TaintVulnerabilityLite.Builder taintBuilder = TaintVulnerabilityLite.newBuilder();
69     Locations locations = issueDto.parseLocations();
70
71     if (componentsMap == null) {
72       refreshComponents();
73     }
74
75     Issues.Location.Builder locationBuilder = Issues.Location.newBuilder();
76     if (issueDto.getMessage() != null) {
77       locationBuilder.setMessage(issueDto.getMessage());
78       locationBuilder.addAllMessageFormattings(MessageFormattingUtils.dbMessageFormattingToWs(issueDto.parseMessageFormattings()));
79     }
80     if (issueDto.getFilePath() != null) {
81       locationBuilder.setFilePath(issueDto.getFilePath());
82     }
83     if (locations != null) {
84       Issues.TextRange textRange = buildTextRange(locations);
85       locationBuilder.setTextRange(textRange);
86       getFlows(taintBuilder, locations, issueDto);
87     }
88
89     taintBuilder.setAssignedToSubscribedUser(issueDto.getAssigneeUuid() != null &&
90       issueDto.getAssigneeUuid().equals(userSession.getUuid()));
91
92     taintBuilder.setKey(issueDto.getKey());
93     if (issueDto.getIssueCreationTime() != null) {
94       taintBuilder.setCreationDate(issueDto.getIssueCreationTime());
95     }
96     taintBuilder.setResolved(issueDto.getStatus().equals(org.sonar.api.issue.Issue.STATUS_RESOLVED));
97     taintBuilder.setRuleKey(issueDto.getRuleKey().toString());
98     if (issueDto.getSeverity() != null) {
99       taintBuilder.setSeverity(Common.Severity.valueOf(issueDto.getSeverity()));
100     }
101     taintBuilder.setType(Common.RuleType.forNumber(issueDto.getType()));
102     CleanCodeAttribute cleanCodeAttribute = issueDto.getEffectiveCleanCodeAttribute();
103     String cleanCodeAttributeString = cleanCodeAttribute != null ? cleanCodeAttribute.name() : null;
104     String cleanCodeAttributeCategoryString = cleanCodeAttribute != null ? cleanCodeAttribute.getAttributeCategory().name() : null;
105     if (cleanCodeAttributeString != null) {
106       taintBuilder.setCleanCodeAttribute(Common.CleanCodeAttribute.valueOf(cleanCodeAttributeString));
107       taintBuilder.setCleanCodeAttributeCategory(Common.CleanCodeAttributeCategory.valueOf(cleanCodeAttributeCategoryString));
108     }
109     taintBuilder.addAllImpacts(issueDto.getEffectiveImpacts().entrySet()
110       .stream().map(entry -> Common.Impact.newBuilder()
111         .setSoftwareQuality(Common.SoftwareQuality.valueOf(entry.getKey().name()))
112         .setSeverity(ImpactFormatter.mapImpactSeverity(entry.getValue()))
113         .build())
114       .toList());
115
116     taintBuilder.setClosed(false);
117     taintBuilder.setMainLocation(locationBuilder.build());
118     issueDto.getOptionalRuleDescriptionContextKey().ifPresent(taintBuilder::setRuleDescriptionContextKey);
119
120     return taintBuilder.build();
121   }
122
123   @Override
124   public TaintVulnerabilityLite generateClosedIssueMessage(String uuid) {
125     TaintVulnerabilityLite.Builder taintBuilder = TaintVulnerabilityLite.newBuilder();
126     taintBuilder.setKey(uuid);
127     taintBuilder.setClosed(true);
128     return taintBuilder.build();
129   }
130
131   private void getFlows(TaintVulnerabilityLite.Builder taintBuilder, Locations locations, IssueDto issueDto) {
132     List<Issues.Flow> flows = new ArrayList<>();
133
134     for (DbIssues.Flow f : locations.getFlowList()) {
135       Set<String> componentUuids = new HashSet<>();
136
137       Issues.Flow.Builder builder = Issues.Flow.newBuilder();
138       List<Issues.Location> flowLocations = new ArrayList<>();
139       getComponentUuids(f, componentUuids);
140
141       for (DbIssues.Location l : f.getLocationList()) {
142         Issues.Location.Builder flowLocationBuilder = Issues.Location
143           .newBuilder()
144           .setMessage(l.getMsg())
145           .setTextRange(buildTextRange(l));
146         if (l.hasComponentId() && componentsMap.containsKey(l.getComponentId())) {
147           flowLocationBuilder.setFilePath(componentsMap.get(l.getComponentId()).path());
148         } else {
149           flowLocationBuilder.setFilePath(issueDto.getFilePath());
150         }
151         flowLocations.add(flowLocationBuilder.build());
152       }
153       builder.addAllLocations(flowLocations);
154       flows.add(builder.build());
155
156       taintBuilder.addAllFlows(flows);
157     }
158   }
159
160   private void getComponentUuids(DbIssues.Flow f, Set<String> componentUuids) {
161     for (DbIssues.Location l : f.getLocationList()) {
162       if (l.hasComponentId() && !componentsMap.containsKey(l.getComponentId())) {
163         componentUuids.add(l.getComponentId());
164       }
165     }
166
167     if (!componentUuids.isEmpty()) {
168       componentsMap.putAll(getLocationComponents(componentUuids));
169     }
170   }
171
172   private static Issues.TextRange buildTextRange(DbIssues.Location location) {
173     int startLine = location.getTextRange().getStartLine();
174     int endLine = location.getTextRange().getEndLine();
175     int startOffset = location.getTextRange().getStartOffset();
176     int endOffset = location.getTextRange().getEndOffset();
177
178     return Issues.TextRange.newBuilder()
179       .setHash(location.getChecksum())
180       .setStartLine(startLine)
181       .setEndLine(endLine)
182       .setStartLineOffset(startOffset)
183       .setEndLineOffset(endOffset).build();
184   }
185
186   private void refreshComponents() {
187     componentsMap = new HashMap<>();
188   }
189
190   private Map<String, ComponentDto> getLocationComponents(Set<String> components) {
191     try (DbSession dbSession = dbClient.openSession(false)) {
192       return dbClient.componentDao().selectByUuids(dbSession, components)
193         .stream().collect(Collectors.toMap(ComponentDto::uuid, c -> c));
194     }
195   }
196 }