ghcli

package
v0.3.26-rc.4 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 14 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 ghcli talks to GitHub through the gh command-line tool, the same way internal/gitx talks to git: by running the binary and reading what it says, rather than through a client of GitHub's API. gh already knows how to authenticate, how to find the repository a working tree belongs to and how to talk to GitHub Enterprise as well as github.com, and duplicating that with an HTTP client of Flockdeck's own would mean keeping a second copy of all of it in step.

Everything here needs gh on PATH; Installed reports whether it is, and Login walks a person through getting it authenticated when it is not (or its login has expired). Nothing in this package stores a token itself -- gh keeps its own, in its own configuration -- so there is nothing here for Flockdeck to leak.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Installed

func Installed() bool

Installed reports whether the gh binary can be found on PATH.

func Login

func Login(ctx context.Context, onEvent func(LoginEvent)) error

Login walks a person through `gh auth login --web`: gh opens github.com/login/device in their browser itself, showing a one-time code there for them to copy in, and waits for that to be approved. Every event gh prints along the way is sent to onEvent as it happens, so a dialog can show the code as soon as it exists and let the person open the browser themselves if gh's own attempt to failed (a machine with no default browser set, over a remote desktop with none configured).

gh's flow pauses once to ask "Press Enter to open github.com in your browser...". There is nothing to gain by waiting for a person to answer that from here -- the one thing it might otherwise ask, confirming the browser should open, is answered the same way every time -- so a newline is sent the moment gh starts.

ctx cancelled closes the flow early, as does loginTimeout passing; either way onEvent is sent a final event with Err set, unless gh had already finished.

func Logout

func Logout(dir string) error

Logout signs this machine out of github.com, for the settings panel's own "sign out" -- see internal/server's ghcli.go for where it is offered.

func Platform

func Platform() string

Platform names the OS an install hint is being shown for, for the caller that has no package manager to offer and wants to say something more specific than "visit a website".

func TryInstall

func TryInstall(ctx context.Context, in Installer, onLine func(string)) error

TryInstall runs the installer's command, if it has one, streaming its combined output a line at a time to onLine as it runs -- apt and dnf, run as a regular user, print why they refused rather than fail silently, and that is worth showing rather than swallowing.

It returns an error when there is no command to run (Manager == ""), when the command could not be started, when it exits non-zero, or when installTimeout passes.

Types

type Actor

type Actor struct {
	Login string `json:"login"`
}

Actor is whoever opened or wrote something -- a PR, an issue, a comment -- as gh's --json reports it. Only Login is ever shown; the rest of what gh sends (id, whether they are a bot, their display name) is decoded past and dropped, the same as any field of gh's JSON this package has not asked to keep.

type AuthStatus

type AuthStatus struct {
	// LoggedIn is false both when nobody has ever run gh auth login here and
	// when a login has expired or been revoked; gh answers both the same way.
	LoggedIn bool
	Host     string
	Account  string
	// Scopes is the token's scopes as gh prints them, comma-separated -- e.g.
	// "gist, read:org, repo, workflow" -- or "" when LoggedIn is false or gh
	// did not report them (a token from a fine-grained PAT reports none).
	Scopes string
}

AuthStatus is what `gh auth status` says about this machine's login.

func GetAuthStatus

func GetAuthStatus(dir string) (AuthStatus, error)

GetAuthStatus asks gh whether it is logged in to github.com. dir only matters in that gh reads it to decide which host an enterprise repository found there defaults to; github.com is asked for explicitly regardless, so an empty dir (no project open) still gets an answer.

type Check

type Check struct {
	Name string `json:"name"`
	// State is the raw state gh reports: e.g. "SUCCESS", "FAILURE",
	// "IN_PROGRESS", "PENDING", "SKIPPED", "CANCELLED". Bucket below is the
	// simpler question most callers actually want answered.
	State string `json:"state"`
	// Bucket groups State into "pass", "fail", "pending", "skipping" or
	// "cancel" -- what Summarize is built from.
	Bucket   string `json:"bucket"`
	Link     string `json:"link"`
	Workflow string `json:"workflow"`
}

Check is one status check on a pull request, as `gh pr checks` reports it -- a workflow job, or a check some other integration posted.

func PRChecks

func PRChecks(dir string, number int) ([]Check, error)

PRChecks lists the status checks on a pull request -- the Actions jobs and any other integration's checks together, the same list the Checks tab of a PR on github.com shows.

type Comment

type Comment struct {
	Author    Actor     `json:"author"`
	Body      string    `json:"body"`
	CreatedAt time.Time `json:"createdAt"`
	URL       string    `json:"url"`
}

Comment is one comment on a PR or an issue.

func CommentIssue

func CommentIssue(dir string, number int, body string) (*Comment, error)

CommentIssue posts a comment on an issue.

func CommentPR

func CommentPR(dir string, number int, body string) (*Comment, error)

CommentPR posts a comment on a pull request.

type Installer

type Installer struct {
	// Manager names what would run it -- "winget", "brew", "apt", "dnf",
	// "pacman", "apk" -- or "" when none of those was found, and only the
	// manual route is offered.
	Manager string
	// Command is the command line TryInstall runs for this installer, or nil
	// when Manager is "".
	Command []string
	// URL is where to install gh by hand, offered whether or not a package
	// manager was found: a manager can still fail (no network, a repository
	// not yet enabled), and the button should not be a dead end.
	URL string
}

Installer is one way to install gh on this machine: a package manager command, or nothing more than a place to go and do it by hand.

func DetectInstaller

func DetectInstaller() Installer

DetectInstaller looks for a package manager this machine has, to offer as a one-click install; the manual URL is always filled in, since a manager found here can still turn out to need a password, a repository that is not enabled, or a network this machine does not have right now.

type Issue

type Issue struct {
	Number    int       `json:"number"`
	Title     string    `json:"title"`
	Body      string    `json:"body"`
	State     string    `json:"state"` // OPEN, CLOSED
	Author    Actor     `json:"author"`
	URL       string    `json:"url"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
	Comments  []Comment `json:"comments"`
}

Issue is an issue, as much of it as the panel shows.

func CreateIssue

func CreateIssue(dir string, opts IssueCreateOptions) (*Issue, error)

CreateIssue opens an issue in the repository dir sits in.

func ListIssues

func ListIssues(dir, state string, limit int) ([]Issue, error)

ListIssues lists issues in the repository dir sits in. state is one of "open", "closed" or "all"; "" means "open", gh's own default.

func ViewIssue

func ViewIssue(dir string, number int) (*Issue, error)

ViewIssue fetches one issue by number, with its body and comments.

type IssueCreateOptions

type IssueCreateOptions struct {
	Title string
	Body  string
}

IssueCreateOptions is what CreateIssue needs.

type LoginEvent

type LoginEvent struct {
	// Code is the one-time code to type at github.com/login/device, once gh
	// has printed it.
	Code string
	// Line is one line of gh's own commentary, kept for a log a person can
	// read if something goes wrong partway through.
	Line string
	// Done is true on the last event, once gh has confirmed the login. No
	// further events follow it.
	Done bool
	// Err is set instead of Done when the flow did not finish -- the person
	// closed the browser tab, the code expired, the timeout passed.
	Err error
}

LoginEvent is one thing that happened during Login, sent as it happens so the dialog on screen can show the one-time code the moment gh has one to show, rather than only once the whole flow has finished.

Exactly one of Code, Line, Done or Err is set on any event but Code, which also carries the Line it was read from.

type PR

type PR struct {
	Number      int       `json:"number"`
	Title       string    `json:"title"`
	Body        string    `json:"body"`
	State       string    `json:"state"` // OPEN, CLOSED, MERGED
	IsDraft     bool      `json:"isDraft"`
	Author      Actor     `json:"author"`
	URL         string    `json:"url"`
	HeadRefName string    `json:"headRefName"`
	BaseRefName string    `json:"baseRefName"`
	CreatedAt   time.Time `json:"createdAt"`
	UpdatedAt   time.Time `json:"updatedAt"`
	// Comments is filled in by ViewPR; ListPRs leaves it nil, since asking
	// gh for every open PR's comments in one listing call is not something
	// --json supports doing cheaply.
	Comments []Comment `json:"comments"`
}

PR is a pull request, as much of it as the panel shows.

func CreatePR

func CreatePR(dir string, opts PRCreateOptions) (*PR, error)

CreatePR opens a pull request from the branch checked out in dir. dir's branch has to already be pushed with an upstream -- the same thing Push in internal/gitx sets up -- or gh refuses with its own explanation of that.

func CurrentPR

func CurrentPR(dir string) (*PR, error)

CurrentPR looks up the pull request open from the branch checked out in dir, if there is one -- what the CI panel shows without anybody having to know their PR's number. It answers (nil, nil), not an error, when the branch has no PR: gh's own "no pull requests found" is not a failure here, just an empty answer.

func ListPRs

func ListPRs(dir, state string, limit int) ([]PR, error)

ListPRs lists pull requests in the repository dir sits in. state is one of "open", "closed", "merged" or "all"; "" means "open", gh's own default.

func ViewPR

func ViewPR(dir string, number int) (*PR, error)

ViewPR fetches one pull request by number, with its body and comments.

type PRCreateOptions

type PRCreateOptions struct {
	Title string
	Body  string
	Base  string
	Draft bool
}

PRCreateOptions is what CreatePR needs. Title and Body are required; Base is the branch to merge into, and empty means the repository's own default branch, gh's choice to make. Draft opens it as a draft.

type RepoInfo

type RepoInfo struct {
	NameWithOwner    string `json:"nameWithOwner"`
	URL              string `json:"url"`
	DefaultBranchRef struct {
		Name string `json:"name"`
	} `json:"defaultBranchRef"`
}

RepoInfo is what CI and the PR/issue panels show about the repository a working tree belongs to.

func Info

func Info(dir string) (*RepoInfo, error)

Info asks gh which GitHub repository dir belongs to -- gh works this out from the checkout's remotes the same way `gh pr create` does, so a caller never has to parse a remote URL itself. It fails with gh's own explanation when dir is not inside a GitHub repository at all (no remote, or a remote pointed somewhere else).

type Summary

type Summary struct {
	Pending int `json:"pending"`
	Passing int `json:"passing"`
	Failing int `json:"failing"`
	// Overall is "pending" while anything is still running, else "failing"
	// if anything failed, else "passing" if there was anything to check at
	// all, else "none".
	Overall string `json:"overall"`
}

Summary is the CI panel's headline for a set of checks: how many are still running, how many passed, how many failed, and Overall, the one word a pane header or a PR row can show.

func Summarize

func Summarize(checks []Check) Summary

Summarize reduces a list of checks to one headline. It is kept apart from PRChecks so it can be tested against fixed input without running gh.

type WorkflowRun

type WorkflowRun struct {
	DatabaseID   int64  `json:"databaseId"`
	WorkflowName string `json:"workflowName"`
	DisplayTitle string `json:"displayTitle"`
	// Status is "queued", "in_progress" or "completed".
	Status string `json:"status"`
	// Conclusion is set once Status is "completed": "success", "failure",
	// "cancelled", "skipped", "neutral", "timed_out", "action_required" or
	// "startup_failure".
	Conclusion string    `json:"conclusion"`
	Event      string    `json:"event"`
	HeadBranch string    `json:"headBranch"`
	URL        string    `json:"url"`
	CreatedAt  time.Time `json:"createdAt"`
	UpdatedAt  time.Time `json:"updatedAt"`
}

WorkflowRun is one Actions run, as `gh run list` reports it.

func ListRuns

func ListRuns(dir, branch string, limit int) ([]WorkflowRun, error)

ListRuns lists Actions runs for branch, most recent first -- the workflow history the CI panel shows for whatever branch is checked out in dir.

Jump to

Keyboard shortcuts

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