Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions src/main/java/com/spotify/github/v3/clients/GithubAppClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.spotify.github.v3.checks.Installation;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import javax.ws.rs.core.HttpHeaders;

Expand All @@ -38,9 +39,15 @@ public class GithubAppClient {
private static final String GET_INSTALLATION_REPO_URL = "/repos/%s/%s/installation";
private static final String LIST_ACCESSIBLE_REPOS_URL = "/installation/repositories";

/*
Owner and org are interchangeable and therefore "owner" is used to
refer to the organisation in the installation endpoint
*/
private static final String GET_INSTALLATION_ORG_URL = "/orgs/%s/installation";

private final GitHubClient github;
private final String owner;
private final String repo;
private final Optional<String> maybeRepo;

private final Map<String, String> extraHeaders =
ImmutableMap.of(HttpHeaders.ACCEPT, "application/vnd.github.machine-man-preview+json");
Expand All @@ -51,7 +58,13 @@ public class GithubAppClient {
GithubAppClient(final GitHubClient github, final String owner, final String repo) {
this.github = github;
this.owner = owner;
this.repo = repo;
this.maybeRepo = Optional.of(repo);
}

GithubAppClient(final GitHubClient github, final String owner) {
this.github = github;
this.owner = owner;
this.maybeRepo = Optional.empty();
}

/**
Expand All @@ -64,13 +77,30 @@ public CompletableFuture<List<Installation>> getInstallations() {
}

/**
* Get Installation of a repo
* Get Installation
*
* @return a list of Installation
* @return an Installation
*/
public CompletableFuture<Installation> getInstallation() {
return maybeRepo.map(this::getRepoInstallation).orElseGet(this::getOrgInstallation);
}

/**
* Get an installation of a repo
* @return an Installation
*/
private CompletableFuture<Installation> getRepoInstallation(final String repo) {
return github.request(
String.format(GET_INSTALLATION_REPO_URL, owner, repo), Installation.class);
}

/**
* Get an installation of an org
* @return an Installation
*/
private CompletableFuture<Installation> getOrgInstallation() {
return github.request(
String.format(GET_INSTALLATION_REPO_URL, owner, repo), Installation.class, extraHeaders);
String.format(GET_INSTALLATION_ORG_URL, owner), Installation.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,8 @@
//
package com.spotify.github.v3.clients;

import java.lang.invoke.MethodHandles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OrganisationClient {

private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private static final String TEAM_TEMPLATE = "/orgs/%s/teams";

private static final String TEAM_SLUG_TEMPLATE = "/orgs/%s/teams/%s";

private final GitHubClient github;

private final String org;
Expand All @@ -53,4 +43,13 @@ static OrganisationClient create(final GitHubClient github, final String org) {
public TeamClient createTeamClient() {
return TeamClient.create(github, org);
}

/**
* Create GitHub App API client
*
* @return GitHub App API client
*/
public GithubAppClient createGithubAppClient() {
return new GithubAppClient(github, org);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@

package com.spotify.github.v3.clients;

import static com.google.common.io.Resources.getResource;
import static java.nio.charset.Charset.defaultCharset;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.Resources;
import com.spotify.github.FixtureHelper;
import com.spotify.github.v3.apps.InstallationRepositoriesResponse;
import com.spotify.github.v3.checks.Installation;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
Expand All @@ -49,13 +56,17 @@ public class GithubAppClientTest {
private final int appId = 42;
private GithubAppClient client;

private static String getFixture(String resource) throws IOException {
return Resources.toString(getResource(GithubAppClientTest.class, resource), defaultCharset());
}

@Before
public void setUp() throws Exception {
URI uri = mockServer.url("").uri();
File key = FixtureHelper.loadFile("githubapp/key.pem");

GitHubClient rootclient = GitHubClient.create(uri, key, appId);
client = rootclient.createRepositoryClient("", "").createGithubAppClient();
client = rootclient.createRepositoryClient("owner", "repo").createGithubAppClient();
}

@Test
Expand Down Expand Up @@ -122,4 +133,20 @@ public void listAccessibleRepositories() throws Exception {
assertThat(listReposRequest.getMethod(), is("GET"));
assertThat(listReposRequest.getRequestUrl().encodedPath(), is("/installation/repositories"));
}

@Test
public void getInstallation() throws Exception {
mockServer.enqueue(
new MockResponse()
.setResponseCode(200)
.setBody(FixtureHelper.loadFixture("githubapp/installation.json")));

Installation installation = client.getInstallation().join();

assertThat(installation.id(), is(1));
assertThat(installation.account().login(), is("github"));

RecordedRequest recordedRequest = mockServer.takeRequest(1, TimeUnit.MILLISECONDS);
assertThat(recordedRequest.getRequestUrl().encodedPath(), is("/repos/owner/repo/installation"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.common.io.Resources;
import com.spotify.github.jackson.Json;
import com.spotify.github.v3.Team;
import com.spotify.github.v3.checks.Installation;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import org.junit.Before;
Expand Down Expand Up @@ -66,4 +67,15 @@ public void testTeamClient() throws Exception {
assertThat(team.id(), is(1));
assertThat(team.name(), is("Justice League"));
}

@Test
public void testAppClient() throws Exception {
final GithubAppClient githubAppClient = organisationClient.createGithubAppClient();
final CompletableFuture<Installation> fixture =
completedFuture(json.fromJson(getFixture("../githubapp/installation.json"), Installation.class));
when(github.request("/orgs/github/installation", Installation.class)).thenReturn(fixture);
final Installation installation = githubAppClient.getInstallation().get();
assertThat(installation.id(), is(1));
assertThat(installation.account().login(), is("github"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"id": 1,
"account": {
"login": "github",
"id": 1,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE=",
"url": "https://api.github.com/orgs/github",
"repos_url": "https://api.github.com/orgs/github/repos",
"events_url": "https://api.github.com/orgs/github/events",
"hooks_url": "https://api.github.com/orgs/github/hooks",
"issues_url": "https://api.github.com/orgs/github/issues",
"members_url": "https://api.github.com/orgs/github/members{/member}",
"public_members_url": "https://api.github.com/orgs/github/public_members{/member}",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"description": "A great organization"
},
"access_tokens_url": "https://api.github.com/installations/1/access_tokens",
"repositories_url": "https://api.github.com/installation/repositories",
"html_url": "https://github.com/organizations/github/settings/installations/1",
"app_id": 1,
"target_id": 1,
"target_type": "Organization",
"permissions": {
"metadata": "read",
"contents": "read",
"issues": "write",
"single_file": "write"
},
"events": [
"push",
"pull_request"
],
"single_file_name": "config.yml"
}