Skip to content

Commit

Permalink
Merge pull request #582 from gsmet/reintroduce-parsed-payload
Browse files Browse the repository at this point in the history
Reintroduce GitHubEvent#getParsedPayload()
  • Loading branch information
gsmet authored Mar 17, 2024
2 parents 7a4101b + 6034011 commit 03417b3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/developer-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class TriageIssue {
}
----

The `GitHubEvent` exposes the raw JSON either as a string or as a Vert.x `JsonObject`,
The `GitHubEvent` exposes the raw JSON either as a string (via `GitHubEvent#getPayload()`) or as a Vert.x `JsonObject` (via `GitHubEvent#getParsedPayload()`),
together with some additional information like the installation id, the event or the action.

If you miss an event type, it is also possible to listen to events by using the `@RawEvent` annotation.
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/includes/attributes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
:github-api-javadoc-root-url: https://github-api.kohsuke.org/apidocs/org/kohsuke/github
:github-reference-documentation-root-url: https://docs.github.com/en/free-pro-team@latest/developers

:command-airline-examples-dir: ./../examples/command/airline/
:command-airline-examples-dir: ./../examples/command/airline/
16 changes: 15 additions & 1 deletion runtime/src/main/java/io/quarkiverse/githubapp/GitHubEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import com.fasterxml.jackson.annotation.JsonIgnore;

import io.vertx.core.json.JsonObject;

/**
* This object will be serialized to JSON when the replay is enabled.
* <p>
Expand All @@ -29,17 +31,20 @@ public class GitHubEvent {

private final String payload;

private final JsonObject parsedPayload;

private final boolean replayed;

public GitHubEvent(Long installationId, String appName, String deliveryId, String repository, String event, String action,
String payload, boolean replayed) {
String payload, JsonObject parsedPayload, boolean replayed) {
this.installationId = installationId;
this.appName = Optional.ofNullable(appName);
this.deliveryId = deliveryId;
this.repository = Optional.ofNullable(repository);
this.event = event;
this.action = action;
this.payload = payload;
this.parsedPayload = parsedPayload;
this.replayed = replayed;

StringBuilder eventActionSb = new StringBuilder();
Expand Down Expand Up @@ -90,6 +95,15 @@ public String getPayload() {
return payload;
}

@JsonIgnore
public JsonObject getParsedPayload() {
if (parsedPayload == null) {
throw new IllegalStateException("getParsedPayload() may not be called on GitHubEvents that have been serialized");
}

return parsedPayload;
}

public boolean isReplayed() {
return replayed;
}
Expand Down
17 changes: 10 additions & 7 deletions runtime/src/main/java/io/quarkiverse/githubapp/runtime/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static io.quarkiverse.githubapp.runtime.Headers.X_REQUEST_ID;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
Expand All @@ -32,6 +33,7 @@
import io.quarkus.vertx.web.Route.HandlerType;
import io.quarkus.vertx.web.Route.HttpMethod;
import io.quarkus.vertx.web.RoutingExchange;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;

Expand Down Expand Up @@ -103,25 +105,26 @@ public void handleRequest(RoutingContext routingContext,
}
}

JsonObject body = routingContext.body().asJsonObject();

if (body == null) {
if (bodyBytes.length == 0) {
routingExchange.ok().end();
return;
}

String action = body.getString("action");
String payload = new String(bodyBytes, StandardCharsets.UTF_8);
JsonObject payloadObject = (JsonObject) Json.decodeValue(payload);

String action = payloadObject.getString("action");

if (!isBlank(deliveryId) && checkedConfigProvider.debug().payloadDirectory.isPresent()) {
String fileName = DATE_TIME_FORMATTER.format(LocalDateTime.now()) + "-" + event + "-"
+ (!isBlank(action) ? action + "-" : "") + deliveryId + ".json";
Files.write(checkedConfigProvider.debug().payloadDirectory.get().resolve(fileName), bodyBytes);
}

Long installationId = extractInstallationId(body);
String repository = extractRepository(body);
Long installationId = extractInstallationId(payloadObject);
String repository = extractRepository(payloadObject);
GitHubEvent gitHubEvent = new GitHubEvent(installationId, checkedConfigProvider.appName().orElse(null), deliveryId,
repository, event, action, routingContext.body().asString(), "true".equals(replayed) ? true : false);
repository, event, action, payload, payloadObject, "true".equals(replayed) ? true : false);

if (launchMode == LaunchMode.DEVELOPMENT && replayRouteInstance.isResolvable()) {
replayRouteInstance.get().pushEvent(gitHubEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import io.quarkiverse.githubapp.runtime.github.PayloadHelper;
import io.quarkus.arc.DefaultBean;
import io.quarkus.runtime.LaunchMode;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;

@ApplicationScoped
@DefaultBean
Expand Down Expand Up @@ -50,7 +48,7 @@ public void handleError(GitHubEvent gitHubEvent, GHEventPayload payload, Throwab
if (launchMode.isDevOrTest()) {
errorMessage.append("› Payload:\n")
.append("----\n")
.append(((JsonObject) Json.decodeValue(gitHubEvent.getPayload())).encodePrettily()).append("\n")
.append(gitHubEvent.getParsedPayload().encodePrettily()).append("\n")
.append("----\n");
}

Expand Down

0 comments on commit 03417b3

Please sign in to comment.