cli/cli
API client
Active contributors: Mislav, Kynan, Corey Johnson
Purpose
The api/ package wraps cli/go-gh with GitHub-specific concerns: SSO redirect detection, OAuth scope hints in errors, telemetry headers, GraphQL feature flags, and a curated set of typed queries used throughout the CLI.
Directory layout
api/
client.go # NewClientFromHTTP, GraphQL/REST/Mutate/Query methods
http_client.go # Transport stack (auth, retry, sso, debug)
query_builder.go # GraphQL query reflection
export_pr.go # PR export to JSON
export_repo.go # Repo export to JSON
reaction_groups.go # Reaction renderers
queries_repo.go # ~46 KB of repository queries
queries_pr.go # ~25 KB of PR queries
queries_pr_review.go # PR review queries
queries_issue.go # Issue queries
queries_comments.go # Comment queries
queries_org.go # Organisation queries
queries_projects_v2.go # ProjectsV2 fragments
queries_branch_issue_reference.go
queries_user.goKey abstractions
| Symbol | File | Role |
|---|---|---|
NewClientFromHTTP |
client.go |
Wraps an *http.Client and returns a *Client. |
Client.GraphQL / Mutate / Query |
client.go |
Run a query string, a typed mutation, or a typed query. |
Client.REST |
client.go |
Generic REST helper with JSON encoding. |
Client.RESTWithNext |
client.go |
REST plus Link: rel="next" cursor for paginated calls. |
HTTPError / GraphQLError |
client.go |
Typed errors carrying status, body, and ScopesSuggestion. |
clientOptions |
http_client.go |
Builds go-gh api.ClientOptions with the canonical headers (X-GitHub-Api-Version: 2022-11-28, User-Agent, GraphQL-Features: merge_queue). |
Pull request types |
queries_pr.go |
The PR domain model (PullRequest, Review, ReviewThread, ...). |
Repository types |
queries_repo.go |
The repository domain model. |
How it works
graph LR
Cmd[pkg/cmd command] --> F[Factory.HttpClient]
F --> T[http.RoundTripper stack]
T --> RT[oauth header injection]
T --> Hpd[httpretty debug -- if GH_DEBUG=api]
T --> SSO[SSO header interceptor]
T --> Tel[Telemetry header tagging]
T --> Net[net/http]
Net --> GitHub[GitHub API]
GitHub --> Net
Net -->|response| Decoder[handleResponse + JSON decode]
Decoder --> CmdThe transport stack is built by pkg/cmd/factory/default.go (HttpClientFunc). It composes:
- The base
http.Transport. httpretty(only whenGH_DEBUG=api).- SSO redirect detection: parses
X-GitHub-SSOand exposes the URL viaSSOURLRE. - OAuth scope hints: when a response has
X-Accepted-OAuth-Scopes, the error string suggestsgh auth refresh -s .... - Telemetry: tags the outgoing request with the invoking agent and CI signals so server-side dashboards can attribute traffic.
The same client is also used by gh api, which is essentially Client.REST and Client.GraphQL exposed through Cobra flags.
Integration points
- The host comes from
cfg.Authentication().DefaultHost()everywhere;ghinstance.Default()is intentionally avoided. - Pagination follows two conventions: REST uses
Link: rel="next", GraphQL usespageInfo.endCursor. Both are implemented inquery_builder.goandRESTWithNext. - All commands that talk to the API access it via
f.HttpClient()andapi.NewClientFromHTTP(httpClient). There is no global API client.
Entry points for modification
- New REST endpoint: add a typed helper to the relevant
queries_*.gofile and add a unit test in*_test.go. Usehttpmock.Registryto assert the request shape. - New GraphQL fragment: extend the matching
queries_*.gofile and update thequery_buildertests if you change reflection-driven assembly. - New transport behaviour: extend
factory/default.go'sHttpClientFuncso all callers benefit. Avoid wrapping per-command.
Key source files
| File | Purpose |
|---|---|
api/client.go |
The Client type and request methods. |
api/http_client.go |
Headers, OAuth scope errors, SSO. |
api/query_builder.go |
GraphQL reflection and pagination. |
api/queries_repo.go |
Repository queries. |
api/queries_pr.go |
PR queries. |
pkg/cmd/factory/default.go |
Transport stack assembly. |
Related pages
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.