3 * Copyright (C) 2009-2024 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.azure;
22 import java.util.List;
24 import java.util.Objects;
25 import java.util.Optional;
27 import java.util.function.BinaryOperator;
28 import java.util.function.Function;
29 import java.util.stream.Collectors;
30 import javax.annotation.Nullable;
32 import org.sonar.alm.client.azure.AzureDevOpsHttpClient;
33 import org.sonar.alm.client.azure.GsonAzureRepo;
34 import org.sonar.alm.client.azure.GsonAzureRepoList;
35 import org.sonar.api.server.ws.Request;
36 import org.sonar.api.server.ws.Response;
37 import org.sonar.api.server.ws.WebService;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.sonar.db.DbClient;
41 import org.sonar.db.DbSession;
42 import org.sonar.db.alm.pat.AlmPatDto;
43 import org.sonar.db.alm.setting.AlmSettingDto;
44 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
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.AzureRepo;
50 import org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse;
52 import static java.util.Comparator.comparing;
53 import static java.util.Objects.requireNonNull;
54 import static java.util.stream.Collectors.toMap;
55 import static java.util.stream.Collectors.toSet;
56 import static org.apache.commons.lang.StringUtils.containsIgnoreCase;
57 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
58 import static org.sonar.server.ws.WsUtils.writeProtobuf;
60 public class SearchAzureReposAction implements AlmIntegrationsWsAction {
62 private static final Logger LOG = LoggerFactory.getLogger(SearchAzureReposAction.class);
64 private static final String PARAM_ALM_SETTING = "almSetting";
65 private static final String PARAM_PROJECT_NAME = "projectName";
66 private static final String PARAM_SEARCH_QUERY = "searchQuery";
68 private final DbClient dbClient;
69 private final UserSession userSession;
70 private final AzureDevOpsHttpClient azureDevOpsHttpClient;
72 public SearchAzureReposAction(DbClient dbClient, UserSession userSession,
73 AzureDevOpsHttpClient azureDevOpsHttpClient) {
74 this.dbClient = dbClient;
75 this.userSession = userSession;
76 this.azureDevOpsHttpClient = azureDevOpsHttpClient;
80 public void define(WebService.NewController context) {
81 WebService.NewAction action = context.createAction("search_azure_repos")
82 .setDescription("Search the Azure repositories<br/>" +
83 "Requires the 'Create Projects' permission")
86 .setResponseExample(getClass().getResource("example-search_azure_repos.json"))
89 action.createParam(PARAM_ALM_SETTING)
91 .setMaximumLength(200)
92 .setDescription("DevOps Platform setting key");
93 action.createParam(PARAM_PROJECT_NAME)
95 .setMaximumLength(200)
96 .setDescription("Project name filter");
97 action.createParam(PARAM_SEARCH_QUERY)
99 .setMaximumLength(200)
100 .setDescription("Search query filter");
104 public void handle(Request request, Response response) {
106 SearchAzureReposWsResponse wsResponse = doHandle(request);
107 writeProtobuf(wsResponse, request, response);
111 private SearchAzureReposWsResponse 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("DevOps Platform 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 searchQuery = request.param(PARAM_SEARCH_QUERY);
124 String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
125 String url = requireNonNull(almSettingDto.getUrl(), "DevOps Platform url cannot be null");
127 GsonAzureRepoList gsonAzureRepoList = azureDevOpsHttpClient.getRepos(url, pat, projectKey);
129 Map<ProjectKeyName, ProjectDto> sqProjectsKeyByAzureKey = getSqProjectsKeyByCustomKey(dbSession, almSettingDto, gsonAzureRepoList);
131 List<AzureRepo> repositories = gsonAzureRepoList.getValues()
133 .filter(r -> isSearchOnlyByProjectName(searchQuery) || doesSearchCriteriaMatchProjectOrRepo(r, searchQuery))
134 .map(repo -> toAzureRepo(repo, sqProjectsKeyByAzureKey))
135 .sorted(comparing(AzureRepo::getName, String::compareToIgnoreCase))
138 LOG.debug(repositories.toString());
140 return SearchAzureReposWsResponse.newBuilder()
141 .addAllRepositories(repositories)
146 private Map<ProjectKeyName, ProjectDto> getSqProjectsKeyByCustomKey(DbSession dbSession, AlmSettingDto almSettingDto,
147 GsonAzureRepoList azureProjectList) {
148 Set<String> projectNames = azureProjectList.getValues().stream().map(r -> r.getProject().getName()).collect(toSet());
149 Set<ProjectKeyName> azureProjectsAndRepos = azureProjectList.getValues().stream().map(ProjectKeyName::from).collect(toSet());
151 List<ProjectAlmSettingDto> projectAlmSettingDtos = dbClient.projectAlmSettingDao()
152 .selectByAlmSettingAndSlugs(dbSession, almSettingDto, projectNames);
154 Map<String, ProjectAlmSettingDto> filteredProjectsByUuid = projectAlmSettingDtos
156 .filter(p -> azureProjectsAndRepos.contains(ProjectKeyName.from(p)))
157 .collect(toMap(ProjectAlmSettingDto::getProjectUuid, Function.identity()));
159 Set<String> projectUuids = filteredProjectsByUuid.values().stream().map(ProjectAlmSettingDto::getProjectUuid).collect(toSet());
161 return dbClient.projectDao().selectByUuids(dbSession, projectUuids)
163 .collect(Collectors.toMap(
164 projectDto -> ProjectKeyName.from(filteredProjectsByUuid.get(projectDto.getUuid())),
166 resolveNameCollisionOperatorByNaturalOrder()));
169 private static boolean isSearchOnlyByProjectName(@Nullable String criteria) {
170 return criteria == null || criteria.isEmpty();
173 private static boolean doesSearchCriteriaMatchProjectOrRepo(GsonAzureRepo repo, String criteria) {
174 boolean matchProject = containsIgnoreCase(repo.getProject().getName(), criteria);
175 boolean matchRepo = containsIgnoreCase(repo.getName(), criteria);
176 return matchProject || matchRepo;
179 private static AzureRepo toAzureRepo(GsonAzureRepo azureRepo, Map<ProjectKeyName, ProjectDto> sqProjectsKeyByAzureKey) {
180 AzureRepo.Builder builder = AzureRepo.newBuilder()
181 .setName(azureRepo.getName())
182 .setProjectName(azureRepo.getProject().getName());
184 ProjectDto projectDto = sqProjectsKeyByAzureKey.get(ProjectKeyName.from(azureRepo));
185 if (projectDto != null) {
186 builder.setSqProjectName(projectDto.getName());
187 builder.setSqProjectKey(projectDto.getKey());
190 return builder.build();
193 private static BinaryOperator<ProjectDto> resolveNameCollisionOperatorByNaturalOrder() {
194 return (a, b) -> b.getKey().compareTo(a.getKey()) > 0 ? a : b;
197 static class ProjectKeyName {
198 final String projectName;
199 final String repoName;
201 ProjectKeyName(String projectName, String repoName) {
202 this.projectName = projectName;
203 this.repoName = repoName;
206 public static ProjectKeyName from(ProjectAlmSettingDto project) {
207 return new ProjectKeyName(project.getAlmSlug(), project.getAlmRepo());
210 public static ProjectKeyName from(GsonAzureRepo gsonAzureRepo) {
211 return new ProjectKeyName(gsonAzureRepo.getProject().getName(), gsonAzureRepo.getName());
215 public boolean equals(Object o) {
220 if (o == null || getClass() != o.getClass()) {
224 ProjectKeyName that = (ProjectKeyName) o;
225 return Objects.equals(projectName, that.projectName) &&
226 Objects.equals(repoName, that.repoName);
230 public int hashCode() {
231 return Objects.hash(projectName, repoName);