Skip to content

Event streaming (1/11): Add a server-sent events reader and an Event Subscriptions API client - #15071

Merged
BagToad merged 2 commits into
trunkfrom
babakks/event-client
Nov 17, 2026
Merged

BagToad merged 2 commits into
trunkfrom
babakks/event-client

Conversation

@babakks

@babakks babakks commented Nov 17, 2026

Copy link
Copy Markdown
Member

Layer 1 of 11 in the stack tracked by #15070.

Description

GitHub's Event Subscriptions API (in preview, github.com only) lets a client subscribe to events on a resource, such as a pull request being reviewed or merged, and receive them over a server-sent events (SSE) stream instead of polling. This is the first PR in a stack that adds gh event commands on top of that API. It adds no commands yet; it adds the two building blocks they share.

internal/eventstream is a small SSE frame reader. Go has no SSE parser in the standard library, and the format is simple enough that a dependency is not worth it. FrameReader follows the parsing and interpretation rules of the WHATWG HTML spec (section 9.2), including multi-line data, the carried-over last event ID, CR, LF, and CRLF line endings, and comment lines (heartbeats), which it reports through an optional callback. It ignores retry, since reconnect timing is the caller's decision. Lines are capped at 64 KiB, which is far above any event the API sends, so a broken or hostile stream cannot grow the buffer without bound.

pkg/cmd/event/client wraps the API behind an EventClient interface (with a generated moq mock): create, get, list (following Link pagination), and delete subscriptions, and open a subscription's event stream. It maps the API's responses to sentinel errors the commands can act on: an unsupported host, a missing resource or subscription, an account not enrolled in the preview, and the subscription limit. SupportedHost reports that only github.com serves the API, so commands can fail before sending a request to GitHub Enterprise Server or GHE.com.

How did you test this change?

This PR has no user-facing surface on its own, so I exercised it through the commands added later in the stack. With a gh built from the complete stack, I ran the live smoke check from the evals PR against a sandbox pull request on github.com. It created a subscription, attached to its stream, and observed three review events (two change requests and an approval) as a second account submitted them, then created, viewed, listed, and deleted a separate subscription. Every request went through this client and every event through this reader.

Key points

  • A hand-written SSE reader rather than a third-party package: the spec is short, the stream only carries small JSON payloads, and the reader is covered by spec-driven tests.
  • The client lives under pkg/cmd/event/client, next to its commands, rather than in api/, because the API is in preview and only these commands use it. It reuses gh's HTTP client, so authentication, host handling, and debug logging behave as elsewhere.
  • The preview-enrollment check matches the exact 403 message the API returns. It is marked temporary and can go once the API is generally available.
  • A 422 without field errors is treated as the subscription limit, since that is the only such response the API sends today.

Notes for reviewers

Stack, bottom to top (this is 1 of 11, based on trunk): 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 internal/eventstream/sse.go; the comments marked "Spec:" point at the rule each branch implements. Then read pkg/cmd/event/client/client.go, especially the error mapping in each method. client_mock.go is generated.

Authorship and follow-up

Who wrote this:

  • A human wrote it.
  • An agent wrote it under close human direction.
  • An agent wrote it independently, and no human has guided the implementation beyond the initial prompt.

Who answers review comments:

  • @babakks will read and reply directly. Name the account.
  • An agent will draft replies and @username will read them before they are posted.
  • Nobody has explicitly committed to replying.

babakks and others added 2 commits November 17, 2026 16:36
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
babakks requested a review from a team as a code owner November 17, 2026 17:11
@babakks
babakks requested review from niik and a balanced review from Copilot November 17, 2026 17:11
@babakks
babakks added this pull request to stack #15082 November 17, 2026 17:14
@babakks
babakks requested review from BagToad and removed request for niik November 17, 2026 17:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Split-CRLF handling can block SSE reads despite buffered frame data, and subscription requests fail to propagate cancellation and deadlines.

2 open issues
1 file not reviewed
  • pkg/cmd/event/client/client_mock.go: Generated file
What changed in this PR

Adds the shared foundations for the event-streaming stack tracked in #15070: a server-sent events reader and a client for GitHub’s preview Event Subscriptions API. This layer adds no user-facing commands.

Changes:

  • Parse event frames, preserve event IDs, and report heartbeat comments.
  • Support subscription management, paginated listing, and live streams with host checks and classified API errors.
  • Add reader and client tests, subscription JSON export support, and a generated client mock.
File Description
pkg/​cmd/​event/​client/​types.go Defines subscription data and selectable JSON export fields.
pkg/​cmd/​event/​client/​client.go Implements subscription operations, stream access, and error mapping.
pkg/​cmd/​event/​client/​client_test.go Tests API operations, pagination, response handling, and host rejection.
pkg/​cmd/​event/​client/​client_mock.go Provides the generated EventClient mock for command tests.
internal/​eventstream/​sse.go Implements frame parsing, line splitting, and heartbeat callbacks.
internal/​eventstream/​sse_test.go Tests parsing rules, fragmented input, line limits, and read errors.

Review effort: Balanced

Comment thread internal/eventstream/sse.go Resolved
}

var sub Subscription
if err := c.api.REST(c.host, http.MethodGet, path.String(), nil, &sub); err != nil {

This comment was marked as spam.

@BagToad
BagToad merged commit 42d52b4 into trunk Nov 17, 2026
19 checks passed
@BagToad
BagToad deleted the babakks/event-client branch November 17, 2026 19:18

@Benz1993com Benz1993com left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok


import (
"bytes"
"context"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants