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.

GetBindingAction.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.db.alm.setting.ProjectAlmSettingDto;
  29. import org.sonar.db.project.ProjectDto;
  30. import org.sonar.server.component.ComponentFinder;
  31. import org.sonar.server.exceptions.NotFoundException;
  32. import org.sonar.server.user.UserSession;
  33. import org.sonarqube.ws.AlmSettings.GetBindingWsResponse;
  34. import static java.lang.String.format;
  35. import static java.util.Optional.ofNullable;
  36. import static org.sonar.api.web.UserRole.ADMIN;
  37. import static org.sonar.server.almsettings.ws.AlmSettingsSupport.toAlmWs;
  38. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  39. public class GetBindingAction implements AlmSettingsWsAction {
  40. private static final String PARAM_PROJECT = "project";
  41. private final DbClient dbClient;
  42. private final UserSession userSession;
  43. private final ComponentFinder componentFinder;
  44. public GetBindingAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder) {
  45. this.dbClient = dbClient;
  46. this.userSession = userSession;
  47. this.componentFinder = componentFinder;
  48. }
  49. @Override
  50. public void define(WebService.NewController context) {
  51. WebService.NewAction action = context.createAction("get_binding")
  52. .setDescription("Get ALM binding of a given project.<br/>" +
  53. "Requires the 'Administer' permission on the project")
  54. .setSince("8.1")
  55. .setResponseExample(getClass().getResource("get_binding-example.json"))
  56. .setChangelog(new Change("8.6", "Azure binding now contains the project and repository names"))
  57. .setChangelog(new Change("8.7", "Azure binding now contains a monorepo flag for monorepo feature in Enterprise Edition and above"))
  58. .setHandler(this);
  59. action
  60. .createParam(PARAM_PROJECT)
  61. .setDescription("Project key")
  62. .setRequired(true);
  63. }
  64. @Override
  65. public void handle(Request request, Response response) {
  66. GetBindingWsResponse wsResponse = doHandle(request);
  67. writeProtobuf(wsResponse, request, response);
  68. }
  69. private GetBindingWsResponse doHandle(Request request) {
  70. String projectKey = request.mandatoryParam(PARAM_PROJECT);
  71. try (DbSession dbSession = dbClient.openSession(false)) {
  72. ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
  73. userSession.checkProjectPermission(ADMIN, project);
  74. ProjectAlmSettingDto projectAlmSetting = dbClient.projectAlmSettingDao().selectByProject(dbSession, project)
  75. .orElseThrow(() -> new NotFoundException(format("Project '%s' is not bound to any ALM", project.getKey())));
  76. AlmSettingDto almSetting = dbClient.almSettingDao().selectByUuid(dbSession, projectAlmSetting.getAlmSettingUuid())
  77. .orElseThrow(() -> new IllegalStateException(format("ALM setting with uuid '%s' cannot be found", projectAlmSetting.getAlmSettingUuid())));
  78. GetBindingWsResponse.Builder builder = GetBindingWsResponse.newBuilder()
  79. .setAlm(toAlmWs(almSetting.getAlm()))
  80. .setKey(almSetting.getKey());
  81. ofNullable(projectAlmSetting.getAlmRepo()).ifPresent(builder::setRepository);
  82. ofNullable(almSetting.getUrl()).ifPresent(builder::setUrl);
  83. ofNullable(projectAlmSetting.getAlmSlug()).ifPresent(builder::setSlug);
  84. ofNullable(projectAlmSetting.getSummaryCommentEnabled()).ifPresent(builder::setSummaryCommentEnabled);
  85. ofNullable(projectAlmSetting.getMonorepo()).ifPresent(builder::setMonorepo);
  86. return builder.build();
  87. }
  88. }
  89. }