Skip to content

Commit

Permalink
Add support to get a user installation for the authenticated app (#191)
Browse files Browse the repository at this point in the history
* Fix merge with master conflicts

* Add support to get a user installation for the authenticated app

* Revert tag changes in pom after master merge

* Create the github app client from the user client and add tests
  • Loading branch information
annelled authored Jun 14, 2024
1 parent a8848f5 commit 392b505
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/spotify/github/v3/clients/GitHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,13 @@ public OrganisationClient createOrganisationClient(final String org) {
return OrganisationClient.create(this, org);
}

public UserClient createUserClient() {
return UserClient.create(this);
/**
* Create user API client
*
* @return user API client
*/
public UserClient createUserClient(final String owner) {
return UserClient.create(this, owner);
}

Json json() {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/spotify/github/v3/clients/GithubAppClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class GithubAppClient {
refer to the organisation in the installation endpoint
*/
private static final String GET_INSTALLATION_ORG_URL = "/orgs/%s/installation";
private static final String GET_INSTALLATION_USER_URL = "/users/%s/installation";

private final GitHubClient github;
private final String owner;
Expand Down Expand Up @@ -114,6 +115,15 @@ private CompletableFuture<Installation> getOrgInstallation() {
String.format(GET_INSTALLATION_ORG_URL, owner), Installation.class);
}

/**
* Get an installation of a user
* @return an Installation
*/
public CompletableFuture<Installation> getUserInstallation() {
return github.request(
String.format(GET_INSTALLATION_USER_URL, owner), Installation.class);
}

/**
* Authenticates as an installation
*
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/spotify/github/v3/clients/UserClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ public class UserClient {

public static final int NO_CONTENT = 204;
private final GitHubClient github;
private final String owner;

private static final String SUSPEND_USER_TEMPLATE = "/users/%s/suspended";

UserClient(final GitHubClient github) {
UserClient(final GitHubClient github, final String owner) {
this.github = github;
this.owner = owner;
}

static UserClient create(final GitHubClient github) {
return new UserClient(github);
static UserClient create(final GitHubClient github, final String owner) {
return new UserClient(github, owner);
}

public GithubAppClient createGithubAppClient() {
return new GithubAppClient(this.github, this.owner);
}

/**
Expand Down
31 changes: 29 additions & 2 deletions src/test/java/com/spotify/github/v3/clients/UserClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,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.core.Is.is;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.common.io.Resources;
import com.spotify.github.jackson.Json;
import com.spotify.github.v3.checks.Installation;
import com.spotify.github.v3.user.requests.ImmutableSuspensionReason;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;

import okhttp3.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -37,12 +46,17 @@ public class UserClientTest {

private GitHubClient github;
private UserClient userClient;
private String owner = "github";
private Json json;
private static String getFixture(String resource) throws IOException {
return Resources.toString(getResource(TeamClientTest.class, resource), defaultCharset());
}

@BeforeEach
public void setUp() {
github = mock(GitHubClient.class);
userClient = new UserClient(github);
Json json = Json.create();
userClient = new UserClient(github, owner);
json = Json.create();
when(github.json()).thenReturn(json);
}

Expand Down Expand Up @@ -81,4 +95,17 @@ public void testUnSuspendUserFailure() throws Exception {
final CompletableFuture<Boolean> result = userClient.unSuspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build());
assertFalse(result.get());
}

@Test
public void testAppClient() throws Exception {
final GithubAppClient githubAppClient = userClient.createGithubAppClient();
final CompletableFuture<Installation> fixture =
completedFuture(json.fromJson(getFixture("../githubapp/installation.json"), Installation.class));
when(github.request("/users/github/installation", Installation.class)).thenReturn(fixture);

final Installation installation = githubAppClient.getUserInstallation().get();

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

0 comments on commit 392b505

Please sign in to comment.