pullrequests

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 19 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 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

View Source
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.

View Source
const ViewerOwner = "@me"

ViewerOwner is the owner GitHub's search reads as the token's own account.

Variables

View Source
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.

View Source
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,
		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 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.

func Run added in v0.3.0

func Run(ctx context.Context, dir string, argv ...string) ([]byte, error)

Run runs a client a key uses as a read runs one, and returns its stdout. It fails with the first line the client said, or with ErrNoAnswer.

Types

type Check added in v0.4.0

type Check struct {
	Name  string `json:"name"`
	State string `json:"state"`
	URL   string `json:"url"`
}

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 NewDetails added in v0.4.0

func NewDetails() *Details

NewDetails is an empty cache.

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

func ReadBitbucket(ctx context.Context, scope Scope, last Listing) (Listing, error)

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

func ReadGitHub(ctx context.Context, scope Scope, last Listing) (Listing, error)

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.

func ReadGitHubClosed added in v0.8.0

func ReadGitHubClosed(ctx context.Context, scope Scope, since time.Time, last Listing) (Listing, error)

ReadGitHubClosed is every pull request in the scope merged or closed at or after since, in one request, as ReadGitHub reads the open ones.

func (Listing) Note

func (l Listing) Note() string

Note is the listing's notes as one line.

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

func ReadReference(ctx context.Context, ref Reference, repos []registry.Repo) (Named, error)

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)
	// 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

func ProviderOf(name string) (Provider, error)

ProviderOf is the Provider named name.

func (Provider) Word added in v0.8.0

func (p Provider) Word(standing Standing) string

Word is the state word the host prints for standing, the first in sorted order where it prints several, such as Bitbucket's DECLINED and SUPERSEDED.

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

func ParseReference(text string) (Reference, error)

ParseReference reads owner/name#number, or the URL of a pull request's page on a host whose PagePath reads it.

func (Reference) String added in v0.5.0

func (r Reference) String() string

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.

func (Scope) Empty added in v0.4.0

func (s Scope) Empty() bool

Empty says the scope names nothing to read.

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

func ParseSince(text string, loc *time.Location) (Since, error)

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.

func (Since) From added in v0.8.0

func (s Since) From(now time.Time) time.Time

From is the first instant inside the window for a read at now.

func (Since) String added in v0.8.0

func (s Since) String() string

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.

const (
	Open Standing = iota
	Merged
	Closed
)

func (Standing) String added in v0.5.0

func (s Standing) String() string

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.

Jump to

Keyboard shortcuts

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