Search in sources :

Example 1 with GsonAzureProjectList

use of org.sonar.alm.client.azure.GsonAzureProjectList in project sonarqube by SonarSource.

the class ListAzureProjectsAction method doHandle.

private ListAzureProjectsWsResponse 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 is not 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 pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        String url = requireNonNull(almSettingDto.getUrl(), "URL cannot be null");
        GsonAzureProjectList projectList = azureDevOpsHttpClient.getProjects(url, pat);
        List<AzureProject> values = projectList.getValues().stream().map(ListAzureProjectsAction::toAzureProject).sorted(comparing(AzureProject::getName, String::compareToIgnoreCase)).collect(Collectors.toList());
        ListAzureProjectsWsResponse.Builder builder = ListAzureProjectsWsResponse.newBuilder().addAllProjects(values);
        return builder.build();
    }
}
Also used : GsonAzureProjectList(org.sonar.alm.client.azure.GsonAzureProjectList) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) ListAzureProjectsWsResponse(org.sonarqube.ws.AlmIntegrations.ListAzureProjectsWsResponse) GsonAzureProject(org.sonar.alm.client.azure.GsonAzureProject) AzureProject(org.sonarqube.ws.AlmIntegrations.AzureProject) DbSession(org.sonar.db.DbSession) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto)

Example 2 with GsonAzureProjectList

use of org.sonar.alm.client.azure.GsonAzureProjectList in project sonarqube by SonarSource.

the class ListAzureProjectsActionTest method mockClient.

private void mockClient(List<GsonAzureProject> projects) {
    GsonAzureProjectList projectList = new GsonAzureProjectList();
    projectList.setValues(projects);
    when(azureDevOpsHttpClient.getProjects(anyString(), anyString())).thenReturn(projectList);
}
Also used : GsonAzureProjectList(org.sonar.alm.client.azure.GsonAzureProjectList)

Aggregations

GsonAzureProjectList (org.sonar.alm.client.azure.GsonAzureProjectList)2 GsonAzureProject (org.sonar.alm.client.azure.GsonAzureProject)1 DbSession (org.sonar.db.DbSession)1 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)1 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)1 NotFoundException (org.sonar.server.exceptions.NotFoundException)1 AzureProject (org.sonarqube.ws.AlmIntegrations.AzureProject)1 ListAzureProjectsWsResponse (org.sonarqube.ws.AlmIntegrations.ListAzureProjectsWsResponse)1