選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CreateAzureAction.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.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.sonar.db.alm.setting.ALM.AZURE_DEVOPS;
  30. import static org.sonar.server.projectlink.ws.ProjectLinksWsParameters.PARAM_URL;
  31. public class CreateAzureAction implements AlmSettingsWsAction {
  32. private static final String PARAM_KEY = "key";
  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 CreateAzureAction(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_azure")
  45. .setDescription("Create Azure ALM instance Setting. <br/>" +
  46. "Requires the 'Administer System' permission")
  47. .setPost(true)
  48. .setSince("8.1")
  49. .setChangelog(new Change("8.6", "Parameter 'URL' was added"))
  50. .setHandler(this);
  51. action.createParam(PARAM_KEY)
  52. .setRequired(true)
  53. .setMaximumLength(200)
  54. .setDescription("Unique key of the Azure Devops instance setting");
  55. action.createParam(PARAM_PERSONAL_ACCESS_TOKEN)
  56. .setRequired(true)
  57. .setMaximumLength(2000)
  58. .setDescription("Azure Devops personal access token");
  59. action.createParam(PARAM_URL)
  60. .setRequired(true)
  61. .setMaximumLength(2000)
  62. .setDescription("Azure API URL");
  63. }
  64. @Override
  65. public void handle(Request request, Response response) {
  66. userSession.checkIsSystemAdministrator();
  67. doHandle(request);
  68. response.noContent();
  69. }
  70. private void doHandle(Request request) {
  71. String key = request.mandatoryParam(PARAM_KEY);
  72. String pat = request.mandatoryParam(PARAM_PERSONAL_ACCESS_TOKEN);
  73. String url = request.mandatoryParam(PARAM_URL);
  74. try (DbSession dbSession = dbClient.openSession(false)) {
  75. almSettingsSupport.checkAlmMultipleFeatureEnabled(AZURE_DEVOPS);
  76. almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, key);
  77. dbClient.almSettingDao().insert(dbSession, new AlmSettingDto()
  78. .setAlm(AZURE_DEVOPS)
  79. .setKey(key)
  80. .setPersonalAccessToken(pat)
  81. .setUrl(url));
  82. dbSession.commit();
  83. }
  84. }
  85. }