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.v2.api.gitlab.config.controller;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Optional;
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;
39 import static org.sonar.api.utils.Preconditions.checkArgument;
40 import static org.sonar.server.common.gitlab.config.GitlabConfigurationService.UNIQUE_GITLAB_CONFIGURATION_ID;
42 public class DefaultGitlabConfigurationController implements GitlabConfigurationController {
44 private final UserSession userSession;
45 private final GitlabConfigurationService gitlabConfigurationService;
47 public DefaultGitlabConfigurationController(UserSession userSession, GitlabConfigurationService gitlabConfigurationService) {
48 this.userSession = userSession;
49 this.gitlabConfigurationService = gitlabConfigurationService;
53 public GitlabConfigurationResource getGitlabConfiguration(String id) {
54 userSession.checkIsSystemAdministrator();
55 return getGitlabConfigurationResource(id);
59 public GitlabConfigurationSearchRestResponse searchGitlabConfiguration() {
60 userSession.checkIsSystemAdministrator();
62 List<GitlabConfigurationResource> gitlabConfigurationResources = gitlabConfigurationService.findConfigurations()
64 .map(this::toGitLabConfigurationResource)
67 PageRestResponse pageRestResponse = new PageRestResponse(1, 1000, gitlabConfigurationResources.size());
68 return new GitlabConfigurationSearchRestResponse(gitlabConfigurationResources, pageRestResponse);
72 public GitlabConfigurationResource create(GitlabConfigurationCreateRestRequest createRequest) {
73 userSession.checkIsSystemAdministrator();
74 GitlabConfiguration createdConfiguration = gitlabConfigurationService.createConfiguration(toGitlabConfiguration(createRequest));
75 return toGitLabConfigurationResource(createdConfiguration);
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());
92 private GitlabConfigurationResource getGitlabConfigurationResource(String id) {
93 return toGitLabConfigurationResource(gitlabConfigurationService.getConfiguration(id));
97 public GitlabConfigurationResource updateGitlabConfiguration(String id, GitlabConfigurationUpdateRestRequest updateRequest) {
98 userSession.checkIsSystemAdministrator();
99 UpdateGitlabConfigurationRequest updateGitlabConfigurationRequest = toUpdateGitlabConfigurationRequest(id, updateRequest);
100 return toGitLabConfigurationResource(gitlabConfigurationService.updateConfiguration(updateGitlabConfigurationRequest));
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())
119 private static Set<String> getGroups(@Nullable List<String> groups) {
120 checkArgument(groups != null, "allowedGroups must not be null");
121 return new HashSet<>(groups);
124 private GitlabConfigurationResource toGitLabConfigurationResource(GitlabConfiguration configuration) {
125 Optional<String> configurationError = gitlabConfigurationService.validate(configuration);
126 return new GitlabConfigurationResource(
128 configuration.enabled(),
129 configuration.applicationId(),
131 configuration.synchronizeGroups(),
132 sortGroups(configuration.allowedGroups()),
133 configuration.allowUsersToSignUp(),
134 toRestProvisioningType(configuration),
135 StringUtils.isNotEmpty(configuration.provisioningToken()),
136 configurationError.orElse(null));
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());
143 private static ProvisioningType toProvisioningType(org.sonar.server.v2.api.gitlab.config.resource.ProvisioningType provisioningType) {
144 return ProvisioningType.valueOf(provisioningType.name());
147 private static List<String> sortGroups(Set<String> groups) {
148 return groups.stream().sorted().toList();
152 public void deleteGitlabConfiguration(String id) {
153 userSession.checkIsSystemAdministrator();
154 gitlabConfigurationService.deleteConfiguration(id);