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.almsettings.ws;
22 import org.sonar.api.server.ws.Request;
23 import org.sonar.api.server.ws.Response;
24 import org.sonar.api.server.ws.WebService;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.alm.setting.AlmSettingDto;
28 import org.sonar.server.user.UserSession;
30 import static org.apache.commons.lang.StringUtils.isNotBlank;
32 public class UpdateBitbucketCloudAction implements AlmSettingsWsAction {
34 private static final String PARAM_KEY = "key";
35 private static final String PARAM_NEW_KEY = "newKey";
36 private static final String PARAM_CLIENT_ID = "clientId";
37 private static final String PARAM_CLIENT_SECRET = "clientSecret";
38 private static final String PARAM_WORKSPACE = "workspace";
40 private final DbClient dbClient;
41 private final UserSession userSession;
42 private final AlmSettingsSupport almSettingsSupport;
44 public UpdateBitbucketCloudAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) {
45 this.dbClient = dbClient;
46 this.userSession = userSession;
47 this.almSettingsSupport = almSettingsSupport;
51 public void define(WebService.NewController context) {
52 WebService.NewAction action = context.createAction("update_bitbucketcloud")
53 .setDescription("Update Bitbucket Cloud Setting. <br/>" +
54 "Requires the 'Administer System' permission")
59 action.createParam(PARAM_KEY)
61 .setMaximumLength(200)
62 .setDescription("Unique key of the Bitbucket Cloud setting");
63 action.createParam(PARAM_NEW_KEY)
65 .setMaximumLength(200)
66 .setDescription("Optional new value for an unique key of the Bitbucket Cloud setting");
67 action.createParam(PARAM_WORKSPACE)
70 .setDescription("Bitbucket Cloud workspace ID");
71 action.createParam(PARAM_CLIENT_ID)
74 .setDescription("Bitbucket Cloud Client ID");
75 action.createParam(PARAM_CLIENT_SECRET)
77 .setMaximumLength(160)
78 .setDescription("Optional new value for the Bitbucket Cloud client secret");
82 public void handle(Request request, Response response) throws Exception {
83 userSession.checkIsSystemAdministrator();
88 private void doHandle(Request request) {
89 String key = request.mandatoryParam(PARAM_KEY);
90 String newKey = request.param(PARAM_NEW_KEY);
91 String workspace = request.mandatoryParam(PARAM_WORKSPACE);
92 String clientId = request.mandatoryParam(PARAM_CLIENT_ID);
93 String clientSecret = request.param(PARAM_CLIENT_SECRET);
95 try (DbSession dbSession = dbClient.openSession(false)) {
96 AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key);
97 if (isNotBlank(newKey) && !newKey.equals(key)) {
98 almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey);
101 if (isNotBlank(clientSecret)) {
102 almSettingDto.setClientSecret(clientSecret);
105 almSettingsSupport.checkBitbucketCloudWorkspaceIDFormat(workspace);
107 dbClient.almSettingDao().update(dbSession, almSettingDto
108 .setKey(isNotBlank(newKey) ? newKey : key)
109 .setClientId(clientId)
110 .setAppId(workspace),
111 clientSecret != null);