Skip to content

Commit 392b505

Browse files
authored
Add support to get a user installation for the authenticated app (spotify#191)
* 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
1 parent a8848f5 commit 392b505

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

src/main/java/com/spotify/github/v3/clients/GitHubClient.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,13 @@ public OrganisationClient createOrganisationClient(final String org) {
431431
return OrganisationClient.create(this, org);
432432
}
433433

434-
public UserClient createUserClient() {
435-
return UserClient.create(this);
434+
/**
435+
* Create user API client
436+
*
437+
* @return user API client
438+
*/
439+
public UserClient createUserClient(final String owner) {
440+
return UserClient.create(this, owner);
436441
}
437442

438443
Json json() {

src/main/java/com/spotify/github/v3/clients/GithubAppClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class GithubAppClient {
4545
refer to the organisation in the installation endpoint
4646
*/
4747
private static final String GET_INSTALLATION_ORG_URL = "/orgs/%s/installation";
48+
private static final String GET_INSTALLATION_USER_URL = "/users/%s/installation";
4849

4950
private final GitHubClient github;
5051
private final String owner;
@@ -114,6 +115,15 @@ private CompletableFuture<Installation> getOrgInstallation() {
114115
String.format(GET_INSTALLATION_ORG_URL, owner), Installation.class);
115116
}
116117

118+
/**
119+
* Get an installation of a user
120+
* @return an Installation
121+
*/
122+
public CompletableFuture<Installation> getUserInstallation() {
123+
return github.request(
124+
String.format(GET_INSTALLATION_USER_URL, owner), Installation.class);
125+
}
126+
117127
/**
118128
* Authenticates as an installation
119129
*

src/main/java/com/spotify/github/v3/clients/UserClient.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ public class UserClient {
2727

2828
public static final int NO_CONTENT = 204;
2929
private final GitHubClient github;
30+
private final String owner;
3031

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

33-
UserClient(final GitHubClient github) {
34+
UserClient(final GitHubClient github, final String owner) {
3435
this.github = github;
36+
this.owner = owner;
3537
}
3638

37-
static UserClient create(final GitHubClient github) {
38-
return new UserClient(github);
39+
static UserClient create(final GitHubClient github, final String owner) {
40+
return new UserClient(github, owner);
41+
}
42+
43+
public GithubAppClient createGithubAppClient() {
44+
return new GithubAppClient(this.github, this.owner);
3945
}
4046

4147
/**

src/test/java/com/spotify/github/v3/clients/UserClientTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,25 @@
1919
*/
2020
package com.spotify.github.v3.clients;
2121

22+
import static com.google.common.io.Resources.getResource;
23+
import static java.nio.charset.Charset.defaultCharset;
2224
import static java.util.concurrent.CompletableFuture.completedFuture;
25+
import static org.hamcrest.MatcherAssert.assertThat;
26+
import static org.hamcrest.core.Is.is;
2327
import static org.junit.jupiter.api.Assertions.*;
2428
import static org.mockito.ArgumentMatchers.any;
2529
import static org.mockito.ArgumentMatchers.eq;
2630
import static org.mockito.Mockito.mock;
2731
import static org.mockito.Mockito.when;
2832

33+
import com.google.common.io.Resources;
2934
import com.spotify.github.jackson.Json;
35+
import com.spotify.github.v3.checks.Installation;
3036
import com.spotify.github.v3.user.requests.ImmutableSuspensionReason;
37+
38+
import java.io.IOException;
3139
import java.util.concurrent.CompletableFuture;
40+
3241
import okhttp3.Response;
3342
import org.junit.jupiter.api.BeforeEach;
3443
import org.junit.jupiter.api.Test;
@@ -37,12 +46,17 @@ public class UserClientTest {
3746

3847
private GitHubClient github;
3948
private UserClient userClient;
49+
private String owner = "github";
50+
private Json json;
51+
private static String getFixture(String resource) throws IOException {
52+
return Resources.toString(getResource(TeamClientTest.class, resource), defaultCharset());
53+
}
4054

4155
@BeforeEach
4256
public void setUp() {
4357
github = mock(GitHubClient.class);
44-
userClient = new UserClient(github);
45-
Json json = Json.create();
58+
userClient = new UserClient(github, owner);
59+
json = Json.create();
4660
when(github.json()).thenReturn(json);
4761
}
4862

@@ -81,4 +95,17 @@ public void testUnSuspendUserFailure() throws Exception {
8195
final CompletableFuture<Boolean> result = userClient.unSuspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build());
8296
assertFalse(result.get());
8397
}
98+
99+
@Test
100+
public void testAppClient() throws Exception {
101+
final GithubAppClient githubAppClient = userClient.createGithubAppClient();
102+
final CompletableFuture<Installation> fixture =
103+
completedFuture(json.fromJson(getFixture("../githubapp/installation.json"), Installation.class));
104+
when(github.request("/users/github/installation", Installation.class)).thenReturn(fixture);
105+
106+
final Installation installation = githubAppClient.getUserInstallation().get();
107+
108+
assertThat(installation.id(), is(1));
109+
assertThat(installation.account().login(), is("github"));
110+
}
84111
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy