Include canonicalWebURL in X-Origin-Url header in POST requests
When one build server receives webhook requests from multiple Gerrit
servers it needs a way to differentiate the origin of the events.
Include canonicalWebURL in the X-Origin-Url header:
Content-Type: application/json; charset=utf-8
X-Origin-Url: https://gerrit.example.com:8080/
...
{"type":"ref-updated", ... }
Including canonicalWebURL in the request header instead of request body
allows us to keep the event json format compatible with the
stream-events format.
Change-Id: I856fb474176a40a6183adb54980a43e043a0eed3
diff --git a/src/main/java/com/googlesource/gerrit/plugins/webhooks/processors/GerritEventProcessor.java b/src/main/java/com/googlesource/gerrit/plugins/webhooks/processors/GerritEventProcessor.java
index e9c373c..1a8d6d0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/webhooks/processors/GerritEventProcessor.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/webhooks/processors/GerritEventProcessor.java
@@ -15,10 +15,13 @@
package com.googlesource.gerrit.plugins.webhooks.processors;
import com.google.common.base.Supplier;
+import com.google.common.collect.ImmutableMap;
+import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.events.ProjectEvent;
import com.google.gerrit.server.events.SupplierSerializer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
+import com.google.inject.Inject;
import com.googlesource.gerrit.plugins.webhooks.EventProcessor;
import com.googlesource.gerrit.plugins.webhooks.RemoteConfig;
import java.util.Optional;
@@ -27,8 +30,17 @@
private static Gson GSON =
new GsonBuilder().registerTypeAdapter(Supplier.class, new SupplierSerializer()).create();
+ private final String canonicalWebUrl;
+
+ @Inject
+ GerritEventProcessor(@CanonicalWebUrl String canonicalWebUrl) {
+ this.canonicalWebUrl = canonicalWebUrl;
+ }
+
@Override
public Optional<EventProcessor.Request> doProcess(ProjectEvent event, RemoteConfig remote) {
- return Optional.of(new EventProcessor.Request(GSON.toJson(event)));
+ return Optional.of(
+ new EventProcessor.Request(
+ GSON.toJson(event), ImmutableMap.of("X-Origin-Url", canonicalWebUrl)));
}
}