gh

package
v0.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gh is the thin GitHub API layer: one interface the commands are written against, and a go-gh-backed implementation that reuses gh's stored credentials.

Index

Constants

This section is empty.

Variables

View Source
var ErrClaimConflict = errors.New("issue is already claimed")

ErrClaimConflict marks a Claimer's refusal because another writer claimed the issue first. Start turns it into the same exit codes as a claim the pre-read saw: 3 when someone else holds it, 5 when the identity matches.

Functions

This section is empty.

Types

type AuthError

type AuthError struct{ Err error }

AuthError marks failures that mean "run gh auth login", mapped to a distinct exit code by main.

func (*AuthError) Error

func (e *AuthError) Error() string

func (*AuthError) Unwrap

func (e *AuthError) Unwrap() error

type ClaimReleaser added in v0.18.0

type ClaimReleaser interface {
	// ReleaseIssueClaim removes all assignees and in-progress labels. Without
	// force, login must be the sole assignee at the time of the write. An
	// unclaimed issue is an error, including with force. A refusal changes nothing.
	ReleaseIssueClaim(ctx context.Context, number int, login string, force bool) error
}

ClaimReleaser is an optional atomic release capability. GitHub uses check-then-act; local stores can guard ownership and clear the claim together.

type Claimer added in v0.18.0

type Claimer interface {
	// ClaimIssue assigns login and adds the in-progress label in one locked
	// write. force replaces an existing claim. It fails with
	// ErrClaimConflict, leaving the issue unchanged, when another claimant
	// won between Start's read and this write.
	ClaimIssue(ctx context.Context, number int, login string, force bool) error
}

Claimer is an optional IssueStore capability: an atomic claim, for a backend that can make the write conditional. GitHub has no conditional writes, so its client does not implement it and Start keeps the check-then-act guard with a re-read after claiming; the local store holds an exclusive lock, so it can decide and write in one step.

type Client

type Client interface {
	IssueStore
	PullRequests
}

Client is the full GitHub API surface; the tests fake it. It is the union of the two halves below so a backend with no pull requests (the local store) can implement IssueStore alone.

type CloseReason

type CloseReason string

CloseReason is why an issue is closed, matching GitHub's IssueClosedStateReason enum.

const (
	CloseCompleted  CloseReason = "COMPLETED"
	CloseNotPlanned CloseReason = "NOT_PLANNED"
	CloseDuplicate  CloseReason = "DUPLICATE"
)

type GitHub

type GitHub struct {
	// contains filtered or unexported fields
}

GitHub implements Client against the real API via go-gh.

One client serves a long-lived process, so it is safe for concurrent use: the node-ID cache, the viewer cache, the observed rate limit and the API clients themselves are all synchronized. The clients are replaced when the token behind them goes stale, which is why they are read through accessors rather than used directly.

func New

func New(repo Repo) (*GitHub, error)

New builds a client using gh's stored credentials and host config.

func NewWithOptions

func NewWithOptions(repo Repo, opts api.ClientOptions) (*GitHub, error)

NewWithOptions exists for tests, which inject a Transport, and is where the retry policy's defaults are installed.

func NewWithTransport added in v0.19.0

func NewWithTransport(repo Repo, transport http.RoundTripper) (*GitHub, error)

NewWithTransport builds a client whose requests go through transport. It is the seam an in-process consumer uses to serve GitHub responses from an httptest server, with no network and no `gh` process; hew's own CLI leaves the transport nil and the default HTTP client is used.

func (*GitHub) AddAssignee

func (g *GitHub) AddAssignee(ctx context.Context, number int, login string) error

func (*GitHub) AddBlockedBy

func (g *GitHub) AddBlockedBy(ctx context.Context, number, blockingNumber int) error

func (*GitHub) AddLabels

func (g *GitHub) AddLabels(ctx context.Context, number int, labels []string) error

func (*GitHub) AddSubIssue

func (g *GitHub) AddSubIssue(ctx context.Context, parentNumber, childNumber int, replaceParent bool) error

func (*GitHub) CloseIssue

func (g *GitHub) CloseIssue(ctx context.Context, number int, reason CloseReason) error

func (*GitHub) Comment

func (g *GitHub) Comment(ctx context.Context, number int, body string) error

func (*GitHub) CreateIssue

func (g *GitHub) CreateIssue(ctx context.Context, title, body string, labels []string) (model.Issue, error)

func (*GitHub) CreateLabel

func (g *GitHub) CreateLabel(ctx context.Context, label Label) error

func (*GitHub) CreatePullRequest

func (g *GitHub) CreatePullRequest(ctx context.Context, pr NewPullRequest) (PullRequest, error)

func (*GitHub) EditBody

func (g *GitHub) EditBody(ctx context.Context, number int, body string) error

func (*GitHub) EditTitle

func (g *GitHub) EditTitle(ctx context.Context, number int, title string) error

func (*GitHub) GetIssue

func (g *GitHub) GetIssue(ctx context.Context, number int) (model.Issue, error)

func (*GitHub) ListIssues

func (g *GitHub) ListIssues(ctx context.Context, states []IssueState) ([]model.Issue, error)

func (*GitHub) ListLabels

func (g *GitHub) ListLabels(ctx context.Context) ([]Label, error)

func (*GitHub) PullRequestContext

func (g *GitHub) PullRequestContext(ctx context.Context, head string) (PRContext, error)

func (*GitHub) RateLimit added in v0.19.0

func (g *GitHub) RateLimit() (RateLimit, bool)

RateLimit reports the budget captured from the last ListIssues query, and false when no list query has run on this client yet.

func (*GitHub) RemoveAssignees

func (g *GitHub) RemoveAssignees(ctx context.Context, number int, logins []string) error

func (*GitHub) RemoveBlockedBy

func (g *GitHub) RemoveBlockedBy(ctx context.Context, number, blockingNumber int) error

func (*GitHub) RemoveLabel

func (g *GitHub) RemoveLabel(ctx context.Context, number int, label string) error

func (*GitHub) RemoveSubIssue

func (g *GitHub) RemoveSubIssue(ctx context.Context, parentNumber, childNumber int) error

func (*GitHub) ReopenIssue added in v0.10.0

func (g *GitHub) ReopenIssue(ctx context.Context, number int) error

func (*GitHub) SearchIssues

func (g *GitHub) SearchIssues(ctx context.Context, terms string, states []IssueState) ([]model.Issue, int, error)

func (*GitHub) UpdatePullRequest added in v0.15.0

func (g *GitHub) UpdatePullRequest(ctx context.Context, number int, title, body string) error

func (*GitHub) Viewer

func (g *GitHub) Viewer(ctx context.Context) (string, error)

Viewer returns the authenticated login. It is resolved once and cached: it cannot change without a different token, and a long-lived caller asks for it on every command.

type IssueState

type IssueState string

IssueState is a GitHub issue state, used to filter list queries. Typed so callers can't misspell the wire enum.

const (
	StateOpen   IssueState = "OPEN"
	StateClosed IssueState = "CLOSED"
)

type IssueStore added in v0.18.0

type IssueStore interface {
	// Viewer returns the identity this backend attributes new work to: the
	// authenticated login on GitHub, the agent's local identity in a local
	// store (see local.DefaultViewer). It is what `start` assigns and what
	// `pr` looks for when it infers the claimed issue.
	Viewer(ctx context.Context) (string, error)
	// ListIssues fetches all issues in the given states with body, labels,
	// assignees, parent, sub-issues, and blockers, paginating the outer
	// connection. Nested connections are capped (see query). Relationships,
	// totals and labels are part of this view, not just GetIssue's: the
	// list-shaped commands read nothing else.
	ListIssues(ctx context.Context, states []IssueState) ([]model.Issue, error)
	// GetIssue fetches one issue in any state, including body, creation
	// time and recent comments.
	GetIssue(ctx context.Context, number int) (model.Issue, error)
	// SearchIssues runs a repo-scoped text search over issues in both
	// states. Results are in the backend's relevance order — GitHub's
	// best-match, the local store's title-first — and callers must not
	// depend on a more specific order. Results are capped (see searchCap);
	// total is the backend's full match count, or len(issues) when it has
	// no server-side count, so a caller warns about truncation only when
	// total exceeds what came back. states narrows the search before the
	// cap is applied — filtering afterwards would let the other state's
	// matches crowd out the ones asked for; empty means both.
	SearchIssues(ctx context.Context, terms string, states []IssueState) (issues []model.Issue, total int, err error)
	CreateIssue(ctx context.Context, title, body string, labels []string) (model.Issue, error)
	EditTitle(ctx context.Context, number int, title string) error
	EditBody(ctx context.Context, number int, body string) error
	AddLabels(ctx context.Context, number int, labels []string) error
	RemoveLabel(ctx context.Context, number int, label string) error
	// AddAssignee records login as given. Whether the login resolves to a
	// real user is a server concern — GitHub validates it, a local store has
	// no user directory — so it is not part of this contract.
	AddAssignee(ctx context.Context, number int, login string) error
	RemoveAssignees(ctx context.Context, number int, logins []string) error
	Comment(ctx context.Context, number int, body string) error
	// Every method below identifies issues by number; the GraphQL-backed
	// implementations resolve node IDs internally, so callers never juggle
	// two identifier families.
	CloseIssue(ctx context.Context, number int, reason CloseReason) error
	// ReopenIssue reopens a closed issue. There is no reason enum to pass:
	// GitHub records the reopen itself as the state reason.
	ReopenIssue(ctx context.Context, number int) error
	// AddBlockedBy marks number as blocked by blockingNumber.
	AddBlockedBy(ctx context.Context, number, blockingNumber int) error
	RemoveBlockedBy(ctx context.Context, number, blockingNumber int) error
	AddSubIssue(ctx context.Context, parentNumber, childNumber int, replaceParent bool) error
	RemoveSubIssue(ctx context.Context, parentNumber, childNumber int) error
	ListLabels(ctx context.Context) ([]Label, error)
	CreateLabel(ctx context.Context, label Label) error
}

IssueStore is the issue-tracking half of the API: everything every command but `pr` needs.

type Label

type Label struct {
	Name        string
	Color       string
	Description string
}

Label mirrors a repository label for init's bootstrap pass.

type NewPullRequest

type NewPullRequest struct {
	Title string
	Body  string
	// Head and Base are branch names in this repo; cross-fork heads are out
	// of scope (the workflow branches in place).
	Head  string
	Base  string
	Draft bool
}

NewPullRequest is the pr command's creation payload.

type NotFoundError added in v0.19.0

type NotFoundError struct {
	Issue int
	Repo  Repo
}

NotFoundError marks a lookup for an issue the store does not have. Repo is the GitHub repository the lookup ran against and stays empty in a local store, so the message names the same target it did before the type existed.

func (*NotFoundError) Error added in v0.19.0

func (e *NotFoundError) Error() string

type PRContext

type PRContext struct {
	DefaultBranch string
	Existing      *PullRequest
	// OpenCount includes all open PRs with this head name, including fork heads.
	OpenCount int
}

PRContext is the repo-side state needed before creating or revising a PR. Existing is only a sample: amendment also requires OpenCount == 1 and a matching head repository. Creation can refuse on any existing match.

type PullRequest

type PullRequest struct {
	Number int
	URL    string
	Draft  bool
	Title  string
	Body   string
	Base   string
	// HeadRepository is owner/name; empty means the head repository is unknown.
	HeadRepository string
}

PullRequest carries the metadata and body needed to create or revise a PR.

type PullRequests added in v0.18.0

type PullRequests interface {
	// PullRequestContext fetches the default branch and any open pull
	// request whose head is the given branch.
	PullRequestContext(ctx context.Context, head string) (PRContext, error)
	CreatePullRequest(ctx context.Context, pr NewPullRequest) (PullRequest, error)
	// UpdatePullRequest changes only the title and body, preserving draft/base state.
	UpdatePullRequest(ctx context.Context, number int, title, body string) error
}

PullRequests is the pull-request half of the API, used only by `pr`.

type RateLimit added in v0.19.0

type RateLimit struct {
	Cost      int
	Remaining int
	ResetAt   time.Time
}

RateLimit is the GraphQL budget GitHub reported on the most recent list query. A long-lived caller reads it to slow down before it is refused, rather than discovering the limit by hitting it.

type Repo

type Repo struct {
	Owner string
	Name  string
}

Repo identifies the target repository.

func (Repo) String

func (r Repo) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL