Search in sources :

Example 1 with GsonRepositorySearch

use of org.sonar.alm.client.github.GithubBinding.GsonRepositorySearch in project sonarqube by SonarSource.

the class GithubApplicationClientImpl method listRepositories.

@Override
public Repositories listRepositories(String appUrl, AccessToken accessToken, String organization, @Nullable String query, int page, int pageSize) {
    checkPageArgs(page, pageSize);
    String searchQuery = "fork:true+org:" + organization;
    if (query != null) {
        searchQuery = query.replace(" ", "+") + "+" + searchQuery;
    }
    try {
        Repositories repositories = new Repositories();
        GetResponse response = appHttpClient.get(appUrl, accessToken, String.format("/search/repositories?q=%s&page=%s&per_page=%s", searchQuery, page, pageSize));
        Optional<GsonRepositorySearch> gsonRepositories = response.getContent().map(content -> GSON.fromJson(content, GsonRepositorySearch.class));
        if (!gsonRepositories.isPresent()) {
            return repositories;
        }
        repositories.setTotal(gsonRepositories.get().totalCount);
        if (gsonRepositories.get().items != null) {
            repositories.setRepositories(gsonRepositories.get().items.stream().map(GsonGithubRepository::toRepository).collect(toList()));
        }
        return repositories;
    } catch (Exception e) {
        throw new IllegalStateException(format("Failed to list all repositories of '%s' accessible by user access token on '%s' using query '%s'", organization, appUrl, searchQuery), e);
    }
}
Also used : GsonGithubRepository(org.sonar.alm.client.github.GithubBinding.GsonGithubRepository) GetResponse(org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse) IOException(java.io.IOException) GsonRepositorySearch(org.sonar.alm.client.github.GithubBinding.GsonRepositorySearch)

Aggregations

IOException (java.io.IOException)1 GetResponse (org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse)1 GsonGithubRepository (org.sonar.alm.client.github.GithubBinding.GsonGithubRepository)1 GsonRepositorySearch (org.sonar.alm.client.github.GithubBinding.GsonRepositorySearch)1