Search in sources :

Example 6 with UserAccessToken

use of org.sonar.alm.client.github.security.UserAccessToken in project sonarqube by SonarSource.

the class GithubApplicationClientImplTest method listRepositories_fail_if_pageIndex_out_of_bounds.

@Test
public void listRepositories_fail_if_pageIndex_out_of_bounds() {
    UserAccessToken token = new UserAccessToken("token");
    assertThatThrownBy(() -> underTest.listRepositories(appUrl, token, "test", null, 0, 100)).isInstanceOf(IllegalArgumentException.class).hasMessage("'page' must be larger than 0.");
}
Also used : UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) Test(org.junit.Test)

Example 7 with UserAccessToken

use of org.sonar.alm.client.github.security.UserAccessToken in project sonarqube by SonarSource.

the class GithubApplicationClientImplTest method listRepositories_returns_empty_results.

@Test
public void listRepositories_returns_empty_results() throws IOException {
    String appUrl = "https://github.sonarsource.com";
    AccessToken accessToken = new UserAccessToken(randomAlphanumeric(10));
    String responseJson = "{\n" + "  \"total_count\": 0\n" + "}";
    when(httpClient.get(appUrl, accessToken, String.format("/search/repositories?q=%s&page=%s&per_page=%s", "fork:true+org:github", 1, 100))).thenReturn(new OkGetResponse(responseJson));
    GithubApplicationClient.Repositories repositories = underTest.listRepositories(appUrl, accessToken, "github", null, 1, 100);
    assertThat(repositories.getTotal()).isZero();
    assertThat(repositories.getRepositories()).isNull();
}
Also used : UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) AccessToken(org.sonar.alm.client.github.security.AccessToken) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) Test(org.junit.Test)

Example 8 with UserAccessToken

use of org.sonar.alm.client.github.security.UserAccessToken in project sonarqube by SonarSource.

the class GithubApplicationClientImplTest method listOrganizations_fail_on_failure.

@Test
public void listOrganizations_fail_on_failure() throws IOException {
    String appUrl = "https://github.sonarsource.com";
    AccessToken accessToken = new UserAccessToken(randomAlphanumeric(10));
    when(httpClient.get(appUrl, accessToken, String.format("/user/installations?page=%s&per_page=%s", 1, 100))).thenThrow(new IOException("OOPS"));
    assertThatThrownBy(() -> underTest.listOrganizations(appUrl, accessToken, 1, 100)).isInstanceOf(IllegalStateException.class).hasMessage("Failed to list all organizations accessible by user access token on %s", appUrl);
}
Also used : UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) AccessToken(org.sonar.alm.client.github.security.AccessToken) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with UserAccessToken

use of org.sonar.alm.client.github.security.UserAccessToken in project sonarqube by SonarSource.

the class GithubApplicationClientImplTest method listRepositories_fail_if_pageSize_out_of_bounds.

@Test
public void listRepositories_fail_if_pageSize_out_of_bounds() {
    UserAccessToken token = new UserAccessToken("token");
    assertThatThrownBy(() -> underTest.listRepositories(appUrl, token, "test", null, 1, 0)).isInstanceOf(IllegalArgumentException.class).hasMessage("'pageSize' must be a value larger than 0 and smaller or equal to 100.");
    assertThatThrownBy(() -> underTest.listRepositories("", token, "test", null, 1, 101)).isInstanceOf(IllegalArgumentException.class).hasMessage("'pageSize' must be a value larger than 0 and smaller or equal to 100.");
}
Also used : UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) Test(org.junit.Test)

Example 10 with UserAccessToken

use of org.sonar.alm.client.github.security.UserAccessToken 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

UserAccessToken (org.sonar.alm.client.github.security.UserAccessToken)21 Test (org.junit.Test)18 AccessToken (org.sonar.alm.client.github.security.AccessToken)11 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)6 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)4 NotFoundException (org.sonar.server.exceptions.NotFoundException)4 ListGithubOrganizationsWsResponse (org.sonarqube.ws.AlmIntegrations.ListGithubOrganizationsWsResponse)4 IOException (java.io.IOException)3 GithubApplicationClient (org.sonar.alm.client.github.GithubApplicationClient)3 DbSession (org.sonar.db.DbSession)3 Common (org.sonarqube.ws.Common)3 Optional (java.util.Optional)2 Repository (org.sonar.alm.client.github.GithubApplicationClient.Repository)2 ProjectAlmSettingDto (org.sonar.db.alm.setting.ProjectAlmSettingDto)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1 Assertions.tuple (org.assertj.core.api.Assertions.tuple)1