3 * Copyright (C) 2009-2023 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.Optional;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.alm.client.azure.AzureDevOpsHttpClient;
27 import org.sonar.alm.client.azure.GsonAzureProject;
28 import org.sonar.alm.client.azure.GsonAzureRepo;
29 import org.sonar.api.config.internal.Encryption;
30 import org.sonar.api.server.ws.WebService;
31 import org.sonar.api.utils.System2;
32 import org.sonar.core.platform.EditionProvider;
33 import org.sonar.core.platform.PlatformEditionProvider;
34 import org.sonar.core.util.SequenceUuidFactory;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.alm.pat.AlmPatDto;
37 import org.sonar.db.alm.setting.AlmSettingDto;
38 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
39 import org.sonar.db.component.BranchDto;
40 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
41 import org.sonar.db.project.ProjectDto;
42 import org.sonar.db.user.UserDto;
43 import org.sonar.server.almintegration.ws.ImportHelper;
44 import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
45 import org.sonar.server.component.ComponentUpdater;
46 import org.sonar.server.es.TestIndexers;
47 import org.sonar.server.exceptions.BadRequestException;
48 import org.sonar.server.exceptions.ForbiddenException;
49 import org.sonar.server.exceptions.NotFoundException;
50 import org.sonar.server.exceptions.UnauthorizedException;
51 import org.sonar.server.favorite.FavoriteUpdater;
52 import org.sonar.server.l18n.I18nRule;
53 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
54 import org.sonar.server.permission.PermissionTemplateService;
55 import org.sonar.server.project.DefaultBranchNameResolver;
56 import org.sonar.server.project.ProjectDefaultVisibility;
57 import org.sonar.server.project.Visibility;
58 import org.sonar.server.tester.UserSessionRule;
59 import org.sonar.server.ws.TestRequest;
60 import org.sonar.server.ws.WsActionTester;
61 import org.sonarqube.ws.Projects;
63 import static org.assertj.core.api.Assertions.assertThat;
64 import static org.assertj.core.api.Assertions.assertThatThrownBy;
65 import static org.assertj.core.api.Assertions.tuple;
66 import static org.mockito.ArgumentMatchers.any;
67 import static org.mockito.Mockito.mock;
68 import static org.mockito.Mockito.verify;
69 import static org.mockito.Mockito.when;
70 import static org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto;
71 import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
72 import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
73 import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
74 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
75 import static org.sonar.db.permission.GlobalPermission.SCAN;
76 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
77 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
79 public class ImportAzureProjectActionIT {
81 private static final String GENERATED_PROJECT_KEY = "TEST_PROJECT_KEY";
84 public UserSessionRule userSession = UserSessionRule.standalone();
86 public DbTester db = DbTester.create();
88 public final I18nRule i18n = new I18nRule();
90 private final AzureDevOpsHttpClient azureDevOpsHttpClient = mock(AzureDevOpsHttpClient.class);
92 private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
94 private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), i18n, System2.INSTANCE,
95 mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestIndexers(), new SequenceUuidFactory(),
96 defaultBranchNameResolver, true);
98 private final Encryption encryption = mock(Encryption.class);
99 private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
100 private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
101 private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class);
103 private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
104 private NewCodeDefinitionResolver newCodeDefinitionResolver = new NewCodeDefinitionResolver(db.getDbClient(), editionProvider);
105 private final ImportAzureProjectAction importAzureProjectAction = new ImportAzureProjectAction(db.getDbClient(), userSession,
106 azureDevOpsHttpClient, projectDefaultVisibility, componentUpdater, importHelper, projectKeyGenerator, newCodeDefinitionResolver,
107 defaultBranchNameResolver);
108 private final WsActionTester ws = new WsActionTester(importAzureProjectAction);
111 public void before() {
112 when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
113 when(projectKeyGenerator.generateUniqueProjectKey(any(), any())).thenReturn(GENERATED_PROJECT_KEY);
114 when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_MAIN_BRANCH_NAME);
118 public void import_project() {
119 UserDto user = db.users().insertUser();
120 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
121 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
122 db.almPats().insert(dto -> {
123 dto.setAlmSettingUuid(almSetting.getUuid());
124 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
125 dto.setUserUuid(user.getUuid());
127 GsonAzureRepo repo = getGsonAzureRepo();
128 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
129 "project-name", "repo-name"))
132 Projects.CreateWsResponse response = ws.newRequest()
133 .setParam("almSetting", almSetting.getKey())
134 .setParam("projectName", "project-name")
135 .setParam("repositoryName", "repo-name")
136 .executeProtobuf(Projects.CreateWsResponse.class);
138 Projects.CreateWsResponse.Project result = response.getProject();
139 assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
140 assertThat(result.getName()).isEqualTo(repo.getName());
142 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
143 assertThat(projectDto).isPresent();
145 Optional<ProjectAlmSettingDto> projectAlmSettingDto = db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(),
147 assertThat(projectAlmSettingDto.get().getAlmRepo()).isEqualTo("repo-name");
148 assertThat(projectAlmSettingDto.get().getAlmSettingUuid()).isEqualTo(almSetting.getUuid());
149 assertThat(projectAlmSettingDto.get().getAlmSlug()).isEqualTo("project-name");
151 Optional<BranchDto> mainBranch = db.getDbClient()
153 .selectByProject(db.getSession(), projectDto.get())
155 .filter(BranchDto::isMain)
157 assertThat(mainBranch).isPresent();
158 assertThat(mainBranch.get().getKey()).hasToString("repo-default-branch");
160 verify(projectKeyGenerator).generateUniqueProjectKey(repo.getProject().getName(), repo.getName());
164 public void import_project_with_NCD_developer_edition() {
165 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
167 UserDto user = db.users().insertUser();
168 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
169 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
170 db.almPats().insert(dto -> {
171 dto.setAlmSettingUuid(almSetting.getUuid());
172 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
173 dto.setUserUuid(user.getUuid());
175 GsonAzureRepo repo = getGsonAzureRepo();
176 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
177 "project-name", "repo-name"))
180 Projects.CreateWsResponse response = ws.newRequest()
181 .setParam("almSetting", almSetting.getKey())
182 .setParam("projectName", "project-name")
183 .setParam("repositoryName", "repo-name")
184 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
185 .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
186 .executeProtobuf(Projects.CreateWsResponse.class);
188 Projects.CreateWsResponse.Project result = response.getProject();
190 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
191 assertThat(projectDto).isPresent();
193 assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
196 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
197 .containsExactly(NUMBER_OF_DAYS, "30", null);
201 public void import_project_with_NCD_community_edition() {
202 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
204 UserDto user = db.users().insertUser();
205 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
206 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
207 db.almPats().insert(dto -> {
208 dto.setAlmSettingUuid(almSetting.getUuid());
209 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
210 dto.setUserUuid(user.getUuid());
212 GsonAzureRepo repo = getGsonAzureRepo();
213 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
214 "project-name", "repo-name"))
217 Projects.CreateWsResponse response = ws.newRequest()
218 .setParam("almSetting", almSetting.getKey())
219 .setParam("projectName", "project-name")
220 .setParam("repositoryName", "repo-name")
221 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
222 .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
223 .executeProtobuf(Projects.CreateWsResponse.class);
225 Projects.CreateWsResponse.Project result = response.getProject();
227 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
228 BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.get().getUuid()).orElseThrow();
229 assertThat(projectDto).isPresent();
231 String projectUuid = projectDto.get().getUuid();
232 assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
235 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
236 .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
240 public void import_project_throw_IAE_when_newCodeDefinitionValue_provided_and_no_newCodeDefinitionType() {
241 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
243 UserDto user = db.users().insertUser();
244 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
245 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
246 db.almPats().insert(dto -> {
247 dto.setAlmSettingUuid(almSetting.getUuid());
248 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
249 dto.setUserUuid(user.getUuid());
251 GsonAzureRepo repo = getGsonAzureRepo();
252 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
253 "project-name", "repo-name"))
256 Projects.CreateWsResponse response = ws.newRequest()
257 .setParam("almSetting", almSetting.getKey())
258 .setParam("projectName", "project-name")
259 .setParam("repositoryName", "repo-name")
260 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
261 .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
262 .executeProtobuf(Projects.CreateWsResponse.class);
264 Projects.CreateWsResponse.Project result = response.getProject();
266 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
267 assertThat(projectDto).isPresent();
269 assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
272 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
273 .containsExactly(NUMBER_OF_DAYS, "30", null);
277 public void import_project_reference_branch_ncd_no_default_branch_name() {
278 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
280 UserDto user = db.users().insertUser();
281 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
282 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
283 db.almPats().insert(dto -> {
284 dto.setAlmSettingUuid(almSetting.getUuid());
285 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
286 dto.setUserUuid(user.getUuid());
288 GsonAzureRepo repo = getEmptyGsonAzureRepo();
289 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
290 "project-name", "repo-name"))
293 Projects.CreateWsResponse response = ws.newRequest()
294 .setParam("almSetting", almSetting.getKey())
295 .setParam("projectName", "project-name")
296 .setParam("repositoryName", "repo-name")
297 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "REFERENCE_BRANCH")
298 .executeProtobuf(Projects.CreateWsResponse.class);
300 Projects.CreateWsResponse.Project result = response.getProject();
302 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
303 assertThat(projectDto).isPresent();
305 assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
308 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
309 .containsExactly(REFERENCE_BRANCH, DEFAULT_MAIN_BRANCH_NAME);
313 public void import_project_reference_branch_ncd() {
314 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
316 UserDto user = db.users().insertUser();
317 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
318 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
319 db.almPats().insert(dto -> {
320 dto.setAlmSettingUuid(almSetting.getUuid());
321 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
322 dto.setUserUuid(user.getUuid());
324 GsonAzureRepo repo = getGsonAzureRepo();
325 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
326 "project-name", "repo-name"))
329 Projects.CreateWsResponse response = ws.newRequest()
330 .setParam("almSetting", almSetting.getKey())
331 .setParam("projectName", "project-name")
332 .setParam("repositoryName", "repo-name")
333 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "REFERENCE_BRANCH")
334 .executeProtobuf(Projects.CreateWsResponse.class);
336 Projects.CreateWsResponse.Project result = response.getProject();
338 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
339 assertThat(projectDto).isPresent();
341 assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
344 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
345 .containsExactly(REFERENCE_BRANCH, "repo-default-branch");
349 public void import_project_from_empty_repo() {
350 UserDto user = db.users().insertUser();
351 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
352 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
353 db.almPats().insert(dto -> {
354 dto.setAlmSettingUuid(almSetting.getUuid());
355 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
356 dto.setUserUuid(user.getUuid());
358 GsonAzureRepo repo = getEmptyGsonAzureRepo();
359 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
360 "project-name", "repo-name"))
363 TestRequest request = ws.newRequest()
364 .setParam("almSetting", almSetting.getKey())
365 .setParam("projectName", "project-name")
366 .setParam("repositoryName", "repo-name")
367 .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30");
369 assertThatThrownBy(() -> request.executeProtobuf(Projects.CreateWsResponse.class))
370 .isInstanceOf(IllegalArgumentException.class)
371 .hasMessage("New code definition type is required when new code definition value is provided");
375 public void fail_when_not_logged_in() {
376 TestRequest request = ws.newRequest()
377 .setParam("almSetting", "azure")
378 .setParam("projectName", "project-name")
379 .setParam("repositoryName", "repo-name");
381 assertThatThrownBy(request::execute)
382 .isInstanceOf(UnauthorizedException.class);
386 public void fail_when_missing_project_creator_permission() {
387 UserDto user = db.users().insertUser();
388 userSession.logIn(user).addPermission(SCAN);
390 TestRequest request = ws.newRequest()
391 .setParam("almSetting", "azure")
392 .setParam("projectName", "project-name")
393 .setParam("repositoryName", "repo-name");
395 assertThatThrownBy(request::execute)
396 .isInstanceOf(ForbiddenException.class)
397 .hasMessage("Insufficient privileges");
401 public void check_pat_is_missing() {
402 UserDto user = db.users().insertUser();
403 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
404 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
406 TestRequest request = ws.newRequest()
407 .setParam("almSetting", almSetting.getKey())
408 .setParam("projectName", "project-name")
409 .setParam("repositoryName", "repo-name");
411 assertThatThrownBy(request::execute)
412 .isInstanceOf(IllegalArgumentException.class)
413 .hasMessage("personal access token for '" + almSetting.getKey() + "' is missing");
417 public void fail_check_alm_setting_not_found() {
418 UserDto user = db.users().insertUser();
419 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
420 AlmPatDto almPatDto = newAlmPatDto();
421 db.getDbClient().almPatDao().insert(db.getSession(), almPatDto, user.getLogin(), null);
423 TestRequest request = ws.newRequest()
424 .setParam("almSetting", "testKey");
426 assertThatThrownBy(request::execute)
427 .isInstanceOf(NotFoundException.class)
428 .hasMessage("DevOps Platform Setting 'testKey' not found");
432 public void fail_project_already_exists() {
433 UserDto user = db.users().insertUser();
434 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
435 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
436 db.almPats().insert(dto -> {
437 dto.setAlmSettingUuid(almSetting.getUuid());
438 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
439 dto.setUserUuid(user.getUuid());
441 GsonAzureRepo repo = getGsonAzureRepo();
442 db.components().insertPublicProject(p -> p.setKey(GENERATED_PROJECT_KEY)).getMainBranchComponent();
444 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
445 "project-name", "repo-name")).thenReturn(repo);
446 TestRequest request = ws.newRequest()
447 .setParam("almSetting", almSetting.getKey())
448 .setParam("projectName", "project-name")
449 .setParam("repositoryName", "repo-name");
451 assertThatThrownBy(request::execute)
452 .isInstanceOf(BadRequestException.class)
453 .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s\"", GENERATED_PROJECT_KEY,
454 GENERATED_PROJECT_KEY);
458 public void define() {
459 WebService.Action def = ws.getDef();
461 assertThat(def.since()).isEqualTo("8.6");
462 assertThat(def.isPost()).isTrue();
463 assertThat(def.params())
464 .extracting(WebService.Param::key, WebService.Param::isRequired)
465 .containsExactlyInAnyOrder(
466 tuple("almSetting", true),
467 tuple("projectName", true),
468 tuple("repositoryName", true),
469 tuple(PARAM_NEW_CODE_DEFINITION_TYPE, false),
470 tuple(PARAM_NEW_CODE_DEFINITION_VALUE, false));
473 private GsonAzureRepo getGsonAzureRepo() {
474 return new GsonAzureRepo("repo-id", "repo-name", "repo-url",
475 new GsonAzureProject("project-name", "project-description"),
476 "refs/heads/repo-default-branch");
479 private GsonAzureRepo getEmptyGsonAzureRepo() {
480 return new GsonAzureRepo("repo-id", "repo-name", "repo-url",
481 new GsonAzureProject("project-name", "project-description"), null);