]> source.dussan.org Git - sonarqube.git/blob
90ab338bb21cc2b263a059582156251fbefd3451
[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.hotspot.ws;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.function.Consumer;
28 import javax.annotation.Nullable;
29 import org.sonar.api.utils.Paging;
30 import org.sonar.db.component.BranchDto;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.issue.IssueDto;
33 import org.sonar.db.project.ProjectDto;
34 import org.sonar.db.protobuf.DbIssues;
35 import org.sonar.server.issue.TextRangeResponseFormatter;
36 import org.sonar.server.security.SecurityStandards;
37 import org.sonar.server.ws.MessageFormattingUtils;
38 import org.sonarqube.ws.Common;
39 import org.sonarqube.ws.Hotspots;
40
41 import static java.util.Collections.emptyList;
42 import static java.util.Optional.ofNullable;
43 import static org.sonar.api.utils.DateUtils.formatDateTime;
44 import static org.sonar.server.security.SecurityStandards.fromSecurityStandards;
45 import static org.sonarqube.ws.WsUtils.nullToEmpty;
46
47 public class HotspotWsResponseFormatter {
48
49   private final TextRangeResponseFormatter textRangeFormatter;
50
51   public HotspotWsResponseFormatter(TextRangeResponseFormatter textRangeFormatter) {
52     this.textRangeFormatter = textRangeFormatter;
53   }
54
55   Hotspots.Component formatProject(Hotspots.Component.Builder builder, ProjectDto project, @Nullable String branch, @Nullable String pullRequest) {
56     builder
57       .clear()
58       .setKey(project.getKey())
59       .setQualifier(project.getQualifier())
60       .setName(project.getName())
61       .setLongName(project.getName());
62     ofNullable(branch).ifPresent(builder::setBranch);
63     ofNullable(pullRequest).ifPresent(builder::setPullRequest);
64     return builder.build();
65   }
66
67   Hotspots.Component formatComponent(Hotspots.Component.Builder builder, ComponentDto component, @Nullable String branch, @Nullable String pullRequest) {
68     builder
69       .clear()
70       .setKey(component.getKey())
71       .setQualifier(component.qualifier())
72       .setName(component.name())
73       .setLongName(component.longName());
74     ofNullable(branch).ifPresent(builder::setBranch);
75     ofNullable(pullRequest).ifPresent(builder::setPullRequest);
76     ofNullable(component.path()).ifPresent(builder::setPath);
77     return builder.build();
78   }
79
80   void formatHotspots(SearchResponseData searchResponseData, Hotspots.ListWsResponse.Builder responseBuilder) {
81     responseBuilder.addAllHotspots(mapHotspots(searchResponseData));
82   }
83
84   void formatHotspots(SearchResponseData searchResponseData, Hotspots.SearchWsResponse.Builder responseBuilder) {
85     responseBuilder.addAllHotspots(mapHotspots(searchResponseData));
86   }
87
88   private List<Hotspots.SearchWsResponse.Hotspot> mapHotspots(SearchResponseData searchResponseData) {
89     List<IssueDto> hotspots = searchResponseData.getHotspots();
90     if (hotspots.isEmpty()) {
91       return emptyList();
92     }
93
94     Hotspots.SearchWsResponse.Hotspot.Builder builder = Hotspots.SearchWsResponse.Hotspot.newBuilder();
95     List<Hotspots.SearchWsResponse.Hotspot> hotspotsList = new ArrayList<>(hotspots.size());
96     for (IssueDto hotspot : hotspots) {
97       SecurityStandards.SQCategory sqCategory = fromSecurityStandards(hotspot.getSecurityStandards()).getSqCategory();
98       builder
99         .clear()
100         .setKey(hotspot.getKey())
101         .setComponent(hotspot.getComponentKey())
102         .setProject(hotspot.getProjectKey())
103         .setSecurityCategory(sqCategory.getKey())
104         .setVulnerabilityProbability(sqCategory.getVulnerability().name())
105         .setRuleKey(hotspot.getRuleKey().toString());
106       ofNullable(hotspot.getStatus()).ifPresent(builder::setStatus);
107       ofNullable(hotspot.getResolution()).ifPresent(builder::setResolution);
108       ofNullable(hotspot.getLine()).ifPresent(builder::setLine);
109       builder.setMessage(nullToEmpty(hotspot.getMessage()));
110       builder.addAllMessageFormattings(MessageFormattingUtils.dbMessageFormattingToWs(hotspot.parseMessageFormattings()));
111       ofNullable(hotspot.getAssigneeUuid()).ifPresent(builder::setAssignee);
112       builder.setAuthor(nullToEmpty(hotspot.getAuthorLogin()));
113       builder.setCreationDate(formatDateTime(hotspot.getIssueCreationDate()));
114       builder.setUpdateDate(formatDateTime(hotspot.getIssueUpdateDate()));
115       completeHotspotLocations(hotspot, builder, searchResponseData);
116       hotspotsList.add(builder.build());
117     }
118     return hotspotsList;
119   }
120
121   void completeHotspotLocations(IssueDto hotspot, Hotspots.SearchWsResponse.Hotspot.Builder hotspotBuilder, SearchResponseData data) {
122     DbIssues.Locations locations = hotspot.parseLocations();
123
124     if (locations == null) {
125       return;
126     }
127
128     textRangeFormatter.formatTextRange(locations, hotspotBuilder::setTextRange);
129     hotspotBuilder.addAllFlows(textRangeFormatter.formatFlows(locations, hotspotBuilder.getComponent(), data.getComponentsByUuid()));
130   }
131
132   Hotspots.Component formatComponent(Hotspots.Component.Builder builder, ComponentDto component, @Nullable BranchDto branchDto) {
133     if (branchDto == null || branchDto.isMain()) {
134       return formatComponent(builder, component, null, null);
135     }
136     return formatComponent(builder, component, branchDto.getBranchKey(), branchDto.getPullRequestKey());
137   }
138
139   void formatTextRange(IssueDto dto, Consumer<Common.TextRange> rangeConsumer) {
140     textRangeFormatter.formatTextRange(dto, rangeConsumer);
141   }
142
143   List<Common.Flow> formatFlows(DbIssues.Locations locations, String issueComponent, Map<String, ComponentDto> componentsByUuid) {
144     return textRangeFormatter.formatFlows(locations, issueComponent, componentsByUuid);
145   }
146
147   static final class SearchResponseData {
148     private final Paging paging;
149     private final List<IssueDto> hotspots;
150     private final Map<String, ComponentDto> componentsByUuid = new HashMap<>();
151     private final Map<String, BranchDto> branchesByBranchUuid = new HashMap<>();
152
153     SearchResponseData(Paging paging, List<IssueDto> hotspots) {
154       this.paging = paging;
155       this.hotspots = hotspots;
156     }
157
158     boolean isPresent() {
159       return !hotspots.isEmpty();
160     }
161
162     public Paging getPaging() {
163       return paging;
164     }
165
166     List<IssueDto> getHotspots() {
167       return hotspots;
168     }
169
170     void addComponents(Collection<ComponentDto> components) {
171       for (ComponentDto component : components) {
172         componentsByUuid.put(component.uuid(), component);
173       }
174     }
175
176     public void addBranches(List<BranchDto> branchDtos) {
177       for (BranchDto branch : branchDtos) {
178         branchesByBranchUuid.put(branch.getUuid(), branch);
179       }
180     }
181
182     public BranchDto getBranch(String branchUuid) {
183       return branchesByBranchUuid.get(branchUuid);
184     }
185
186     Collection<ComponentDto> getComponents() {
187       return componentsByUuid.values();
188     }
189
190     public Map<String, ComponentDto> getComponentsByUuid() {
191       return componentsByUuid;
192     }
193   }
194
195 }