You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateBitbucketCloudAction.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.almsettings.ws;
  21. import org.sonar.api.server.ws.Request;
  22. import org.sonar.api.server.ws.Response;
  23. import org.sonar.api.server.ws.WebService;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.alm.setting.AlmSettingDto;
  27. import org.sonar.server.user.UserSession;
  28. import static org.apache.commons.lang.StringUtils.isNotBlank;
  29. public class UpdateBitbucketCloudAction implements AlmSettingsWsAction {
  30. private static final String PARAM_KEY = "key";
  31. private static final String PARAM_NEW_KEY = "newKey";
  32. private static final String PARAM_CLIENT_ID = "clientId";
  33. private static final String PARAM_CLIENT_SECRET = "clientSecret";
  34. private static final String PARAM_WORKSPACE = "workspace";
  35. private final DbClient dbClient;
  36. private final UserSession userSession;
  37. private final AlmSettingsSupport almSettingsSupport;
  38. public UpdateBitbucketCloudAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) {
  39. this.dbClient = dbClient;
  40. this.userSession = userSession;
  41. this.almSettingsSupport = almSettingsSupport;
  42. }
  43. @Override
  44. public void define(WebService.NewController context) {
  45. WebService.NewAction action = context.createAction("update_bitbucketcloud")
  46. .setDescription("Update Bitbucket Cloud Setting. <br/>" +
  47. "Requires the 'Administer System' permission")
  48. .setPost(true)
  49. .setSince("8.7")
  50. .setHandler(this);
  51. action.createParam(PARAM_KEY)
  52. .setRequired(true)
  53. .setMaximumLength(200)
  54. .setDescription("Unique key of the Bitbucket Cloud setting");
  55. action.createParam(PARAM_NEW_KEY)
  56. .setRequired(false)
  57. .setMaximumLength(200)
  58. .setDescription("Optional new value for an unique key of the Bitbucket Cloud setting");
  59. action.createParam(PARAM_WORKSPACE)
  60. .setRequired(true)
  61. .setDescription("Bitbucket Cloud workspace ID");
  62. action.createParam(PARAM_CLIENT_ID)
  63. .setRequired(true)
  64. .setMaximumLength(2000)
  65. .setDescription("Bitbucket Cloud Client ID");
  66. action.createParam(PARAM_CLIENT_SECRET)
  67. .setRequired(false)
  68. .setMaximumLength(2000)
  69. .setDescription("Optional new value for the Bitbucket Cloud client secret");
  70. }
  71. @Override
  72. public void handle(Request request, Response response) throws Exception {
  73. userSession.checkIsSystemAdministrator();
  74. doHandle(request);
  75. response.noContent();
  76. }
  77. private void doHandle(Request request) {
  78. String key = request.mandatoryParam(PARAM_KEY);
  79. String newKey = request.param(PARAM_NEW_KEY);
  80. String workspace = request.mandatoryParam(PARAM_WORKSPACE);
  81. String clientId = request.mandatoryParam(PARAM_CLIENT_ID);
  82. String clientSecret = request.param(PARAM_CLIENT_SECRET);
  83. try (DbSession dbSession = dbClient.openSession(false)) {
  84. AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key);
  85. if (isNotBlank(newKey) && !newKey.equals(key)) {
  86. almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey);
  87. }
  88. dbClient.almSettingDao().update(dbSession, almSettingDto
  89. .setKey(isNotBlank(newKey) ? newKey : key)
  90. .setClientId(clientId)
  91. .setAppId(workspace)
  92. .setClientSecret(isNotBlank(clientSecret) ? clientSecret : almSettingDto.getClientSecret()));
  93. dbSession.commit();
  94. }
  95. }
  96. }