From 60340116f2c96336342e26a9309fb1c9d5f512c1 Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Sun, 17 Mar 2024 20:00:29 +0100 Subject: [PATCH] Reintroduce GitHubEvent#getParsedPayload() This version behaves a bit differently than the previous one. It won't be serialized so should not be used in the Replay UI. Also improve things a bit on how we manipulate the response buffer. --- .../modules/ROOT/pages/developer-reference.adoc | 2 +- .../modules/ROOT/pages/includes/attributes.adoc | 2 +- .../io/quarkiverse/githubapp/GitHubEvent.java | 16 +++++++++++++++- .../quarkiverse/githubapp/runtime/Routes.java | 17 ++++++++++------- .../runtime/error/DefaultErrorHandler.java | 4 +--- 5 files changed, 28 insertions(+), 13 deletions(-) diff --git a/docs/modules/ROOT/pages/developer-reference.adoc b/docs/modules/ROOT/pages/developer-reference.adoc index ed83c570..c1ce210e 100644 --- a/docs/modules/ROOT/pages/developer-reference.adoc +++ b/docs/modules/ROOT/pages/developer-reference.adoc @@ -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. diff --git a/docs/modules/ROOT/pages/includes/attributes.adoc b/docs/modules/ROOT/pages/includes/attributes.adoc index 3a0f7dc9..e1c93067 100644 --- a/docs/modules/ROOT/pages/includes/attributes.adoc +++ b/docs/modules/ROOT/pages/includes/attributes.adoc @@ -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/ \ No newline at end of file diff --git a/runtime/src/main/java/io/quarkiverse/githubapp/GitHubEvent.java b/runtime/src/main/java/io/quarkiverse/githubapp/GitHubEvent.java index 19404d58..f64b214f 100644 --- a/runtime/src/main/java/io/quarkiverse/githubapp/GitHubEvent.java +++ b/runtime/src/main/java/io/quarkiverse/githubapp/GitHubEvent.java @@ -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. *

@@ -29,10 +31,12 @@ 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; @@ -40,6 +44,7 @@ public GitHubEvent(Long installationId, String appName, String deliveryId, Strin this.event = event; this.action = action; this.payload = payload; + this.parsedPayload = parsedPayload; this.replayed = replayed; StringBuilder eventActionSb = new StringBuilder(); @@ -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; } diff --git a/runtime/src/main/java/io/quarkiverse/githubapp/runtime/Routes.java b/runtime/src/main/java/io/quarkiverse/githubapp/runtime/Routes.java index 3f9b5870..671c98d0 100644 --- a/runtime/src/main/java/io/quarkiverse/githubapp/runtime/Routes.java +++ b/runtime/src/main/java/io/quarkiverse/githubapp/runtime/Routes.java @@ -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; @@ -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; @@ -103,14 +105,15 @@ 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 + "-" @@ -118,10 +121,10 @@ public void handleRequest(RoutingContext routingContext, 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); diff --git a/runtime/src/main/java/io/quarkiverse/githubapp/runtime/error/DefaultErrorHandler.java b/runtime/src/main/java/io/quarkiverse/githubapp/runtime/error/DefaultErrorHandler.java index e1ec3279..25f279f4 100644 --- a/runtime/src/main/java/io/quarkiverse/githubapp/runtime/error/DefaultErrorHandler.java +++ b/runtime/src/main/java/io/quarkiverse/githubapp/runtime/error/DefaultErrorHandler.java @@ -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 @@ -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"); }