Search in sources :

Example 6 with SearchAzureReposWsResponse

use of org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse in project sonarqube by SonarSource.

the class SearchAzureReposActionTest method search_repos_with_project_name.

@Test
public void search_repos_with_project_name() {
    AlmSettingDto almSetting = insertAlmSetting();
    SearchAzureReposWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).setParam("projectName", "project-1").executeProtobuf(SearchAzureReposWsResponse.class);
    assertThat(response.getRepositoriesList()).extracting(AzureRepo::getName, AzureRepo::getProjectName).containsExactlyInAnyOrder(tuple("repoName-1", "project-1"), tuple("repoName-2", "project-2"));
}
Also used : SearchAzureReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Example 7 with SearchAzureReposWsResponse

use of org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse in project sonarqube by SonarSource.

the class SearchAzureReposAction method doHandle.

private SearchAzureReposWsResponse doHandle(Request request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
        String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
        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 projectKey = request.param(PARAM_PROJECT_NAME);
        String searchQuery = request.param(PARAM_SEARCH_QUERY);
        String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        String url = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
        GsonAzureRepoList gsonAzureRepoList = azureDevOpsHttpClient.getRepos(url, pat, projectKey);
        Map<ProjectKeyName, ProjectDto> sqProjectsKeyByAzureKey = getSqProjectsKeyByCustomKey(dbSession, almSettingDto, gsonAzureRepoList);
        List<AzureRepo> repositories = gsonAzureRepoList.getValues().stream().filter(r -> isSearchOnlyByProjectName(searchQuery) || doesSearchCriteriaMatchProjectOrRepo(r, searchQuery)).map(repo -> toAzureRepo(repo, sqProjectsKeyByAzureKey)).sorted(comparing(AzureRepo::getName, String::compareToIgnoreCase)).collect(toList());
        LOG.debug(repositories.toString());
        return SearchAzureReposWsResponse.newBuilder().addAllRepositories(repositories).build();
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) StringUtils.containsIgnoreCase(org.apache.commons.lang.StringUtils.containsIgnoreCase) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) GsonAzureRepoList(org.sonar.alm.client.azure.GsonAzureRepoList) Request(org.sonar.api.server.ws.Request) Loggers(org.sonar.api.utils.log.Loggers) WebService(org.sonar.api.server.ws.WebService) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) Comparator.comparing(java.util.Comparator.comparing) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) NotFoundException(org.sonar.server.exceptions.NotFoundException) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ProjectDto(org.sonar.db.project.ProjectDto) Optional(java.util.Optional) GsonAzureRepo(org.sonar.alm.client.azure.GsonAzureRepo) AlmIntegrationsWsAction(org.sonar.server.almintegration.ws.AlmIntegrationsWsAction) UserSession(org.sonar.server.user.UserSession) SearchAzureReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse) AzureDevOpsHttpClient(org.sonar.alm.client.azure.AzureDevOpsHttpClient) AzureRepo(org.sonarqube.ws.AlmIntegrations.AzureRepo) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) GsonAzureRepo(org.sonar.alm.client.azure.GsonAzureRepo) AzureRepo(org.sonarqube.ws.AlmIntegrations.AzureRepo) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) DbSession(org.sonar.db.DbSession) GsonAzureRepoList(org.sonar.alm.client.azure.GsonAzureRepoList) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Example 8 with SearchAzureReposWsResponse

use of org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse in project sonarqube by SonarSource.

the class SearchAzureReposAction method handle.

@Override
public void handle(Request request, Response response) {
    SearchAzureReposWsResponse wsResponse = doHandle(request);
    writeProtobuf(wsResponse, request, response);
}
Also used : SearchAzureReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse)

Example 9 with SearchAzureReposWsResponse

use of org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse in project sonarqube by SonarSource.

the class SearchAzureReposActionTest method search_repos_alphabetically_sorted.

@Test
public void search_repos_alphabetically_sorted() {
    mockClient(new GsonAzureRepoList(ImmutableList.of(getGsonAzureRepo("project-1", "Z-repo"), getGsonAzureRepo("project-1", "A-repo-1"), getGsonAzureRepo("project-1", "a-repo"), getGsonAzureRepo("project-1", "b-repo"))));
    AlmSettingDto almSetting = insertAlmSetting();
    SearchAzureReposWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).executeProtobuf(SearchAzureReposWsResponse.class);
    assertThat(response.getRepositoriesList()).extracting(AzureRepo::getName, AzureRepo::getProjectName).containsExactly(tuple("a-repo", "project-1"), tuple("A-repo-1", "project-1"), tuple("b-repo", "project-1"), tuple("Z-repo", "project-1"));
}
Also used : SearchAzureReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse) GsonAzureRepoList(org.sonar.alm.client.azure.GsonAzureRepoList) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Example 10 with SearchAzureReposWsResponse

use of org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse in project sonarqube by SonarSource.

the class SearchAzureReposActionTest method search_repos_with_projects_already_set_up_and_no_collision.

@Test
public void search_repos_with_projects_already_set_up_and_no_collision() {
    mockClient(new GsonAzureRepoList(ImmutableList.of(getGsonAzureRepo("project-1", "repoName-1"), getGsonAzureRepo("project", "1-repoName-1"))));
    AlmSettingDto almSetting = insertAlmSetting();
    ProjectDto projectDto1 = insertProject(almSetting, "repoName-1", "project-1");
    ProjectDto projectDto2 = insertProject(almSetting, "1-repoName-1", "project");
    SearchAzureReposWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).executeProtobuf(SearchAzureReposWsResponse.class);
    assertThat(response.getRepositoriesCount()).isEqualTo(2);
    assertThat(response.getRepositoriesList()).extracting(AzureRepo::getName, AzureRepo::getProjectName, AzureRepo::getSqProjectKey, AzureRepo::getSqProjectName).containsExactlyInAnyOrder(tuple("repoName-1", "project-1", projectDto1.getKey(), projectDto1.getName()), tuple("1-repoName-1", "project", projectDto2.getKey(), projectDto2.getName()));
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) SearchAzureReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse) GsonAzureRepoList(org.sonar.alm.client.azure.GsonAzureRepoList) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Aggregations

SearchAzureReposWsResponse (org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse)13 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)12 Test (org.junit.Test)11 GsonAzureRepoList (org.sonar.alm.client.azure.GsonAzureRepoList)6 ProjectDto (org.sonar.db.project.ProjectDto)5 Comparator.comparing (java.util.Comparator.comparing)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)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.toMap (java.util.stream.Collectors.toMap)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Nullable (javax.annotation.Nullable)1 StringUtils.containsIgnoreCase (org.apache.commons.lang.StringUtils.containsIgnoreCase)1