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.

ListBitbucketServerProjectsAction.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.almintegration.ws.bitbucketserver;
  21. import java.util.List;
  22. import java.util.Optional;
  23. import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
  24. import org.sonar.alm.client.bitbucketserver.Project;
  25. import org.sonar.alm.client.bitbucketserver.ProjectList;
  26. import org.sonar.api.server.ws.Request;
  27. import org.sonar.api.server.ws.Response;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.alm.pat.AlmPatDto;
  32. import org.sonar.db.alm.setting.AlmSettingDto;
  33. import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
  34. import org.sonar.server.exceptions.NotFoundException;
  35. import org.sonar.server.user.UserSession;
  36. import org.sonarqube.ws.AlmIntegrations.AlmProject;
  37. import org.sonarqube.ws.AlmIntegrations.ListBitbucketserverProjectsWsResponse;
  38. import static java.util.Objects.requireNonNull;
  39. import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
  40. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  41. public class ListBitbucketServerProjectsAction implements AlmIntegrationsWsAction {
  42. private static final String PARAM_ALM_SETTING = "almSetting";
  43. private final DbClient dbClient;
  44. private final UserSession userSession;
  45. private final BitbucketServerRestClient bitbucketServerRestClient;
  46. public ListBitbucketServerProjectsAction(DbClient dbClient, UserSession userSession, BitbucketServerRestClient bitbucketServerRestClient) {
  47. this.dbClient = dbClient;
  48. this.userSession = userSession;
  49. this.bitbucketServerRestClient = bitbucketServerRestClient;
  50. }
  51. @Override
  52. public void define(WebService.NewController context) {
  53. WebService.NewAction action = context.createAction("list_bitbucketserver_projects")
  54. .setDescription("List the Bitbucket Server projects<br/>" +
  55. "Requires the 'Create Projects' permission")
  56. .setPost(false)
  57. .setSince("8.2")
  58. .setResponseExample(getClass().getResource("example-list_bitbucketserver_projects.json"))
  59. .setHandler(this);
  60. action.createParam(PARAM_ALM_SETTING)
  61. .setRequired(true)
  62. .setMaximumLength(200)
  63. .setDescription("DevOps Platform setting key");
  64. }
  65. @Override
  66. public void handle(Request request, Response response) {
  67. ListBitbucketserverProjectsWsResponse wsResponse = doHandle(request);
  68. writeProtobuf(wsResponse, request, response);
  69. }
  70. private ListBitbucketserverProjectsWsResponse doHandle(Request request) {
  71. try (DbSession dbSession = dbClient.openSession(false)) {
  72. userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
  73. String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
  74. String userUuid = requireNonNull(userSession.getUuid(), "User UUID is not null");
  75. AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey)
  76. .orElseThrow(() -> new NotFoundException(String.format("DevOps Platform Setting '%s' not found", almSettingKey)));
  77. Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
  78. String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
  79. String url = requireNonNull(almSettingDto.getUrl(), "URL cannot be null");
  80. ProjectList projectList = bitbucketServerRestClient.getProjects(url, pat);
  81. List<AlmProject> values = projectList.getValues().stream().map(ListBitbucketServerProjectsAction::toAlmProject).toList();
  82. ListBitbucketserverProjectsWsResponse.Builder builder = ListBitbucketserverProjectsWsResponse.newBuilder()
  83. .addAllProjects(values);
  84. return builder.build();
  85. }
  86. }
  87. private static AlmProject toAlmProject(Project project) {
  88. return AlmProject.newBuilder()
  89. .setKey(project.getKey())
  90. .setName(project.getName())
  91. .build();
  92. }
  93. }