]> source.dussan.org Git - sonarqube.git/blob
7cbc4ab1c1c9595a7b1c536d764c0c07bdce3f83
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.bitbucketcloud;
21
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.bitbucket.bitbucketcloud.BitbucketCloudRestClient;
27 import org.sonar.alm.client.bitbucket.bitbucketcloud.MainBranch;
28 import org.sonar.alm.client.bitbucket.bitbucketcloud.Project;
29 import org.sonar.alm.client.bitbucket.bitbucketcloud.Repository;
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.CreationMethod;
42 import org.sonar.db.project.ProjectDto;
43 import org.sonar.db.user.UserDto;
44 import org.sonar.server.almintegration.ws.ImportHelper;
45 import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
46 import org.sonar.server.component.ComponentUpdater;
47 import org.sonar.server.es.TestIndexers;
48 import org.sonar.server.exceptions.BadRequestException;
49 import org.sonar.server.exceptions.ForbiddenException;
50 import org.sonar.server.exceptions.NotFoundException;
51 import org.sonar.server.exceptions.UnauthorizedException;
52 import org.sonar.server.favorite.FavoriteUpdater;
53 import org.sonar.server.l18n.I18nRule;
54 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
55 import org.sonar.server.permission.PermissionService;
56 import org.sonar.server.permission.PermissionTemplateService;
57 import org.sonar.server.permission.PermissionUpdater;
58 import org.sonar.server.project.DefaultBranchNameResolver;
59 import org.sonar.server.project.ProjectDefaultVisibility;
60 import org.sonar.server.project.Visibility;
61 import org.sonar.server.tester.UserSessionRule;
62 import org.sonar.server.ws.TestRequest;
63 import org.sonar.server.ws.WsActionTester;
64 import org.sonarqube.ws.Projects;
65
66 import static java.util.Objects.requireNonNull;
67 import static org.assertj.core.api.Assertions.assertThat;
68 import static org.assertj.core.api.Assertions.assertThatThrownBy;
69 import static org.assertj.core.api.Assertions.tuple;
70 import static org.mockito.ArgumentMatchers.any;
71 import static org.mockito.Mockito.mock;
72 import static org.mockito.Mockito.verify;
73 import static org.mockito.Mockito.when;
74 import static org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto;
75 import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
76 import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
77 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
78 import static org.sonar.db.permission.GlobalPermission.SCAN;
79 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
80 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
81
82 public class ImportBitbucketCloudRepoActionIT {
83
84   private static final String GENERATED_PROJECT_KEY = "TEST_PROJECT_KEY";
85
86   @Rule
87   public UserSessionRule userSession = UserSessionRule.standalone();
88   @Rule
89   public DbTester db = DbTester.create();
90   @Rule
91   public final I18nRule i18n = new I18nRule();
92
93   private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
94   private final BitbucketCloudRestClient bitbucketCloudRestClient = mock(BitbucketCloudRestClient.class);
95
96   DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
97   private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), i18n, System2.INSTANCE,
98     mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestIndexers(), new SequenceUuidFactory(),
99     defaultBranchNameResolver, mock(PermissionUpdater.class), mock(PermissionService.class));
100
101   private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
102   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 WsActionTester ws = new WsActionTester(new ImportBitbucketCloudRepoAction(db.getDbClient(), userSession,
106     bitbucketCloudRestClient, projectDefaultVisibility, componentUpdater, importHelper, projectKeyGenerator, newCodeDefinitionResolver, defaultBranchNameResolver));
107
108   @Before
109   public void before() {
110     when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
111     when(projectKeyGenerator.generateUniqueProjectKey(any(), any())).thenReturn(GENERATED_PROJECT_KEY);
112   }
113
114   @Test
115   public void import_project() {
116     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
117     Repository repo = mockBitbucketCloudRepo();
118
119     Projects.CreateWsResponse.Project result = callWebServiceAndVerifyProjectCreation(almSetting, repo);
120
121     ProjectDto projectDto = getProjectDto(result);
122     assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_API);
123
124     Optional<ProjectAlmSettingDto> projectAlmSettingDto = db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto);
125     assertThat(projectAlmSettingDto).isPresent();
126     assertThat(projectAlmSettingDto.get().getAlmRepo()).isEqualTo("repo-slug-1");
127
128     Optional<BranchDto> branchDto = db.getDbClient().branchDao().selectByBranchKey(db.getSession(), projectDto.getUuid(), "develop");
129     assertThat(branchDto).isPresent();
130     assertThat(branchDto.get().isMain()).isTrue();
131     verify(projectKeyGenerator).generateUniqueProjectKey(requireNonNull(almSetting.getAppId()), repo.getSlug());
132
133     assertThat(db.getDbClient().newCodePeriodDao().selectAll(db.getSession()))
134       .isEmpty();
135   }
136
137   @Test
138   public void importProject_whenCallIsNotFromBrowser_shouldFlagTheProjectAsCreatedFromApi() {
139     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
140     Repository repo = mockBitbucketCloudRepo();
141
142     Projects.CreateWsResponse.Project result = callWebServiceAndVerifyProjectCreation(almSetting, repo);
143
144     ProjectDto projectDto = getProjectDto(result);
145     assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_API);
146   }
147
148   @Test
149   public void importProject_whenCallIsFromBrowser_shouldFlagTheProjectAsCreatedFromBrowser() {
150     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
151     userSession.flagSessionAsGui();
152     Repository repo = mockBitbucketCloudRepo();
153
154     Projects.CreateWsResponse.Project result = callWebServiceAndVerifyProjectCreation(almSetting, repo);
155
156     ProjectDto projectDto = getProjectDto(result);
157     assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_BROWSER);
158   }
159
160   private Projects.CreateWsResponse.Project callWebServiceAndVerifyProjectCreation(AlmSettingDto almSetting, Repository repo) {
161     Projects.CreateWsResponse response = ws.newRequest()
162       .setParam("almSetting", almSetting.getKey())
163       .setParam("repositorySlug", "repo-slug-1")
164       .executeProtobuf(Projects.CreateWsResponse.class);
165
166     Projects.CreateWsResponse.Project result = response.getProject();
167     assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
168     assertThat(result.getName()).isEqualTo(repo.getName());
169     return result;
170   }
171
172   @Test
173   public void import_project_with_NCD_developer_edition() {
174     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
175
176     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
177     Repository repo = mockBitbucketCloudRepo();
178
179     Projects.CreateWsResponse response = ws.newRequest()
180       .setParam("almSetting", almSetting.getKey())
181       .setParam("repositorySlug", "repo-slug-1")
182       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
183       .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
184       .executeProtobuf(Projects.CreateWsResponse.class);
185
186     Projects.CreateWsResponse.Project result = response.getProject();
187     assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
188     assertThat(result.getName()).isEqualTo(repo.getName());
189
190     ProjectDto projectDto = getProjectDto(result);
191     assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.getUuid()))
192       .isPresent()
193       .get()
194       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
195       .containsExactly(NUMBER_OF_DAYS, "30", null);
196   }
197
198   @Test
199   public void import_project_with_NCD_community_edition() {
200     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
201
202     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
203     Repository repo = mockBitbucketCloudRepo();
204
205     Projects.CreateWsResponse response = ws.newRequest()
206       .setParam("almSetting", almSetting.getKey())
207       .setParam("repositorySlug", "repo-slug-1")
208       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
209       .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
210       .executeProtobuf(Projects.CreateWsResponse.class);
211
212     Projects.CreateWsResponse.Project result = response.getProject();
213     assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
214     assertThat(result.getName()).isEqualTo(repo.getName());
215
216     ProjectDto projectDto = getProjectDto(result);
217     BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.getUuid()).orElseThrow();
218
219     String projectUuid = projectDto.getUuid();
220     assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
221       .isPresent()
222       .get()
223       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
224       .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
225   }
226
227   @Test
228   public void import_project_reference_branch_ncd_no_default_branch_name() {
229     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
230     when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn("default-branch");
231
232     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
233     Repository repo = getGsonBBCRepoWithNoMainBranchName();
234     when(bitbucketCloudRestClient.getRepo(any(), any(), any())).thenReturn(repo);
235
236     Projects.CreateWsResponse response = ws.newRequest()
237       .setParam("almSetting", almSetting.getKey())
238       .setParam("repositorySlug", "repo-slug-1")
239       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "REFERENCE_BRANCH")
240       .executeProtobuf(Projects.CreateWsResponse.class);
241
242     Projects.CreateWsResponse.Project result = response.getProject();
243     assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
244     assertThat(result.getName()).isEqualTo(repo.getName());
245
246     ProjectDto projectDto = getProjectDto(result);
247     assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.getUuid()))
248       .isPresent()
249       .get()
250       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
251       .containsExactly(REFERENCE_BRANCH, "default-branch");
252   }
253
254   @Test
255   public void import_project_reference_branch_NCD() {
256     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
257
258     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
259     Repository repo = mockBitbucketCloudRepo();
260
261     Projects.CreateWsResponse response = ws.newRequest()
262       .setParam("almSetting", almSetting.getKey())
263       .setParam("repositorySlug", "repo-slug-1")
264       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "REFERENCE_BRANCH")
265       .executeProtobuf(Projects.CreateWsResponse.class);
266
267     Projects.CreateWsResponse.Project result = response.getProject();
268     assertThat(result.getKey()).isEqualTo(GENERATED_PROJECT_KEY);
269     assertThat(result.getName()).isEqualTo(repo.getName());
270
271     ProjectDto projectDto = getProjectDto(result);
272     assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.getUuid()))
273       .isPresent()
274       .get()
275       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
276       .containsExactly(REFERENCE_BRANCH, "develop");
277   }
278
279   @Test
280   public void import_project_throw_IAE_when_newCodeDefinitionValue_provided_and_no_newCodeDefinitionType() {
281     AlmSettingDto almSetting = configureUserAndPatAndAlmSettings();
282     mockBitbucketCloudRepo();
283
284     TestRequest request = ws.newRequest()
285       .setParam("almSetting", almSetting.getKey())
286       .setParam("repositorySlug", "repo-slug-1")
287       .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30");
288
289     assertThatThrownBy(() -> request.executeProtobuf(Projects.CreateWsResponse.class))
290       .isInstanceOf(IllegalArgumentException.class)
291       .hasMessage("New code definition type is required when new code definition value is provided");
292   }
293
294   @Test
295   public void fail_project_already_exist() {
296     UserDto user = db.users().insertUser();
297     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
298     AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
299     db.almPats().insert(dto -> {
300       dto.setAlmSettingUuid(almSetting.getUuid());
301       dto.setUserUuid(user.getUuid());
302     });
303     Repository repo = mockBitbucketCloudRepo();
304     db.components().insertPublicProject(p -> p.setKey(GENERATED_PROJECT_KEY)).getMainBranchComponent();
305
306     TestRequest request = ws.newRequest()
307       .setParam("almSetting", almSetting.getKey())
308       .setParam("repositorySlug", "repo-slug-1");
309
310     assertThatThrownBy(request::execute)
311       .isInstanceOf(BadRequestException.class)
312       .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s\"", GENERATED_PROJECT_KEY, GENERATED_PROJECT_KEY);
313   }
314
315   @Test
316   public void fail_when_not_logged_in() {
317     TestRequest request = ws.newRequest()
318       .setParam("almSetting", "sdgfdshfjztutz")
319       .setParam("projectKey", "projectKey")
320       .setParam("repositorySlug", "repo-slug");
321
322     assertThatThrownBy(request::execute)
323       .isInstanceOf(UnauthorizedException.class);
324   }
325
326   @Test
327   public void fail_when_missing_project_creator_permission() {
328     UserDto user = db.users().insertUser();
329     userSession.logIn(user).addPermission(SCAN);
330
331     TestRequest request = ws.newRequest()
332       .setParam("almSetting", "sdgfdshfjztutz")
333       .setParam("projectKey", "projectKey")
334       .setParam("repositorySlug", "repo-slug");
335
336     assertThatThrownBy(request::execute)
337       .isInstanceOf(ForbiddenException.class)
338       .hasMessageContaining("Insufficient privileges");
339   }
340
341   @Test
342   public void check_pat_is_missing() {
343     UserDto user = db.users().insertUser();
344     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
345     AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
346
347     TestRequest request = ws.newRequest()
348       .setParam("almSetting", almSetting.getKey())
349       .setParam("repositorySlug", "repo");
350
351     assertThatThrownBy(request::execute)
352       .isInstanceOf(IllegalArgumentException.class)
353       .hasMessageContaining("Username and App Password for '" + almSetting.getKey() + "' is missing");
354   }
355
356   @Test
357   public void fail_check_alm_setting_not_found() {
358     UserDto user = db.users().insertUser();
359     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
360     AlmPatDto almPatDto = newAlmPatDto();
361     db.getDbClient().almPatDao().insert(db.getSession(), almPatDto, user.getLogin(), null);
362
363     TestRequest request = ws.newRequest()
364       .setParam("almSetting", "testKey")
365       .setParam("repositorySlug", "repo");
366
367     assertThatThrownBy(request::execute)
368       .isInstanceOf(NotFoundException.class)
369       .hasMessageContaining("DevOps Platform Setting 'testKey' not found");
370   }
371
372   @Test
373   public void fail_when_no_creation_project_permission() {
374     UserDto user = db.users().insertUser();
375     userSession.logIn(user);
376
377     TestRequest request = ws.newRequest()
378       .setParam("almSetting", "anyvalue");
379
380     assertThatThrownBy(request::execute)
381       .isInstanceOf(ForbiddenException.class)
382       .hasMessageContaining("Insufficient privileges");
383   }
384
385   @Test
386   public void definition() {
387     WebService.Action def = ws.getDef();
388
389     assertThat(def.since()).isEqualTo("9.0");
390     assertThat(def.isPost()).isTrue();
391     assertThat(def.params())
392       .extracting(WebService.Param::key, WebService.Param::isRequired)
393       .containsExactlyInAnyOrder(
394         tuple("almSetting", true),
395         tuple("repositorySlug", true),
396         tuple(PARAM_NEW_CODE_DEFINITION_TYPE, false),
397         tuple(PARAM_NEW_CODE_DEFINITION_VALUE, false));
398   }
399
400   private AlmSettingDto configureUserAndPatAndAlmSettings() {
401     UserDto user = db.users().insertUser();
402     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
403     AlmSettingDto almSetting = db.almSettings().insertBitbucketCloudAlmSetting();
404     db.almPats().insert(dto -> {
405       dto.setAlmSettingUuid(almSetting.getUuid());
406       dto.setUserUuid(user.getUuid());
407     });
408     return almSetting;
409   }
410
411   private Repository mockBitbucketCloudRepo() {
412     Project project1 = new Project("PROJECT-UUID-ONE", "projectKey1", "projectName1");
413     MainBranch mainBranch = new MainBranch("branch", "develop");
414     Repository repo = new Repository("REPO-UUID-ONE", "repo-slug-1", "repoName1", project1, mainBranch);
415     when(bitbucketCloudRestClient.getRepo(any(), any(), any())).thenReturn(repo);
416     return repo;
417   }
418
419   private Repository getGsonBBCRepoWithNoMainBranchName() {
420     Project project1 = new Project("PROJECT-UUID-ONE", "projectKey1", "projectName1");
421     MainBranch mainBranch = new MainBranch("branch", null);
422     return new Repository("REPO-UUID-ONE", "repo-slug-1", "repoName1", project1, mainBranch);
423   }
424
425   private ProjectDto getProjectDto(Projects.CreateWsResponse.Project result) {
426     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
427     assertThat(projectDto).isPresent();
428     return projectDto.orElseThrow();
429   }
430
431 }