Search in sources :

Example 1 with Organization

use of org.sonar.alm.client.github.GithubApplicationClient.Organization in project sonarqube by SonarSource.

the class ListGithubOrganizationsAction method doHandle.

private ListGithubOrganizationsWsResponse doHandle(Request request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
        String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
        AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey).orElseThrow(() -> new NotFoundException(String.format("GitHub ALM Setting '%s' not found", almSettingKey)));
        String userUuid = requireNonNull(userSession.getUuid(), "User UUID is not null");
        String url = requireNonNull(almSettingDto.getUrl(), String.format("No URL set for GitHub ALM '%s'", almSettingKey));
        AccessToken accessToken;
        if (request.hasParam(PARAM_TOKEN)) {
            String code = request.mandatoryParam(PARAM_TOKEN);
            String clientId = requireNonNull(almSettingDto.getClientId(), String.format("No clientId set for GitHub ALM '%s'", almSettingKey));
            String clientSecret = requireNonNull(almSettingDto.getDecryptedClientSecret(encryption), String.format("No clientSecret set for GitHub ALM '%s'", almSettingKey));
            try {
                accessToken = githubApplicationClient.createUserAccessToken(url, clientId, clientSecret, code);
            } catch (IllegalArgumentException e) {
                // it could also be that the code has expired!
                throw BadRequestException.create("Unable to authenticate with GitHub. " + "Check the GitHub App client ID and client secret configured in the Global Settings and try again.");
            }
            Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
            if (almPatDto.isPresent()) {
                AlmPatDto almPat = almPatDto.get();
                almPat.setPersonalAccessToken(accessToken.getValue());
                dbClient.almPatDao().update(dbSession, almPat, userSession.getLogin(), almSettingDto.getKey());
            } else {
                AlmPatDto almPat = new AlmPatDto().setPersonalAccessToken(accessToken.getValue()).setAlmSettingUuid(almSettingDto.getUuid()).setUserUuid(userUuid);
                dbClient.almPatDao().insert(dbSession, almPat, userSession.getLogin(), almSettingDto.getKey());
            }
            dbSession.commit();
        } else {
            accessToken = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto).map(AlmPatDto::getPersonalAccessToken).map(UserAccessToken::new).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        }
        int page = request.hasParam(PAGE) ? request.mandatoryParamAsInt(PAGE) : 1;
        int pageSize = request.hasParam(PAGE_SIZE) ? request.mandatoryParamAsInt(PAGE_SIZE) : 100;
        GithubApplicationClient.Organizations githubOrganizations = githubApplicationClient.listOrganizations(url, accessToken, page, pageSize);
        ListGithubOrganizationsWsResponse.Builder response = ListGithubOrganizationsWsResponse.newBuilder().setPaging(Common.Paging.newBuilder().setPageIndex(page).setPageSize(pageSize).setTotal(githubOrganizations.getTotal()).build());
        List<Organization> organizations = githubOrganizations.getOrganizations();
        if (organizations != null) {
            organizations.forEach(githubOrganization -> response.addOrganizations(AlmIntegrations.GithubOrganization.newBuilder().setKey(githubOrganization.getLogin()).setName(githubOrganization.getLogin()).build()));
        }
        return response.build();
    }
}
Also used : GithubApplicationClient(org.sonar.alm.client.github.GithubApplicationClient) Organization(org.sonar.alm.client.github.GithubApplicationClient.Organization) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) DbSession(org.sonar.db.DbSession) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) AccessToken(org.sonar.alm.client.github.security.AccessToken) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ListGithubOrganizationsWsResponse(org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse)

Aggregations

GithubApplicationClient (org.sonar.alm.client.github.GithubApplicationClient)1 Organization (org.sonar.alm.client.github.GithubApplicationClient.Organization)1 AccessToken (org.sonar.alm.client.github.security.AccessToken)1 UserAccessToken (org.sonar.alm.client.github.security.UserAccessToken)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 ListGithubOrganizationsWsResponse (org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse)1