Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to get a user installation for the authenticated app #191

Merged
merged 4 commits into from
Jun 14, 2024
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
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 @@
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);

Check warning on line 440 in src/main/java/com/spotify/github/v3/clients/GitHubClient.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L440 was not covered by tests
}

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 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);

Check warning on line 40 in src/main/java/com/spotify/github/v3/clients/UserClient.java

View check run for this annotation

Codecov / codecov/patch

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

Added line #L40 was not covered by tests
}

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"));
}
}
Loading