Skip to content

Commit 8a0fa55

Browse files
authored
Add repository_dispatch endpoint (#186)
* add repositry_dispatch endpoint Signed-off-by: Ajmal Kottilingal <[email protected]> * remove unused import Signed-off-by: Ajmal Kottilingal <[email protected]> * remove unused import Signed-off-by: Ajmal Kottilingal <[email protected]> * change order of owner and repo in path Signed-off-by: Ajmal Kottilingal <[email protected]> * make param final Signed-off-by: Ajmal Kottilingal <[email protected]> * add header Signed-off-by: Ajmal Kottilingal <[email protected]> * update comment Signed-off-by: Ajmal Kottilingal <[email protected]> * change access of method Signed-off-by: Ajmal Kottilingal <[email protected]> * remove nullable annotation Signed-off-by: Ajmal Kottilingal <[email protected]> * remove nullable import Signed-off-by: Ajmal Kottilingal <[email protected]> * add test Signed-off-by: Ajmal Kottilingal <[email protected]> * remove unused import Signed-off-by: Ajmal Kottilingal <[email protected]> --------- Signed-off-by: Ajmal Kottilingal <[email protected]>
1 parent abc0285 commit 8a0fa55

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

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
}
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;
@@ -71,6 +73,7 @@
7173
import java.util.List;
7274
import java.util.Optional;
7375
import java.util.concurrent.CompletableFuture;
76+
import java.util.concurrent.ExecutionException;
7477
import java.util.stream.Collectors;
7578
import okhttp3.MediaType;
7679
import okhttp3.Protocol;
@@ -714,4 +717,26 @@ public void shouldReturnEmptyOptionalWhenResponseBodyNotPresent() throws Excepti
714717
Optional<InputStream> response = repoClient.downloadZipball("master").get();
715718
assertThat(response, is(Optional.empty()));
716719
}
720+
721+
@Test
722+
public void shouldReturnEmptyResponseWhenRepositoryDispatchEndpointTriggered() throws Exception {
723+
final Response response = mock(Response.class);
724+
when(response.code()).thenReturn(204);
725+
726+
ObjectMapper mapper = new ObjectMapper();
727+
ObjectNode clientPayload = mapper.createObjectNode();
728+
clientPayload.put("my-custom-true-property","true");
729+
clientPayload.put("my-custom-false-property", "false");
730+
731+
RepositoryDispatch repositoryDispatchRequest = ImmutableRepositoryDispatch.builder()
732+
.eventType("my-custom-event")
733+
.clientPayload(clientPayload)
734+
.build();
735+
736+
when(github.post("/repos/someowner/somerepo/dispatches", json.toJsonUnchecked(repositoryDispatchRequest))).thenReturn(completedFuture(response));
737+
738+
boolean repoDispatchResult = repoClient.createRepositoryDispatchEvent(repositoryDispatchRequest).get();
739+
assertTrue(repoDispatchResult);
740+
}
741+
717742
}

0 commit comments

Comments
 (0)