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.

UpdateGithubAction.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.isBlank;
  30. import static org.apache.commons.lang3.StringUtils.isNotBlank;
  31. import static org.apache.commons.lang3.StringUtils.removeEnd;
  32. public class UpdateGithubAction implements AlmSettingsWsAction {
  33. private static final String PARAM_KEY = "key";
  34. private static final String PARAM_NEW_KEY = "newKey";
  35. private static final String PARAM_URL = "url";
  36. private static final String PARAM_APP_ID = "appId";
  37. private static final String PARAM_CLIENT_ID = "clientId";
  38. private static final String PARAM_CLIENT_SECRET = "clientSecret";
  39. private static final String PARAM_PRIVATE_KEY = "privateKey";
  40. private static final String PARAM_WEBHOOK_SECRET = "webhookSecret";
  41. private final DbClient dbClient;
  42. private final UserSession userSession;
  43. private final AlmSettingsSupport almSettingsSupport;
  44. public UpdateGithubAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) {
  45. this.dbClient = dbClient;
  46. this.userSession = userSession;
  47. this.almSettingsSupport = almSettingsSupport;
  48. }
  49. @Override
  50. public void define(WebService.NewController context) {
  51. WebService.NewAction action = context.createAction("update_github")
  52. .setDescription("Update GitHub instance Setting. <br/>" +
  53. "Requires the 'Administer System' permission")
  54. .setPost(true)
  55. .setSince("8.1")
  56. .setChangelog(
  57. new Change("9.7", String.format("Optional parameter '%s' was added", PARAM_WEBHOOK_SECRET)),
  58. new Change("8.7", String.format("Parameter '%s' is no longer required", PARAM_PRIVATE_KEY)),
  59. new Change("8.7", String.format("Parameter '%s' is no longer required", PARAM_CLIENT_SECRET)))
  60. .setHandler(this);
  61. action.createParam(PARAM_KEY)
  62. .setRequired(true)
  63. .setMaximumLength(200)
  64. .setDescription("Unique key of the GitHub instance setting");
  65. action.createParam(PARAM_NEW_KEY)
  66. .setRequired(false)
  67. .setMaximumLength(200)
  68. .setDescription("Optional new value for an unique key of the GitHub instance setting");
  69. action.createParam(PARAM_URL)
  70. .setRequired(true)
  71. .setMaximumLength(2000)
  72. .setDescription("GitHub API URL");
  73. action.createParam(PARAM_APP_ID)
  74. .setRequired(true)
  75. .setMaximumLength(80)
  76. .setDescription("GitHub API ID");
  77. action.createParam(PARAM_PRIVATE_KEY)
  78. .setRequired(false)
  79. .setMaximumLength(2500)
  80. .setDescription("GitHub App private key");
  81. action.createParam(PARAM_CLIENT_ID)
  82. .setRequired(true)
  83. .setMaximumLength(80)
  84. .setDescription("GitHub App Client ID");
  85. action.createParam(PARAM_CLIENT_SECRET)
  86. .setRequired(false)
  87. .setMaximumLength(160)
  88. .setDescription("GitHub App Client Secret");
  89. action.createParam(PARAM_WEBHOOK_SECRET)
  90. .setRequired(false)
  91. .setMaximumLength(160)
  92. .setDescription("GitHub App Webhook Secret");
  93. }
  94. @Override
  95. public void handle(Request request, Response response) throws Exception {
  96. userSession.checkIsSystemAdministrator();
  97. tryDoHandle(request);
  98. response.noContent();
  99. }
  100. private void tryDoHandle(Request request) {
  101. try (DbSession dbSession = dbClient.openSession(false)) {
  102. doHandle(request, dbSession);
  103. }
  104. }
  105. private void doHandle(Request request, DbSession dbSession) {
  106. String key = request.mandatoryParam(PARAM_KEY);
  107. String newKey = request.param(PARAM_NEW_KEY);
  108. if (isNotBlank(newKey) && !newKey.equals(key)) {
  109. almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey);
  110. }
  111. AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key);
  112. String url = request.mandatoryParam(PARAM_URL);
  113. String privateKey = request.param(PARAM_PRIVATE_KEY);
  114. almSettingsSupport.checkPrivateKeyOnUrlUpdate(almSettingDto, url, privateKey);
  115. if (isNotBlank(privateKey)) {
  116. almSettingDto.setPrivateKey(privateKey);
  117. }
  118. String clientSecret = request.param(PARAM_CLIENT_SECRET);
  119. if (isNotBlank(clientSecret)) {
  120. almSettingDto.setClientSecret(clientSecret);
  121. }
  122. boolean hasWebhookSecretParam = request.hasParam(PARAM_WEBHOOK_SECRET);
  123. if (hasWebhookSecretParam) {
  124. String webhookSecret = request.getParam(PARAM_WEBHOOK_SECRET).getValue();
  125. almSettingDto.setWebhookSecret(isBlank(webhookSecret) ? null : webhookSecret);
  126. }
  127. almSettingDto
  128. .setKey(isNotBlank(newKey) ? newKey : key)
  129. .setUrl(removeEnd(request.mandatoryParam(PARAM_URL), "/"))
  130. .setAppId(request.mandatoryParam(PARAM_APP_ID))
  131. .setClientId(request.mandatoryParam(PARAM_CLIENT_ID));
  132. boolean isAnySecretUpdated = clientSecret != null || privateKey != null || hasWebhookSecretParam;
  133. dbClient.almSettingDao().update(dbSession, almSettingDto, isAnySecretUpdated);
  134. dbSession.commit();
  135. }
  136. }