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.

CountBindingAction.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.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 org.sonarqube.ws.AlmSettings.CountBindingWsResponse;
  29. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  30. public class CountBindingAction implements AlmSettingsWsAction {
  31. private static final String PARAM_ALM_SETTING = "almSetting";
  32. private final DbClient dbClient;
  33. private final UserSession userSession;
  34. private final AlmSettingsSupport almSettingsSupport;
  35. public CountBindingAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) {
  36. this.dbClient = dbClient;
  37. this.userSession = userSession;
  38. this.almSettingsSupport = almSettingsSupport;
  39. }
  40. @Override
  41. public void define(WebService.NewController context) {
  42. WebService.NewAction action = context.createAction("count_binding")
  43. .setDescription("Count number of project bound to an DevOps Platform setting.<br/>" +
  44. "Requires the 'Administer System' permission")
  45. .setSince("8.1")
  46. .setResponseExample(getClass().getResource("example-count_binding.json"))
  47. .setHandler(this);
  48. action
  49. .createParam(PARAM_ALM_SETTING)
  50. .setDescription("DevOps Platform setting key")
  51. .setRequired(true);
  52. }
  53. @Override
  54. public void handle(Request request, Response response) throws Exception {
  55. userSession.checkIsSystemAdministrator();
  56. CountBindingWsResponse wsResponse = doHandle(request);
  57. writeProtobuf(wsResponse, request, response);
  58. }
  59. private CountBindingWsResponse doHandle(Request request) {
  60. String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
  61. try (DbSession dbSession = dbClient.openSession(false)) {
  62. AlmSettingDto almSetting = almSettingsSupport.getAlmSetting(dbSession, almSettingKey);
  63. int projectsBound = dbClient.projectAlmSettingDao().countByAlmSetting(dbSession, almSetting);
  64. return CountBindingWsResponse.newBuilder()
  65. .setKey(almSetting.getKey())
  66. .setProjects(projectsBound)
  67. .build();
  68. }
  69. }
  70. }