]> source.dussan.org Git - sonarqube.git/blob
a2748b8af4254d020a43d6505ab40ffa2c3583ab
[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.gitlab;
21
22 import java.util.Optional;
23 import org.assertj.core.api.Assertions;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.alm.client.gitlab.GitLabBranch;
28 import org.sonar.alm.client.gitlab.GitlabHttpClient;
29 import org.sonar.alm.client.gitlab.Project;
30 import org.sonar.api.utils.System2;
31 import org.sonar.core.i18n.I18n;
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.setting.AlmSettingDto;
37 import org.sonar.db.component.BranchDto;
38 import org.sonar.db.newcodeperiod.NewCodePeriodDto;
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.favorite.FavoriteUpdater;
46 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
47 import org.sonar.server.permission.PermissionTemplateService;
48 import org.sonar.server.project.DefaultBranchNameResolver;
49 import org.sonar.server.project.ProjectDefaultVisibility;
50 import org.sonar.server.project.Visibility;
51 import org.sonar.server.tester.UserSessionRule;
52 import org.sonar.server.ws.WsActionTester;
53 import org.sonarqube.ws.Projects;
54
55 import static java.util.Collections.emptyList;
56 import static java.util.Collections.singletonList;
57 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
58 import static org.assertj.core.api.Assertions.assertThat;
59 import static org.assertj.core.api.Assertions.tuple;
60 import static org.mockito.ArgumentMatchers.any;
61 import static org.mockito.Mockito.mock;
62 import static org.mockito.Mockito.verify;
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.permission.GlobalPermission.PROVISION_PROJECTS;
67 import static org.sonar.server.tester.UserSessionRule.standalone;
68 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_TYPE;
69 import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NEW_CODE_DEFINITION_VALUE;
70
71 public class ImportGitLabProjectActionIT {
72
73   private static final String PROJECT_KEY_NAME = "PROJECT_NAME";
74
75   private final System2 system2 = mock(System2.class);
76
77   @Rule
78   public UserSessionRule userSession = standalone();
79
80   @Rule
81   public DbTester db = DbTester.create(system2, true);
82
83   DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
84
85   private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), System2.INSTANCE,
86     mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), new SequenceUuidFactory(),
87     defaultBranchNameResolver, true);
88
89   private final GitlabHttpClient gitlabHttpClient = mock(GitlabHttpClient.class);
90   private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
91   private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
92   private final ProjectKeyGenerator projectKeyGenerator = mock(ProjectKeyGenerator.class);
93   private PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
94   private NewCodeDefinitionResolver newCodeDefinitionResolver = new NewCodeDefinitionResolver(db.getDbClient(), editionProvider);
95   private final ImportGitLabProjectAction importGitLabProjectAction = new ImportGitLabProjectAction(
96     db.getDbClient(), userSession, projectDefaultVisibility, gitlabHttpClient, componentUpdater, importHelper, projectKeyGenerator, newCodeDefinitionResolver,
97     defaultBranchNameResolver);
98   private final WsActionTester ws = new WsActionTester(importGitLabProjectAction);
99
100   @Before
101   public void before() {
102     when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
103     when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_MAIN_BRANCH_NAME);
104   }
105
106   @Test
107   public void import_project_developer_edition() {
108     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
109
110     UserDto user = db.users().insertUser();
111     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
112     AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
113     db.almPats().insert(dto -> {
114       dto.setAlmSettingUuid(almSetting.getUuid());
115       dto.setUserUuid(user.getUuid());
116       dto.setPersonalAccessToken("PAT");
117     });
118     Project project = getGitlabProject();
119     when(gitlabHttpClient.getProject(any(), any(), any())).thenReturn(project);
120     when(gitlabHttpClient.getBranches(any(), any(), any())).thenReturn(singletonList(new GitLabBranch("master", true)));
121     when(projectKeyGenerator.generateUniqueProjectKey(project.getPathWithNamespace())).thenReturn(PROJECT_KEY_NAME);
122
123     Projects.CreateWsResponse response = ws.newRequest()
124       .setParam("almSetting", almSetting.getKey())
125       .setParam("gitlabProjectId", "12345")
126       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
127       .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
128       .executeProtobuf(Projects.CreateWsResponse.class);
129
130     verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
131
132     Projects.CreateWsResponse.Project result = response.getProject();
133     assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
134     assertThat(result.getName()).isEqualTo(project.getName());
135
136     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
137     assertThat(projectDto).isPresent();
138     assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
139
140     assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(),  projectDto.get().getUuid()))
141       .isPresent()
142       .get()
143       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
144       .containsExactly(NUMBER_OF_DAYS, "30", null);
145   }
146
147   @Test
148   public void import_project_community_edition() {
149     when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
150
151     UserDto user = db.users().insertUser();
152     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
153     AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
154     db.almPats().insert(dto -> {
155       dto.setAlmSettingUuid(almSetting.getUuid());
156       dto.setUserUuid(user.getUuid());
157       dto.setPersonalAccessToken("PAT");
158     });
159     Project project = getGitlabProject();
160     when(gitlabHttpClient.getProject(any(), any(), any())).thenReturn(project);
161     when(gitlabHttpClient.getBranches(any(), any(), any())).thenReturn(singletonList(new GitLabBranch("master", true)));
162     when(projectKeyGenerator.generateUniqueProjectKey(project.getPathWithNamespace())).thenReturn(PROJECT_KEY_NAME);
163
164     Projects.CreateWsResponse response = ws.newRequest()
165       .setParam("almSetting", almSetting.getKey())
166       .setParam("gitlabProjectId", "12345")
167       .setParam(PARAM_NEW_CODE_DEFINITION_TYPE, "NUMBER_OF_DAYS")
168       .setParam(PARAM_NEW_CODE_DEFINITION_VALUE, "30")
169       .executeProtobuf(Projects.CreateWsResponse.class);
170
171     Projects.CreateWsResponse.Project result = response.getProject();
172
173     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
174
175     String projectUuid = projectDto.get().getUuid();
176     assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, projectUuid))
177       .isPresent()
178       .get()
179       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
180       .containsExactly(NUMBER_OF_DAYS, "30", projectUuid);
181   }
182
183   @Test
184   public void import_project_with_specific_different_default_branch() {
185     UserDto user = db.users().insertUser();
186     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
187     AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
188     db.almPats().insert(dto -> {
189       dto.setAlmSettingUuid(almSetting.getUuid());
190       dto.setUserUuid(user.getUuid());
191       dto.setPersonalAccessToken("PAT");
192     });
193     Project project = getGitlabProject();
194     when(gitlabHttpClient.getProject(any(), any(), any())).thenReturn(project);
195     when(gitlabHttpClient.getBranches(any(), any(), any())).thenReturn(singletonList(new GitLabBranch("main", true)));
196     when(projectKeyGenerator.generateUniqueProjectKey(project.getPathWithNamespace())).thenReturn(PROJECT_KEY_NAME);
197
198     Projects.CreateWsResponse response = ws.newRequest()
199       .setParam("almSetting", almSetting.getKey())
200       .setParam("gitlabProjectId", "12345")
201       .executeProtobuf(Projects.CreateWsResponse.class);
202
203     verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
204     verify(gitlabHttpClient).getBranches(almSetting.getUrl(), "PAT", 12345L);
205
206     Projects.CreateWsResponse.Project result = response.getProject();
207     assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
208     assertThat(result.getName()).isEqualTo(project.getName());
209
210     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
211     assertThat(projectDto).isPresent();
212     assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
213
214     Assertions.assertThat(db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()))
215       .extracting(BranchDto::getKey, BranchDto::isMain)
216       .containsExactlyInAnyOrder(tuple("main", true));
217   }
218
219   @Test
220   public void import_project_no_gitlab_default_branch() {
221     UserDto user = db.users().insertUser();
222     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
223     AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
224     db.almPats().insert(dto -> {
225       dto.setAlmSettingUuid(almSetting.getUuid());
226       dto.setUserUuid(user.getUuid());
227       dto.setPersonalAccessToken("PAT");
228     });
229     Project project = getGitlabProject();
230     when(gitlabHttpClient.getProject(any(), any(), any())).thenReturn(project);
231     when(gitlabHttpClient.getBranches(any(), any(), any())).thenReturn(emptyList());
232     when(projectKeyGenerator.generateUniqueProjectKey(project.getPathWithNamespace())).thenReturn(PROJECT_KEY_NAME);
233
234     Projects.CreateWsResponse response = ws.newRequest()
235       .setParam("almSetting", almSetting.getKey())
236       .setParam("gitlabProjectId", "12345")
237       .executeProtobuf(Projects.CreateWsResponse.class);
238
239     verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
240     verify(gitlabHttpClient).getBranches(almSetting.getUrl(), "PAT", 12345L);
241
242     Projects.CreateWsResponse.Project result = response.getProject();
243     assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
244     assertThat(result.getName()).isEqualTo(project.getName());
245
246     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
247     assertThat(projectDto).isPresent();
248     assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
249
250     Assertions.assertThat(db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()))
251       .extracting(BranchDto::getKey, BranchDto::isMain)
252       .containsExactlyInAnyOrder(tuple(DEFAULT_MAIN_BRANCH_NAME, true));
253   }
254
255
256   @Test
257   public void import_project_without_NCD() {
258     UserDto user = db.users().insertUser();
259     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
260     AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
261     db.almPats().insert(dto -> {
262       dto.setAlmSettingUuid(almSetting.getUuid());
263       dto.setUserUuid(user.getUuid());
264       dto.setPersonalAccessToken("PAT");
265     });
266     Project project = getGitlabProject();
267     when(gitlabHttpClient.getProject(any(), any(), any())).thenReturn(project);
268     when(gitlabHttpClient.getBranches(any(), any(), any())).thenReturn(singletonList(new GitLabBranch("master", true)));
269     when(projectKeyGenerator.generateUniqueProjectKey(project.getPathWithNamespace())).thenReturn(PROJECT_KEY_NAME);
270
271     Projects.CreateWsResponse response = ws.newRequest()
272       .setParam("almSetting", almSetting.getKey())
273       .setParam("gitlabProjectId", "12345")
274       .executeProtobuf(Projects.CreateWsResponse.class);
275
276     verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
277
278     Projects.CreateWsResponse.Project result = response.getProject();
279     assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
280     assertThat(result.getName()).isEqualTo(project.getName());
281
282     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
283     assertThat(projectDto).isPresent();
284     assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
285   }
286
287
288   private Project getGitlabProject() {
289     return new Project(randomAlphanumeric(5), randomAlphanumeric(5));
290   }
291 }