From 804e1ee6064f23da38ec5ba3a09ac52d88fa8fdc Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Thu, 9 May 2024 00:43:08 +0100 Subject: [PATCH 01/12] add repositry_dispatch endpoint Signed-off-by: Ajmal Kottilingal --- .../github/v3/clients/RepositoryClient.java | 15 +++++++++++ .../v3/repos/requests/RepositoryDispatch.java | 25 +++++++++++++++++++ .../v3/clients/RepositoryClientTest.java | 3 +++ 3 files changed, 43 insertions(+) create mode 100644 src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java diff --git a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java index 97fd5bc1..99102378 100644 --- a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java +++ b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java @@ -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"; @@ -698,4 +699,18 @@ 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. + */ + + private CompletableFuture createRepositoryDispatchEvent(RepositoryDispatch request) { + final String path = String.format(CREATE_REPOSITORY_DISPATCH_EVENT_TEMPLATE, repo, owner); + + return github + .post(path, github.json().toJsonUnchecked(request)) + .thenAccept(IGNORE_RESPONSE_CONSUMER); //Should return a 204 for happy flow. + } } diff --git a/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java b/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java new file mode 100644 index 00000000..34884c7c --- /dev/null +++ b/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java @@ -0,0 +1,25 @@ +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 javax.annotation.Nullable; +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. */ + @Nullable + Optional clientPayload(); + +} diff --git a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java index 3f1e8e56..1358cb05 100644 --- a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java @@ -42,6 +42,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import com.fasterxml.jackson.databind.JsonNode; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.io.Resources; @@ -77,6 +78,7 @@ import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; +import org.jetbrains.annotations.Nullable; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -714,4 +716,5 @@ public void shouldReturnEmptyOptionalWhenResponseBodyNotPresent() throws Excepti Optional response = repoClient.downloadZipball("master").get(); assertThat(response, is(Optional.empty())); } + } From 44944b084f2c649352a4dc165b33841938f1b340 Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Thu, 9 May 2024 00:44:41 +0100 Subject: [PATCH 02/12] remove unused import Signed-off-by: Ajmal Kottilingal --- .../java/com/spotify/github/v3/clients/RepositoryClient.java | 2 +- .../com/spotify/github/v3/clients/RepositoryClientTest.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java index 99102378..d2d2eaf4 100644 --- a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java +++ b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java @@ -711,6 +711,6 @@ private CompletableFuture createRepositoryDispatchEvent(RepositoryDispatch return github .post(path, github.json().toJsonUnchecked(request)) - .thenAccept(IGNORE_RESPONSE_CONSUMER); //Should return a 204 for happy flow. + .thenAccept(IGNORE_RESPONSE_CONSUMER); //Should return a 204 for happy flow. } } diff --git a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java index 1358cb05..d772781a 100644 --- a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java @@ -42,7 +42,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import com.fasterxml.jackson.databind.JsonNode; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.io.Resources; From 7f12a4a05188a292e9df872247434f9d7b323663 Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Thu, 9 May 2024 00:45:06 +0100 Subject: [PATCH 03/12] remove unused import Signed-off-by: Ajmal Kottilingal --- .../java/com/spotify/github/v3/clients/RepositoryClientTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java index d772781a..023777a5 100644 --- a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java @@ -77,7 +77,6 @@ import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; -import org.jetbrains.annotations.Nullable; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; From 91159fa95de0ea156abb5c1c05c064f990bcaf2a Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Thu, 9 May 2024 00:47:31 +0100 Subject: [PATCH 04/12] change order of owner and repo in path Signed-off-by: Ajmal Kottilingal --- .../java/com/spotify/github/v3/clients/RepositoryClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java index d2d2eaf4..2d9aec7c 100644 --- a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java +++ b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java @@ -707,7 +707,7 @@ private String getContentPath(final String path, final String query) { */ private CompletableFuture createRepositoryDispatchEvent(RepositoryDispatch request) { - final String path = String.format(CREATE_REPOSITORY_DISPATCH_EVENT_TEMPLATE, repo, owner); + final String path = String.format(CREATE_REPOSITORY_DISPATCH_EVENT_TEMPLATE, owner, repo); return github .post(path, github.json().toJsonUnchecked(request)) From 81791f2482729425ab97a277f1255fb578c6c5bc Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Thu, 9 May 2024 00:49:43 +0100 Subject: [PATCH 05/12] make param final Signed-off-by: Ajmal Kottilingal --- .../java/com/spotify/github/v3/clients/RepositoryClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java index 2d9aec7c..edd81182 100644 --- a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java +++ b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java @@ -706,7 +706,7 @@ private String getContentPath(final String path, final String query) { * @param request The repository dispatch request. */ - private CompletableFuture createRepositoryDispatchEvent(RepositoryDispatch request) { + private CompletableFuture createRepositoryDispatchEvent(final RepositoryDispatch request) { final String path = String.format(CREATE_REPOSITORY_DISPATCH_EVENT_TEMPLATE, owner, repo); return github From d50a983ab38bd23e8a1fb79b511119fad6f0419b Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Thu, 9 May 2024 00:53:50 +0100 Subject: [PATCH 06/12] add header Signed-off-by: Ajmal Kottilingal --- .../v3/repos/requests/RepositoryDispatch.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java b/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java index 34884c7c..ddccd6e7 100644 --- a/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java +++ b/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java @@ -1,3 +1,23 @@ +/*- + * -\-\- + * 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; From f71c23240e2a2b9765f2a913d3c361fbd4b453d6 Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Thu, 9 May 2024 00:58:30 +0100 Subject: [PATCH 07/12] update comment Signed-off-by: Ajmal Kottilingal --- .../java/com/spotify/github/v3/clients/RepositoryClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java index edd81182..d9fcddbf 100644 --- a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java +++ b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java @@ -711,6 +711,6 @@ private CompletableFuture createRepositoryDispatchEvent(final RepositoryDi return github .post(path, github.json().toJsonUnchecked(request)) - .thenAccept(IGNORE_RESPONSE_CONSUMER); //Should return a 204 for happy flow. + .thenAccept(IGNORE_RESPONSE_CONSUMER); //Should return a 204 with an empty response body. } } From 4e36959e5bd4c56faa27aa9f7e7538e119516dcf Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Thu, 9 May 2024 00:58:58 +0100 Subject: [PATCH 08/12] change access of method Signed-off-by: Ajmal Kottilingal --- .../java/com/spotify/github/v3/clients/RepositoryClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java index d9fcddbf..fb47cf45 100644 --- a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java +++ b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java @@ -706,7 +706,7 @@ private String getContentPath(final String path, final String query) { * @param request The repository dispatch request. */ - private CompletableFuture createRepositoryDispatchEvent(final RepositoryDispatch request) { + public CompletableFuture createRepositoryDispatchEvent(final RepositoryDispatch request) { final String path = String.format(CREATE_REPOSITORY_DISPATCH_EVENT_TEMPLATE, owner, repo); return github From ec830f458eda537ed98e642c47dc670f93f964db Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Thu, 9 May 2024 01:10:06 +0100 Subject: [PATCH 09/12] remove nullable annotation Signed-off-by: Ajmal Kottilingal --- .../com/spotify/github/v3/repos/requests/RepositoryDispatch.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java b/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java index ddccd6e7..2310567c 100644 --- a/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java +++ b/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java @@ -39,7 +39,6 @@ public interface RepositoryDispatch { /** JSON payload with extra information about the webhook event * that your action or workflow may use. */ - @Nullable Optional clientPayload(); } From 4c8cb1caa285220f494c27ccf6006ddbffc9387d Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Thu, 9 May 2024 09:13:59 +0100 Subject: [PATCH 10/12] remove nullable import Signed-off-by: Ajmal Kottilingal --- .../com/spotify/github/v3/repos/requests/RepositoryDispatch.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java b/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java index 2310567c..49f2575e 100644 --- a/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java +++ b/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java @@ -25,7 +25,6 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.spotify.github.GithubStyle; import java.util.Optional; -import javax.annotation.Nullable; import org.immutables.value.Value; @Value.Immutable From 464f27d8b28ecf544ac5359ee462c887910c13fa Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Fri, 10 May 2024 14:53:35 +0100 Subject: [PATCH 11/12] add test Signed-off-by: Ajmal Kottilingal --- .../github/v3/clients/RepositoryClient.java | 6 ++--- .../v3/repos/requests/RepositoryDispatch.java | 1 + .../v3/clients/RepositoryClientTest.java | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java index fb47cf45..1460ffbd 100644 --- a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java +++ b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java @@ -60,6 +60,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import javax.ws.rs.core.HttpHeaders; +import okhttp3.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -706,11 +707,10 @@ private String getContentPath(final String path, final String query) { * @param request The repository dispatch request. */ - public CompletableFuture createRepositoryDispatchEvent(final RepositoryDispatch request) { + public CompletableFuture createRepositoryDispatchEvent(final RepositoryDispatch request) { final String path = String.format(CREATE_REPOSITORY_DISPATCH_EVENT_TEMPLATE, owner, repo); - return github .post(path, github.json().toJsonUnchecked(request)) - .thenAccept(IGNORE_RESPONSE_CONSUMER); //Should return a 204 with an empty response body. + .thenApply(response -> response.code() == NO_CONTENT); //should always return a 204 } } diff --git a/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java b/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java index 49f2575e..5c7c79e1 100644 --- a/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java +++ b/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java @@ -34,6 +34,7 @@ public interface RepositoryDispatch { /** The custom webhook event name */ + String eventType(); /** JSON payload with extra information about the webhook event diff --git a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java index 023777a5..bd8b7ace 100644 --- a/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java @@ -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; @@ -71,6 +73,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 okhttp3.MediaType; import okhttp3.Protocol; @@ -715,4 +718,25 @@ public void shouldReturnEmptyOptionalWhenResponseBodyNotPresent() throws Excepti 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); + } + } From 822beafb485550e3df8373557543e61403e36d82 Mon Sep 17 00:00:00 2001 From: Ajmal Kottilingal Date: Fri, 10 May 2024 14:54:53 +0100 Subject: [PATCH 12/12] remove unused import Signed-off-by: Ajmal Kottilingal --- .../java/com/spotify/github/v3/clients/RepositoryClient.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java index 1460ffbd..c2c49c04 100644 --- a/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java +++ b/src/main/java/com/spotify/github/v3/clients/RepositoryClient.java @@ -60,7 +60,6 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import javax.ws.rs.core.HttpHeaders; -import okhttp3.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory;