]> source.dussan.org Git - sonarqube.git/blob
52ac7f05a487e1fbc6fc10870b38ca83c3fc0995
[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.github;
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.github.GithubApplicationClient;
27 import org.sonar.alm.client.github.GithubApplicationClientImpl;
28 import org.sonar.api.server.ws.WebService;
29 import org.sonar.api.utils.System2;
30 import org.sonar.core.i18n.I18n;
31 import org.sonar.core.platform.EditionProvider;
32 import org.sonar.core.platform.PlatformEditionProvider;
33 import org.sonar.core.util.SequenceUuidFactory;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.alm.setting.AlmSettingDto;
36 import org.sonar.db.component.BranchDto;
37 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
38 import org.sonar.db.permission.GlobalPermission;
39 import org.sonar.db.project.ProjectDto;
40 import org.sonar.db.user.UserDto;
41 import org.sonar.server.almintegration.ws.ImportHelper;
42 import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
43 import org.sonar.server.component.ComponentUpdater;
44 import org.sonar.server.es.TestProjectIndexers;
45 import org.sonar.server.exceptions.NotFoundException;
46 import org.sonar.server.exceptions.UnauthorizedException;
47 import org.sonar.server.favorite.FavoriteUpdater;
48 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
49 import org.sonar.server.permission.PermissionTemplateService;
50 import org.sonar.server.project.DefaultBranchNameResolver;
51 import org.sonar.server.project.ProjectDefaultVisibility;
52 import org.sonar.server.project.Visibility;
53 import org.sonar.server.tester.UserSessionRule;
54 import org.sonar.server.ws.TestRequest;
55 import org.sonar.server.ws.WsActionTester;
56 import org.sonarqube.ws.Projects;
57
58 import static org.assertj.core.api.Assertions.assertThat;
59 import static org.assertj.core.api.Assertions.assertThatThrownBy;
60 import static org.assertj.core.api.Assertions.tuple;
61 import static org.mockito.ArgumentMatchers.any;
62 import static org.mockito.Mockito.mock;
63 import static org.mockito.Mockito.when;
64 import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
65 import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
66 import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
67 import static org.sonar.server.almintegration.ws.ImportHelper.PARAM_ALM_SETTING;
68 import static org.sonar.server.almintegration.ws.github.ImportGithubProjectAction.PARAM_ORGANIZATION;
69 import static org.sonar.server.almintegration.ws.github.ImportGithubProjectAction.PARAM_REPOSITORY_KEY;
70 import static org.sonar.server.tester.UserSessionRule.standalone;
71 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
72 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
73
74 public class ImportGithubProjectActionIT {
75
76   private static final String PROJECT_KEY_NAME = "PROJECT_NAME";
77
78   @Rule
79   public UserSessionRule userSession = standalone();
80
81   private final System2 system2 = mock(System2.class);
82   private final GithubApplicationClientImpl appClient = mock(GithubApplicationClientImpl.class);
83   private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
84
85   @Rule
86   public DbTester db = DbTester.create(system2, true);
87
88
89   private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), System2.INSTANCE,
90     mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), new SequenceUuidFactory(),
91     defaultBranchNameResolver, true);
92
93   private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
94   private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class);
95   private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
96   private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
97   private NewCodeDefinitionResolver newCodeDefinitionResolver = new NewCodeDefinitionResolver(db.getDbClient(), editionProvider);
98   private final WsActionTester ws = new WsActionTester(new ImportGithubProjectAction(db.getDbClient(), userSession,
99     projectDefaultVisibility, appClient, componentUpdater, importHelper, projectKeyGenerator, newCodeDefinitionResolver,
100     defaultBranchNameResolver));
101
102   @Before
103   public void before() {
104     when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
105     when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_MAIN_BRANCH_NAME);
106   }
107
108   @Test
109   public void importProject_ifProjectWithSameNameDoesNotExist_importSucceed() {
110     AlmSettingDto githubAlmSetting = setupAlm();
111     db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSetting.getUuid()).setUserUuid(userSession.getUuid()));
112
113     GithubApplicationClient.Repository repository = new GithubApplicationClient.Repository(1L, PROJECT_KEY_NAME, false,
114       "octocat/" + PROJECT_KEY_NAME,
115       "https://github.sonarsource.com/api/v3/repos/octocat/" + PROJECT_KEY_NAME, "default-branch");
116     when(appClient.getRepository(any(), any(), any(), any())).thenReturn(Optional.of(repository));
117     when(projectKeyGenerator.generateUniqueProjectKey(repository.getFullName())).thenReturn(PROJECT_KEY_NAME);
118
119     Projects.CreateWsResponse response = ws.newRequest()
120       .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
121       .setParam(PARAM_ORGANIZATION, "octocat")
122       .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
123       .executeProtobuf(Projects.CreateWsResponse.class);
124
125     Projects.CreateWsResponse.Project result = response.getProject();
126     assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
127     assertThat(result.getName()).isEqualTo(repository.getName());
128
129     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
130     assertThat(projectDto).isPresent();
131     assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
132     Optional<BranchDto> mainBranch =
133       db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()).stream().filter(BranchDto::isMain).findAny();
134     assertThat(mainBranch).isPresent();
135     assertThat(mainBranch.get().getKey()).isEqualTo("default-branch");
136
137   }
138
139   @Test
140   public void importProject_withNCD_developer_edition() {
141     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
142
143     AlmSettingDto githubAlmSetting = setupAlm();
144     db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSetting.getUuid()).setUserUuid(userSession.getUuid()));
145
146     GithubApplicationClient.Repository repository = new GithubApplicationClient.Repository(1L, PROJECT_KEY_NAME, false,
147       "octocat/" + PROJECT_KEY_NAME,
148       "https://github.sonarsource.com/api/v3/repos/octocat/" + PROJECT_KEY_NAME, "default-branch");
149     when(appClient.getRepository(any(), any(), any(), any())).thenReturn(Optional.of(repository));
150     when(projectKeyGenerator.generateUniqueProjectKey(repository.getFullName())).thenReturn(PROJECT_KEY_NAME);
151
152     Projects.CreateWsResponse response = ws.newRequest()
153       .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
154       .setParam(PARAM_ORGANIZATION, "octocat")
155       .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
156       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
157       .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
158       .executeProtobuf(Projects.CreateWsResponse.class);
159
160     Projects.CreateWsResponse.Project result = response.getProject();
161
162     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
163     assertThat(projectDto).isPresent();
164
165     assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(),  projectDto.get().getUuid()))
166       .isPresent()
167       .get()
168       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
169       .containsExactly(NUMBER_OF_DAYS, "30", null);
170   }
171
172   @Test
173   public void importProject_withNCD_community_edition() {
174     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
175
176     AlmSettingDto githubAlmSetting = setupAlm();
177     db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSetting.getUuid()).setUserUuid(userSession.getUuid()));
178
179     GithubApplicationClient.Repository repository = new GithubApplicationClient.Repository(1L, PROJECT_KEY_NAME, false,
180       "octocat/" + PROJECT_KEY_NAME,
181       "https://github.sonarsource.com/api/v3/repos/octocat/" + PROJECT_KEY_NAME, "default-branch");
182     when(appClient.getRepository(any(), any(), any(), any())).thenReturn(Optional.of(repository));
183     when(projectKeyGenerator.generateUniqueProjectKey(repository.getFullName())).thenReturn(PROJECT_KEY_NAME);
184
185     Projects.CreateWsResponse response = ws.newRequest()
186       .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
187       .setParam(PARAM_ORGANIZATION, "octocat")
188       .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
189       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
190       .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
191       .executeProtobuf(Projects.CreateWsResponse.class);
192
193     Projects.CreateWsResponse.Project result = response.getProject();
194
195     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
196     assertThat(projectDto).isPresent();
197
198     String projectUuid = projectDto.get().getUuid();
199     assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, projectUuid))
200       .isPresent()
201       .get()
202       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
203       .containsExactly(NUMBER_OF_DAYS, "30", projectUuid);
204   }
205
206   @Test
207   public void importProject_reference_branch_ncd_no_default_branch() {
208     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
209     when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn("default-branch");
210
211     AlmSettingDto githubAlmSetting = setupAlm();
212     db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSetting.getUuid()).setUserUuid(userSession.getUuid()));
213
214     GithubApplicationClient.Repository repository = new GithubApplicationClient.Repository(1L, PROJECT_KEY_NAME, false,
215       "octocat/" + PROJECT_KEY_NAME,
216       "https://github.sonarsource.com/api/v3/repos/octocat/" + PROJECT_KEY_NAME, null);
217     when(appClient.getRepository(any(), any(), any(), any())).thenReturn(Optional.of(repository));
218     when(projectKeyGenerator.generateUniqueProjectKey(repository.getFullName())).thenReturn(PROJECT_KEY_NAME);
219
220     Projects.CreateWsResponse response = ws.newRequest()
221       .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
222       .setParam(PARAM_ORGANIZATION, "octocat")
223       .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
224       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "reference_branch")
225       .executeProtobuf(Projects.CreateWsResponse.class);
226
227     Projects.CreateWsResponse.Project result = response.getProject();
228
229     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
230     assertThat(projectDto).isPresent();
231
232     assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
233       .isPresent()
234       .get()
235       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
236       .containsExactly(REFERENCE_BRANCH, "default-branch");
237   }
238
239   @Test
240   public void importProject_reference_branch_ncd() {
241     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
242
243     AlmSettingDto githubAlmSetting = setupAlm();
244     db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSetting.getUuid()).setUserUuid(userSession.getUuid()));
245
246     GithubApplicationClient.Repository repository = new GithubApplicationClient.Repository(1L, PROJECT_KEY_NAME, false,
247       "octocat/" + PROJECT_KEY_NAME,
248       "https://github.sonarsource.com/api/v3/repos/octocat/" + PROJECT_KEY_NAME, "mainBranch");
249     when(appClient.getRepository(any(), any(), any(), any())).thenReturn(Optional.of(repository));
250     when(projectKeyGenerator.generateUniqueProjectKey(repository.getFullName())).thenReturn(PROJECT_KEY_NAME);
251
252     Projects.CreateWsResponse response = ws.newRequest()
253       .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
254       .setParam(PARAM_ORGANIZATION, "octocat")
255       .setParam(PARAM_REPOSITORY_KEY, "octocat/" + PROJECT_KEY_NAME)
256       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "reference_branch")
257       .executeProtobuf(Projects.CreateWsResponse.class);
258
259     Projects.CreateWsResponse.Project result = response.getProject();
260
261     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
262     assertThat(projectDto).isPresent();
263
264     assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
265       .isPresent()
266       .get()
267       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue)
268       .containsExactly(REFERENCE_BRANCH, "mainBranch");
269   }
270
271   @Test
272   public void importProject_ifProjectWithSameNameAlreadyExists_importSucceed() {
273     AlmSettingDto githubAlmSetting = setupAlm();
274     db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSetting.getUuid()).setUserUuid(userSession.getUuid()));
275     db.components().insertPublicProject(p -> p.setKey("Hello-World")).getMainBranchComponent();
276
277     GithubApplicationClient.Repository repository = new GithubApplicationClient.Repository(1L, "Hello-World", false, "Hello-World",
278       "https://github.sonarsource.com/api/v3/repos/octocat/Hello-World", "main");
279     when(appClient.getRepository(any(), any(), any(), any())).thenReturn(Optional.of(repository));
280     when(projectKeyGenerator.generateUniqueProjectKey(repository.getFullName())).thenReturn(PROJECT_KEY_NAME);
281
282     Projects.CreateWsResponse response = ws.newRequest()
283       .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
284       .setParam(PARAM_ORGANIZATION, "octocat")
285       .setParam(PARAM_REPOSITORY_KEY, "Hello-World")
286       .executeProtobuf(Projects.CreateWsResponse.class);
287
288     Projects.CreateWsResponse.Project result = response.getProject();
289     assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
290     assertThat(result.getName()).isEqualTo(repository.getName());
291   }
292
293   @Test
294   public void fail_when_not_logged_in() {
295     TestRequest request = ws.newRequest()
296       .setParam(PARAM_ALM_SETTING, "asdfghjkl")
297       .setParam(PARAM_ORGANIZATION, "test")
298       .setParam(PARAM_REPOSITORY_KEY, "test/repo");
299     assertThatThrownBy(request::execute)
300       .isInstanceOf(UnauthorizedException.class);
301   }
302
303   @Test
304   public void fail_when_missing_create_project_permission() {
305     TestRequest request = ws.newRequest();
306     assertThatThrownBy(request::execute)
307       .isInstanceOf(UnauthorizedException.class);
308   }
309
310   @Test
311   public void fail_when_almSetting_does_not_exist() {
312     UserDto user = db.users().insertUser();
313     userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
314
315     TestRequest request = ws.newRequest()
316       .setParam(PARAM_ALM_SETTING, "unknown")
317       .setParam(PARAM_ORGANIZATION, "test")
318       .setParam(PARAM_REPOSITORY_KEY, "test/repo");
319     assertThatThrownBy(request::execute)
320       .isInstanceOf(NotFoundException.class)
321       .hasMessage("DevOps Platform Setting 'unknown' not found");
322   }
323
324   @Test
325   public void fail_when_personal_access_token_doesnt_exist() {
326     AlmSettingDto githubAlmSetting = setupAlm();
327
328     TestRequest request = ws.newRequest()
329       .setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey())
330       .setParam(PARAM_ORGANIZATION, "test")
331       .setParam(PARAM_REPOSITORY_KEY, "test/repo");
332     assertThatThrownBy(request::execute)
333       .isInstanceOf(IllegalArgumentException.class)
334       .hasMessage("No personal access token found");
335   }
336
337   @Test
338   public void definition() {
339     WebService.Action def = ws.getDef();
340
341     assertThat(def.since()).isEqualTo("8.4");
342     assertThat(def.isPost()).isTrue();
343     assertThat(def.params())
344       .extracting(WebService.Param::key, WebService.Param::isRequired)
345       .containsExactlyInAnyOrder(
346         tuple(PARAM_ALM_SETTING, true),
347         tuple(PARAM_ORGANIZATION, true),
348         tuple(PARAM_REPOSITORY_KEY, true),
349         tuple(PARAM_NEW_CODE_DEFINITION_TYPE, false),
350         tuple(PARAM_NEW_CODE_DEFINITION_VALUE, false));
351   }
352
353   private AlmSettingDto setupAlm() {
354     UserDto user = db.users().insertUser();
355     userSession.logIn(user).addPermission(GlobalPermission.PROVISION_PROJECTS);
356
357     return db.almSettings().insertGitHubAlmSetting(alm -> alm.setClientId("client_123").setClientSecret("client_secret_123"));
358   }
359 }