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