Search in sources :

Example 1 with GetResponse

use of org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse 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)

Example 2 with GetResponse

use of org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse in project sonarqube by SonarSource.

the class GithubApplicationHttpClientImplTest method get_returns_empty_endPoint_when_no_link_header.

@Test
public void get_returns_empty_endPoint_when_no_link_header() throws IOException {
    server.enqueue(new MockResponse().setBody(randomBody));
    GetResponse response = underTest.get(appUrl, accessToken, randomEndPoint);
    assertThat(response.getNextEndPoint()).isEmpty();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) GetResponse(org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse) Test(org.junit.Test)

Example 3 with GetResponse

use of org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse in project sonarqube by SonarSource.

the class GithubApplicationHttpClientImplTest method get_returns_body_as_response_if_code_is_200.

@Test
public void get_returns_body_as_response_if_code_is_200() throws IOException {
    server.enqueue(new MockResponse().setResponseCode(200).setBody(randomBody));
    GetResponse response = underTest.get(appUrl, accessToken, randomEndPoint);
    assertThat(response.getContent()).contains(randomBody);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) GetResponse(org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse) Test(org.junit.Test)

Example 4 with GetResponse

use of org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse in project sonarqube by SonarSource.

the class GithubApplicationHttpClientImplTest method get_returns_endPoint_when_link_header_has_next_rel.

@Test
@UseDataProvider("linkHeadersWithNextRel")
public void get_returns_endPoint_when_link_header_has_next_rel(String linkHeader) throws IOException {
    server.enqueue(new MockResponse().setBody(randomBody).setHeader("link", linkHeader));
    GetResponse response = underTest.get(appUrl, accessToken, randomEndPoint);
    assertThat(response.getNextEndPoint()).contains("https://api.github.com/installation/repositories?per_page=5&page=2");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) GetResponse(org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 5 with GetResponse

use of org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse in project sonarqube by SonarSource.

the class GithubApplicationHttpClientImplTest method get_empty_response_if_code_is_not_200.

@Test
@UseDataProvider("someHttpCodesWithContentBut200")
public void get_empty_response_if_code_is_not_200(int code) throws IOException {
    server.enqueue(new MockResponse().setResponseCode(code).setBody(randomBody));
    GetResponse response = underTest.get(appUrl, accessToken, randomEndPoint);
    assertThat(response.getContent()).isEmpty();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) GetResponse(org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

GetResponse (org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse)10 MockResponse (okhttp3.mockwebserver.MockResponse)7 Test (org.junit.Test)7 IOException (java.io.IOException)3 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2 GsonGithubRepository (org.sonar.alm.client.github.GithubBinding.GsonGithubRepository)2 GsonInstallations (org.sonar.alm.client.github.GithubBinding.GsonInstallations)2 GsonRepositorySearch (org.sonar.alm.client.github.GithubBinding.GsonRepositorySearch)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Gson (com.google.gson.Gson)1 String.format (java.lang.String.format)1 HTTP_FORBIDDEN (java.net.HttpURLConnection.HTTP_FORBIDDEN)1 HTTP_OK (java.net.HttpURLConnection.HTTP_OK)1 HTTP_UNAUTHORIZED (java.net.HttpURLConnection.HTTP_UNAUTHORIZED)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1