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.

UpdateAzureAction.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.sonar.api.server.ws.Change;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.alm.setting.AlmSettingDto;
  28. import org.sonar.server.user.UserSession;
  29. import static org.apache.commons.lang3.StringUtils.isNotBlank;
  30. import static org.sonar.server.projectlink.ws.ProjectLinksWsParameters.PARAM_URL;
  31. public class UpdateAzureAction 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_PERSONAL_ACCESS_TOKEN = "personalAccessToken";
  35. private final DbClient dbClient;
  36. private UserSession userSession;
  37. private final AlmSettingsSupport almSettingsSupport;
  38. public UpdateAzureAction(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_azure")
  46. .setDescription("Update Azure instance Setting. <br/>" +
  47. "Requires the 'Administer System' permission")
  48. .setPost(true)
  49. .setSince("8.1")
  50. .setChangelog(new Change("8.6", "Parameter 'URL' was added"),
  51. new Change("8.7", String.format("Parameter '%s' is no longer required", PARAM_PERSONAL_ACCESS_TOKEN)))
  52. .setHandler(this);
  53. action.createParam(PARAM_KEY)
  54. .setRequired(true)
  55. .setMaximumLength(200)
  56. .setDescription("Unique key of the Azure instance setting");
  57. action.createParam(PARAM_NEW_KEY)
  58. .setRequired(false)
  59. .setMaximumLength(200)
  60. .setDescription("Optional new value for an unique key of the Azure Devops instance setting");
  61. action.createParam(PARAM_PERSONAL_ACCESS_TOKEN)
  62. .setRequired(false)
  63. .setMaximumLength(2000)
  64. .setDescription("Azure Devops personal access token");
  65. action.createParam(PARAM_URL)
  66. .setRequired(true)
  67. .setMaximumLength(2000)
  68. .setDescription("Azure API URL");
  69. }
  70. @Override
  71. public void handle(Request request, Response response) throws Exception {
  72. userSession.checkIsSystemAdministrator();
  73. doHandle(request);
  74. response.noContent();
  75. }
  76. private void doHandle(Request request) {
  77. String key = request.mandatoryParam(PARAM_KEY);
  78. String newKey = request.param(PARAM_NEW_KEY);
  79. String pat = request.param(PARAM_PERSONAL_ACCESS_TOKEN);
  80. String url = request.mandatoryParam(PARAM_URL);
  81. try (DbSession dbSession = dbClient.openSession(false)) {
  82. AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key);
  83. if (isNotBlank(newKey) && !newKey.equals(key)) {
  84. almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey);
  85. }
  86. almSettingsSupport.checkPatOnUrlUpdate(almSettingDto, url, pat);
  87. if (isNotBlank(pat)) {
  88. almSettingDto.setPersonalAccessToken(pat);
  89. }
  90. dbClient.almSettingDao().update(dbSession, almSettingDto
  91. .setKey(isNotBlank(newKey) ? newKey : key)
  92. .setUrl(url),
  93. pat != null);
  94. dbSession.commit();
  95. }
  96. }
  97. }