3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.almintegration.ws.gitlab;
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;
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;
71 public class ImportGitLabProjectActionIT {
73 private static final String PROJECT_KEY_NAME = "PROJECT_NAME";
75 private final System2 system2 = mock(System2.class);
78 public UserSessionRule userSession = standalone();
81 public DbTester db = DbTester.create(system2);
83 DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
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);
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);
101 public void before() {
102 when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
103 when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_MAIN_BRANCH_NAME);
107 public void import_project_developer_edition() {
108 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.DEVELOPER));
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");
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);
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);
130 verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
132 Projects.CreateWsResponse.Project result = response.getProject();
133 assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
134 assertThat(result.getName()).isEqualTo(project.getName());
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();
140 assertThat(db.getDbClient().newCodePeriodDao().selectByProject(db.getSession(), projectDto.get().getUuid()))
143 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
144 .containsExactly(NUMBER_OF_DAYS, "30", null);
148 public void import_project_community_edition() {
149 when(editionProvider.get()).thenReturn(Optional.of(EditionProvider.Edition.COMMUNITY));
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");
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);
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);
171 Projects.CreateWsResponse.Project result = response.getProject();
173 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
175 String projectUuid = projectDto.get().getUuid();
176 assertThat(db.getDbClient().newCodePeriodDao().selectByBranch(db.getSession(), projectUuid, projectUuid))
179 .extracting(NewCodePeriodDto::getType, NewCodePeriodDto::getValue, NewCodePeriodDto::getBranchUuid)
180 .containsExactly(NUMBER_OF_DAYS, "30", projectUuid);
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");
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);
198 Projects.CreateWsResponse response = ws.newRequest()
199 .setParam("almSetting", almSetting.getKey())
200 .setParam("gitlabProjectId", "12345")
201 .executeProtobuf(Projects.CreateWsResponse.class);
203 verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
204 verify(gitlabHttpClient).getBranches(almSetting.getUrl(), "PAT", 12345L);
206 Projects.CreateWsResponse.Project result = response.getProject();
207 assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
208 assertThat(result.getName()).isEqualTo(project.getName());
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();
214 Assertions.assertThat(db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()))
215 .extracting(BranchDto::getKey, BranchDto::isMain)
216 .containsExactlyInAnyOrder(tuple("main", true));
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");
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);
234 Projects.CreateWsResponse response = ws.newRequest()
235 .setParam("almSetting", almSetting.getKey())
236 .setParam("gitlabProjectId", "12345")
237 .executeProtobuf(Projects.CreateWsResponse.class);
239 verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
240 verify(gitlabHttpClient).getBranches(almSetting.getUrl(), "PAT", 12345L);
242 Projects.CreateWsResponse.Project result = response.getProject();
243 assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
244 assertThat(result.getName()).isEqualTo(project.getName());
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();
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));
256 public void import_project_without_NCD() {
257 UserDto user = db.users().insertUser();
258 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
259 AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
260 db.almPats().insert(dto -> {
261 dto.setAlmSettingUuid(almSetting.getUuid());
262 dto.setUserUuid(user.getUuid());
263 dto.setPersonalAccessToken("PAT");
265 Project project = getGitlabProject();
266 when(gitlabHttpClient.getProject(any(), any(), any())).thenReturn(project);
267 when(gitlabHttpClient.getBranches(any(), any(), any())).thenReturn(singletonList(new GitLabBranch("master", true)));
268 when(projectKeyGenerator.generateUniqueProjectKey(project.getPathWithNamespace())).thenReturn(PROJECT_KEY_NAME);
270 Projects.CreateWsResponse response = ws.newRequest()
271 .setParam("almSetting", almSetting.getKey())
272 .setParam("gitlabProjectId", "12345")
273 .executeProtobuf(Projects.CreateWsResponse.class);
275 verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
277 Projects.CreateWsResponse.Project result = response.getProject();
278 assertThat(result.getKey()).isEqualTo(PROJECT_KEY_NAME);
279 assertThat(result.getName()).isEqualTo(project.getName());
281 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
282 assertThat(projectDto).isPresent();
283 assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
286 private Project getGitlabProject() {
287 return new Project(randomAlphanumeric(5), randomAlphanumeric(5));