use of io.quarkiverse.githubapp.GitHubEvent in project quarkus-github-app by quarkiverse.
the class Routes method handleRequest.
@Route(path = "/", type = HandlerType.BLOCKING, methods = HttpMethod.POST, consumes = "application/json", produces = "application/json")
public void handleRequest(RoutingContext routingContext, RoutingExchange routingExchange, @Header(X_REQUEST_ID) String requestId, @Header(X_HUB_SIGNATURE_256) String hubSignature, @Header(X_GITHUB_DELIVERY) String deliveryId, @Header(X_GITHUB_EVENT) String event, @Header(X_QUARKIVERSE_GITHUB_APP_REPLAYED) String replayed) throws IOException {
if (!launchMode.isDevOrTest() && (isBlank(deliveryId) || isBlank(hubSignature))) {
routingExchange.response().setStatusCode(400).end();
return;
}
JsonObject body = routingContext.getBodyAsJson();
if (body == null) {
routingExchange.ok().end();
return;
}
byte[] bodyBytes = routingContext.getBody().getBytes();
String action = body.getString("action");
if (!isBlank(deliveryId) && gitHubAppRuntimeConfig.debug.payloadDirectory.isPresent()) {
String fileName = DATE_TIME_FORMATTER.format(LocalDateTime.now()) + "-" + event + "-" + (!isBlank(action) ? action + "-" : "") + deliveryId + ".json";
Files.write(gitHubAppRuntimeConfig.debug.payloadDirectory.get().resolve(fileName), bodyBytes);
}
Long installationId = extractInstallationId(body);
String repository = extractRepository(body);
GitHubEvent gitHubEvent = new GitHubEvent(installationId, gitHubAppRuntimeConfig.appName.orElse(null), deliveryId, repository, event, action, routingContext.getBodyAsString(), body, "true".equals(replayed) ? true : false);
if (launchMode == LaunchMode.DEVELOPMENT && replayRouteInstance.isResolvable()) {
replayRouteInstance.get().pushEvent(gitHubEvent);
}
if (gitHubAppRuntimeConfig.webhookSecret.isPresent() && !launchMode.isDevOrTest()) {
if (!payloadSignatureChecker.matches(bodyBytes, hubSignature)) {
StringBuilder signatureError = new StringBuilder("Invalid signature for delivery: ").append(deliveryId).append("\n");
signatureError.append("› Signature: ").append(hubSignature);
LOG.error(signatureError.toString());
routingExchange.response().setStatusCode(400).end("Invalid signature.");
return;
}
}
gitHubEventEmitter.fire(gitHubEvent);
routingExchange.ok().end();
}
Aggregations