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 CheckoutOf(pr PullRequest) (string, error)
- 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 Diff
- type DiffState
- type Diffs
- type Fetched
- type File
- type FileDiff
- type Listing
- type LocalDiff
- type Named
- type Provider
- type PullRequest
- type Reference
- type Review
- type Scope
- type Since
- type Standing
- type Thread
Constants ¶
const ReposPerSearch = 20
ReposPerSearch is how many repos one search names. Each search has its own page of 100, so a few busy repos fill their own page rather than every repo's.
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, Standings: map[string]Standing{"OPEN": Open, "MERGED": Merged, "CLOSED": Closed}, PagePath: func(parts []string) (string, string, bool) { if len(parts) < 4 || parts[2] != "pull" { return "", "", false } return parts[0] + "/" + parts[1], parts[3], true }, Read: ReadGitHub, ReadClosed: ReadGitHubClosed, Detail: readGitHubDetail, ReadOne: readGitHubReference, Diff: readGitHubDiff, 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 }, 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, Standings: map[string]Standing{"OPEN": Open, "MERGED": Merged, "DECLINED": Closed, "SUPERSEDED": Closed}, PagePath: func(parts []string) (string, string, bool) { switch { case len(parts) >= 6 && parts[0] == "projects" && parts[2] == "repos" && parts[4] == "pull-requests": return parts[1] + "/" + parts[3], parts[5], true case len(parts) >= 4 && parts[2] == "pull-requests": return parts[0] + "/" + parts[1], parts[3], true } return "", "", false }, Read: ReadBitbucket, ReadOne: readBitbucketReference, 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 CheckoutOf ¶ added in v0.10.0
func CheckoutOf(pr PullRequest) (string, error)
CheckoutOf is the pull request's checkout on this disk, which d, a window action and bbkt each run in.
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"`
AuthorType string `json:"author_type"`
AuthorAssociation string `json:"author_association"`
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. AuthorType and AuthorAssociation are as on a Review.
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 Diff ¶ added in v0.10.0
type Diff struct {
// Head is the commit it was read at, and empty where its source does not say.
Head string
// Lines is how many lines it adds and removes, together.
Lines int
// Whole says Files holds every file. It is false where Lines ran past the limit.
Whole bool
Files []FileDiff
ReadAt time.Time
}
Diff is one pull request's changes as git prints them, from where its branch left its base, split by file. Where they change more lines than its reader keeps whole, Files is empty and Lines says how many they change.
type DiffState ¶ added in v0.10.0
type DiffState struct {
Diff Diff
// Held says Diff 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
// Cannot is why prs reads no diff of it here, and empty where it can.
Cannot string
// InCheckout says it is read in its repo's checkout here rather than from its host.
InCheckout bool
// CheckedOut says its repo is checked out here, which is what d needs to open it.
CheckedOut bool
// Limit is the most lines a Diff changes for Files to be kept.
Limit int
}
DiffState is how one pull request's diff stands.
type Diffs ¶ added in v0.10.0
type Diffs struct {
// contains filtered or unexported fields
}
Diffs holds each pull request's Diff in memory, by Key, read in the background after a listing lands, so the Diff section never waits on the network. A diff comes from its host where the host's client has one, and from the repo's checkout otherwise, its head fetched by FetchHead. One is read again when its row's head commit or base moves, or once a failed read has waited out its retry. A row naming no head commit is read again when its update time moves.
func NewDiffs ¶ added in v0.10.0
NewDiffs is an empty cache whose diffs keep their files up to limit changed lines.
func (*Diffs) Of ¶ added in v0.10.0
func (d *Diffs) Of(pr PullRequest) DiffState
Of is how the pull request's diff stands now.
func (*Diffs) Prefetch ¶ added in v0.10.0
func (d *Diffs) Prefetch(ctx context.Context, from string, prs []PullRequest)
Prefetch reads, in the background, the diff of each of prs whose held diff is missing, older than its row, or failed and waited out. Each carries its Path, as Finish sets it. from names who listed prs, a provider or a read of one pull request, and Prefetch drops the diff of each pull request from listed that prs leaves out.
type Fetched ¶ added in v0.10.0
type Fetched struct {
// Head is the commit the target's Ref holds.
Head string
// BaseFault is why fetching the base failed. A diff then runs against the origin/<base>
// an earlier fetch left.
BaseFault string
}
Fetched is what a fetch left in the checkout.
func Fetch ¶ added in v0.10.0
Fetch is d's fetch, which the person asked for: the target's head into its Ref, then its base, as their own git fetch would move origin/<base>. It never checks the branch out, which would abort on uncommitted work and disturb the tree where there is none.
func FetchHead ¶ added in v0.10.0
FetchHead is the Diff section's fetch, which nobody asked for: the target's head into its Ref and nothing more. It writes no FETCH_HEAD and starts no maintenance, so every ref and file another program reads stays as it was, and the diff runs against the origin/<base> already there.
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 FileDiff ¶ added in v0.10.0
type FileDiff struct {
Path string
// OldPath is where a renamed or copied file came from, and empty otherwise.
OldPath string
// Change is added, deleted, renamed, copied or modified.
Change string
// Binary says git found the content binary, so there are no hunks to show.
Binary bool
// Hunks is each @@ hunk and its lines, as git printed them.
Hunks string
}
FileDiff is one file's part of a Diff: its hunks, without the header git writes above them.
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 LocalDiff ¶ added in v0.10.0
type LocalDiff struct {
Number int
Repo string
Checkout string
// Head is the pull request's head on origin, as its provider names it.
Head string
// Base is the branch the listing says it targets, or origin's default where it names none.
Base string
}
LocalDiff is a pull request as git diffs it in its checkout here: its head fetched into a ref of its own, diffed three-dot from origin/<base>, from where the branch left its base, which is what the provider shows. d opens it, and the Diff section reads it where the host's client has no diff.
func LocalDiffOf ¶ added in v0.10.0
func LocalDiffOf(ctx context.Context, pr PullRequest) (LocalDiff, error)
LocalDiffOf is the pull request's LocalDiff, or why it has none. The base is the one the listing reports, because a pull request stacked on another targets its parent, and a diff against main would show both as one change.
type Named ¶ added in v0.5.0
type Named struct {
PullRequest PullRequest
Detail *Detail
Standing Standing
}
Named is one pull request a reference names, as its own read found it. Detail is nil where the provider reads none.
func ReadReference ¶ added in v0.5.0
ReadReference reads the pull request ref names, open or not. owner/name#number is the registry's repo of that slug, on whichever provider hosts it, and GitHub where the registry names none. A URL whose host is not the one the pull request was read from is refused, because a repo of the same name there holds a different pull request.
type Provider ¶
type Provider struct {
Name string
// Standings is each state word the host's client prints, and where it puts the pull
// request.
Standings map[string]Standing
// PagePath reads owner/name and the number out of the path of a pull request's page,
// split at its slashes, and is false for a path that is not one.
PagePath func(parts []string) (slug, number string, ok bool)
// 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)
// ReadClosed lists the pull requests in the scope merged or closed at or after since, as
// Read lists the open ones. Nil where the host's client cannot say when one closed.
ReadClosed func(ctx context.Context, scope Scope, since time.Time, 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)
// ReadOne reads the one pull request ref names, open or not, with its detail where the
// host has one.
ReadOne func(ctx context.Context, ref Reference) (PullRequest, *Detail, error)
// Diff reads the pull request's diff in git's format from the host, for one whose repo is
// not checked out here. Nil where the host's client has no diff.
Diff func(ctx context.Context, pr PullRequest) (string, 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)
// 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. Name, Standings, PagePath, Read and Head every host has. Every other function 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.
func (PullRequest) Standing ¶ added in v0.5.0
func (pr PullRequest) Standing() (Standing, error)
Standing is where the pull request stands, by its provider's Standings. A word the provider does not name is refused, because a guess would draw a declined pull request as open, or the reverse.
type Reference ¶ added in v0.5.0
type Reference struct {
// Provider is empty for owner/name#number, which the registry resolves.
Provider string
// Host is the URL's, and empty for owner/name#number.
Host string
Slug string
Number int
}
Reference names one pull request as a person or another program hands it to prs.
func ParseReference ¶ added in v0.5.0
ParseReference reads owner/name#number, or the URL of a pull request's page on a host whose PagePath reads it.
type Review ¶ added in v0.4.0
type Review struct {
Author string `json:"author"`
// AuthorType is the author's kind, such as User or Bot, and AuthorAssociation what the
// author is to the repo, such as OWNER or NONE, in the provider's words. Both are empty
// where the provider does not say.
AuthorType string `json:"author_type"`
AuthorAssociation string `json:"author_association"`
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 Since ¶ added in v0.8.0
type Since struct {
// contains filtered or unexported fields
}
Since is where the recently closed pull requests start: a span back from each read, so a board left open keeps the same stretch behind it, or a fixed instant.
func ParseSince ¶ added in v0.8.0
ParseSince reads --since: a span such as 30m, 12h, 2d or 1w, a day such as 2026-10-01, whose first instant in loc it starts at, or an RFC 3339 instant.
type Standing ¶ added in v0.5.0
type Standing int
Standing is where a pull request stands, in the three words prs acts on, whatever word its host prints for it.
type Thread ¶ added in v0.4.0
type Thread struct {
Path string `json:"path"`
Line *int `json:"line"`
Side string `json:"side"`
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. Side is LEFT where Line counts in the base's copy of the file and RIGHT where it counts in the head's.