Documentation
¶
Overview ¶
Package pullrequests reads the open pull requests on the repos a registry names, from each provider that hosts one.
GitHub is read in-process through go-gh, with the token gh holds or GH_TOKEN, so prs keeps no credentials of its own. Bitbucket is read through bbkt, which holds its own.
Index ¶
- Constants
- Variables
- func ReadGitHubPullRequest(ctx context.Context, slug string, number int) (PullRequest, Detail, error)
- func Run(ctx context.Context, dir string, argv ...string) ([]byte, error)
- type Check
- type Comment
- type Commit
- type Detail
- type DetailState
- type Details
- type File
- type Listing
- type Provider
- type PullRequest
- type Review
- type Scope
- type Thread
Constants ¶
const ViewerOwner = "@me"
ViewerOwner is the owner GitHub's search reads as the token's own account.
Variables ¶
var ErrNoAnswer = fmt.Errorf("no answer in %s", readTimeout)
ErrNoAnswer is a client killed at its timeout. A client that changes a pull request may have changed it by then.
var Providers = []Provider{ { Name: registry.GitHub, Read: ReadGitHub, Detail: readGitHubDetail, ReadsOwners: true, Head: func(pr PullRequest) string { return fmt.Sprintf("pull/%d/head", pr.Number) }, Browse: func(pr PullRequest) ([]string, bool) { return []string{"gh", "pr", "view", strconv.Itoa(pr.Number), "--repo", pr.Slug, "--web"}, false }, Page: func(pr PullRequest) (string, []string) { return fmt.Sprintf("GH_FORCE_TTY=$(tput cols) gh pr view %d --repo %s --comments | less -R", pr.Number, pr.Slug), []string{"gh", "tput", "less"} }, Merge: func(pr PullRequest, head string) ([]string, bool) { argv := []string{"gh", "pr", "merge", strconv.Itoa(pr.Number), "--repo", pr.Slug, "--merge"} if head != "" { argv = append(argv, "--match-head-commit", head) } return argv, false }, Close: func(pr PullRequest) ([]string, bool) { return []string{"gh", "pr", "close", strconv.Itoa(pr.Number), "--repo", pr.Slug}, false }, Comment: func(pr PullRequest, path string) ([]string, bool) { return []string{"gh", "pr", "comment", strconv.Itoa(pr.Number), "--repo", pr.Slug, "--body-file", path}, false }, }, { Name: registry.Bitbucket, Read: ReadBitbucket, Head: func(pr PullRequest) string { return pr.Branch }, Browse: func(pr PullRequest) ([]string, bool) { return []string{"bbkt", "pr", "open", strconv.Itoa(pr.Number)}, true }, Merge: func(pr PullRequest, _ string) ([]string, bool) { return []string{"bbkt", "pr", "merge", strconv.Itoa(pr.Number), "--yes"}, true }, Comment: func(pr PullRequest, path string) ([]string, bool) { return []string{"bbkt", "pr", "comment", "create", strconv.Itoa(pr.Number), "--body-file", path}, true }, }, }
Providers is a Provider for each name registry.Providers holds.
Functions ¶
func ReadGitHubPullRequest ¶ added in v0.4.0
func ReadGitHubPullRequest(ctx context.Context, slug string, number int) (PullRequest, Detail, error)
ReadGitHubPullRequest is one pull request's row and detail, read by its owner/name slug and number, open or not. The row has no Path or AgeDays until Finish sets them.
Types ¶
type Check ¶ added in v0.4.0
Check is one check run or commit status on the head commit. State is a run's conclusion once it has one and its status before, or a status's state.
type Comment ¶ added in v0.4.0
type Comment struct {
Author string `json:"author"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
URL string `json:"url"`
}
Comment is one comment, on the conversation or in a review thread.
type Commit ¶ added in v0.4.0
type Commit struct {
OID string `json:"oid"`
Headline string `json:"headline"`
Author string `json:"author"`
CommittedAt time.Time `json:"committed_at"`
}
Commit is one commit on the branch, newest last.
type Detail ¶ added in v0.4.0
type Detail struct {
// HeadOID, UpdatedAt and ChecksRollup are the pull request's as this read found them. A
// listing whose row differs in any reads it again. A check finishing moves neither the
// head nor the update time, so the rollup is what reads checks again once they finish.
HeadOID string `json:"head_oid"`
UpdatedAt time.Time `json:"updated_at"`
ChecksRollup string `json:"checks_rollup"`
ReadAt time.Time `json:"read_at"`
ReviewRequests []string `json:"review_requests"`
Reviews []Review `json:"reviews"`
ReviewCount int `json:"review_count"`
Comments []Comment `json:"comments"`
CommentCount int `json:"comment_count"`
Threads []Thread `json:"threads"`
ThreadCount int `json:"thread_count"`
Files []File `json:"files"`
FileCount int `json:"file_count"`
Commits []Commit `json:"commits"`
CommitCount int `json:"commit_count"`
Checks []Check `json:"checks"`
}
Detail is what a pull request's own read adds to its row: who reviewed it at which commit, its conversation, its files, its commits and its head's checks. Each list holds as many as its query asks for, and each count is the whole.
type DetailState ¶ added in v0.4.0
type DetailState struct {
Detail Detail
// Held says Detail was read. A read under way or failed keeps the last one held.
Held bool
Reading bool
// Fault is why the newest read failed, and empty where it read.
Fault string
}
DetailState is how one pull request's detail stands.
type Details ¶ added in v0.4.0
type Details struct {
// contains filtered or unexported fields
}
Details holds each pull request's Detail in memory, by Key, read in the background after a listing lands, so moving the cursor never waits on the network. An entry is read again when its row's head commit, update time or checks rollup moves, or when its last read failed, so a pull request that reads cleanly and stays unchanged is asked for once.
func (*Details) Of ¶ added in v0.4.0
func (d *Details) Of(pr PullRequest) DetailState
Of is how the pull request's detail stands now.
func (*Details) Prefetch ¶ added in v0.4.0
func (d *Details) Prefetch(ctx context.Context, provider Provider, prs []PullRequest) <-chan struct{}
Prefetch reads, in the background and detailsAtOnce at a time, the detail of each of provider's pull requests in prs whose held detail is missing, failed or older than its row. prs is that provider's whole listing, so it also drops the detail of each of the provider's pull requests prs does not hold. It returns at once, and the channel closes when every read it started has ended.
type File ¶ added in v0.4.0
type File struct {
Path string `json:"path"`
Additions int `json:"additions"`
Deletions int `json:"deletions"`
Change string `json:"change"`
}
File is one changed file. Change is the provider's word, such as MODIFIED or ADDED.
type Listing ¶
type Listing struct {
PullRequests []PullRequest
Notes []string
}
Listing is what one provider's read found: its pull requests, and what it could not cover while still reading.
func ReadBitbucket ¶
ReadBitbucket lists each repo's open pull requests with bbkt, one call per repo, run in the repo's checkout because that is how bbkt finds the repo. The calls run at the same time, and their rows keep the registry's order.
A repo bbkt cannot list keeps its rows from last, the previous good listing, with a note saying so, so one repo's failure does not take the others' rows with it. A repo not checked out here cannot be read, and the notes name it.
func ReadGitHub ¶
ReadGitHub is every open pull request in the scope, in one request. A search GitHub refused while others answered keeps the rows it found in last.
type Provider ¶
type Provider struct {
Name string
// Read lists the open pull requests in the scope. last is the previous good listing,
// for a reader that can keep one repo's rows when only that repo failed.
Read func(ctx context.Context, scope Scope, last Listing) (Listing, error)
// Detail reads what the listing leaves out of one pull request. Nil where the host's
// client reads no more than its listing.
Detail func(ctx context.Context, pr PullRequest) (Detail, error)
// ReadsOwners says Read can read an owner whole, for a config that names owners.
ReadsOwners bool
// Head is what git fetches from origin as the pull request's head.
Head func(PullRequest) string
// Browse is the command that opens the pull request in the browser, and whether it
// runs in the checkout.
Browse func(PullRequest) (argv []string, inCheckout bool)
// Page is a shell command paging the pull request with its comments, and the programs
// it runs.
Page func(PullRequest) (command string, needs []string)
// Merge, Close and Comment are the commands that change the pull request, and whether
// each runs in the checkout. Each runs with no terminal, so none may prompt. Where head
// is not empty and the client can pin a commit, Merge refuses a head other than head.
// Comment posts the file at path.
Merge func(pr PullRequest, head string) (argv []string, inCheckout bool)
Close func(PullRequest) (argv []string, inCheckout bool)
Comment func(pr PullRequest, path string) (argv []string, inCheckout bool)
}
Provider is everything prs does differently by host, so a host is added in one place. Every field after Head is a verb a host's client may lack, and is nil there. The key for a nil verb says the host cannot do it.
func ProviderOf ¶
ProviderOf is the Provider named name.
type PullRequest ¶
type PullRequest struct {
// Provider says who hosts it. Every action but the local diff dispatches on it.
Provider string `json:"provider"`
// Repo is the bare name and only a label, since two repositories can share one. Slug
// identifies the repository.
Repo string `json:"repo"`
Slug string `json:"slug"`
Number int `json:"number"`
Title string `json:"title"`
// Author is the login that opened it, and empty for a deleted account.
Author string `json:"author"`
// Bot is true where the provider says an app opened it, and null where its listing
// does not say.
Bot *bool `json:"bot"`
Body string `json:"body"`
URL string `json:"url"`
Branch string `json:"branch"`
Base string `json:"base"`
Draft bool `json:"draft"`
CreatedAt time.Time `json:"created_at"`
// The diff counts are null, never 0, from a provider whose listing does not count
// them. A pull request nobody counted is not one that changed nothing.
Additions *int `json:"additions"`
Deletions *int `json:"deletions"`
ChangedFiles *int `json:"changed_files"`
// Review is the provider's decision, and Reviews how many were submitted. A review can
// set no decision, so the two disagree routinely. Both review and checks are empty
// until a review is requested and a workflow has run.
Review string `json:"review"`
Reviews int `json:"reviews"`
Checks string `json:"checks"`
// AgeDays is how long it has been open, and Path where its repo is checked out here.
AgeDays int `json:"age_days"`
Path string `json:"path"`
// State is OPEN, MERGED or CLOSED on GitHub, and bbkt's word on Bitbucket.
State string `json:"state"`
// The times are null until they happen, and UpdatedAt where the listing omits it.
MergedAt *time.Time `json:"merged_at"`
ClosedAt *time.Time `json:"closed_at"`
UpdatedAt *time.Time `json:"updated_at"`
// Mergeable and MergeStateStatus are GitHub's enums, which read UNKNOWN until GitHub has
// computed them. They, InMergeQueue and HeadOID are null where the listing does not
// carry them, as bbkt's does not.
Mergeable *string `json:"mergeable"`
MergeStateStatus *string `json:"merge_state_status"`
InMergeQueue *bool `json:"in_merge_queue"`
HeadOID *string `json:"head_oid"`
}
PullRequest is one pull request, in the fields `prs list --json` prints, in the order it prints them. cmd/prs/list.schema.json is that shape.
func Finish ¶
func Finish(prs []PullRequest, repos []registry.Repo, now time.Time) []PullRequest
Finish sets each pull request's age and its repo's checkout, and sorts them oldest first, because a backlog is a gauge and age is what it measures. A slug matches in any case, as GitHub matches owners and names.
func (PullRequest) Key ¶
func (pr PullRequest) Key() string
Key names a pull request across reads, when its position and every field can change. It carries the provider, because a GitHub repo and a Bitbucket one can share owner/name.
type Review ¶ added in v0.4.0
type Review struct {
Author string `json:"author"`
State string `json:"state"`
Body string `json:"body"`
// SubmittedAt is null for a review still pending, and CommitOID empty where the commit
// is gone from the branch.
SubmittedAt *time.Time `json:"submitted_at"`
CommitOID string `json:"commit_oid"`
URL string `json:"url"`
}
Review is one submitted review, and the commit it was submitted against, which is what says how much changed since.
type Scope ¶ added in v0.4.0
type Scope struct {
Repos []registry.Repo
// Owners are GitHub owners searched whole. ViewerOwner stands for whoever the token is.
Owners []string
}
Scope is what one provider's read covers: the registry's repos it hosts, each asked for by name, or with no registry the whole of some owners.
type Thread ¶ added in v0.4.0
type Thread struct {
Path string `json:"path"`
Line *int `json:"line"`
Resolved bool `json:"resolved"`
Outdated bool `json:"outdated"`
Comments []Comment `json:"comments"`
CommentCount int `json:"comment_count"`
}
Thread is one review thread on a line of a file. Line is null where the thread's line is gone from the diff.