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 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)
69 .setDescription("Bitbucket Cloud workspace ID");
70 action.createParam(PARAM_CLIENT_ID)
72 .setMaximumLength(2000)
73 .setDescription("Bitbucket Cloud Client ID");
74 action.createParam(PARAM_CLIENT_SECRET)
76 .setMaximumLength(2000)
77 .setDescription("Optional new value for the Bitbucket Cloud client secret");
81 public void handle(Request request, Response response) throws Exception {
82 userSession.checkIsSystemAdministrator();
87 private void doHandle(Request request) {
88 String key = request.mandatoryParam(PARAM_KEY);
89 String newKey = request.param(PARAM_NEW_KEY);
90 String workspace = request.mandatoryParam(PARAM_WORKSPACE);
91 String clientId = request.mandatoryParam(PARAM_CLIENT_ID);
92 String clientSecret = request.param(PARAM_CLIENT_SECRET);
94 try (DbSession dbSession = dbClient.openSession(false)) {
95 AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key);
96 if (isNotBlank(newKey) && !newKey.equals(key)) {
97 almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey);
100 if (isNotBlank(clientSecret)) {
101 almSettingDto.setClientSecret(clientSecret);
104 dbClient.almSettingDao().update(dbSession, almSettingDto
105 .setKey(isNotBlank(newKey) ? newKey : key)
106 .setClientId(clientId)
107 .setAppId(workspace),
108 clientSecret != null);