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