Search in sources :

Example 1 with ProjectList

use of org.sonar.alm.client.gitlab.ProjectList in project sonarqube by SonarSource.

the class SearchGitlabReposAction method doHandle.

private AlmIntegrations.SearchGitlabReposWsResponse doHandle(Request request) {
    String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
    String projectName = request.param(PARAM_PROJECT_NAME);
    int pageNumber = request.mandatoryParamAsInt("p");
    int pageSize = request.mandatoryParamAsInt("ps");
    try (DbSession dbSession = dbClient.openSession(false)) {
        userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
        String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null");
        AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey).orElseThrow(() -> new NotFoundException(String.format("ALM Setting '%s' not found", almSettingKey)));
        Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
        String personalAccessToken = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        String gitlabUrl = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
        ProjectList gitlabProjectList = gitlabHttpClient.searchProjects(gitlabUrl, personalAccessToken, projectName, pageNumber, pageSize);
        Map<String, ProjectKeyName> sqProjectsKeyByGitlabProjectId = getSqProjectsKeyByGitlabProjectId(dbSession, almSettingDto, gitlabProjectList);
        List<GitlabRepository> gitlabRepositories = gitlabProjectList.getProjects().stream().map(project -> toGitlabRepository(project, sqProjectsKeyByGitlabProjectId)).collect(toList());
        Paging.Builder pagingBuilder = Paging.newBuilder().setPageIndex(gitlabProjectList.getPageNumber()).setPageSize(gitlabProjectList.getPageSize());
        Integer gitlabProjectListTotal = gitlabProjectList.getTotal();
        if (gitlabProjectListTotal != null) {
            pagingBuilder.setTotal(gitlabProjectListTotal);
        }
        return AlmIntegrations.SearchGitlabReposWsResponse.newBuilder().addAllRepositories(gitlabRepositories).setPaging(pagingBuilder.build()).build();
    }
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectList(org.sonar.alm.client.gitlab.ProjectList) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) AlmIntegrations(org.sonarqube.ws.AlmIntegrations) GitlabRepository(org.sonarqube.ws.AlmIntegrations.GitlabRepository) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) WebService(org.sonar.api.server.ws.WebService) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) Map(java.util.Map) GitlabHttpClient(org.sonar.alm.client.gitlab.GitlabHttpClient) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) Collectors.toSet(java.util.stream.Collectors.toSet) Project(org.sonar.alm.client.gitlab.Project) Set(java.util.Set) Paging(org.sonarqube.ws.Common.Paging) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) NotFoundException(org.sonar.server.exceptions.NotFoundException) DbClient(org.sonar.db.DbClient) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Optional(java.util.Optional) AlmIntegrationsWsAction(org.sonar.server.almintegration.ws.AlmIntegrationsWsAction) UserSession(org.sonar.server.user.UserSession) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) Paging(org.sonarqube.ws.Common.Paging) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) GitlabRepository(org.sonarqube.ws.AlmIntegrations.GitlabRepository) DbSession(org.sonar.db.DbSession) ProjectList(org.sonar.alm.client.gitlab.ProjectList) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Example 2 with ProjectList

use of org.sonar.alm.client.gitlab.ProjectList in project sonarqube by SonarSource.

the class SearchGitlabReposActionTest method list_gitlab_repos.

@Test
public void list_gitlab_repos() {
    Project gitlabProject1 = new Project(1L, "repoName1", "repoNamePath1", "repo-slug-1", "repo-path-slug-1", "url-1");
    Project gitlabProject2 = new Project(2L, "repoName2", "path1 / repoName2", "repo-slug-2", "path-1/repo-slug-2", "url-2");
    Project gitlabProject3 = new Project(3L, "repoName3", "repoName3 / repoName3", "repo-slug-3", "repo-slug-3/repo-slug-3", "url-3");
    Project gitlabProject4 = new Project(4L, "repoName4", "repoName4 / repoName4 / repoName4", "repo-slug-4", "repo-slug-4/repo-slug-4/repo-slug-4", "url-4");
    when(gitlabHttpClient.searchProjects(any(), any(), any(), anyInt(), anyInt())).thenReturn(new ProjectList(Arrays.asList(gitlabProject1, gitlabProject2, gitlabProject3, gitlabProject4), 1, 10, 4));
    UserDto user = db.users().insertUser();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
    db.almPats().insert(dto -> {
        dto.setAlmSettingUuid(almSetting.getUuid());
        dto.setUserUuid(user.getUuid());
        dto.setPersonalAccessToken("some-pat");
    });
    ProjectDto projectDto = db.components().insertPrivateProjectDto();
    db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto);
    AlmIntegrations.SearchGitlabReposWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).executeProtobuf(SearchGitlabReposWsResponse.class);
    assertThat(response.getRepositoriesCount()).isEqualTo(4);
    assertThat(response.getRepositoriesList()).extracting(GitlabRepository::getId, GitlabRepository::getName, GitlabRepository::getPathName, GitlabRepository::getSlug, GitlabRepository::getPathSlug, GitlabRepository::getUrl, GitlabRepository::hasSqProjectKey, GitlabRepository::hasSqProjectName).containsExactlyInAnyOrder(tuple(1L, "repoName1", "repoNamePath1", "repo-slug-1", "repo-path-slug-1", "url-1", false, false), tuple(2L, "repoName2", "path1", "repo-slug-2", "path-1", "url-2", false, false), tuple(3L, "repoName3", "repoName3", "repo-slug-3", "repo-slug-3", "url-3", false, false), tuple(4L, "repoName4", "repoName4 / repoName4", "repo-slug-4", "repo-slug-4/repo-slug-4", "url-4", false, false));
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) Project(org.sonar.alm.client.gitlab.Project) ProjectList(org.sonar.alm.client.gitlab.ProjectList) UserDto(org.sonar.db.user.UserDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) SearchGitlabReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchGitlabReposWsResponse) AlmIntegrations(org.sonarqube.ws.AlmIntegrations) Test(org.junit.Test)

Example 3 with ProjectList

use of org.sonar.alm.client.gitlab.ProjectList in project sonarqube by SonarSource.

the class SearchGitlabReposActionTest method return_empty_list_when_no_gitlab_projects.

@Test
public void return_empty_list_when_no_gitlab_projects() {
    when(gitlabHttpClient.searchProjects(any(), any(), any(), anyInt(), anyInt())).thenReturn(new ProjectList(new LinkedList<>(), 1, 10, 0));
    UserDto user = db.users().insertUser();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
    db.almPats().insert(dto -> {
        dto.setAlmSettingUuid(almSetting.getUuid());
        dto.setUserUuid(user.getUuid());
    });
    ProjectDto projectDto = db.components().insertPrivateProjectDto();
    db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto);
    AlmIntegrations.SearchGitlabReposWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).executeProtobuf(SearchGitlabReposWsResponse.class);
    assertThat(response.getRepositoriesList()).isEmpty();
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) ProjectList(org.sonar.alm.client.gitlab.ProjectList) UserDto(org.sonar.db.user.UserDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) SearchGitlabReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchGitlabReposWsResponse) LinkedList(java.util.LinkedList) AlmIntegrations(org.sonarqube.ws.AlmIntegrations) Test(org.junit.Test)

Example 4 with ProjectList

use of org.sonar.alm.client.gitlab.ProjectList in project sonarqube by SonarSource.

the class SearchGitlabReposActionTest method list_gitlab_repos_some_projects_already_set_up.

@Test
public void list_gitlab_repos_some_projects_already_set_up() {
    Project gitlabProject1 = new Project(1L, "repoName1", "repoNamePath1", "repo-slug-1", "repo-path-slug-1", "url-1");
    Project gitlabProject2 = new Project(2L, "repoName2", "path1 / repoName2", "repo-slug-2", "path-1/repo-slug-2", "url-2");
    Project gitlabProject3 = new Project(3L, "repoName3", "repoName3 / repoName3", "repo-slug-3", "repo-slug-3/repo-slug-3", "url-3");
    Project gitlabProject4 = new Project(4L, "repoName4", "repoName4 / repoName4 / repoName4", "repo-slug-4", "repo-slug-4/repo-slug-4/repo-slug-4", "url-4");
    when(gitlabHttpClient.searchProjects(any(), any(), any(), anyInt(), anyInt())).thenReturn(new ProjectList(Arrays.asList(gitlabProject1, gitlabProject2, gitlabProject3, gitlabProject4), 1, 10, 4));
    UserDto user = db.users().insertUser();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
    db.almPats().insert(dto -> {
        dto.setAlmSettingUuid(almSetting.getUuid());
        dto.setUserUuid(user.getUuid());
        dto.setPersonalAccessToken("some-pat");
    });
    ProjectDto projectDto1 = db.components().insertPrivateProjectDto();
    db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto1);
    ProjectDto projectDto2 = db.components().insertPrivateProjectDto();
    db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto2, projectAlmSettingDto -> projectAlmSettingDto.setAlmRepo("2"));
    ProjectDto projectDto3 = db.components().insertPrivateProjectDto();
    db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto3, projectAlmSettingDto -> projectAlmSettingDto.setAlmRepo("3"));
    ProjectDto projectDto4 = db.components().insertPrivateProjectDto();
    db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto4, projectAlmSettingDto -> projectAlmSettingDto.setAlmRepo("3"));
    AlmIntegrations.SearchGitlabReposWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).executeProtobuf(SearchGitlabReposWsResponse.class);
    assertThat(response.getRepositoriesCount()).isEqualTo(4);
    assertThat(response.getRepositoriesList()).extracting(GitlabRepository::getId, GitlabRepository::getName, GitlabRepository::getPathName, GitlabRepository::getSlug, GitlabRepository::getPathSlug, GitlabRepository::getUrl, GitlabRepository::getSqProjectKey, GitlabRepository::getSqProjectName).containsExactlyInAnyOrder(tuple(1L, "repoName1", "repoNamePath1", "repo-slug-1", "repo-path-slug-1", "url-1", "", ""), tuple(2L, "repoName2", "path1", "repo-slug-2", "path-1", "url-2", projectDto2.getKey(), projectDto2.getName()), tuple(3L, "repoName3", "repoName3", "repo-slug-3", "repo-slug-3", "url-3", projectDto3.getKey(), projectDto3.getName()), tuple(4L, "repoName4", "repoName4 / repoName4", "repo-slug-4", "repo-slug-4/repo-slug-4", "url-4", "", ""));
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) Project(org.sonar.alm.client.gitlab.Project) ProjectList(org.sonar.alm.client.gitlab.ProjectList) UserDto(org.sonar.db.user.UserDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) SearchGitlabReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchGitlabReposWsResponse) AlmIntegrations(org.sonarqube.ws.AlmIntegrations) Test(org.junit.Test)

Example 5 with ProjectList

use of org.sonar.alm.client.gitlab.ProjectList in project sonarqube by SonarSource.

the class SearchGitlabReposActionTest method verify_response_example.

@Test
public void verify_response_example() {
    Project gitlabProject1 = new Project(1L, "Gitlab repo name 1", "Group / Gitlab repo name 1", "gitlab-repo-name-1", "group/gitlab-repo-name-1", "https://example.gitlab.com/group/gitlab-repo-name-1");
    Project gitlabProject2 = new Project(2L, "Gitlab repo name 2", "Group / Gitlab repo name 2", "gitlab-repo-name-2", "group/gitlab-repo-name-2", "https://example.gitlab.com/group/gitlab-repo-name-2");
    Project gitlabProject3 = new Project(3L, "Gitlab repo name 3", "Group / Gitlab repo name 3", "gitlab-repo-name-3", "group/gitlab-repo-name-3", "https://example.gitlab.com/group/gitlab-repo-name-3");
    when(gitlabHttpClient.searchProjects(any(), any(), any(), anyInt(), anyInt())).thenReturn(new ProjectList(Arrays.asList(gitlabProject1, gitlabProject2, gitlabProject3), 1, 3, 10));
    UserDto user = db.users().insertUser();
    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
    AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
    db.almPats().insert(dto -> {
        dto.setAlmSettingUuid(almSetting.getUuid());
        dto.setUserUuid(user.getUuid());
        dto.setPersonalAccessToken("some-pat");
    });
    ProjectDto projectDto = db.components().insertPrivateProjectDto();
    db.almSettings().insertGitlabProjectAlmSetting(almSetting, projectDto);
    WebService.Action def = ws.getDef();
    String responseExample = def.responseExampleAsString();
    assertThat(responseExample).isNotBlank();
    ws.newRequest().setParam("almSetting", almSetting.getKey()).execute().assertJson(responseExample);
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) Project(org.sonar.alm.client.gitlab.Project) WebService(org.sonar.api.server.ws.WebService) ProjectList(org.sonar.alm.client.gitlab.ProjectList) UserDto(org.sonar.db.user.UserDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Aggregations

ProjectList (org.sonar.alm.client.gitlab.ProjectList)5 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)5 Test (org.junit.Test)4 Project (org.sonar.alm.client.gitlab.Project)4 ProjectDto (org.sonar.db.project.ProjectDto)4 UserDto (org.sonar.db.user.UserDto)4 AlmIntegrations (org.sonarqube.ws.AlmIntegrations)4 SearchGitlabReposWsResponse (org.sonarqube.ws.AlmIntegrations.SearchGitlabReposWsResponse)3 WebService (org.sonar.api.server.ws.WebService)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Set (java.util.Set)1 BinaryOperator (java.util.function.BinaryOperator)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toSet (java.util.stream.Collectors.toSet)1