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.

CreateGithubAction.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.GITHUB;
  29. public class CreateGithubAction implements AlmSettingsWsAction {
  30. private static final String PARAM_KEY = "key";
  31. private static final String PARAM_URL = "url";
  32. private static final String PARAM_APP_ID = "appId";
  33. private static final String PARAM_CLIENT_ID = "clientId";
  34. private static final String PARAM_CLIENT_SECRET = "clientSecret";
  35. private static final String PARAM_PRIVATE_KEY = "privateKey";
  36. private final DbClient dbClient;
  37. private final UserSession userSession;
  38. private final AlmSettingsSupport almSettingsSupport;
  39. public CreateGithubAction(DbClient dbClient, UserSession userSession,
  40. AlmSettingsSupport almSettingsSupport) {
  41. this.dbClient = dbClient;
  42. this.userSession = userSession;
  43. this.almSettingsSupport = almSettingsSupport;
  44. }
  45. @Override
  46. public void define(WebService.NewController context) {
  47. WebService.NewAction action = context.createAction("create_github")
  48. .setDescription("Create GitHub ALM instance Setting. <br/>" +
  49. "Requires the 'Administer System' permission")
  50. .setPost(true)
  51. .setSince("8.1")
  52. .setHandler(this);
  53. action.createParam(PARAM_KEY)
  54. .setRequired(true)
  55. .setMaximumLength(200)
  56. .setDescription("Unique key of the GitHub instance setting");
  57. action.createParam(PARAM_URL)
  58. .setRequired(true)
  59. .setMaximumLength(2000)
  60. .setDescription("GitHub API URL");
  61. action.createParam(PARAM_APP_ID)
  62. .setRequired(true)
  63. .setMaximumLength(80)
  64. .setDescription("GitHub App ID");
  65. action.createParam(PARAM_PRIVATE_KEY)
  66. .setRequired(true)
  67. .setMaximumLength(2000)
  68. .setDescription("GitHub App private key");
  69. action.createParam(PARAM_CLIENT_ID)
  70. .setRequired(true)
  71. .setMaximumLength(80)
  72. .setDescription("GitHub App Client ID");
  73. action.createParam(PARAM_CLIENT_SECRET)
  74. .setRequired(true)
  75. .setMaximumLength(80)
  76. .setDescription("GitHub App Client Secret");
  77. }
  78. @Override
  79. public void handle(Request request, Response response) {
  80. userSession.checkIsSystemAdministrator();
  81. doHandle(request);
  82. response.noContent();
  83. }
  84. private void doHandle(Request request) {
  85. String key = request.mandatoryParam(PARAM_KEY);
  86. String url = request.mandatoryParam(PARAM_URL);
  87. String appId = request.mandatoryParam(PARAM_APP_ID);
  88. String clientId = request.mandatoryParam(PARAM_CLIENT_ID);
  89. String clientSecret = request.mandatoryParam(PARAM_CLIENT_SECRET);
  90. String privateKey = request.mandatoryParam(PARAM_PRIVATE_KEY);
  91. if (url.endsWith("/")) {
  92. url = url.substring(0, url.length() - 1);
  93. }
  94. try (DbSession dbSession = dbClient.openSession(false)) {
  95. almSettingsSupport.checkAlmMultipleFeatureEnabled(GITHUB);
  96. almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, key);
  97. dbClient.almSettingDao().insert(dbSession, new AlmSettingDto()
  98. .setAlm(GITHUB)
  99. .setKey(key)
  100. .setUrl(url)
  101. .setAppId(appId)
  102. .setPrivateKey(privateKey)
  103. .setClientId(clientId)
  104. .setClientSecret(clientSecret));
  105. dbSession.commit();
  106. }
  107. }
  108. }