]> source.dussan.org Git - sonarqube.git/blob
db88cbdc36e50c38940c527ac076b4c0494c8a3a
[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.TestIndexers;
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);
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 TestIndexers(), 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     BranchDto branchDto = db.getDbClient().branchDao().selectMainBranchByProjectUuid(db.getSession(), projectDto.get().getUuid()).orElseThrow();
175
176     String projectUuid = projectDto.get().getUuid();
177     assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, branchDto.getUuid()))
178       .isPresent()
179       .get()
180       .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
181       .containsExactly(NUMBER_OF_DAYS, "30", branchDto.getUuid());
182   }
183
184   @Test
185   public void import_project_with_specific_different_default_branch() {
186     UserDto user = db.users().insertUser();
187     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
188     AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
189     db.almPats().insert(dto -> {
190       dto.setAlmSettingUuid(almSetting.getUuid());
191       dto.setUserUuid(user.getUuid());
192       dto.setPersonalAccessToken("PAT");
193     });
194     Project project = getGitlabProject();
195     when(gitlabHttpClient.getProject(any(), any(), any())).thenReturn(project);
196     when(gitlabHttpClient.getBranches(any(), any(), any())).thenReturn(singletonList(new GitLabBranch("main", true)));
197     when(projectKeyGenerator.generateUniqueProjectKey(project.getPathWithNamespace())).thenReturn(PROJECT_KEY_NAME);
198
199     Projects.CreateWsResponse response = ws.newRequest()
200       .setParam("almSetting", almSetting.getKey())
201       .setParam("gitlabProjectId", "12345")
202       .executeProtobuf(Projects.CreateWsResponse.class);
203
204     verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
205     verify(gitlabHttpClient).getBranches(almSetting.getUrl(), "PAT", 12345L);
206
207     Projects.CreateWsResponse.Project result = response.getProject();
208     assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
209     assertThat(result.getName()).isEqualTo(project.getName());
210
211     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
212     assertThat(projectDto).isPresent();
213     assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
214
215     Assertions.assertThat(db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()))
216       .extracting(BranchDto::getKey, BranchDto::isMain)
217       .containsExactlyInAnyOrder(tuple("main", true));
218   }
219
220   @Test
221   public void import_project_no_gitlab_default_branch() {
222     UserDto user = db.users().insertUser();
223     userSession.logIn(user).addPermission(PROVISION_PROJECTS);
224     AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
225     db.almPats().insert(dto -> {
226       dto.setAlmSettingUuid(almSetting.getUuid());
227       dto.setUserUuid(user.getUuid());
228       dto.setPersonalAccessToken("PAT");
229     });
230     Project project = getGitlabProject();
231     when(gitlabHttpClient.getProject(any(), any(), any())).thenReturn(project);
232     when(gitlabHttpClient.getBranches(any(), any(), any())).thenReturn(emptyList());
233     when(projectKeyGenerator.generateUniqueProjectKey(project.getPathWithNamespace())).thenReturn(PROJECT_KEY_NAME);
234
235     Projects.CreateWsResponse response = ws.newRequest()
236       .setParam("almSetting", almSetting.getKey())
237       .setParam("gitlabProjectId", "12345")
238       .executeProtobuf(Projects.CreateWsResponse.class);
239
240     verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
241     verify(gitlabHttpClient).getBranches(almSetting.getUrl(), "PAT", 12345L);
242
243     Projects.CreateWsResponse.Project result = response.getProject();
244     assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
245     assertThat(result.getName()).isEqualTo(project.getName());
246
247     Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
248     assertThat(projectDto).isPresent();
249     assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
250
251     Assertions.assertThat(db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()))
252       .extracting(BranchDto::getKey, BranchDto::isMain)
253       .containsExactlyInAnyOrder(tuple(DEFAULT_MAIN_BRANCH_NAME, true));
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   private Project getGitlabProject() {
288     return new Project(randomAlphanumeric(5), randomAlphanumeric(5));
289   }
290 }