3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.almintegration.ws.bitbucketserver;
22 import java.util.Arrays;
23 import java.util.List;
25 import java.util.Optional;
27 import java.util.TreeSet;
28 import java.util.function.BinaryOperator;
29 import java.util.function.Function;
30 import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
31 import org.sonar.alm.client.bitbucketserver.Repository;
32 import org.sonar.alm.client.bitbucketserver.RepositoryList;
33 import org.sonar.api.server.ws.Request;
34 import org.sonar.api.server.ws.Response;
35 import org.sonar.api.server.ws.WebService;
36 import org.sonar.api.utils.log.Logger;
37 import org.sonar.api.utils.log.Loggers;
38 import org.sonar.db.DbClient;
39 import org.sonar.db.DbSession;
40 import org.sonar.db.alm.pat.AlmPatDto;
41 import org.sonar.db.alm.setting.AlmSettingDto;
42 import org.sonar.db.alm.setting.ProjectAlmSettingDao;
43 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
44 import org.sonar.db.project.ProjectDao;
45 import org.sonar.db.project.ProjectDto;
46 import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
47 import org.sonar.server.exceptions.NotFoundException;
48 import org.sonar.server.user.UserSession;
49 import org.sonarqube.ws.AlmIntegrations.BBSRepo;
50 import org.sonarqube.ws.AlmIntegrations.SearchBitbucketserverReposWsResponse;
52 import static java.util.Objects.requireNonNull;
53 import static java.util.stream.Collectors.toList;
54 import static java.util.stream.Collectors.toMap;
55 import static java.util.stream.Collectors.toSet;
56 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
57 import static org.sonar.server.ws.WsUtils.writeProtobuf;
59 public class SearchBitbucketServerReposAction implements AlmIntegrationsWsAction {
61 private static final Logger LOG = Loggers.get(SearchBitbucketServerReposAction.class);
63 private static final String PARAM_ALM_SETTING = "almSetting";
64 private static final String PARAM_REPO_NAME = "repositoryName";
65 private static final String PARAM_PROJECT_NAME = "projectName";
67 private final DbClient dbClient;
68 private final UserSession userSession;
69 private final BitbucketServerRestClient bitbucketServerRestClient;
70 private final ProjectAlmSettingDao projectAlmSettingDao;
71 private final ProjectDao projectDao;
73 public SearchBitbucketServerReposAction(DbClient dbClient, UserSession userSession,
74 BitbucketServerRestClient bitbucketServerRestClient, ProjectAlmSettingDao projectAlmSettingDao, ProjectDao projectDao) {
75 this.dbClient = dbClient;
76 this.userSession = userSession;
77 this.bitbucketServerRestClient = bitbucketServerRestClient;
78 this.projectAlmSettingDao = projectAlmSettingDao;
79 this.projectDao = projectDao;
83 public void define(WebService.NewController context) {
84 WebService.NewAction action = context.createAction("search_bitbucketserver_repos")
85 .setDescription("Search the Bitbucket Server repositories with REPO_ADMIN access<br/>" +
86 "Requires the 'Create Projects' permission")
91 action.createParam(PARAM_ALM_SETTING)
93 .setMaximumLength(200)
94 .setDescription("ALM setting key");
95 action.createParam(PARAM_PROJECT_NAME)
97 .setMaximumLength(200)
98 .setDescription("Project name filter");
99 action.createParam(PARAM_REPO_NAME)
101 .setMaximumLength(200)
102 .setDescription("Repository name filter");
106 public void handle(Request request, Response response) {
107 SearchBitbucketserverReposWsResponse wsResponse = doHandle(request);
108 writeProtobuf(wsResponse, request, response);
111 private SearchBitbucketserverReposWsResponse doHandle(Request request) {
113 try (DbSession dbSession = dbClient.openSession(false)) {
114 userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
116 String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
117 String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null");
118 AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey)
119 .orElseThrow(() -> new NotFoundException(String.format("ALM Setting '%s' not found", almSettingKey)));
120 Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
122 String projectKey = request.param(PARAM_PROJECT_NAME);
123 String repoName = request.param(PARAM_REPO_NAME);
124 String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
125 String url = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
126 RepositoryList gsonBBSRepoList = bitbucketServerRestClient.getRepos(url, pat, projectKey, repoName);
128 Map<String, String> sqProjectsKeyByBBSKey = getSqProjectsKeyByBBSKey(dbSession, almSettingDto, gsonBBSRepoList);
129 List<BBSRepo> bbsRepos = gsonBBSRepoList.getValues().stream().map(gsonBBSRepo -> toBBSRepo(gsonBBSRepo, sqProjectsKeyByBBSKey))
132 SearchBitbucketserverReposWsResponse.Builder builder = SearchBitbucketserverReposWsResponse.newBuilder()
133 .setIsLastPage(gsonBBSRepoList.isLastPage())
134 .addAllRepositories(bbsRepos);
135 return builder.build();
139 private Map<String, String> getSqProjectsKeyByBBSKey(DbSession dbSession, AlmSettingDto almSettingDto, RepositoryList gsonBBSRepoList) {
140 Set<String> slugs = gsonBBSRepoList.getValues().stream().map(Repository::getSlug).collect(toSet());
142 List<ProjectAlmSettingDto> projectAlmSettingDtos = projectAlmSettingDao.selectByAlmSettingAndSlugs(dbSession, almSettingDto, slugs);
143 // As the previous request return bbs only filtered by slug, we need to do an additional filtering on bitbucketServer projectKey + slug
144 Set<String> bbsProjectsAndRepos = gsonBBSRepoList.getValues().stream().map(SearchBitbucketServerReposAction::customKey).collect(toSet());
145 Map<String, ProjectAlmSettingDto> filteredProjectsByUuid = projectAlmSettingDtos.stream()
146 .filter(p -> bbsProjectsAndRepos.contains(customKey(p)))
147 .collect(toMap(ProjectAlmSettingDto::getProjectUuid, Function.identity()));
149 Set<String> projectUuids = filteredProjectsByUuid.values().stream().map(ProjectAlmSettingDto::getProjectUuid).collect(toSet());
150 return projectDao.selectByUuids(dbSession, projectUuids).stream()
151 .collect(toMap(p -> customKey(filteredProjectsByUuid.get(p.getUuid())), ProjectDto::getKey, resolveNameCollisionOperatorByNaturalOrder()));
154 private static BBSRepo toBBSRepo(Repository gsonBBSRepo, Map<String, String> sqProjectsKeyByBBSKey) {
155 BBSRepo.Builder builder = BBSRepo.newBuilder()
156 .setSlug(gsonBBSRepo.getSlug())
157 .setId(gsonBBSRepo.getId())
158 .setName(gsonBBSRepo.getName())
159 .setProjectKey(gsonBBSRepo.getProject().getKey());
161 String sqProjectKey = sqProjectsKeyByBBSKey.get(customKey(gsonBBSRepo));
162 if (sqProjectKey != null) {
163 builder.setSqProjectKey(sqProjectKey);
166 return builder.build();
169 private static String customKey(ProjectAlmSettingDto project) {
170 return project.getAlmRepo() + "/" + project.getAlmSlug();
173 private static String customKey(Repository gsonBBSRepo) {
174 return gsonBBSRepo.getProject().getKey() + "/" + gsonBBSRepo.getSlug();
177 private static BinaryOperator<String> resolveNameCollisionOperatorByNaturalOrder() {
178 return (a, b) -> new TreeSet<>(Arrays.asList(a, b)).first();