Skip to content

Commit

Permalink
Add repository_dispatch endpoint (#186)
Browse files Browse the repository at this point in the history
* add repositry_dispatch endpoint

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

* remove unused import

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

* remove unused import

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

* change order of owner and repo in path

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

* make param final

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

* add header

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

* update comment

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

* change access of method

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

* remove nullable annotation

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

* remove nullable import

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

* add test

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

* remove unused import

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>

---------

Signed-off-by: Ajmal Kottilingal <ajmal.kottilingal@transferwise.com>
  • Loading branch information
ajmalab authored Jun 13, 2024
1 parent abc0285 commit 8a0fa55
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
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
}
}
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 @@ -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;
Expand Down Expand Up @@ -714,4 +717,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);
}

}

0 comments on commit 8a0fa55

Please sign in to comment.