Search in sources :

Example 6 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_link_header_does_not_have_next_rel.

@Test
public void get_returns_empty_endPoint_when_link_header_does_not_have_next_rel() throws IOException {
    server.enqueue(new MockResponse().setBody(randomBody).setHeader("link", "<https://api.github.com/installation/repositories?per_page=5&page=4>; rel=\"prev\", " + "<https://api.github.com/installation/repositories?per_page=5&page=1>; rel=\"first\""));
    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 7 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_different_case.

@Test
public void get_returns_endPoint_when_link_header_has_next_rel_different_case() throws IOException {
    String linkHeader = "<https://api.github.com/installation/repositories?per_page=5&page=2>; rel=\"next\"";
    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)

Example 8 with GetResponse

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

the class GithubApplicationHttpClientImplTest method get_adds_authentication_header_with_Bearer_type_and_Accept_header.

@Test
public void get_adds_authentication_header_with_Bearer_type_and_Accept_header() throws IOException, InterruptedException {
    server.enqueue(new MockResponse());
    GetResponse response = underTest.get(appUrl, accessToken, randomEndPoint);
    assertThat(response).isNotNull();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getMethod()).isEqualTo("GET");
    assertThat(recordedRequest.getPath()).isEqualTo(randomEndPoint);
    assertThat(recordedRequest.getHeader("Authorization")).isEqualTo("token " + accessToken.getValue());
    assertThat(recordedRequest.getHeader("Accept")).isEqualTo(BETA_API_HEADER);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) GetResponse(org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse) Test(org.junit.Test)

Example 9 with GetResponse

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

the class GithubApplicationClientImpl method checkAppPermissions.

@Override
public void checkAppPermissions(GithubAppConfiguration githubAppConfiguration) {
    AppToken appToken = appSecurity.createAppToken(githubAppConfiguration.getId(), githubAppConfiguration.getPrivateKey());
    Map<String, String> permissions = new HashMap<>();
    permissions.put("checks", WRITE_PERMISSION_NAME);
    permissions.put("pull_requests", WRITE_PERMISSION_NAME);
    permissions.put("statuses", READ_PERMISSION_NAME);
    permissions.put("metadata", READ_PERMISSION_NAME);
    String endPoint = "/app";
    GetResponse response;
    try {
        response = appHttpClient.get(githubAppConfiguration.getApiEndpoint(), appToken, endPoint);
    } catch (IOException e) {
        LOG.warn(FAILED_TO_REQUEST_BEGIN_MSG + githubAppConfiguration.getApiEndpoint() + endPoint, e);
        throw new IllegalArgumentException("Failed to validate configuration, check URL and Private Key");
    }
    if (response.getCode() == HTTP_OK) {
        Map<String, String> perms = handleResponse(response, endPoint, GsonApp.class).map(GsonApp::getPermissions).orElseThrow(() -> new IllegalArgumentException("Failed to get app permissions, unexpected response body"));
        List<String> missingPermissions = permissions.entrySet().stream().filter(permission -> !Objects.equals(permission.getValue(), perms.get(permission.getKey()))).map(Map.Entry::getKey).collect(toList());
        if (!missingPermissions.isEmpty()) {
            String message = missingPermissions.stream().map(perm -> perm + " is '" + perms.get(perm) + "', should be '" + permissions.get(perm) + "'").collect(Collectors.joining(", "));
            throw new IllegalArgumentException("Missing permissions; permission granted on " + message);
        }
    } else if (response.getCode() == HTTP_UNAUTHORIZED || response.getCode() == HTTP_FORBIDDEN) {
        throw new IllegalArgumentException("Authentication failed, verify the Client Id, Client Secret and Private Key fields");
    } else {
        throw new IllegalArgumentException("Failed to check permissions with Github, check the configuration");
    }
}
Also used : AppToken(org.sonar.alm.client.github.security.AppToken) Arrays(java.util.Arrays) StringUtils(org.sonar.api.internal.apachecommons.lang.StringUtils) HashMap(java.util.HashMap) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) GsonApp(org.sonar.alm.client.gitlab.GsonApp) HTTP_OK(java.net.HttpURLConnection.HTTP_OK) AccessToken(org.sonar.alm.client.github.security.AccessToken) Loggers(org.sonar.api.utils.log.Loggers) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Gson(com.google.gson.Gson) Locale(java.util.Locale) Map(java.util.Map) GsonRepositorySearch(org.sonar.alm.client.github.GithubBinding.GsonRepositorySearch) URI(java.net.URI) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) GsonInstallations(org.sonar.alm.client.github.GithubBinding.GsonInstallations) GithubAppConfiguration(org.sonar.alm.client.github.config.GithubAppConfiguration) AppToken(org.sonar.alm.client.github.security.AppToken) HTTP_UNAUTHORIZED(java.net.HttpURLConnection.HTTP_UNAUTHORIZED) IOException(java.io.IOException) GithubAppSecurity(org.sonar.alm.client.github.security.GithubAppSecurity) GsonGithubRepository(org.sonar.alm.client.github.GithubBinding.GsonGithubRepository) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Optional(java.util.Optional) HTTP_FORBIDDEN(java.net.HttpURLConnection.HTTP_FORBIDDEN) GetResponse(org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse) HashMap(java.util.HashMap) IOException(java.io.IOException) GetResponse(org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with GetResponse

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

the class GithubApplicationClientImpl method listOrganizations.

@Override
public Organizations listOrganizations(String appUrl, AccessToken accessToken, int page, int pageSize) {
    checkPageArgs(page, pageSize);
    try {
        Organizations organizations = new Organizations();
        GetResponse response = appHttpClient.get(appUrl, accessToken, String.format("/user/installations?page=%s&per_page=%s", page, pageSize));
        Optional<GsonInstallations> gsonInstallations = response.getContent().map(content -> GSON.fromJson(content, GsonInstallations.class));
        if (!gsonInstallations.isPresent()) {
            return organizations;
        }
        organizations.setTotal(gsonInstallations.get().totalCount);
        if (gsonInstallations.get().installations != null) {
            organizations.setOrganizations(gsonInstallations.get().installations.stream().map(gsonInstallation -> new Organization(gsonInstallation.account.id, gsonInstallation.account.login, null, null, null, null, null, gsonInstallation.targetType)).collect(toList()));
        }
        return organizations;
    } catch (IOException e) {
        throw new IllegalStateException(format("Failed to list all organizations accessible by user access token on %s", appUrl), e);
    }
}
Also used : IOException(java.io.IOException) GetResponse(org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse) GsonInstallations(org.sonar.alm.client.github.GithubBinding.GsonInstallations)

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