Skip to content

Commit

Permalink
merge: Merge from master
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhi347 committed Sep 11, 2024
2 parents d86b494 + 7cf19e5 commit a27750f
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

- uses: actions/checkout@v4

- uses: actions/setup-java@v3
- uses: actions/setup-java@v4
with:
java-version: 11
distribution: corretto
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>github-client</artifactId>
<version>0.2.18-SNAPSHOT</version>
<version>0.3.2-SNAPSHOT</version>

<parent>
<groupId>com.spotify</groupId>
Expand Down Expand Up @@ -84,7 +84,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.outputTimestamp>1710939443</project.build.outputTimestamp>
<project.build.outputTimestamp>1720619003</project.build.outputTimestamp>
<spotbugs.excludeFilterFile>spotbugsexclude.xml</spotbugs.excludeFilterFile>
<checkstyle.violationSeverity>error</checkstyle.violationSeverity>
<checkstyle.config.location>checkstyle.xml</checkstyle.config.location>
Expand Down
4 changes: 2 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 @@ -427,8 +427,8 @@ public OrganisationClient createOrganisationClient(final String org) {
return OrganisationClient.create(this, org);
}

public UserClient createUserClient() {
return UserClient.create(this);
public UserClient createUserClient(final String owner) {
return UserClient.create(this, owner);
}

Json json() {
Expand Down
12 changes: 11 additions & 1 deletion 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 @@ -78,7 +79,7 @@ public CompletableFuture<List<Installation>> getInstallations() {
}

/**
* Get Installation
* Get Installation of repo or org
*
* @return an Installation
*/
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
14 changes: 14 additions & 0 deletions src/main/java/com/spotify/github/v3/clients/RepositoryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public class RepositoryClient {
private static final String BRANCH_TEMPLATE = "/repos/%s/%s/branches/%s";
private static final String LIST_BRANCHES_TEMPLATE = "/repos/%s/%s/branches";
private static final String CREATE_COMMENT_TEMPLATE = "/repos/%s/%s/commits/%s/comments";
private static final String CREATE_REPOSITORY_DISPATCH_EVENT_TEMPLATE = "/repos/%s/%s/dispatches";
private static final String COMMENT_TEMPLATE = "/repos/%s/%s/comments/%s";
private static final String LANGUAGES_TEMPLATE = "/repos/%s/%s/languages";
private static final String MERGE_TEMPLATE = "/repos/%s/%s/merges";
Expand Down Expand Up @@ -698,4 +699,17 @@ private String getContentPath(final String path, final String query) {
}
return String.format(CONTENTS_URI_TEMPLATE, owner, repo, path, query);
}

/**
* Create a repository_dispatch event.
*
* @param request The repository dispatch request.
*/

public CompletableFuture<Boolean> createRepositoryDispatchEvent(final RepositoryDispatch request) {
final String path = String.format(CREATE_REPOSITORY_DISPATCH_EVENT_TEMPLATE, owner, repo);
return github
.post(path, github.json().toJsonUnchecked(request))
.thenApply(response -> response.code() == NO_CONTENT); //should always return a 204
}
}
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2023 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.v3.repos.requests;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.spotify.github.GithubStyle;
import java.util.Optional;
import org.immutables.value.Value;

@Value.Immutable
@GithubStyle
@JsonSerialize(as = ImmutableRepositoryDispatch.class)
@JsonDeserialize(as = ImmutableRepositoryDispatch.class)
public interface RepositoryDispatch {

/** The custom webhook event name */

String eventType();

/** JSON payload with extra information about the webhook event
* that your action or workflow may use. */
Optional<JsonNode> clientPayload();

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.io.Resources;
Expand Down Expand Up @@ -73,6 +75,7 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import com.spotify.github.v3.repos.requests.ImmutableAuthenticatedUserRepositoriesFilter;
Expand Down Expand Up @@ -722,4 +725,26 @@ public void shouldReturnEmptyOptionalWhenResponseBodyNotPresent() throws Excepti
Optional<InputStream> response = repoClient.downloadZipball("master").get();
assertThat(response, is(Optional.empty()));
}

@Test
public void shouldReturnEmptyResponseWhenRepositoryDispatchEndpointTriggered() throws Exception {
final Response response = mock(Response.class);
when(response.code()).thenReturn(204);

ObjectMapper mapper = new ObjectMapper();
ObjectNode clientPayload = mapper.createObjectNode();
clientPayload.put("my-custom-true-property","true");
clientPayload.put("my-custom-false-property", "false");

RepositoryDispatch repositoryDispatchRequest = ImmutableRepositoryDispatch.builder()
.eventType("my-custom-event")
.clientPayload(clientPayload)
.build();

when(github.post("/repos/someowner/somerepo/dispatches", json.toJsonUnchecked(repositoryDispatchRequest))).thenReturn(completedFuture(response));

boolean repoDispatchResult = repoClient.createRepositoryDispatchEvent(repositoryDispatchRequest).get();
assertTrue(repoDispatchResult);
}

}
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 a27750f

Please sign in to comment.