Skip to content

Commit fd72410

Browse files
committed
merge: Merge from master
2 parents d86b494 + 7cf19e5 commit fd72410

File tree

8 files changed

+135
-9
lines changed

8 files changed

+135
-9
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
- uses: actions/checkout@v4
1717

18-
- uses: actions/setup-java@v3
18+
- uses: actions/setup-java@v4
1919
with:
2020
java-version: 11
2121
distribution: corretto

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44

55
<artifactId>github-client</artifactId>
6-
<version>0.2.18-SNAPSHOT</version>
6+
<version>0.3.2-SNAPSHOT</version>
77

88
<parent>
99
<groupId>com.spotify</groupId>
@@ -84,7 +84,7 @@
8484
<properties>
8585
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
8686
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
87-
<project.build.outputTimestamp>1710939443</project.build.outputTimestamp>
87+
<project.build.outputTimestamp>1720619003</project.build.outputTimestamp>
8888
<spotbugs.excludeFilterFile>spotbugsexclude.xml</spotbugs.excludeFilterFile>
8989
<checkstyle.violationSeverity>error</checkstyle.violationSeverity>
9090
<checkstyle.config.location>checkstyle.xml</checkstyle.config.location>

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

Lines changed: 11 additions & 1 deletion
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;
@@ -78,7 +79,7 @@ public CompletableFuture<List<Installation>> getInstallations() {
7879
}
7980

8081
/**
81-
* Get Installation
82+
* Get Installation of repo or org
8283
*
8384
* @return an Installation
8485
*/
@@ -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/RepositoryClient.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class RepositoryClient {
8585
private static final String BRANCH_TEMPLATE = "/repos/%s/%s/branches/%s";
8686
private static final String LIST_BRANCHES_TEMPLATE = "/repos/%s/%s/branches";
8787
private static final String CREATE_COMMENT_TEMPLATE = "/repos/%s/%s/commits/%s/comments";
88+
private static final String CREATE_REPOSITORY_DISPATCH_EVENT_TEMPLATE = "/repos/%s/%s/dispatches";
8889
private static final String COMMENT_TEMPLATE = "/repos/%s/%s/comments/%s";
8990
private static final String LANGUAGES_TEMPLATE = "/repos/%s/%s/languages";
9091
private static final String MERGE_TEMPLATE = "/repos/%s/%s/merges";
@@ -698,4 +699,17 @@ private String getContentPath(final String path, final String query) {
698699
}
699700
return String.format(CONTENTS_URI_TEMPLATE, owner, repo, path, query);
700701
}
702+
703+
/**
704+
* Create a repository_dispatch event.
705+
*
706+
* @param request The repository dispatch request.
707+
*/
708+
709+
public CompletableFuture<Boolean> createRepositoryDispatchEvent(final RepositoryDispatch request) {
710+
final String path = String.format(CREATE_REPOSITORY_DISPATCH_EVENT_TEMPLATE, owner, repo);
711+
return github
712+
.post(path, github.json().toJsonUnchecked(request))
713+
.thenApply(response -> response.code() == NO_CONTENT); //should always return a 204
714+
}
701715
}

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
/**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2023 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.repos.requests;
22+
23+
import com.fasterxml.jackson.databind.JsonNode;
24+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
25+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
26+
import com.spotify.github.GithubStyle;
27+
import java.util.Optional;
28+
import org.immutables.value.Value;
29+
30+
@Value.Immutable
31+
@GithubStyle
32+
@JsonSerialize(as = ImmutableRepositoryDispatch.class)
33+
@JsonDeserialize(as = ImmutableRepositoryDispatch.class)
34+
public interface RepositoryDispatch {
35+
36+
/** The custom webhook event name */
37+
38+
String eventType();
39+
40+
/** JSON payload with extra information about the webhook event
41+
* that your action or workflow may use. */
42+
Optional<JsonNode> clientPayload();
43+
44+
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import static org.mockito.Mockito.mock;
4343
import static org.mockito.Mockito.when;
4444

45+
import com.fasterxml.jackson.databind.ObjectMapper;
46+
import com.fasterxml.jackson.databind.node.ObjectNode;
4547
import com.google.common.collect.ImmutableMap;
4648
import com.google.common.collect.Lists;
4749
import com.google.common.io.Resources;
@@ -73,6 +75,7 @@
7375
import java.util.List;
7476
import java.util.Optional;
7577
import java.util.concurrent.CompletableFuture;
78+
import java.util.concurrent.ExecutionException;
7679
import java.util.stream.Collectors;
7780

7881
import com.spotify.github.v3.repos.requests.ImmutableAuthenticatedUserRepositoriesFilter;
@@ -722,4 +725,26 @@ public void shouldReturnEmptyOptionalWhenResponseBodyNotPresent() throws Excepti
722725
Optional<InputStream> response = repoClient.downloadZipball("master").get();
723726
assertThat(response, is(Optional.empty()));
724727
}
728+
729+
@Test
730+
public void shouldReturnEmptyResponseWhenRepositoryDispatchEndpointTriggered() throws Exception {
731+
final Response response = mock(Response.class);
732+
when(response.code()).thenReturn(204);
733+
734+
ObjectMapper mapper = new ObjectMapper();
735+
ObjectNode clientPayload = mapper.createObjectNode();
736+
clientPayload.put("my-custom-true-property","true");
737+
clientPayload.put("my-custom-false-property", "false");
738+
739+
RepositoryDispatch repositoryDispatchRequest = ImmutableRepositoryDispatch.builder()
740+
.eventType("my-custom-event")
741+
.clientPayload(clientPayload)
742+
.build();
743+
744+
when(github.post("/repos/someowner/somerepo/dispatches", json.toJsonUnchecked(repositoryDispatchRequest))).thenReturn(completedFuture(response));
745+
746+
boolean repoDispatchResult = repoClient.createRepositoryDispatchEvent(repositoryDispatchRequest).get();
747+
assertTrue(repoDispatchResult);
748+
}
749+
725750
}

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)