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 assertThat(projectDto).isPresent();
230 String projectUuid = projectDto.get().getUuid();
231 assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, projectUuid))
234 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
235 .containsExactly(NUMBER_OF_DAYS, "30", projectUuid);
239 public void import_project_throw_IAE_when_newCodeDefinitionValue_provided_and_no_newCodeDefinitionType() {
240 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
242 UserDto user = db.users().insertUser();
243 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
244 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
245 db.almPats().insert(dto -> {
246 dto.setAlmSettingUuid(almSetting.getUuid());
247 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
248 dto.setUserUuid(user.getUuid());
250 GsonAzureRepo repo = getGsonAzureRepo();
251 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
252 "project-name", "repo-name"))
255 Projects.CreateWsResponse response = ws.newRequest()
256 .setParam("almSetting", almSetting.getKey())
257 .setParam("projectName", "project-name")
258 .setParam("repositoryName", "repo-name")
259 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
260 .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
261 .executeProtobuf(Projects.CreateWsResponse.class);
263 Projects.CreateWsResponse.Project result = response.getProject();
265 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
266 assertThat(projectDto).isPresent();
268 assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
271 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
272 .containsExactly(NUMBER_OF_DAYS, "30", null);
276 public void import_project_reference_branch_ncd_no_default_branch_name() {
277 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
279 UserDto user = db.users().insertUser();
280 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
281 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
282 db.almPats().insert(dto -> {
283 dto.setAlmSettingUuid(almSetting.getUuid());
284 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
285 dto.setUserUuid(user.getUuid());
287 GsonAzureRepo repo = getEmptyGsonAzureRepo();
288 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
289 "project-name", "repo-name"))
292 Projects.CreateWsResponse response = ws.newRequest()
293 .setParam("almSetting", almSetting.getKey())
294 .setParam("projectName", "project-name")
295 .setParam("repositoryName", "repo-name")
296 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "REFERENCE_BRANCH")
297 .executeProtobuf(Projects.CreateWsResponse.class);
299 Projects.CreateWsResponse.Project result = response.getProject();
301 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
302 assertThat(projectDto).isPresent();
304 assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
307 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
308 .containsExactly(REFERENCE_BRANCH, DEFAULT_MAIN_BRANCH_NAME);
312 public void import_project_reference_branch_ncd() {
313 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
315 UserDto user = db.users().insertUser();
316 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
317 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
318 db.almPats().insert(dto -> {
319 dto.setAlmSettingUuid(almSetting.getUuid());
320 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
321 dto.setUserUuid(user.getUuid());
323 GsonAzureRepo repo = getGsonAzureRepo();
324 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
325 "project-name", "repo-name"))
328 Projects.CreateWsResponse response = ws.newRequest()
329 .setParam("almSetting", almSetting.getKey())
330 .setParam("projectName", "project-name")
331 .setParam("repositoryName", "repo-name")
332 .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "REFERENCE_BRANCH")
333 .executeProtobuf(Projects.CreateWsResponse.class);
335 Projects.CreateWsResponse.Project result = response.getProject();
337 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
338 assertThat(projectDto).isPresent();
340 assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
343 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
344 .containsExactly(REFERENCE_BRANCH, "repo-default-branch");
348 public void import_project_from_empty_repo() {
349 UserDto user = db.users().insertUser();
350 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
351 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
352 db.almPats().insert(dto -> {
353 dto.setAlmSettingUuid(almSetting.getUuid());
354 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
355 dto.setUserUuid(user.getUuid());
357 GsonAzureRepo repo = getEmptyGsonAzureRepo();
358 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
359 "project-name", "repo-name"))
362 TestRequest request = ws.newRequest()
363 .setParam("almSetting", almSetting.getKey())
364 .setParam("projectName", "project-name")
365 .setParam("repositoryName", "repo-name")
366 .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30");
368 assertThatThrownBy(() -> request.executeProtobuf(Projects.CreateWsResponse.class))
369 .isInstanceOf(IllegalArgumentException.class)
370 .hasMessage("New code definition type is required when new code definition value is provided");
374 public void fail_when_not_logged_in() {
375 TestRequest request = ws.newRequest()
376 .setParam("almSetting", "azure")
377 .setParam("projectName", "project-name")
378 .setParam("repositoryName", "repo-name");
380 assertThatThrownBy(request::execute)
381 .isInstanceOf(UnauthorizedException.class);
385 public void fail_when_missing_project_creator_permission() {
386 UserDto user = db.users().insertUser();
387 userSession.logIn(user).addPermission(SCAN);
389 TestRequest request = ws.newRequest()
390 .setParam("almSetting", "azure")
391 .setParam("projectName", "project-name")
392 .setParam("repositoryName", "repo-name");
394 assertThatThrownBy(request::execute)
395 .isInstanceOf(ForbiddenException.class)
396 .hasMessage("Insufficient privileges");
400 public void check_pat_is_missing() {
401 UserDto user = db.users().insertUser();
402 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
403 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
405 TestRequest request = ws.newRequest()
406 .setParam("almSetting", almSetting.getKey())
407 .setParam("projectName", "project-name")
408 .setParam("repositoryName", "repo-name");
410 assertThatThrownBy(request::execute)
411 .isInstanceOf(IllegalArgumentException.class)
412 .hasMessage("personal access token for '" + almSetting.getKey() + "' is missing");
416 public void fail_check_alm_setting_not_found() {
417 UserDto user = db.users().insertUser();
418 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
419 AlmPatDto almPatDto = newAlmPatDto();
420 db.getDbClient().almPatDao().insert(db.getSession(), almPatDto, user.getLogin(), null);
422 TestRequest request = ws.newRequest()
423 .setParam("almSetting", "testKey");
425 assertThatThrownBy(request::execute)
426 .isInstanceOf(NotFoundException.class)
427 .hasMessage("DevOps Platform Setting 'testKey' not found");
431 public void fail_project_already_exists() {
432 UserDto user = db.users().insertUser();
433 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
434 AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
435 db.almPats().insert(dto -> {
436 dto.setAlmSettingUuid(almSetting.getUuid());
437 dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
438 dto.setUserUuid(user.getUuid());
440 GsonAzureRepo repo = getGsonAzureRepo();
441 db.components().insertPublicProject(p -> p.setKey(GENERATED_PROJECT_KEY)).getMainBranchComponent();
443 when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption),
444 "project-name", "repo-name")).thenReturn(repo);
445 TestRequest request = ws.newRequest()
446 .setParam("almSetting", almSetting.getKey())
447 .setParam("projectName", "project-name")
448 .setParam("repositoryName", "repo-name");
450 assertThatThrownBy(request::execute)
451 .isInstanceOf(BadRequestException.class)
452 .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s\"", GENERATED_PROJECT_KEY,
453 GENERATED_PROJECT_KEY);
457 public void define() {
458 WebService.Action def = ws.getDef();
460 assertThat(def.since()).isEqualTo("8.6");
461 assertThat(def.isPost()).isTrue();
462 assertThat(def.params())
463 .extracting(WebService.Param::key, WebService.Param::isRequired)
464 .containsExactlyInAnyOrder(
465 tuple("almSetting", true),
466 tuple("projectName", true),
467 tuple("repositoryName", true),
468 tuple(PARAM_NEW_CODE_DEFINITION_TYPE, false),
469 tuple(PARAM_NEW_CODE_DEFINITION_VALUE, false));
472 private GsonAzureRepo getGsonAzureRepo() {
473 return new GsonAzureRepo("repo-id", "repo-name", "repo-url",
474 new GsonAzureProject("project-name", "project-description"),
475 "refs/heads/repo-default-branch");
478 private GsonAzureRepo getEmptyGsonAzureRepo() {
479 return new GsonAzureRepo("repo-id", "repo-name", "repo-url",
480 new GsonAzureProject("project-name", "project-description"), null);