github

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 13 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 github is a read-only GraphQL client that searches a user's PR feeds and fetches PR detail as Snapshots.

Index

Constants

View Source
const ActivityLimit = 50

ActivityLimit is how many timeline items ActivityQuery asks for. A PR with that many may have older ones not fetched.

View Source
const ActivityQuery = `query Activity($id: ID!) {
  node(id: $id) { ... on PullRequest {
    timelineItems(last: 50, itemTypes: [PULL_REQUEST_COMMIT, HEAD_REF_FORCE_PUSHED_EVENT, PULL_REQUEST_REVIEW, ISSUE_COMMENT]) {
      nodes {
        __typename
        ... on PullRequestCommit { url commit { oid messageHeadline committedDate author { name user { login } } } }
        ... on HeadRefForcePushedEvent { createdAt actor { __typename login } beforeCommit { oid } afterCommit { oid } }
        ... on PullRequestReview { url state submittedAt author { __typename login } bodyText comments { totalCount } }
        ... on IssueComment { url createdAt author { __typename login } bodyText }
      }
    }
  } }
  ` + rateLimitFields + `
}`

ActivityQuery fetches a PR's last 50 commits, force-pushes, reviews and conversation comments, as the 2026-11-20 live call ran it.

Variables

This section is empty.

Functions

func DetailQuery

func DetailQuery(n int) string

DetailQuery builds a query that fetches n pull requests as aliases p0..p{n-1}, taking their node IDs from variables $id0..$id{n-1} and the viewer's login from $login.

func FeedQuery

func FeedQuery(f config.Feed, now time.Time) string

FeedQuery turns a feed into a GitHub search string, bounded to PRs updated within the feed's window. Drafts are up to the query: the default feed's has draft:false. The bound is truncated to the UTC day (or hour for windows under a day) so the string, and so the search cache key, is stable. Search returns at most 100 PRs; sorting by update makes the ones it drops the least recently active.

func IsRateLimited

func IsRateLimited(err error) bool

IsRateLimited reports whether err is GitHub refusing a call for rate limiting: a GraphQL RATE_LIMITED error, an HTTP 429, or an HTTP 403 that is a primary or secondary rate limit rather than a permission problem.

func IsRefused

func IsRefused(err error) bool

IsRefused reports whether a PostReview error means nothing was posted: GitHub answered with errors and no review, or with an HTTP 4xx, or the input was refused before anything was sent. Any other error, a timeout or a 5xx among them, may have come after GitHub took the review.

func IsUnauthorized

func IsUnauthorized(err error) bool

IsUnauthorized reports whether err is GitHub rejecting the login itself: an HTTP 401, as for an expired or revoked token. A 403 is a permission or rate limit problem, not a bad login.

func RateLimitReset

func RateLimitReset(err error, now time.Time) time.Time

RateLimitReset is when a rate-limited call may be retried, from the Retry-After or X-RateLimit-Reset header. It is zero when err carries neither, as GraphQL errors never do.

func SkippedTypes

func SkippedTypes(skipped []SkippedPR) string

SkippedTypes lists the GraphQL error types in skipped, sorted and without repeats, such as "FORBIDDEN, NOT_FOUND".

Types

type ActivityItem

type ActivityItem struct {
	Kind   ActivityKind
	Author string // the login, or a commit's git author name; "" for a deleted user
	Bot    bool
	At     time.Time
	URL    string // "" for a force-push, which has no page of its own
	// Text is a commit's headline, or a review's or comment's body.
	Text string
	// State is a review's: APPROVED, CHANGES_REQUESTED, COMMENTED or
	// DISMISSED.
	State          string
	OID, BeforeOID string // a commit's, or a force-push's after and before
	InlineComments int    // a review's inline comments
}

ActivityItem is one entry in a PR's timeline (plan 5): a commit, a force-push, a review or a conversation comment.

type ActivityKind

type ActivityKind string

ActivityKind is what an ActivityItem records.

const (
	ActivityCommit    ActivityKind = "commit"
	ActivityForcePush ActivityKind = "force-push"
	ActivityReview    ActivityKind = "review"
	ActivityComment   ActivityKind = "comment"
)

type Check

type Check struct {
	Name     string
	State    CheckState
	Required bool
	// URL is the check's own page (a CheckRun's detailsUrl, a
	// StatusContext's targetUrl). It's empty when the check has none, and in
	// snapshots stored before snow fetched it.
	URL string `json:",omitempty"`
}

type CheckState

type CheckState string
const (
	CheckPending CheckState = "PENDING"
	CheckSuccess CheckState = "SUCCESS"
	CheckFailure CheckState = "FAILURE"
	CheckNeutral CheckState = "NEUTRAL"
	CheckSkipped CheckState = "SKIPPED"
	// CheckActionRequired is a check run held until a person acts through
	// its details link, such as approving the PR's head.
	CheckActionRequired CheckState = "ACTION_REQUIRED"
)

type Client

type Client interface {
	Viewer(ctx context.Context) (string, error)
	// Search returns the fingerprints of up to 100 PRs, 2 pages of 50.
	Search(ctx context.Context, query string) ([]Fingerprint, RateLimit, error)
	// Fingerprints looks PRs up by node ID, 100 to a query. A PR GitHub
	// answers with an error, such as one that no longer resolves, is left
	// out and returned as skipped, even when that is every PR: whether that
	// fails the poll is the poll's decision.
	Fingerprints(ctx context.Context, ids []string) ([]Fingerprint, []SkippedPR, RateLimit, error)
	// Details fetches full Snapshots in batches of aliases, a few batches at
	// a time, in the order of ids. A PR GitHub answers with an error is left
	// out and returned as skipped, as with Fingerprints.
	Details(ctx context.Context, viewer string, ids []string) ([]Snapshot, []SkippedPR, RateLimit, error)
	// ReviewContext fetches what a review reads besides the diff: the PR body,
	// comments, review threads and up to 500 changed file paths.
	ReviewContext(ctx context.Context, id string) (PRContext, RateLimit, error)
	// Activity fetches the PR's last 50 commits, force-pushes, reviews and
	// conversation comments, oldest first, for the drawer's Activity tab.
	Activity(ctx context.Context, id string) ([]ActivityItem, RateLimit, error)
}

func NewClient

func NewClient(d Doer) Client

func NewGHClient

func NewGHClient() (Client, error)

NewGHClient builds a client that authenticates with the user's gh login.

type Comment

type Comment struct {
	Author string
	At     time.Time
}

type Commit

type Commit struct {
	OID     string
	Parents []string
}

type Doer

type Doer interface {
	DoWithContext(ctx context.Context, query string, vars map[string]any, resp any) error
}

Doer is the slice of go-gh's GraphQLClient the client uses.

type Fingerprint

type Fingerprint struct {
	ID          string
	UpdatedAt   time.Time
	HeadOID     string
	RollupState string
}

Fingerprint is the little a cheap query can say about a PR: enough to tell whether it changed since its last Snapshot.

func (Fingerprint) Same

func (f Fingerprint) Same(o Fingerprint) bool

Same reports whether f and o describe the same PR at the same point.

type PRContext

type PRContext struct {
	Body     string
	Comments []TextComment
	Threads  []Thread
	Files    []string
}

PRContext is what a review reads about a PR besides its diff.

type PRState

type PRState string
const (
	PROpen   PRState = "OPEN"
	PRMerged PRState = "MERGED"
	PRClosed PRState = "CLOSED"
)

type PostInput

type PostInput struct {
	PRID, CommitOID, Event, Body string
	Threads                      []PostThread
}

type PostResult

type PostResult struct {
	ReviewID, URL string
	Comments      []PostedComment
}

type PostThread

type PostThread struct {
	Path      string
	Line      int
	StartLine int // 0 = single line
	Body      string
}

type PostedComment

type PostedComment struct {
	ID, Path           string
	Line, OriginalLine int
}

PostedComment is one comment of a posted review. Line is where it sits at the PR's head, 0 when that line is gone; OriginalLine is the line it was posted on, which is what a review of an older commit can match.

type Poster

type Poster interface {
	PostReview(ctx context.Context, in PostInput) (PostResult, error)
	// ThreadIDs maps comment IDs to the IDs of their review threads. A
	// comment it does not find is left out.
	ThreadIDs(ctx context.Context, prID string, commentIDs []string) (map[string]string, error)
}

func NewGHPoster

func NewGHPoster() (Poster, error)

NewGHPoster builds a poster that authenticates with the user's gh login.

func NewPoster

func NewPoster(d Doer) Poster

type RateLimit

type RateLimit struct {
	Remaining, Cost int
	ResetAt         time.Time
}

func (*RateLimit) Add

func (r *RateLimit) Add(next RateLimit)

Add folds another call's rate limit into the total: the lowest remaining with its reset, and the summed cost. Calls run concurrently, so "latest" has no meaning. An empty RateLimit, from a failed call, is ignored.

type RefusedError

type RefusedError struct{ Err error }

RefusedError is a post GitHub answered and declined, so nothing was posted and it is safe to send again.

func (*RefusedError) Error

func (e *RefusedError) Error() string

func (*RefusedError) Unwrap

func (e *RefusedError) Unwrap() error

type Review

type Review struct {
	Author, State, Body, CommitOID string
	SubmittedAt                    time.Time
	// Bot is set when a GitHub App or other bot account wrote the review.
	// Snapshots stored before snow asked are false until the PR is fetched
	// again.
	Bot bool `json:",omitempty"`
	// NewThreads is how many of the review's comments start a thread rather
	// than reply in one. Only the viewer's own reviews carry it; same ignores
	// it so a review isn't seen as two when only one fetch asked for it.
	NewThreads int
}

Review State is one of APPROVED, CHANGES_REQUESTED, COMMENTED, DISMISSED, PENDING.

type SkippedPR

type SkippedPR struct {
	ID, Type, Message string
}

SkippedPR is a requested PR a call left out because GitHub answered it with an error, such as FORBIDDEN when an SSO session has expired.

type Snapshot

type Snapshot struct {
	ID, Repo                         string
	Number                           int
	Title, Author, URL               string
	State                            PRState
	IsDraft                          bool
	HeadOID, BaseRef, ReviewDecision string
	HeadCommittedAt                  time.Time
	RollupState                      string // the head's combined check state; "" with no checks
	Additions, Deletions             int
	Labels                           []string
	UpdatedAt, MergedAt, ClosedAt    time.Time
	Checks                           []Check
	Reviews                          []Review
	Comments                         []Comment
	Commits                          []Commit // oldest to newest
}

func (Snapshot) Fingerprint

func (s Snapshot) Fingerprint() Fingerprint

Fingerprint is the snapshot's own fingerprint, to compare with a fresh one.

type TextComment

type TextComment struct {
	Author, Body string
	At           time.Time
}

TextComment is a PR conversation or review-thread comment. Author is empty for a deleted user.

type Thread

type Thread struct {
	ID, Path string
	Line     int
	Resolved bool
	Comments []TextComment
}

Thread is a review thread. Line is 0 for an outdated thread, which GitHub reports with a null line.

Jump to

Keyboard shortcuts

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