Developers frequently need to wait for something to happen on a GitHub pull request. A reviewer might submit feedback, a pull request might be merged, a new commit might be pushed, or a GitHub Actions workflow might finish running. These events often determine what a developer or automation script should do next.
A common way to handle this requirement is to repeatedly call GitHub APIs and check whether the pull request has changed. For example, a script might call gh pr view every few seconds until the review status changes. Another script might repeatedly execute gh pr checks to determine whether all required checks have completed.
This approach works, but it creates unnecessary API requests, introduces delays between checks, and makes scripts more complicated. It also requires developers to manage polling intervals, retry logic, timeouts, and error handling.
GitHub Event Streams provide another approach. With recent versions of GitHub CLI, developers can use the gh event listen command to wait for specific pull request events instead of repeatedly checking the API.
In this article, we will explore how GitHub CLI Event Streams work, how to listen for pull request changes, how to use event data in shell scripts, and what to consider when using this capability in development automation.
Why Polling Pull Requests Becomes a Problem
Polling means repeatedly requesting the current state of a resource until the expected condition becomes true.
For example, suppose a developer opens a pull request and wants a script to continue only after someone submits a review. A basic polling script might look like this:
#!/bin/bash
PR_NUMBER=128
while true; do
REVIEW_COUNT=$(gh api \
repos/OWNER/REPO/pulls/$PR_NUMBER/reviews \
--jq 'length')
if [ "$REVIEW_COUNT" -gt 0 ]; then
echo "A review has been submitted."
break
fi
sleep 10
done
The script checks the review collection every ten seconds. If the reviewer submits feedback immediately after a check, the script will not notice until its next request.
This creates several problems.
First, the script sends requests even when nothing has changed. If multiple scripts monitor different pull requests, the number of unnecessary requests can increase quickly.
Second, the response is not immediate. The script only detects a change during its next polling cycle.
Third, polling logic becomes more complicated when the script needs to monitor multiple conditions. For example, it might need to wait for a review, a merge, a new commit, or completed CI checks.
Developers can reduce the polling interval, but doing so increases the number of API requests. Increasing the interval reduces requests but makes the script slower to react.
GitHub Event Streams address this problem by allowing an application or command-line tool to subscribe to selected events and receive notifications when those events occur.
Understanding GitHub Event Streams
GitHub Event Streams provide an event-driven way to observe changes to supported GitHub resources.
Instead of repeatedly requesting the current pull request state, a client creates an event subscription and listens for notifications. When a matching event occurs, GitHub sends it through the event stream.
GitHub CLI provides commands for working with these subscriptions:
gh event listen starts listening for events.
gh event subscription create creates a subscription without immediately listening.
gh event subscription list displays active subscriptions.
gh event subscription view displays subscription details.
gh event subscription delete removes a subscription.
The gh event commands are available in GitHub CLI 2.105.0 and later. They are currently in preview, and GitHub Event Streams access is also in preview on GitHub.com. Access may require joining the Event Streams waitlist.
The feature is designed for specific pull request resources rather than providing an unrestricted stream of every activity across GitHub.
Supported Pull Request Events
GitHub provides several event types that developers can use when monitoring a pull request.
Event type | What it indicates |
|---|
com.github.pull_request.review.submitted.v1 | A review was submitted |
com.github.pull_request.state.changed.v1 | The pull request was merged, closed, reopened, marked ready, or converted to draft |
com.github.pull_request.head.changed.v1 | The pull request's head branch changed |
com.github.pull_request.merge_conflict.v1 | A merge conflict appeared |
com.github.pull_request.check_suite.status.changed.v1 | A check suite completed |
These event types allow scripts to wait for specific changes instead of repeatedly checking the entire pull request.
For example, a developer who only cares about code reviews can subscribe to the review-submitted event. A deployment script can wait for completed check suites before deciding whether to continue.
Preparing GitHub CLI
Before using Event Streams, install or upgrade GitHub CLI to a version that supports the gh event commands.
Check the installed version:
gh --version
The command should report version 2.105.0 or later.
Next, authenticate with GitHub:
gh auth login
Follow the authentication instructions and select the GitHub account you want to use.
You can verify the authentication status with:
gh auth status
The authenticated account must have the required access to the pull request and the Event Streams feature.
You can also verify that the event commands are available:
gh event --help
If the commands are unavailable, check the installed CLI version and whether your account has access to GitHub Event Streams.
Listening for a Pull Request Review
Let's start with a practical example.
Suppose a developer opens a pull request:
https://github.com/OWNER/REPO/pull/128
The developer wants a terminal script to wait until someone submits a review.
With GitHub CLI, the command is straightforward:
gh event listen \
--resource https://github.com/OWNER/REPO/pull/128 \
--event com.github.pull_request.review.submitted.v1 \
--timeout 30m
The command performs several operations:
It creates an event subscription for the specified pull request or reuses an existing matching subscription.
It opens the event stream.
It waits for the selected event.
It prints the event as a JSON object when a matching change occurs.
By default, it exits after receiving the first event.
The timeout prevents the script from waiting indefinitely. In this example, the command waits for up to 30 minutes.
If the timeout is reached before an event arrives, the command exits with status code 8.
This behavior is useful in automation because a script can wait for an event and then continue with its next operation.
Listening for Multiple Event Types
A pull request can change in several ways while a developer is waiting for a review. It might be reviewed, merged, closed, or updated with new commits.
You can subscribe to multiple event types in one command:
gh event listen \
--resource https://github.com/OWNER/REPO/pull/128 \
--event com.github.pull_request.review.submitted.v1,com.github.pull_request.state.changed.v1,com.github.pull_request.head.changed.v1 \
--timeout 30m
The command waits for any of the selected events.
You can also repeat the event option instead of providing a comma-separated list:
gh event listen \
--resource https://github.com/OWNER/REPO/pull/128 \
--event com.github.pull_request.review.submitted.v1 \
--event com.github.pull_request.state.changed.v1 \
--timeout 30m
Choose event types based on the actual condition your automation needs to monitor. Subscribing to unrelated events makes it harder to interpret the results.
Controlling How Many Events the Command Receives
By default, gh event listen exits after the first matching event. This is convenient when a script needs to wait for one specific change.
However, some development workflows need to monitor several events.
GitHub CLI provides three useful options:
Option | Purpose |
|---|
--count | Exit after receiving the specified number of events |
--follow | Continue listening until stopped |
--timeout | Stop waiting after the specified duration |
For example, to receive three review events:
gh event listen \
--resource https://github.com/OWNER/REPO/pull/128 \
--event com.github.pull_request.review.submitted.v1 \
--count 3 \
--timeout 1h
To keep listening until the process is stopped:
gh event listen \
--resource https://github.com/OWNER/REPO/pull/128 \
--event com.github.pull_request.review.submitted.v1 \
--follow
The --follow option is useful for a terminal session or a long-running development process that needs to observe multiple changes.
For temporary automation, a timeout is usually preferable. It prevents a script from remaining active forever when the expected event never occurs.
Understanding the Event JSON Output
When GitHub CLI receives an event, it prints a CloudEvent JSON object.
The output contains information about the event and a reference to the GitHub resource that changed.
For example, a simplified event could look like this:
{
"specversion": "1.0",
"type": "com.github.pull_request.review.submitted.v1",
"source": "https://github.com/OWNER/REPO",
"subject": "pull/128",
"data": {
"api_url": "https://api.github.com/repos/OWNER/REPO/pulls/128/reviews/456"
}
}
The important property is data.api_url.
The event tells you that something happened and provides a URL that can be used to retrieve the current resource information. It is not necessarily a complete copy of the changed GitHub resource.
For example, after receiving a review event, you can retrieve the review details:
gh api \
repos/OWNER/REPO/pulls/128/reviews/456
For a more flexible script, extract the API URL directly from the event:
gh event listen \
--resource https://github.com/OWNER/REPO/pull/128 \
--event com.github.pull_request.review.submitted.v1 \
--jq '.data.api_url'
The --jq option uses a jq expression to format the event output.
You can also use --template when Go template formatting is more suitable for the script.
Why Fetch the Current Resource State?
An event notification should be treated as an indication that a change occurred, not necessarily as the complete current state of the pull request.
For example, a pull request may receive several updates while a script is running. By the time the script processes an event, another change may already have occurred.
Fetching the referenced resource helps the script work with the latest available information.
This is particularly important for automation that makes decisions based on review state, merge status, or check results.
Building a Script That Waits for a Review
Let's combine the commands into a practical shell script.
The following example waits for the first submitted review and then retrieves the review information.
#!/bin/bash
set -euo pipefail
OWNER="OWNER"
REPO="REPO"
PR_NUMBER="128"
PR_URL="https://github.com/$OWNER/$REPO/pull/$PR_NUMBER"
echo "Waiting for a review on pull request #$PR_NUMBER"
EVENT=$(gh event listen \
--resource "$PR_URL" \
--event com.github.pull_request.review.submitted.v1 \
--timeout 30m)
REVIEW_URL=$(echo "$EVENT" | jq -r '.data.api_url')
if [ -z "$REVIEW_URL" ] || [ "$REVIEW_URL" = "null" ]; then
echo "The event did not contain a review API URL."
exit 1
fi
echo "A review event was received."
gh api "$REVIEW_URL" \
--jq '{user: .user.login, state: .state, submitted_at: .submitted_at, body: .body}'
The script starts by defining the repository and pull request details. It then waits for a review event.
When an event arrives, jq extracts the API URL from the JSON output. The script uses that URL to retrieve the review information through GitHub CLI.
The final command displays the reviewer, review state, submission time, and review comments.
In a real development workflow, you could extend this script to send a notification, update a task, or trigger another validation process.
Waiting for GitHub Actions Checks to Complete
Pull request reviews are not the only useful events. Developers also need to wait for CI checks before merging or deploying code.
Previously, a script might repeatedly execute gh pr checks to determine whether all checks have finished.
GitHub Event Streams provide a check-suite event:
com.github.pull_request.check_suite.status.changed.v1
You can listen for it using:
gh event listen \
--resource https://github.com/OWNER/REPO/pull/128 \
--event com.github.pull_request.check_suite.status.changed.v1 \
--timeout 30m \
--jq '.data.conclusion'
The event can provide a conclusion such as success or failure.
There is an important detail when working with CI checks: a pull request can have multiple check suites. Each check suite can generate its own completion event.
Receiving one event does not mean that every check associated with the pull request has completed.
If your automation needs to wait for all checks, retrieve the current check information after receiving an event:
gh pr checks 128 --repo OWNER/REPO
You can then inspect the current results and decide whether the workflow can continue.
This is a better approach than assuming that the first completed check suite represents the final status of the entire pull request.
Managing Event Subscriptions
The gh event listen command can create a subscription automatically. However, developers may also want to manage subscriptions separately.
For example, a long-running development tool might create a subscription during initialization and reuse it for multiple listening sessions.
Create a subscription with:
gh event subscription create \
--resource https://github.com/OWNER/REPO/pull/128 \
--event com.github.pull_request.review.submitted.v1
List active subscriptions:
gh event subscription list
View a specific subscription:
gh event subscription view SUBSCRIPTION_ID
Delete a subscription when it is no longer required:
gh event subscription delete SUBSCRIPTION_ID
Creating an identical active subscription is idempotent. GitHub returns the existing matching subscription rather than creating another one.
Subscriptions expire after 24 hours. Therefore, long-running applications should not assume that a subscription remains active permanently.
Also, exiting gh event listen does not automatically delete the subscription. Manage subscriptions explicitly when building tools that create and remove them dynamically.
Handling Connection Interruptions and Missed Events
Event-driven systems reduce unnecessary requests, but they still need failure handling.
A network connection can break while a script is listening. GitHub CLI can reconnect when the stream is interrupted, but events that occur while the client is disconnected are not delivered through that interrupted connection.
There is another important behavior: events that occurred before the listener became active are not replayed.
This means a script should not assume that it has received every change that has ever happened to a pull request.
A safer workflow is:
Start the event listener.
Wait until the command reports that it is listening for events.
Read the current pull request state.
Process any event received after the listener starts.
Retrieve the latest resource state before making important decisions.
The order matters. If you read the pull request first and only start listening afterward, a change could happen between those two operations. The listener would not receive that earlier event.
Starting the listener first reduces this gap. Reading the current state afterward helps establish a reliable baseline.
For workflows where missing an event would cause a serious problem, use additional reconciliation logic rather than relying entirely on the event stream.
Important Limitations to Understand
GitHub Event Streams are useful, but developers should understand the current limitations before building production automation around them.
First, the gh event commands and Event Streams are in preview. Their behavior and command options may change as the feature develops.
Second, Event Streams support for these CLI commands is limited to pull requests on GitHub.com. Do not assume that the same functionality is available on GitHub Enterprise Server or GHE.com.
Third, the event stream reports changes that occur after listening begins. It does not provide a complete historical event feed.
Fourth, subscriptions have a limited lifetime and expire after 24 hours. Applications that depend on long-running subscriptions must account for expiration.
Finally, event notifications are not a replacement for retrieving the latest resource state. A script should use the GitHub API when it needs detailed information about the review, pull request, or check suite.
These limitations do not make the feature less useful. They simply define the situations where Event Streams are appropriate.
Best Practices for Development Automation
When using GitHub CLI Event Streams, keep the automation focused on a specific resource and a small set of relevant event types.
Use --timeout for temporary scripts so they do not wait indefinitely. Use --count when the workflow needs a fixed number of events, and use --follow only when continuous monitoring is required.
Do not assume that receiving one check-suite event means all CI checks have finished. Always inspect the current check status when the workflow depends on the overall result.
Use the event's API URL to retrieve the latest resource details rather than building assumptions around a simplified event payload.
Also, handle command exit codes properly. A timeout, authentication failure, connection problem, or invalid subscription should not be treated as a successful review or completed workflow.
For scripts that run in CI environments, ensure that the authentication token has the necessary permissions and that the account can access Event Streams.
Most importantly, keep a fallback strategy for important workflows. If an event is missed during a connection interruption, the script should be able to retrieve the current pull request state and continue safely.
Summary
GitHub CLI Event Streams provide a practical way to wait for pull request changes without repeatedly calling GitHub APIs. The gh event listen command allows developers to subscribe to review submissions, pull request state changes, head updates, merge conflicts, and completed check suites.
With options such as --count, --follow, --timeout, and --jq, developers can build simpler shell scripts and automation workflows around GitHub activity.
The feature is currently in preview and has specific availability and subscription limitations. Event notifications should also be combined with API calls when the latest resource details are required.
For developers who regularly write scripts around pull requests, reviews, and CI checks, Event Streams offer a more event-driven alternative to traditional polling loops.
Join the conversation! Your thoughts help the community grow.