Repository navigation
Event streaming (2/11): Add gh event listen - #15072
Merged
Merged
Conversation
Co-authored-by: Copilot <[email protected]> Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
Co-authored-by: Copilot <[email protected]> Copilot-Session: bbce5a7f-103c-4e67-99c9-2d6dc3f6a02b
babakks
requested review from
a team and
niik
and
a balanced review from Copilot
and removed request for
a team
November 17, 2026 17:11
2 of 6 tasks
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Frame-terminator buffering can delay event output and cause false timeout exits, while ignoring HTTP-date Retry-After values allows retries before the server’s requested time.
2 open issues
What changed in this PR
Adds the preview gh event listen command so scripts and agents can wait for pull request changes instead of polling. This is the second layer of the event-subscriptions stack tracked in #15070.
Changes:
- Registers
gh event listenwith resource or subscription selection, event counts, continuous listening, and timeout controls. - Adds JSON event output, per-event formatting, readiness messages, and reconnect and resubscribe handling.
- Introduces reusable subscription helpers and tests for command behavior and event-type completion.
| File | Description |
|---|---|
pkg/cmd/root/root.go |
Registers the event command group. |
pkg/cmd/event/shared/subscription.go |
Adds resource-host, event-normalization, and subscription-error helpers. |
pkg/cmd/event/shared/help.go |
Defines known event types and their help descriptions. |
pkg/cmd/event/shared/completion.go |
Completes comma-separated event types. |
pkg/cmd/event/shared/completion_test.go |
Tests completion filtering and duplicate exclusion. |
pkg/cmd/event/shared/client.go |
Creates event clients through the command factory. |
pkg/cmd/event/listen/listen.go |
Implements flags, streaming, formatting, lifecycle controls, and retries. |
pkg/cmd/event/listen/listen_test.go |
Tests validation, output, retries, timeouts, and subscription lifecycle behavior. |
pkg/cmd/event/event.go |
Defines the preview command group and registers listen. |
Review effort: Balanced
Comment on lines
+423
to
+434
| // Event data is JSON, which the API client sanitizes only for JSON responses. Sanitizing it here, rather than | ||
| // printing through ContentOut, keeps the escape sequences that --template helpers such as color generate. | ||
| reader := eventstream.NewFrameReader(transform.NewReader(body, &asciisanitizer.Sanitizer{JSON: true})) | ||
| reader.OnComment = func(string) { l.delivered = true } | ||
| for { | ||
| frame, err := reader.Next() | ||
| if errors.Is(err, io.EOF) { | ||
| return nil, nil | ||
| } | ||
| if err != nil { | ||
| return nil, err | ||
| } |
2 of 6 tasks
BagToad
approved these changes
Nov 17, 2026
This was referenced Nov 18, 2026
Merged
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Layer 2 of 11 in the stack tracked by #15070.
Description
Scripts and coding agents that need to wait for a pull request to change, for example to be reviewed or merged, poll today: they loop over
gh pr vieworgh apiwith asleep. That is slow to react, wastes requests, and for agents burns context on every read. The Event Subscriptions API (in preview, github.com only) can push these changes instead.This PR adds a
gh eventcommand group, marked as preview, with one command:It creates a subscription for the pull request and event types (or reuses the matching one, since creation is idempotent), attaches to its event stream, and prints each event to stdout as one compact CloudEvent JSON object per line. Status messages go to stderr, so stdout carries only events.
--subscription IDattaches to an existing subscription instead.--count Nexits after N events, and--followlistens until stopped.--timeout(a duration or a number of seconds) exits with status 8 when it is reached first.Listening for events.is printed to stderr each time the stream attaches, followed by a reminder to read the current state now.--jqand--templateformat each event, likegh api, and are validated before subscribing.5xxresponses (honoringRetry-After), streams that end without an end frame, andreconnectend frames lead to a reconnect with jittered exponential backoff up to 30 seconds. A stream with no data, not even a heartbeat, for 45 seconds is treated as dead. An expired subscription is created again when listening with--resource.gh event subscription listanddeleteto free a slot.--eventcompletion suggests the four known pull request event types but accepts any value, since the server may support types this build does not know about. The help lists the types with a description of each.The
sharedpackage holds what the subscription commands in the next PRs reuse: the client factory, event type help and completion, and the resource host and error helpers.How did you test this change?
Recording of
gh event listenon my fork's pull request babakks#17, with a check after each case. Background jobs make the changes. It waits for a review and exits after it, times out with exit status 8 when no push arrives within 15 seconds, exits after two state changes with--count 2and--jq, and with--followkeeps printing state changes and a review until Ctrl-C.02-listen.mp4
With a
ghbuilt from the complete stack, I ran the live smoke check from the evals PR against a sandbox pull request on github.com.gh event listen --followprintedListening for events.on stderr, and as a second account submitted two change requests and an approval, I saw each review event arrive on stdout with itsdata.api_urlpointing at that review.Key points
--followis opt-in.422, and new types work without aghrelease.cmdutil.PendingError, the statusgh pr checksreturns for pending checks, so a script can tell "nothing happened yet" from a failure.gh event subscription listanddelete, which land in the next PRs of this stack.Notes for reviewers
Stack, bottom to top (this is 2 of 11, based on
babakks/event-client):babakks/event-client,babakks/event-listen,babakks/event-sub-list,babakks/event-sub-create,babakks/event-sub-view,babakks/event-sub-delete,babakks/event-hints,babakks/event-evals,babakks/event-listen-flush,babakks/event-listen-deleted-sub,babakks/event-check-suite.Start with the help text and flag validation in
NewCmdListen, thenlistener.listeninpkg/cmd/event/listen/listen.go, which holds the attach, reconnect, and resubscribe loop.readEventshandles the stream's frames, andidleReaderthe dead-connection timeout.Authorship and follow-up
Who wrote this:
Who answers review comments: