BetaLenzon is in beta — the Free tier is 100% free while we're in beta (bring your own Anthropic key, public repos).See plans →
Lenzon

cli/cli PR #13918 — JSON output for gh pr create — PR #13918

cli/cli · pull request #13918 ·

Loading…

Transcript

PlainEnglish

We're looking at pull request thirteen thousand nine hundred eighteen for the GitHub CLI. This change adds JSON output capabilities to the pr create command, a feature users have been requesting for scripting and automation workflows.

PlainEnglish

Let's start with what this PR is doing. The primary goal is adding JSON and jq output flags so users can get machine-readable data when creating pull requests. We also need to enforce that dry-run and JSON flags don't mix, and document the new capability in the help text. Importantly, the existing URL output behavior won't change for users who aren't using the JSON flags.

PlainEnglish

Before this change, gh pr create only printed the URL to stdout. There was no way to get structured output, so users had to scrape the URL from shell output for automation. After this PR lands, users can pass the JSON flag to receive a complete PR object, and they can even filter it inline with jq. This addresses issue eleven thousand two hundred forty-seven.

Architecture

The changes are focused in the pr create command package. Two files were touched: create dot go gets the bulk of the work with new JSON flags, validation logic, and export handling. The test file receives some updates as well. Let's walk through the key changes.

Architecture

First, the CreateOptions struct gets a new Exporter field. This field is only populated when the user passes JSON or jq flags. It stays nil for normal operation, so existing behavior is untouched. The Exporter handles all the heavy lifting for formatting and filtering the JSON output.

Architecture

The command adds a validation check that prevents using dry-run and JSON together. This makes sense because dry-run never calls the API, so there's no PR object to serialize. The check happens early in RunE, before any API calls, keeping the error feedback fast and clear.

Architecture

The JSON flags are registered via cmdutil dot AddJSONFlags. This helper populates the Exporter field and validates that requested fields actually exist in the PullRequestFields list. It's the same pattern used across other gh commands like pr view and pr list, so the interface is consistent.

Architecture

Here's the export logic. After the PR is created, the code checks if an Exporter is present. If it is, it re-fetches the PR with the user-requested fields via the Finder, then writes it out as JSON via the Exporter. If no Exporter is set, it falls back to printing the URL just like before. This keeps the two output paths cleanly separated.

PlainEnglish

After this PR lands, users can append JSON field names to gh pr create and receive structured JSON output instead of just the URL. The jq flag enables inline filtering. The feature follows the same pattern as other gh commands, and the dry-run flag remains incompatible with JSON to prevent confusing error states. Non-JSON behavior is completely unchanged.

CodeQuality

There's one thing to keep an eye on. The new validation check for JSON and dry-run doesn't have test coverage yet. Other mutually exclusive flags have tests, like the dry-run and web check at line two hundred one. It's worth asking whether this was deliberately deferred or should be added in a follow-up.

PlainEnglish

This is a focused enhancement that brings JSON output to gh pr create. It follows the established Exporter pattern used across other gh commands and preserves the existing URL output behavior for users who don't need structured data. The test coverage gap is worth noting, but overall this is a clean addition that unlocks automation scenarios users have been requesting.

How this was made

Lenzon read cli/cli at pull request #13918 and generated this walkthrough automatically. The narration above is the transcript of what it says.

Explain a pull request from your own repo

Point Lenzon at a repo or a pull request and get a narrated walkthrough like this one.

Try it
cli/cli PR #13918 — JSON output for gh pr create — PR #13918