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.

CreateGitlabAction.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.apache.commons.lang3.StringUtils;
  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;
  30. import static org.sonar.db.alm.setting.ALM.GITLAB;
  31. public class CreateGitlabAction implements AlmSettingsWsAction {
  32. private static final String PARAM_KEY = "key";
  33. private static final String PARAM_URL = "url";
  34. private static final String PARAM_PERSONAL_ACCESS_TOKEN = "personalAccessToken";
  35. private final DbClient dbClient;
  36. private UserSession userSession;
  37. private final AlmSettingsSupport almSettingsSupport;
  38. public CreateGitlabAction(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("create_gitlab")
  46. .setDescription("Create GitLab instance Setting. <br/>" +
  47. "Requires the 'Administer System' permission")
  48. .setPost(true)
  49. .setSince("8.1")
  50. .setChangelog(new Change("8.2", "Parameter 'URL' was added"))
  51. .setHandler(this);
  52. action.createParam(PARAM_KEY)
  53. .setRequired(true)
  54. .setMaximumLength(200)
  55. .setDescription("Unique key of the GitLab instance setting");
  56. action.createParam(PARAM_URL)
  57. .setRequired(true)
  58. .setMaximumLength(2000)
  59. .setDescription("GitLab API URL");
  60. action.createParam(PARAM_PERSONAL_ACCESS_TOKEN)
  61. .setRequired(true)
  62. .setMaximumLength(2000)
  63. .setDescription("GitLab personal access token");
  64. }
  65. @Override
  66. public void handle(Request request, Response response) {
  67. userSession.checkIsSystemAdministrator();
  68. doHandle(request);
  69. response.noContent();
  70. }
  71. private void doHandle(Request request) {
  72. String key = request.mandatoryParam(PARAM_KEY);
  73. String url = StringUtils.trim(request.mandatoryParam(PARAM_URL));
  74. String pat = request.mandatoryParam(PARAM_PERSONAL_ACCESS_TOKEN);
  75. try (DbSession dbSession = dbClient.openSession(false)) {
  76. almSettingsSupport.checkAlmMultipleFeatureEnabled(GITLAB);
  77. almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, key);
  78. dbClient.almSettingDao().insert(dbSession, new AlmSettingDto()
  79. .setAlm(GITLAB)
  80. .setUrl(url)
  81. .setKey(key)
  82. .setPersonalAccessToken(pat));
  83. dbSession.commit();
  84. }
  85. }
  86. }