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.hotspot.ws;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
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;
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;
47 public class HotspotWsResponseFormatter {
49 private final TextRangeResponseFormatter textRangeFormatter;
51 public HotspotWsResponseFormatter(TextRangeResponseFormatter textRangeFormatter) {
52 this.textRangeFormatter = textRangeFormatter;
55 Hotspots.Component formatProject(Hotspots.Component.Builder builder, ProjectDto project, @Nullable String branch, @Nullable String pullRequest) {
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();
67 Hotspots.Component formatComponent(Hotspots.Component.Builder builder, ComponentDto component, @Nullable String branch, @Nullable String pullRequest) {
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();
80 void formatHotspots(SearchResponseData searchResponseData, Hotspots.ListWsResponse.Builder responseBuilder) {
81 responseBuilder.addAllHotspots(mapHotspots(searchResponseData));
84 void formatHotspots(SearchResponseData searchResponseData, Hotspots.SearchWsResponse.Builder responseBuilder) {
85 responseBuilder.addAllHotspots(mapHotspots(searchResponseData));
88 private List<Hotspots.SearchWsResponse.Hotspot> mapHotspots(SearchResponseData searchResponseData) {
89 List<IssueDto> hotspots = searchResponseData.getHotspots();
90 if (hotspots.isEmpty()) {
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();
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());
121 void completeHotspotLocations(IssueDto hotspot, Hotspots.SearchWsResponse.Hotspot.Builder hotspotBuilder, SearchResponseData data) {
122 DbIssues.Locations locations = hotspot.parseLocations();
124 if (locations == null) {
128 textRangeFormatter.formatTextRange(locations, hotspotBuilder::setTextRange);
129 hotspotBuilder.addAllFlows(textRangeFormatter.formatFlows(locations, hotspotBuilder.getComponent(), data.getComponentsByUuid()));
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);
136 return formatComponent(builder, component, branchDto.getBranchKey(), branchDto.getPullRequestKey());
139 void formatTextRange(IssueDto dto, Consumer<Common.TextRange> rangeConsumer) {
140 textRangeFormatter.formatTextRange(dto, rangeConsumer);
143 List<Common.Flow> formatFlows(DbIssues.Locations locations, String issueComponent, Map<String, ComponentDto> componentsByUuid) {
144 return textRangeFormatter.formatFlows(locations, issueComponent, componentsByUuid);
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<>();
153 SearchResponseData(Paging paging, List<IssueDto> hotspots) {
154 this.paging = paging;
155 this.hotspots = hotspots;
158 boolean isPresent() {
159 return !hotspots.isEmpty();
162 public Paging getPaging() {
166 List<IssueDto> getHotspots() {
170 void addComponents(Collection<ComponentDto> components) {
171 for (ComponentDto component : components) {
172 componentsByUuid.put(component.uuid(), component);
176 public void addBranches(List<BranchDto> branchDtos) {
177 for (BranchDto branch : branchDtos) {
178 branchesByBranchUuid.put(branch.getUuid(), branch);
182 public BranchDto getBranch(String branchUuid) {
183 return branchesByBranchUuid.get(branchUuid);
186 Collection<ComponentDto> getComponents() {
187 return componentsByUuid.values();
190 public Map<String, ComponentDto> getComponentsByUuid() {
191 return componentsByUuid;