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.Change;
23 import org.sonar.api.server.ws.Request;
24 import org.sonar.api.server.ws.Response;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.alm.setting.AlmSettingDto;
29 import org.sonar.server.user.UserSession;
31 import static org.apache.commons.lang.StringUtils.isNotBlank;
33 public class UpdateBitbucketAction implements AlmSettingsWsAction {
35 private static final String PARAM_KEY = "key";
36 private static final String PARAM_NEW_KEY = "newKey";
37 private static final String PARAM_URL = "url";
38 private static final String PARAM_PERSONAL_ACCESS_TOKEN = "personalAccessToken";
40 private final DbClient dbClient;
41 private UserSession userSession;
42 private final AlmSettingsSupport almSettingsSupport;
44 public UpdateBitbucketAction(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_bitbucket")
53 .setDescription("Update Bitbucket ALM instance Setting. <br/>" +
54 "Requires the 'Administer System' permission")
57 .setChangelog(new Change("8.7", String.format("Parameter '%s' is no longer required", PARAM_PERSONAL_ACCESS_TOKEN)))
60 action.createParam(PARAM_KEY)
62 .setMaximumLength(200)
63 .setDescription("Unique key of the Bitbucket instance setting");
64 action.createParam(PARAM_NEW_KEY)
66 .setMaximumLength(200)
67 .setDescription("Optional new value for an unique key of the Bitbucket instance setting");
68 action.createParam(PARAM_URL)
70 .setMaximumLength(2000)
71 .setDescription("Bitbucket API URL");
72 action.createParam(PARAM_PERSONAL_ACCESS_TOKEN)
74 .setMaximumLength(2000)
75 .setDescription("Bitbucket personal access token");
79 public void handle(Request request, Response response) throws Exception {
80 userSession.checkIsSystemAdministrator();
85 private void doHandle(Request request) {
86 String key = request.mandatoryParam(PARAM_KEY);
87 String newKey = request.param(PARAM_NEW_KEY);
88 String url = request.mandatoryParam(PARAM_URL);
89 String pat = request.param(PARAM_PERSONAL_ACCESS_TOKEN);
91 try (DbSession dbSession = dbClient.openSession(false)) {
92 AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key);
93 if (isNotBlank(newKey) && !newKey.equals(key)) {
94 almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey);
96 dbClient.almSettingDao().update(dbSession, almSettingDto
97 .setKey(isNotBlank(newKey) ? newKey : key)
99 .setPersonalAccessToken(isNotBlank(pat) ? pat : almSettingDto.getPersonalAccessToken()));