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.

UpdateGitlabAction.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.apache.commons.lang3.StringUtils.isNotBlank;
  31. public class UpdateGitlabAction implements AlmSettingsWsAction {
  32. private static final String PARAM_KEY = "key";
  33. private static final String PARAM_NEW_KEY = "newKey";
  34. private static final String PARAM_URL = "url";
  35. private static final String PARAM_PERSONAL_ACCESS_TOKEN = "personalAccessToken";
  36. private final DbClient dbClient;
  37. private UserSession userSession;
  38. private final AlmSettingsSupport almSettingsSupport;
  39. public UpdateGitlabAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) {
  40. this.dbClient = dbClient;
  41. this.userSession = userSession;
  42. this.almSettingsSupport = almSettingsSupport;
  43. }
  44. @Override
  45. public void define(WebService.NewController context) {
  46. WebService.NewAction action = context.createAction("update_gitlab")
  47. .setDescription("Update GitLab instance Setting. <br/>" +
  48. "Requires the 'Administer System' permission")
  49. .setPost(true)
  50. .setSince("8.1")
  51. .setChangelog(new Change("8.2", "Parameter 'URL' was added"),
  52. new Change("8.7", String.format("Parameter '%s' is no longer required", PARAM_PERSONAL_ACCESS_TOKEN)))
  53. .setHandler(this);
  54. action.createParam(PARAM_KEY)
  55. .setRequired(true)
  56. .setMaximumLength(200)
  57. .setDescription("Unique key of the GitLab instance setting");
  58. action.createParam(PARAM_NEW_KEY)
  59. .setRequired(false)
  60. .setMaximumLength(200)
  61. .setDescription("Optional new value for an unique key of the GitLab instance setting");
  62. action.createParam(PARAM_URL)
  63. .setRequired(true)
  64. .setMaximumLength(2000)
  65. .setDescription("GitLab API URL");
  66. action.createParam(PARAM_PERSONAL_ACCESS_TOKEN)
  67. .setRequired(false)
  68. .setMaximumLength(2000)
  69. .setDescription("GitLab personal access token");
  70. }
  71. @Override
  72. public void handle(Request request, Response response) {
  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 url = StringUtils.trim(request.mandatoryParam(PARAM_URL));
  81. String pat = request.param(PARAM_PERSONAL_ACCESS_TOKEN);
  82. try (DbSession dbSession = dbClient.openSession(false)) {
  83. AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key);
  84. if (isNotBlank(newKey) && !newKey.equals(key)) {
  85. almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey);
  86. }
  87. almSettingsSupport.checkPatOnUrlUpdate(almSettingDto, url, pat);
  88. if (isNotBlank(pat)) {
  89. almSettingDto.setPersonalAccessToken(pat);
  90. }
  91. dbClient.almSettingDao().update(dbSession, almSettingDto
  92. .setKey(isNotBlank(newKey) ? newKey : key)
  93. .setUrl(url),
  94. pat != null);
  95. dbSession.commit();
  96. }
  97. }
  98. }