3 * Copyright (C) 2009-2023 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 javax.annotation.Nullable;
23 import org.sonar.db.component.BranchDto;
24 import org.sonar.db.component.ComponentDto;
25 import org.sonar.db.project.ProjectDto;
26 import org.sonarqube.ws.Hotspots;
28 import static java.util.Optional.ofNullable;
30 public class HotspotWsResponseFormatter {
32 public HotspotWsResponseFormatter() {
36 Hotspots.Component formatProject(Hotspots.Component.Builder builder, ProjectDto project, @Nullable String branch, @Nullable String pullRequest) {
39 .setKey(project.getKey())
40 .setQualifier(project.getQualifier())
41 .setName(project.getName())
42 .setLongName(project.getName());
43 ofNullable(branch).ifPresent(builder::setBranch);
44 ofNullable(pullRequest).ifPresent(builder::setPullRequest);
45 return builder.build();
48 Hotspots.Component formatComponent(Hotspots.Component.Builder builder, ComponentDto component, @Nullable String branch, @Nullable String pullRequest) {
51 .setKey(component.getKey())
52 .setQualifier(component.qualifier())
53 .setName(component.name())
54 .setLongName(component.longName());
55 ofNullable(branch).ifPresent(builder::setBranch);
56 ofNullable(pullRequest).ifPresent(builder::setPullRequest);
57 ofNullable(component.path()).ifPresent(builder::setPath);
58 return builder.build();
61 Hotspots.Component formatComponent(Hotspots.Component.Builder builder, ComponentDto component, @Nullable BranchDto branchDto) {
62 if (branchDto == null || branchDto.isMain()) {
63 return formatComponent(builder, component, null, null);
65 return formatComponent(builder, component, branchDto.getBranchKey(), branchDto.getPullRequestKey());