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.

CreateBitBucketAction.java 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.sonar.db.alm.setting.ALM.BITBUCKET;
  29. import static org.sonar.db.alm.setting.ALM.BITBUCKET_CLOUD;
  30. public class CreateBitBucketAction implements AlmSettingsWsAction {
  31. private static final String PARAM_KEY = "key";
  32. private static final String PARAM_URL = "url";
  33. private static final String PARAM_PERSONAL_ACCESS_TOKEN = "personalAccessToken";
  34. private final DbClient dbClient;
  35. private UserSession userSession;
  36. private final AlmSettingsSupport almSettingsSupport;
  37. public CreateBitBucketAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) {
  38. this.dbClient = dbClient;
  39. this.userSession = userSession;
  40. this.almSettingsSupport = almSettingsSupport;
  41. }
  42. @Override
  43. public void define(WebService.NewController context) {
  44. WebService.NewAction action = context.createAction("create_bitbucket")
  45. .setDescription("Create Bitbucket ALM instance Setting. <br/>" +
  46. "Requires the 'Administer System' permission")
  47. .setPost(true)
  48. .setSince("8.1")
  49. .setHandler(this);
  50. action.createParam(PARAM_KEY)
  51. .setRequired(true)
  52. .setMaximumLength(200)
  53. .setDescription("Unique key of the Bitbucket instance setting");
  54. action.createParam(PARAM_URL)
  55. .setRequired(true)
  56. .setMaximumLength(2000)
  57. .setDescription("BitBucket server API URL");
  58. action.createParam(PARAM_PERSONAL_ACCESS_TOKEN)
  59. .setRequired(true)
  60. .setMaximumLength(2000)
  61. .setDescription("Bitbucket personal access token");
  62. }
  63. @Override
  64. public void handle(Request request, Response response) {
  65. userSession.checkIsSystemAdministrator();
  66. doHandle(request);
  67. response.noContent();
  68. }
  69. private void doHandle(Request request) {
  70. String key = request.mandatoryParam(PARAM_KEY);
  71. String url = request.mandatoryParam(PARAM_URL);
  72. String pat = request.mandatoryParam(PARAM_PERSONAL_ACCESS_TOKEN);
  73. try (DbSession dbSession = dbClient.openSession(false)) {
  74. // We do not treat Bitbucket Server and Bitbucket Cloud as different ALMs when it comes to limiting the
  75. // number of connections.
  76. almSettingsSupport.checkAlmMultipleFeatureEnabled(BITBUCKET);
  77. almSettingsSupport.checkAlmMultipleFeatureEnabled(BITBUCKET_CLOUD);
  78. almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, key);
  79. dbClient.almSettingDao().insert(dbSession, new AlmSettingDto()
  80. .setAlm(BITBUCKET)
  81. .setKey(key)
  82. .setUrl(url)
  83. .setPersonalAccessToken(pat));
  84. dbSession.commit();
  85. }
  86. }
  87. }