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..c2c49c04 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,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 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 + } } 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..5c7c79e1 --- /dev/null +++ b/src/main/java/com/spotify/github/v3/repos/requests/RepositoryDispatch.java @@ -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 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..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; @@ -714,4 +717,26 @@ public void shouldReturnEmptyOptionalWhenResponseBodyNotPresent() throws Excepti Optional 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); + } + }