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.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.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;
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;
48 public class PullTaintActionProtobufObjectGenerator implements ProtobufObjectGenerator {
49 private final DbClient dbClient;
50 private final UserSession userSession;
51 private Map<String, ComponentDto> componentsMap;
53 public PullTaintActionProtobufObjectGenerator(DbClient dbClient, UserSession userSession) {
54 this.dbClient = dbClient;
55 this.userSession = userSession;
59 public TaintVulnerabilityPullQueryTimestamp generateTimestampMessage(long timestamp) {
61 TaintVulnerabilityPullQueryTimestamp.Builder responseBuilder = TaintVulnerabilityPullQueryTimestamp.newBuilder();
62 responseBuilder.setQueryTimestamp(timestamp);
63 return responseBuilder.build();
67 public TaintVulnerabilityLite generateIssueMessage(IssueDto issueDto, RuleDto ruleDto) {
68 TaintVulnerabilityLite.Builder taintBuilder = TaintVulnerabilityLite.newBuilder();
69 Locations locations = issueDto.parseLocations();
71 if (componentsMap == null) {
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()));
80 if (issueDto.getFilePath() != null) {
81 locationBuilder.setFilePath(issueDto.getFilePath());
83 if (locations != null) {
84 Issues.TextRange textRange = buildTextRange(locations);
85 locationBuilder.setTextRange(textRange);
86 getFlows(taintBuilder, locations, issueDto);
89 taintBuilder.setAssignedToSubscribedUser(issueDto.getAssigneeUuid() != null &&
90 issueDto.getAssigneeUuid().equals(userSession.getUuid()));
92 taintBuilder.setKey(issueDto.getKey());
93 if (issueDto.getIssueCreationTime() != null) {
94 taintBuilder.setCreationDate(issueDto.getIssueCreationTime());
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()));
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));
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()))
116 taintBuilder.setClosed(false);
117 taintBuilder.setMainLocation(locationBuilder.build());
118 issueDto.getOptionalRuleDescriptionContextKey().ifPresent(taintBuilder::setRuleDescriptionContextKey);
120 return taintBuilder.build();
124 public TaintVulnerabilityLite generateClosedIssueMessage(String uuid) {
125 TaintVulnerabilityLite.Builder taintBuilder = TaintVulnerabilityLite.newBuilder();
126 taintBuilder.setKey(uuid);
127 taintBuilder.setClosed(true);
128 return taintBuilder.build();
131 private void getFlows(TaintVulnerabilityLite.Builder taintBuilder, Locations locations, IssueDto issueDto) {
132 List<Issues.Flow> flows = new ArrayList<>();
134 for (DbIssues.Flow f : locations.getFlowList()) {
135 Set<String> componentUuids = new HashSet<>();
137 Issues.Flow.Builder builder = Issues.Flow.newBuilder();
138 List<Issues.Location> flowLocations = new ArrayList<>();
139 getComponentUuids(f, componentUuids);
141 for (DbIssues.Location l : f.getLocationList()) {
142 Issues.Location.Builder flowLocationBuilder = Issues.Location
144 .setMessage(l.getMsg())
145 .setTextRange(buildTextRange(l));
146 if (l.hasComponentId() && componentsMap.containsKey(l.getComponentId())) {
147 flowLocationBuilder.setFilePath(componentsMap.get(l.getComponentId()).path());
149 flowLocationBuilder.setFilePath(issueDto.getFilePath());
151 flowLocations.add(flowLocationBuilder.build());
153 builder.addAllLocations(flowLocations);
154 flows.add(builder.build());
156 taintBuilder.addAllFlows(flows);
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());
167 if (!componentUuids.isEmpty()) {
168 componentsMap.putAll(getLocationComponents(componentUuids));
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();
178 return Issues.TextRange.newBuilder()
179 .setHash(location.getChecksum())
180 .setStartLine(startLine)
182 .setStartLineOffset(startOffset)
183 .setEndLineOffset(endOffset).build();
186 private void refreshComponents() {
187 componentsMap = new HashMap<>();
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));