]> source.dussan.org Git - sonarqube.git/blob
d9c30bb8c5c4329fd7af9a37ef85d42e851eab9a
[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.v2.api.gitlab.config.controller;
21
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.Set;
26 import javax.annotation.Nullable;
27 import org.apache.commons.lang.StringUtils;
28 import org.sonar.server.common.gitlab.config.GitlabConfiguration;
29 import org.sonar.server.common.gitlab.config.GitlabConfigurationService;
30 import org.sonar.server.common.gitlab.config.ProvisioningType;
31 import org.sonar.server.common.gitlab.config.UpdateGitlabConfigurationRequest;
32 import org.sonar.server.user.UserSession;
33 import org.sonar.server.v2.api.gitlab.config.request.GitlabConfigurationCreateRestRequest;
34 import org.sonar.server.v2.api.gitlab.config.request.GitlabConfigurationUpdateRestRequest;
35 import org.sonar.server.v2.api.gitlab.config.resource.GitlabConfigurationResource;
36 import org.sonar.server.v2.api.gitlab.config.response.GitlabConfigurationSearchRestResponse;
37 import org.sonar.server.v2.api.response.PageRestResponse;
38
39 import static org.sonar.api.utils.Preconditions.checkArgument;
40 import static org.sonar.server.common.gitlab.config.GitlabConfigurationService.UNIQUE_GITLAB_CONFIGURATION_ID;
41
42 public class DefaultGitlabConfigurationController implements GitlabConfigurationController {
43
44   private final UserSession userSession;
45   private final GitlabConfigurationService gitlabConfigurationService;
46
47   public DefaultGitlabConfigurationController(UserSession userSession, GitlabConfigurationService gitlabConfigurationService) {
48     this.userSession = userSession;
49     this.gitlabConfigurationService = gitlabConfigurationService;
50   }
51
52   @Override
53   public GitlabConfigurationResource getGitlabConfiguration(String id) {
54     userSession.checkIsSystemAdministrator();
55     return getGitlabConfigurationResource(id);
56   }
57
58   @Override
59   public GitlabConfigurationSearchRestResponse searchGitlabConfiguration() {
60     userSession.checkIsSystemAdministrator();
61
62     List<GitlabConfigurationResource> gitlabConfigurationResources = gitlabConfigurationService.findConfigurations()
63       .stream()
64       .map(this::toGitLabConfigurationResource)
65       .toList();
66
67     PageRestResponse pageRestResponse = new PageRestResponse(1, 1000, gitlabConfigurationResources.size());
68     return new GitlabConfigurationSearchRestResponse(gitlabConfigurationResources, pageRestResponse);
69   }
70
71   @Override
72   public GitlabConfigurationResource create(GitlabConfigurationCreateRestRequest createRequest) {
73     userSession.checkIsSystemAdministrator();
74     GitlabConfiguration createdConfiguration = gitlabConfigurationService.createConfiguration(toGitlabConfiguration(createRequest));
75     return toGitLabConfigurationResource(createdConfiguration);
76   }
77
78   private static GitlabConfiguration toGitlabConfiguration(GitlabConfigurationCreateRestRequest createRestRequest) {
79     return new GitlabConfiguration(
80       UNIQUE_GITLAB_CONFIGURATION_ID,
81       createRestRequest.enabled(),
82       createRestRequest.applicationId(),
83       createRestRequest.url(),
84       createRestRequest.secret(),
85       createRestRequest.synchronizeGroups(),
86       Set.copyOf(createRestRequest.allowedGroups()),
87       createRestRequest.allowUsersToSignUp() != null && createRestRequest.allowUsersToSignUp(),
88       toProvisioningType(createRestRequest.provisioningType()),
89       createRestRequest.provisioningToken());
90   }
91
92   private GitlabConfigurationResource getGitlabConfigurationResource(String id) {
93     return toGitLabConfigurationResource(gitlabConfigurationService.getConfiguration(id));
94   }
95
96   @Override
97   public GitlabConfigurationResource updateGitlabConfiguration(String id, GitlabConfigurationUpdateRestRequest updateRequest) {
98     userSession.checkIsSystemAdministrator();
99     UpdateGitlabConfigurationRequest updateGitlabConfigurationRequest = toUpdateGitlabConfigurationRequest(id, updateRequest);
100     return toGitLabConfigurationResource(gitlabConfigurationService.updateConfiguration(updateGitlabConfigurationRequest));
101   }
102
103   private static UpdateGitlabConfigurationRequest toUpdateGitlabConfigurationRequest(String id,
104     GitlabConfigurationUpdateRestRequest updateRequest) {
105     return UpdateGitlabConfigurationRequest.builder()
106       .gitlabConfigurationId(id)
107       .enabled(updateRequest.getEnabled().toNonNullUpdatedValue())
108       .applicationId(updateRequest.getApplicationId().toNonNullUpdatedValue())
109       .url(updateRequest.getUrl().toNonNullUpdatedValue())
110       .secret(updateRequest.getSecret().toNonNullUpdatedValue())
111       .synchronizeGroups(updateRequest.getSynchronizeGroups().toNonNullUpdatedValue())
112       .allowedGroups(updateRequest.getAllowedGroups().map(DefaultGitlabConfigurationController::getGroups).toNonNullUpdatedValue())
113       .provisioningType(updateRequest.getProvisioningType().map(DefaultGitlabConfigurationController::toProvisioningType).toNonNullUpdatedValue())
114       .allowUserToSignUp(updateRequest.getAllowUsersToSignUp().toNonNullUpdatedValue())
115       .provisioningToken(updateRequest.getProvisioningToken().toUpdatedValue())
116       .build();
117   }
118
119   private static Set<String> getGroups(@Nullable List<String> groups) {
120     checkArgument(groups != null, "allowedGroups must not be null");
121     return new HashSet<>(groups);
122   }
123
124   private GitlabConfigurationResource toGitLabConfigurationResource(GitlabConfiguration configuration) {
125     Optional<String> configurationError = gitlabConfigurationService.validate(configuration);
126     return new GitlabConfigurationResource(
127       configuration.id(),
128       configuration.enabled(),
129       configuration.applicationId(),
130       configuration.url(),
131       configuration.synchronizeGroups(),
132       sortGroups(configuration.allowedGroups()),
133       configuration.allowUsersToSignUp(),
134       toRestProvisioningType(configuration),
135       StringUtils.isNotEmpty(configuration.provisioningToken()),
136       configurationError.orElse(null));
137   }
138
139   private static org.sonar.server.v2.api.gitlab.config.resource.ProvisioningType toRestProvisioningType(GitlabConfiguration configuration) {
140     return org.sonar.server.v2.api.gitlab.config.resource.ProvisioningType.valueOf(configuration.provisioningType().name());
141   }
142
143   private static ProvisioningType toProvisioningType(org.sonar.server.v2.api.gitlab.config.resource.ProvisioningType provisioningType) {
144     return ProvisioningType.valueOf(provisioningType.name());
145   }
146
147   private static List<String> sortGroups(Set<String> groups) {
148     return groups.stream().sorted().toList();
149   }
150
151   @Override
152   public void deleteGitlabConfiguration(String id) {
153     userSession.checkIsSystemAdministrator();
154     gitlabConfigurationService.deleteConfiguration(id);
155   }
156 }