diff --git a/Documentation/cmd-stream-events.txt b/Documentation/cmd-stream-events.txt index dcdbb0717a..ee645c176b 100644 --- a/Documentation/cmd-stream-events.txt +++ b/Documentation/cmd-stream-events.txt @@ -26,6 +26,14 @@ link:access-control.html#capability_streamEvents[the 'Stream Events' global capa == SCRIPTING This command is intended to be used in scripts. +== OPTIONS +--subscribe|-s:: + Type of the event to subscribe to. Multiple --subscribe options + may be specified to subscribe to multiple events. When this option + is provided, only subscribed events are emitted and all other + events are ignored. When this option is omitted, all events are + emitted. + == EXAMPLES ==== @@ -34,6 +42,13 @@ This command is intended to be used in scripts. {"type":"comment-added",change:{"project":"tools/gerrit", ...}, ...} ==== +Only subscribe to specific event types: + +==== + $ ssh -p 29418 review.example.com gerrit stream-events \ + -s draft-published -s patchset-created -s ref-replicated +==== + == SCHEMA The JSON messages consist of nested objects referencing the *change*, *patchSet*, *account* involved, and other attributes as appropriate. diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/StreamEvents.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/StreamEvents.java index 91757585ef..734136f4ae 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/StreamEvents.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/StreamEvents.java @@ -38,10 +38,13 @@ import com.google.gson.JsonSerializer; import com.google.inject.Inject; import org.apache.sshd.server.Environment; +import org.kohsuke.args4j.Option; import java.io.IOException; import java.io.PrintWriter; import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; @@ -55,6 +58,10 @@ final class StreamEvents extends BaseCommand { /** Number of events to write before yielding off the thread. */ private static final int BATCH_SIZE = 32; + @Option(name = "--subscribe", aliases = {"-s"}, metaVar = "SUBSCRIBE", + usage = "subscribe to specific stream-events") + private List subscribedToEvents = new ArrayList<>(); + @Inject private IdentifiedUser currentUser; @@ -87,7 +94,10 @@ final class StreamEvents extends BaseCommand { private final EventListener listener = new EventListener() { @Override public void onEvent(final Event event) { - offer(event); + if (subscribedToEvents.isEmpty() + || subscribedToEvents.contains(event.getType())) { + offer(event); + } } };