3 * Copyright (C) 2009-2021 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.almsettings.ws;
22 import java.util.Comparator;
23 import java.util.List;
25 import java.util.Optional;
26 import java.util.stream.Collectors;
27 import org.sonar.api.server.ws.Change;
28 import org.sonar.api.server.ws.Request;
29 import org.sonar.api.server.ws.Response;
30 import org.sonar.api.server.ws.WebService;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.alm.setting.ALM;
34 import org.sonar.db.alm.setting.AlmSettingDto;
35 import org.sonar.server.user.UserSession;
36 import org.sonarqube.ws.AlmSettings;
37 import org.sonarqube.ws.AlmSettings.AlmSettingBitbucketCloud;
38 import org.sonarqube.ws.AlmSettings.AlmSettingGithub;
40 import static java.util.Collections.emptyList;
41 import static java.util.Objects.requireNonNull;
42 import static org.sonar.server.ws.WsUtils.writeProtobuf;
43 import static org.sonarqube.ws.AlmSettings.AlmSettingAzure;
44 import static org.sonarqube.ws.AlmSettings.AlmSettingBitbucket;
45 import static org.sonarqube.ws.AlmSettings.AlmSettingGitlab;
46 import static org.sonarqube.ws.AlmSettings.ListDefinitionsWsResponse;
48 public class ListDefinitionsAction implements AlmSettingsWsAction {
50 private final DbClient dbClient;
51 private final UserSession userSession;
53 public ListDefinitionsAction(DbClient dbClient, UserSession userSession) {
54 this.dbClient = dbClient;
55 this.userSession = userSession;
59 public void define(WebService.NewController context) {
60 context.createAction("list_definitions")
61 .setDescription("List ALM Settings, sorted by created date.<br/>" +
62 "Requires the 'Administer System' permission")
64 .setResponseExample(getClass().getResource("list_definitions-example.json"))
65 .setChangelog(new Change("8.2", "Field 'URL' added for GitLab definitions"),
66 new Change("8.6", "Field 'URL' added for Azure definitions"),
67 new Change("8.7", "Fields 'personalAccessToken', 'privateKey', and 'clientSecret' are no longer returned"))
72 public void handle(Request request, Response response) {
73 userSession.checkIsSystemAdministrator();
74 ListDefinitionsWsResponse wsResponse = doHandle();
75 writeProtobuf(wsResponse, request, response);
78 private ListDefinitionsWsResponse doHandle() {
79 try (DbSession dbSession = dbClient.openSession(false)) {
80 List<AlmSettingDto> settings = dbClient.almSettingDao().selectAll(dbSession);
81 Map<ALM, List<AlmSettingDto>> settingsByAlm = settings.stream().collect(Collectors.groupingBy(AlmSettingDto::getAlm));
82 List<AlmSettingGithub> githubSettings = settingsByAlm.getOrDefault(ALM.GITHUB, emptyList())
84 .sorted(Comparator.comparing(AlmSettingDto::getCreatedAt))
85 .map(ListDefinitionsAction::toGitHub).collect(Collectors.toList());
86 List<AlmSettingAzure> azureSettings = settingsByAlm.getOrDefault(ALM.AZURE_DEVOPS, emptyList())
88 .sorted(Comparator.comparing(AlmSettingDto::getCreatedAt))
89 .map(ListDefinitionsAction::toAzure).collect(Collectors.toList());
90 List<AlmSettingBitbucket> bitbucketSettings = settingsByAlm.getOrDefault(ALM.BITBUCKET, emptyList())
92 .sorted(Comparator.comparing(AlmSettingDto::getCreatedAt))
93 .map(ListDefinitionsAction::toBitbucket).collect(Collectors.toList());
94 List<AlmSettingBitbucketCloud> bitbucketCloudSettings = settingsByAlm.getOrDefault(ALM.BITBUCKET_CLOUD, emptyList())
96 .sorted(Comparator.comparing(AlmSettingDto::getCreatedAt))
97 .map(ListDefinitionsAction::toBitbucketCloud).collect(Collectors.toList());
98 List<AlmSettingGitlab> gitlabSettings = settingsByAlm.getOrDefault(ALM.GITLAB, emptyList())
100 .sorted(Comparator.comparing(AlmSettingDto::getCreatedAt))
101 .map(ListDefinitionsAction::toGitlab).collect(Collectors.toList());
102 return ListDefinitionsWsResponse.newBuilder()
103 .addAllGithub(githubSettings)
104 .addAllAzure(azureSettings)
105 .addAllBitbucket(bitbucketSettings)
106 .addAllBitbucketcloud(bitbucketCloudSettings)
107 .addAllGitlab(gitlabSettings)
112 private static AlmSettingGithub toGitHub(AlmSettingDto settingDto) {
113 AlmSettingGithub.Builder builder = AlmSettingGithub
115 .setKey(settingDto.getKey())
116 .setUrl(requireNonNull(settingDto.getUrl(), "URL cannot be null for GitHub ALM setting"))
117 .setAppId(requireNonNull(settingDto.getAppId(), "App ID cannot be null for GitHub ALM setting"));
118 // Don't fail if clientId is not set for migration cases
119 Optional.ofNullable(settingDto.getClientId()).ifPresent(builder::setClientId);
120 return builder.build();
123 private static AlmSettingAzure toAzure(AlmSettingDto settingDto) {
124 AlmSettingAzure.Builder builder = AlmSettingAzure
126 .setKey(settingDto.getKey());
128 if (settingDto.getUrl() != null) {
129 builder.setUrl(settingDto.getUrl());
132 return builder.build();
135 private static AlmSettingGitlab toGitlab(AlmSettingDto settingDto) {
136 AlmSettingGitlab.Builder builder = AlmSettingGitlab.newBuilder()
137 .setKey(settingDto.getKey());
139 if (settingDto.getUrl() != null) {
140 builder.setUrl(settingDto.getUrl());
142 return builder.build();
145 private static AlmSettingBitbucket toBitbucket(AlmSettingDto settingDto) {
146 return AlmSettingBitbucket
148 .setKey(settingDto.getKey())
149 .setUrl(requireNonNull(settingDto.getUrl(), "URL cannot be null for Bitbucket ALM setting"))
153 private static AlmSettingBitbucketCloud toBitbucketCloud(AlmSettingDto settingDto) {
154 AlmSettingBitbucketCloud.Builder builder = AlmSettingBitbucketCloud
156 .setKey(settingDto.getKey())
157 .setWorkspace(requireNonNull(settingDto.getAppId()))
158 .setClientId(requireNonNull(settingDto.getClientId(), "Client ID cannot be null for Bitbucket Cloud ALM setting"));
159 return builder.build();