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.AlmSettingBitbucketCloud;
37 import org.sonarqube.ws.AlmSettings.AlmSettingGithub;
39 import static java.util.Collections.emptyList;
40 import static java.util.Objects.requireNonNull;
41 import static org.sonar.server.ws.WsUtils.writeProtobuf;
42 import static org.sonarqube.ws.AlmSettings.AlmSettingAzure;
43 import static org.sonarqube.ws.AlmSettings.AlmSettingBitbucket;
44 import static org.sonarqube.ws.AlmSettings.AlmSettingGitlab;
45 import static org.sonarqube.ws.AlmSettings.ListDefinitionsWsResponse;
47 public class ListDefinitionsAction implements AlmSettingsWsAction {
49 private final DbClient dbClient;
50 private final UserSession userSession;
52 public ListDefinitionsAction(DbClient dbClient, UserSession userSession) {
53 this.dbClient = dbClient;
54 this.userSession = userSession;
58 public void define(WebService.NewController context) {
59 context.createAction("list_definitions")
60 .setDescription("List ALM Settings, sorted by created date.<br/>" +
61 "Requires the 'Administer System' permission")
63 .setResponseExample(getClass().getResource("list_definitions-example.json"))
64 .setChangelog(new Change("8.2", "Field 'URL' added for GitLab definitions"),
65 new Change("8.6", "Field 'URL' added for Azure definitions"),
66 new Change("8.7", "Fields 'personalAccessToken', 'privateKey', and 'clientSecret' are no longer returned"))
71 public void handle(Request request, Response response) {
72 userSession.checkIsSystemAdministrator();
73 ListDefinitionsWsResponse wsResponse = doHandle();
74 writeProtobuf(wsResponse, request, response);
77 private ListDefinitionsWsResponse doHandle() {
78 try (DbSession dbSession = dbClient.openSession(false)) {
79 List<AlmSettingDto> settings = dbClient.almSettingDao().selectAll(dbSession);
80 Map<ALM, List<AlmSettingDto>> settingsByAlm = settings.stream().collect(Collectors.groupingBy(AlmSettingDto::getAlm));
81 List<AlmSettingGithub> githubSettings = settingsByAlm.getOrDefault(ALM.GITHUB, emptyList())
83 .sorted(Comparator.comparing(AlmSettingDto::getCreatedAt))
84 .map(ListDefinitionsAction::toGitHub).collect(Collectors.toList());
85 List<AlmSettingAzure> azureSettings = settingsByAlm.getOrDefault(ALM.AZURE_DEVOPS, emptyList())
87 .sorted(Comparator.comparing(AlmSettingDto::getCreatedAt))
88 .map(ListDefinitionsAction::toAzure).collect(Collectors.toList());
89 List<AlmSettingBitbucket> bitbucketSettings = settingsByAlm.getOrDefault(ALM.BITBUCKET, emptyList())
91 .sorted(Comparator.comparing(AlmSettingDto::getCreatedAt))
92 .map(ListDefinitionsAction::toBitbucket).collect(Collectors.toList());
93 List<AlmSettingBitbucketCloud> bitbucketCloudSettings = settingsByAlm.getOrDefault(ALM.BITBUCKET_CLOUD, emptyList())
95 .sorted(Comparator.comparing(AlmSettingDto::getCreatedAt))
96 .map(ListDefinitionsAction::toBitbucketCloud).collect(Collectors.toList());
97 List<AlmSettingGitlab> gitlabSettings = settingsByAlm.getOrDefault(ALM.GITLAB, emptyList())
99 .sorted(Comparator.comparing(AlmSettingDto::getCreatedAt))
100 .map(ListDefinitionsAction::toGitlab).collect(Collectors.toList());
101 return ListDefinitionsWsResponse.newBuilder()
102 .addAllGithub(githubSettings)
103 .addAllAzure(azureSettings)
104 .addAllBitbucket(bitbucketSettings)
105 .addAllBitbucketcloud(bitbucketCloudSettings)
106 .addAllGitlab(gitlabSettings)
111 private static AlmSettingGithub toGitHub(AlmSettingDto settingDto) {
112 AlmSettingGithub.Builder builder = AlmSettingGithub
114 .setKey(settingDto.getKey())
115 .setUrl(requireNonNull(settingDto.getUrl(), "URL cannot be null for GitHub ALM setting"))
116 .setAppId(requireNonNull(settingDto.getAppId(), "App ID cannot be null for GitHub ALM setting"));
117 // Don't fail if clientId is not set for migration cases
118 Optional.ofNullable(settingDto.getClientId()).ifPresent(builder::setClientId);
119 return builder.build();
122 private static AlmSettingAzure toAzure(AlmSettingDto settingDto) {
123 AlmSettingAzure.Builder builder = AlmSettingAzure
125 .setKey(settingDto.getKey());
127 if (settingDto.getUrl() != null) {
128 builder.setUrl(settingDto.getUrl());
131 return builder.build();
134 private static AlmSettingGitlab toGitlab(AlmSettingDto settingDto) {
135 AlmSettingGitlab.Builder builder = AlmSettingGitlab.newBuilder()
136 .setKey(settingDto.getKey());
138 if (settingDto.getUrl() != null) {
139 builder.setUrl(settingDto.getUrl());
141 return builder.build();
144 private static AlmSettingBitbucket toBitbucket(AlmSettingDto settingDto) {
145 return AlmSettingBitbucket
147 .setKey(settingDto.getKey())
148 .setUrl(requireNonNull(settingDto.getUrl(), "URL cannot be null for Bitbucket ALM setting"))
152 private static AlmSettingBitbucketCloud toBitbucketCloud(AlmSettingDto settingDto) {
153 AlmSettingBitbucketCloud.Builder builder = AlmSettingBitbucketCloud
155 .setKey(settingDto.getKey())
156 .setWorkspace(requireNonNull(settingDto.getAppId()))
157 .setClientId(requireNonNull(settingDto.getClientId(), "Client ID cannot be null for Bitbucket Cloud ALM setting"));
158 return builder.build();