]> source.dussan.org Git - sonarqube.git/blob
9f1ee1c7a0bbf73f54be3cacfd858a407c2f930c
[sonarqube.git] /
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.azure;
21
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.Optional;
26 import java.util.Set;
27 import java.util.function.BinaryOperator;
28 import java.util.function.Function;
29 import java.util.stream.Collectors;
30 import javax.annotation.Nullable;
31
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;
51
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;
59
60 public class SearchAzureReposAction implements AlmIntegrationsWsAction {
61
62   private static final Logger LOG = LoggerFactory.getLogger(SearchAzureReposAction.class);
63
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";
67
68   private final DbClient dbClient;
69   private final UserSession userSession;
70   private final AzureDevOpsHttpClient azureDevOpsHttpClient;
71
72   public SearchAzureReposAction(DbClient dbClient, UserSession userSession,
73     AzureDevOpsHttpClient azureDevOpsHttpClient) {
74     this.dbClient = dbClient;
75     this.userSession = userSession;
76     this.azureDevOpsHttpClient = azureDevOpsHttpClient;
77   }
78
79   @Override
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")
84       .setPost(false)
85       .setSince("8.6")
86       .setResponseExample(getClass().getResource("example-search_azure_repos.json"))
87       .setHandler(this);
88
89     action.createParam(PARAM_ALM_SETTING)
90       .setRequired(true)
91       .setMaximumLength(200)
92       .setDescription("DevOps Platform setting key");
93     action.createParam(PARAM_PROJECT_NAME)
94       .setRequired(false)
95       .setMaximumLength(200)
96       .setDescription("Project name filter");
97     action.createParam(PARAM_SEARCH_QUERY)
98       .setRequired(false)
99       .setMaximumLength(200)
100       .setDescription("Search query filter");
101   }
102
103   @Override
104   public void handle(Request request, Response response) {
105
106     SearchAzureReposWsResponse wsResponse = doHandle(request);
107     writeProtobuf(wsResponse, request, response);
108
109   }
110
111   private SearchAzureReposWsResponse 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("DevOps Platform 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 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");
126
127       GsonAzureRepoList gsonAzureRepoList = azureDevOpsHttpClient.getRepos(url, pat, projectKey);
128
129       Map<ProjectKeyName, ProjectDto> sqProjectsKeyByAzureKey = getSqProjectsKeyByCustomKey(dbSession, almSettingDto, gsonAzureRepoList);
130
131       List<AzureRepo> repositories = gsonAzureRepoList.getValues()
132         .stream()
133         .filter(r -> isSearchOnlyByProjectName(searchQuery) || doesSearchCriteriaMatchProjectOrRepo(r, searchQuery))
134         .map(repo -> toAzureRepo(repo, sqProjectsKeyByAzureKey))
135         .sorted(comparing(AzureRepo::getName, String::compareToIgnoreCase))
136         .toList();
137
138       LOG.debug(repositories.toString());
139
140       return SearchAzureReposWsResponse.newBuilder()
141         .addAllRepositories(repositories)
142         .build();
143     }
144   }
145
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());
150
151     List<ProjectAlmSettingDto> projectAlmSettingDtos = dbClient.projectAlmSettingDao()
152       .selectByAlmSettingAndSlugs(dbSession, almSettingDto, projectNames);
153
154     Map<String, ProjectAlmSettingDto> filteredProjectsByUuid = projectAlmSettingDtos
155       .stream()
156       .filter(p -> azureProjectsAndRepos.contains(ProjectKeyName.from(p)))
157       .collect(toMap(ProjectAlmSettingDto::getProjectUuid, Function.identity()));
158
159     Set<String> projectUuids = filteredProjectsByUuid.values().stream().map(ProjectAlmSettingDto::getProjectUuid).collect(toSet());
160
161     return dbClient.projectDao().selectByUuids(dbSession, projectUuids)
162       .stream()
163       .collect(Collectors.toMap(
164         projectDto -> ProjectKeyName.from(filteredProjectsByUuid.get(projectDto.getUuid())),
165         p -> p,
166         resolveNameCollisionOperatorByNaturalOrder()));
167   }
168
169   private static boolean isSearchOnlyByProjectName(@Nullable String criteria) {
170     return criteria == null || criteria.isEmpty();
171   }
172
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;
177   }
178
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());
183
184     ProjectDto projectDto = sqProjectsKeyByAzureKey.get(ProjectKeyName.from(azureRepo));
185     if (projectDto != null) {
186       builder.setSqProjectName(projectDto.getName());
187       builder.setSqProjectKey(projectDto.getKey());
188     }
189
190     return builder.build();
191   }
192
193   private static BinaryOperator<ProjectDto> resolveNameCollisionOperatorByNaturalOrder() {
194     return (a, b) -> b.getKey().compareTo(a.getKey()) > 0 ? a : b;
195   }
196
197   static class ProjectKeyName {
198     final String projectName;
199     final String repoName;
200
201     ProjectKeyName(String projectName, String repoName) {
202       this.projectName = projectName;
203       this.repoName = repoName;
204     }
205
206     public static ProjectKeyName from(ProjectAlmSettingDto project) {
207       return new ProjectKeyName(project.getAlmSlug(), project.getAlmRepo());
208     }
209
210     public static ProjectKeyName from(GsonAzureRepo gsonAzureRepo) {
211       return new ProjectKeyName(gsonAzureRepo.getProject().getName(), gsonAzureRepo.getName());
212     }
213
214     @Override
215     public boolean equals(Object o) {
216       if (this == o) {
217         return true;
218       }
219
220       if (o == null || getClass() != o.getClass()) {
221         return false;
222       }
223
224       ProjectKeyName that = (ProjectKeyName) o;
225       return Objects.equals(projectName, that.projectName) &&
226         Objects.equals(repoName, that.repoName);
227     }
228
229     @Override
230     public int hashCode() {
231       return Objects.hash(projectName, repoName);
232     }
233   }
234
235 }