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.component.ws;
22 import java.util.Arrays;
24 import javax.annotation.Nullable;
25 import org.sonar.db.component.ComponentQualifiers;
26 import org.sonar.db.component.ComponentDto;
27 import org.sonar.db.component.SnapshotDto;
28 import org.sonar.db.project.ProjectDto;
29 import org.sonar.server.project.Visibility;
30 import org.sonarqube.ws.Components;
32 import static com.google.common.base.Strings.emptyToNull;
33 import static java.util.Optional.ofNullable;
34 import static org.sonar.api.utils.DateUtils.formatDateTime;
36 class ComponentDtoToWsComponent {
39 * The concept of "visibility" will only be configured for these qualifiers.
41 private static final Set<String> QUALIFIERS_WITH_VISIBILITY = Set.of(ComponentQualifiers.PROJECT, ComponentQualifiers.VIEW, ComponentQualifiers.APP);
43 private ComponentDtoToWsComponent() {
44 // prevent instantiation
47 static Components.Component.Builder projectOrAppToWsComponent(ProjectDto project, @Nullable SnapshotDto lastAnalysis) {
48 Components.Component.Builder wsComponent = Components.Component.newBuilder()
49 .setKey(project.getKey())
50 .setName(project.getName())
51 .setQualifier(project.getQualifier());
53 ofNullable(emptyToNull(project.getDescription())).ifPresent(wsComponent::setDescription);
54 ofNullable(lastAnalysis).ifPresent(
56 wsComponent.setAnalysisDate(formatDateTime(analysis.getCreatedAt()));
57 ofNullable(analysis.getPeriodDate()).ifPresent(leak -> wsComponent.setLeakPeriodDate(formatDateTime(leak)));
58 ofNullable(analysis.getProjectVersion()).ifPresent(wsComponent::setVersion);
60 if (QUALIFIERS_WITH_VISIBILITY.contains(project.getQualifier())) {
61 wsComponent.setVisibility(Visibility.getLabel(project.isPrivate()));
62 wsComponent.getTagsBuilder().addAllTags(project.getTags());
68 public static Components.Component.Builder componentDtoToWsComponent(ComponentDto dto, @Nullable ProjectDto parentProjectDto,
69 @Nullable SnapshotDto lastAnalysis, boolean isMainBranch, @Nullable String branch, @Nullable String pullRequest) {
70 Components.Component.Builder wsComponent = Components.Component.newBuilder()
71 .setKey(ComponentDto.removeBranchAndPullRequestFromKey(dto.getKey()))
73 .setQualifier(dto.qualifier());
74 ofNullable(emptyToNull(branch)).ifPresent(wsComponent::setBranch);
75 ofNullable(emptyToNull(pullRequest)).ifPresent(wsComponent::setPullRequest);
76 ofNullable(emptyToNull(dto.path())).ifPresent(wsComponent::setPath);
77 ofNullable(emptyToNull(dto.description())).ifPresent(wsComponent::setDescription);
78 ofNullable(emptyToNull(dto.language())).ifPresent(wsComponent::setLanguage);
79 ofNullable(lastAnalysis).ifPresent(
81 wsComponent.setAnalysisDate(formatDateTime(analysis.getCreatedAt()));
82 ofNullable(analysis.getPeriodDate()).ifPresent(leak -> wsComponent.setLeakPeriodDate(formatDateTime(leak)));
83 ofNullable(analysis.getProjectVersion()).ifPresent(wsComponent::setVersion);
85 if (QUALIFIERS_WITH_VISIBILITY.contains(dto.qualifier())) {
86 wsComponent.setVisibility(Visibility.getLabel(dto.isPrivate()));
87 if (Arrays.asList(ComponentQualifiers.PROJECT, ComponentQualifiers.APP).contains(dto.qualifier()) && parentProjectDto != null && isMainBranch) {
88 wsComponent.getTagsBuilder().addAllTags(parentProjectDto.getTags());