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 ¶
- func Installed() bool
- func Login(ctx context.Context, onEvent func(LoginEvent)) error
- func Logout(dir string) error
- func Platform() string
- func TryInstall(ctx context.Context, in Installer, onLine func(string)) error
- type Actor
- type AuthStatus
- type Check
- type Comment
- type Installer
- type Issue
- type IssueCreateOptions
- type LoginEvent
- type PR
- type PRCreateOptions
- type RepoInfo
- type Summary
- type WorkflowRun
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 ¶
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 ¶
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.
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 ¶
CommentIssue posts a comment on an issue.
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 ¶
ListIssues lists issues in the repository dir sits in. state is one of "open", "closed" or "all"; "" means "open", gh's own default.
type IssueCreateOptions ¶
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 ¶
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.
type PRCreateOptions ¶
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 ¶
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.
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.