]> source.dussan.org Git - sonarqube.git/blob
f9a295db8030de7df1ecb863a7ebcad39720a436
[sonarqube.git] /
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.almintegration.ws.bitbucketserver;
21
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.Set;
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;
51
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;
58
59 public class SearchBitbucketServerReposAction implements AlmIntegrationsWsAction {
60
61   private static final Logger LOG = Loggers.get(SearchBitbucketServerReposAction.class);
62
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";
66
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;
72
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;
80   }
81
82   @Override
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")
87       .setPost(false)
88       .setSince("8.2")
89       .setHandler(this);
90
91     action.createParam(PARAM_ALM_SETTING)
92       .setRequired(true)
93       .setMaximumLength(200)
94       .setDescription("ALM setting key");
95     action.createParam(PARAM_PROJECT_NAME)
96       .setRequired(false)
97       .setMaximumLength(200)
98       .setDescription("Project name filter");
99     action.createParam(PARAM_REPO_NAME)
100       .setRequired(false)
101       .setMaximumLength(200)
102       .setDescription("Repository name filter");
103   }
104
105   @Override
106   public void handle(Request request, Response response) {
107     SearchBitbucketserverReposWsResponse wsResponse = doHandle(request);
108     writeProtobuf(wsResponse, request, response);
109   }
110
111   private SearchBitbucketserverReposWsResponse doHandle(Request request) {
112
113     try (DbSession dbSession = dbClient.openSession(false)) {
114       userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
115
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);
121
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);
127
128       Map<String, String> sqProjectsKeyByBBSKey = getSqProjectsKeyByBBSKey(dbSession, almSettingDto, gsonBBSRepoList);
129       List<BBSRepo> bbsRepos = gsonBBSRepoList.getValues().stream().map(gsonBBSRepo -> toBBSRepo(gsonBBSRepo, sqProjectsKeyByBBSKey))
130         .collect(toList());
131
132       SearchBitbucketserverReposWsResponse.Builder builder = SearchBitbucketserverReposWsResponse.newBuilder()
133         .setIsLastPage(gsonBBSRepoList.isLastPage())
134         .addAllRepositories(bbsRepos);
135       return builder.build();
136     }
137   }
138
139   private Map<String, String> getSqProjectsKeyByBBSKey(DbSession dbSession, AlmSettingDto almSettingDto, RepositoryList gsonBBSRepoList) {
140     Set<String> slugs = gsonBBSRepoList.getValues().stream().map(Repository::getSlug).collect(toSet());
141
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()));
148
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()));
152   }
153
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());
160
161     String sqProjectKey = sqProjectsKeyByBBSKey.get(customKey(gsonBBSRepo));
162     if (sqProjectKey != null) {
163       builder.setSqProjectKey(sqProjectKey);
164     }
165
166     return builder.build();
167   }
168
169   private static String customKey(ProjectAlmSettingDto project) {
170     return project.getAlmRepo() + "/" + project.getAlmSlug();
171   }
172
173   private static String customKey(Repository gsonBBSRepo) {
174     return gsonBBSRepo.getProject().getKey() + "/" + gsonBBSRepo.getSlug();
175   }
176
177   private static BinaryOperator<String> resolveNameCollisionOperatorByNaturalOrder() {
178     return (a, b) -> new TreeSet<>(Arrays.asList(a, b)).first();
179   }
180
181 }