]> source.dussan.org Git - sonarqube.git/blob
3daa11d4f54b5690f0ab99c31192a1f4b5070789
[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.component.ws;
21
22 import java.util.Arrays;
23 import java.util.Set;
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;
31
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;
35
36 class ComponentDtoToWsComponent {
37
38   /**
39    * The concept of "visibility" will only be configured for these qualifiers.
40    */
41   private static final Set<String> QUALIFIERS_WITH_VISIBILITY = Set.of(ComponentQualifiers.PROJECT, ComponentQualifiers.VIEW, ComponentQualifiers.APP);
42
43   private ComponentDtoToWsComponent() {
44     // prevent instantiation
45   }
46
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());
52
53     ofNullable(emptyToNull(project.getDescription())).ifPresent(wsComponent::setDescription);
54     ofNullable(lastAnalysis).ifPresent(
55       analysis -> {
56         wsComponent.setAnalysisDate(formatDateTime(analysis.getCreatedAt()));
57         ofNullable(analysis.getPeriodDate()).ifPresent(leak -> wsComponent.setLeakPeriodDate(formatDateTime(leak)));
58         ofNullable(analysis.getProjectVersion()).ifPresent(wsComponent::setVersion);
59       });
60     if (QUALIFIERS_WITH_VISIBILITY.contains(project.getQualifier())) {
61       wsComponent.setVisibility(Visibility.getLabel(project.isPrivate()));
62       wsComponent.getTagsBuilder().addAllTags(project.getTags());
63     }
64
65     return wsComponent;
66   }
67
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()))
72       .setName(dto.name())
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(
80       analysis -> {
81         wsComponent.setAnalysisDate(formatDateTime(analysis.getCreatedAt()));
82         ofNullable(analysis.getPeriodDate()).ifPresent(leak -> wsComponent.setLeakPeriodDate(formatDateTime(leak)));
83         ofNullable(analysis.getProjectVersion()).ifPresent(wsComponent::setVersion);
84       });
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());
89       }
90     }
91     return wsComponent;
92   }
93 }