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();
}
}
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));
}
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();
}
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", "", ""));
}
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);
}
Aggregations