3 * Copyright (C) 2009-2021 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 com.google.common.annotations.VisibleForTesting;
23 import java.util.Optional;
24 import org.sonar.alm.client.gitlab.GitlabHttpClient;
25 import org.sonar.alm.client.gitlab.Project;
26 import org.sonar.api.server.ws.Request;
27 import org.sonar.api.server.ws.Response;
28 import org.sonar.api.server.ws.WebService;
29 import org.sonar.core.util.UuidFactory;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.alm.pat.AlmPatDto;
33 import org.sonar.db.alm.setting.AlmSettingDto;
34 import org.sonar.db.alm.setting.ProjectAlmSettingDto;
35 import org.sonar.db.component.ComponentDto;
36 import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
37 import org.sonar.server.almintegration.ws.ImportHelper;
38 import org.sonar.server.component.ComponentUpdater;
39 import org.sonar.server.project.ProjectDefaultVisibility;
40 import org.sonar.server.user.UserSession;
41 import org.sonarqube.ws.Projects.CreateWsResponse;
43 import static java.util.Objects.requireNonNull;
44 import static org.sonar.api.resources.Qualifiers.PROJECT;
45 import static org.sonar.server.component.NewComponent.newComponentBuilder;
46 import static org.sonar.server.ws.WsUtils.writeProtobuf;
48 public class ImportGitLabProjectAction implements AlmIntegrationsWsAction {
50 public static final String PARAM_GITLAB_PROJECT_ID = "gitlabProjectId";
52 private final DbClient dbClient;
53 private final UserSession userSession;
54 private final ProjectDefaultVisibility projectDefaultVisibility;
55 private final GitlabHttpClient gitlabHttpClient;
56 private final ComponentUpdater componentUpdater;
57 private final UuidFactory uuidFactory;
58 private final ImportHelper importHelper;
60 public ImportGitLabProjectAction(DbClient dbClient, UserSession userSession,
61 ProjectDefaultVisibility projectDefaultVisibility, GitlabHttpClient gitlabHttpClient,
62 ComponentUpdater componentUpdater, UuidFactory uuidFactory, ImportHelper importHelper) {
63 this.dbClient = dbClient;
64 this.userSession = userSession;
65 this.projectDefaultVisibility = projectDefaultVisibility;
66 this.gitlabHttpClient = gitlabHttpClient;
67 this.componentUpdater = componentUpdater;
68 this.uuidFactory = uuidFactory;
69 this.importHelper = importHelper;
73 public void define(WebService.NewController context) {
74 WebService.NewAction action = context.createAction("import_gitlab_project")
75 .setDescription("Import a GitLab project to SonarQube, creating a new project and configuring MR decoration<br/>" +
76 "Requires the 'Create Projects' permission")
81 action.createParam(ImportHelper.PARAM_ALM_SETTING)
83 .setDescription("ALM setting key");
84 action.createParam(PARAM_GITLAB_PROJECT_ID)
86 .setDescription("GitLab project ID");
90 public void handle(Request request, Response response) throws Exception {
91 CreateWsResponse createResponse = doHandle(request);
92 writeProtobuf(createResponse, request, response);
95 private CreateWsResponse doHandle(Request request) {
96 importHelper.checkProvisionProjectPermission();
97 AlmSettingDto almSettingDto = importHelper.getAlmSetting(request);
98 String userUuid = importHelper.getUserUuid();
99 try (DbSession dbSession = dbClient.openSession(false)) {
100 Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
101 String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken)
102 .orElseThrow(() -> new IllegalArgumentException(String.format("personal access token for '%s' is missing", almSettingDto.getKey())));
104 long gitlabProjectId = request.mandatoryParamAsLong(PARAM_GITLAB_PROJECT_ID);
106 String url = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
107 Project gitlabProject = gitlabHttpClient.getProject(url, pat, gitlabProjectId);
109 ComponentDto componentDto = createProject(dbSession, gitlabProject);
110 populateMRSetting(dbSession, gitlabProjectId, componentDto, almSettingDto);
112 return ImportHelper.toCreateResponse(componentDto);
116 private void populateMRSetting(DbSession dbSession, Long gitlabProjectId, ComponentDto componentDto, AlmSettingDto almSettingDto) {
117 dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, new ProjectAlmSettingDto()
118 .setProjectUuid(componentDto.projectUuid())
119 .setAlmSettingUuid(almSettingDto.getUuid())
120 .setAlmRepo(gitlabProjectId.toString())
122 .setMonorepo(false));
126 private ComponentDto createProject(DbSession dbSession, Project gitlabProject) {
127 boolean visibility = projectDefaultVisibility.get(dbSession).isPrivate();
128 String sqProjectKey = generateProjectKey(gitlabProject.getPathWithNamespace(), uuidFactory.create());
130 return componentUpdater.create(dbSession, newComponentBuilder()
131 .setKey(sqProjectKey)
132 .setName(gitlabProject.getName())
133 .setPrivate(visibility)
134 .setQualifier(PROJECT)
136 userSession.getUuid());
140 String generateProjectKey(String pathWithNamespace, String uuid) {
141 String sqProjectKey = pathWithNamespace + "_" + uuid;
143 if (sqProjectKey.length() > 250) {
144 sqProjectKey = sqProjectKey.substring(sqProjectKey.length() - 250);
147 return sqProjectKey.replace("/", "_");