Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/org/kohsuke/github/GHApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,36 @@ public PagedIterable<GHAppInstallation> listInstallations(final Instant since) {
return requester.toIterable(GHAppInstallation[].class, null);
}

/**
* Get a specific delivery for the webhook configured for a GitHub App.
* <p>
* You must use a JWT to access this endpoint.
*
* @param deliveryId
* the id of the delivery
* @return the delivery
* @throws IOException
* on error
* @see <a href="https://docs.github.com/en/rest/apps/webhooks#get-a-delivery-for-an-app-webhook">Get a delivery for
* an app webhook</a>
*/
public GHAppHookDelivery getDelivery(long deliveryId) throws IOException {
return root().createRequest()
.withUrlPath(String.format("/app/hook/deliveries/%d", deliveryId))
.fetch(GHAppHookDelivery.class);
}

/**
* List deliveries for the webhook configured for a GitHub App.
* <p>
* You must use a JWT to access this endpoint.
*
* @return a list of deliveries
* @see <a href="https://docs.github.com/en/rest/apps/webhooks#list-deliveries-for-an-app-webhook">List deliveries
* for an app webhook</a>
*/
public PagedIterable<GHAppHookDelivery> listDeliveries() {
return root().createRequest().withUrlPath("/app/hook/deliveries").toIterable(GHAppHookDelivery[].class, null);
}

}
174 changes: 174 additions & 0 deletions src/main/java/org/kohsuke/github/GHAppHookDelivery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.IOException;
import java.net.URL;

/**
* Represents a GitHub App webhook delivery.
*
* @see GHApp#listDeliveries()
* @see GHApp#getDelivery(long)
* @see <a href="https://docs.github.com/en/rest/apps/webhooks#list-deliveries-for-an-app-webhook">List deliveries for
* an app webhook</a>
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD" },
justification = "JSON API")
public class GHAppHookDelivery extends GHObject {

private String action;

private String deliveredAt;
private double duration;
private String event;
private String guid;
private String installationId;
private boolean redelivery;
private String repositoryId;
private GHAppHookDeliveryRequest request;
private GHAppHookDeliveryResponse response;
private String status;
private int statusCode;

/**
* Create default GHAppHookDelivery instance.
*/
public GHAppHookDelivery() {
}

/**
* Gets the action that triggered the delivery.
*
* @return the action
*/
public String getAction() {
return action;
}

/**
* Gets the time when the webhook delivery was made.
*
* @return the delivered at timestamp
*/
public String getDeliveredAt() {
return deliveredAt;
}

/**
* Gets the duration of the delivery in seconds.
*
* @return the duration
*/
public double getDuration() {
return duration;
}

/**
* Gets the event that triggered the delivery.
*
* @return the event
*/
public String getEvent() {
return event;
}

/**
* Gets the unique identifier of the delivery.
*
* @return the guid
*/
public String getGuid() {
return guid;
}

/**
* Gets the app installation ID.
*
* @return the installation ID
*/
public String getInstallationId() {
return installationId;
}

/**
* Gets the repository ID.
*
* @return the repository ID
*/
public String getRepositoryId() {
return repositoryId;
}

/**
* Gets the full request that was sent with this delivery (only available in detailed delivery).
*
* @return the request, or null if not available
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHAppHookDeliveryRequest getRequest() {
return request;
}

/**
* Gets the full response for this delivery (only available in detailed delivery).
*
* @return the response, or null if not available
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHAppHookDeliveryResponse getResponse() {
return response;
}

/**
* Gets the status of the delivery.
*
* @return the status
*/
public String getStatus() {
return status;
}

/**
* Gets the HTTP status code of the delivery response.
*
* @return the status code
*/
public int getStatusCode() {
return statusCode;
}

/**
* Gets the URL.
*
* @return the URL
*/
public URL getUrl() {
return GitHubClient.parseURL("/app/hook/deliveries/" + getId());
}

/**
* Whether this delivery is a redelivery.
*
* @return true if this is a redelivery
*/
public boolean isRedelivery() {
return redelivery;
}

/**
* Redeliver this webhook delivery.
*
* @throws IOException
* if the redelivery fails
* @see <a href="https://docs.github.com/en/rest/apps/webhooks#redeliver-a-delivery-for-an-app-webhook">Redeliver a
* delivery for an app webhook</a>
*/
public void redeliver() throws IOException {
root().createRequest()
.method("POST")
.withUrlPath("/app/hook/deliveries/" + getId() + "/attempts")
.send();
}

}
43 changes: 43 additions & 0 deletions src/main/java/org/kohsuke/github/GHAppHookDeliveryRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.Collections;
import java.util.Map;

/**
* Represents the request part of a GitHub App webhook delivery.
*
* @see GHAppHookDelivery#getRequest()
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD" },
justification = "JSON API")
public class GHAppHookDeliveryRequest {

private Map<String, String> headers;
private Object payload;

/**
* Create default GHAppHookDeliveryRequest instance.
*/
public GHAppHookDeliveryRequest() {
}

/**
* Gets the headers of the request.
*
* @return the headers as an unmodifiable map
*/
public Map<String, String> getHeaders() {
return headers != null ? Collections.unmodifiableMap(headers) : null;
}

/**
* Gets the payload of the request. The payload can be a Map or a String depending on the content type.
*
* @return the payload
*/
public Object getPayload() {
return payload;
}
}
43 changes: 43 additions & 0 deletions src/main/java/org/kohsuke/github/GHAppHookDeliveryResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.Collections;
import java.util.Map;

/**
* Represents the response part of a GitHub App webhook delivery.
*
* @see GHAppHookDelivery#getResponse()
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD" },
justification = "JSON API")
public class GHAppHookDeliveryResponse {

private Map<String, String> headers;
private String payload;

/**
* Create default GHAppHookDeliveryResponse instance.
*/
public GHAppHookDeliveryResponse() {
}

/**
* Gets the headers of the response.
*
* @return the headers as an unmodifiable map
*/
public Map<String, String> getHeaders() {
return headers != null ? Collections.unmodifiableMap(headers) : null;
}

/**
* Gets the payload of the response.
*
* @return the payload
*/
public String getPayload() {
return payload;
}
}
86 changes: 86 additions & 0 deletions src/test/java/org/kohsuke/github/GHAppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,90 @@ protected GitHubBuilder getGitHubBuilder() {
.withAuthorizationProvider(jwtProvider1);
}

/**
* Get a specific delivery for the webhook configured for a GitHub App.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getDelivery() throws IOException {
GHApp app = gitHub.getApp();
GHAppHookDelivery delivery = app.getDelivery(12345678);

assertThat(delivery.getId(), is(12345678L));
assertThat(delivery.getGuid(), is("0b989ba4-242f-11e5-81e1-c7b6966d2516"));
assertThat(delivery.getDeliveredAt(), is("2019-06-03T00:57:16Z"));
assertThat(delivery.isRedelivery(), is(false));
assertThat(delivery.getDuration(), is(0.27));
assertThat(delivery.getStatus(), is("OK"));
assertThat(delivery.getStatusCode(), is(200));
assertThat(delivery.getEvent(), is("issues"));
assertThat(delivery.getAction(), is("opened"));
assertThat(delivery.getInstallationId(), is("123456"));
assertThat(delivery.getRepositoryId(), is("654321"));

// Verify request details
GHAppHookDeliveryRequest request = delivery.getRequest();
assertThat(request, is(notNullValue()));
assertThat(request.getHeaders(), is(notNullValue()));
assertThat(request.getHeaders().get("X-GitHub-Event"), is("issues"));
assertThat(request.getPayload(), is(notNullValue()));

// Verify response details
GHAppHookDeliveryResponse response = delivery.getResponse();
assertThat(response, is(notNullValue()));
assertThat(response.getHeaders(), is(notNullValue()));
assertThat(response.getPayload(), is("ok"));
}

/**
* List deliveries for the webhook configured for a GitHub App.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void listDeliveries() throws IOException {
GHApp app = gitHub.getApp();
List<GHAppHookDelivery> deliveries = app.listDeliveries().toList();

assertThat(deliveries.size(), is(2));

GHAppHookDelivery delivery1 = deliveries.get(0);
assertThat(delivery1.getId(), is(12345678L));
assertThat(delivery1.getGuid(), is("0b989ba4-242f-11e5-81e1-c7b6966d2516"));
assertThat(delivery1.isRedelivery(), is(false));
assertThat(delivery1.getStatus(), is("OK"));
assertThat(delivery1.getStatusCode(), is(200));
assertThat(delivery1.getEvent(), is("issues"));
assertThat(delivery1.getAction(), is("opened"));

GHAppHookDelivery delivery2 = deliveries.get(1);
assertThat(delivery2.getId(), is(12345679L));
assertThat(delivery2.getGuid(), is("1c989ba4-242f-11e5-81e1-c7b6966d2517"));
assertThat(delivery2.isRedelivery(), is(true));
assertThat(delivery2.getEvent(), is("push"));
assertThat(delivery2.getAction(), nullValue());
}

/**
* Redeliver a delivery for the webhook configured for a GitHub App.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void redeliverDelivery() throws IOException {
GHApp app = gitHub.getApp();
GHAppHookDelivery delivery = app.getDelivery(12345678);

// Redeliver should not throw an exception
try {
delivery.redeliver();
} catch (IOException e) {
fail("redeliver wasn't suppose to fail in this test");
}
}

}
Loading