github

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package github wraps the GitHub CLI operations live-pr needs.

Index

Constants

View Source
const CacheVersion = 1
View Source
const PRPageSize = 25

Variables

View Source
var ErrPRNotFound = errors.New("pull request not found")

ErrPRNotFound means no open pull request exists for the requested head.

Functions

func SaveCache

func SaveCache(path string, c Cache) error

SaveCache atomically replaces path with c.

func SaveNavigatorCache

func SaveNavigatorCache(path string, c NavigatorCache) error

func SaveReviewDraft added in v0.2.0

func SaveReviewDraft(path string, draft ReviewDraft) error

func ValidateReviewComment added in v0.2.0

func ValidateReviewComment(comment ReviewComment) error

func ValidateReviewDraft added in v0.2.0

func ValidateReviewDraft(draft ReviewDraft) error

func ValidateReviewEvent added in v0.2.0

func ValidateReviewEvent(event ReviewEvent) error

Types

type Activity

type Activity struct {
	ID        int64  `json:"id"`
	NodeID    string `json:"node_id"`
	Event     string `json:"event"`
	CreatedAt string `json:"created_at"`
	CommitID  string `json:"commit_id"`
	Actor     struct {
		Login     string `json:"login"`
		AvatarURL string `json:"avatar_url,omitempty"`
	} `json:"actor"`
	Label struct {
		Name string `json:"name"`
	} `json:"label"`
	Assignee struct {
		Login string `json:"login"`
	} `json:"assignee"`
	RequestedReviewer struct {
		Login string `json:"login"`
	} `json:"requested_reviewer"`
	Rename struct {
		From string `json:"from"`
		To   string `json:"to"`
	} `json:"rename"`
}

Activity is a non-comment PR timeline event from GitHub's issue events API.

type Cache

type Cache struct {
	Version                      int                   `json:"version"`
	Head                         string                `json:"head,omitempty"`
	PR                           *PR                   `json:"pr,omitempty"`
	ExplicitCheckout             bool                  `json:"explicit_checkout,omitempty"`
	Comments                     []Comment             `json:"comments,omitempty"`
	Activities                   []Activity            `json:"activities,omitempty"`
	Reviews                      []Review              `json:"reviews,omitempty"`
	ReviewComments               []ReviewThreadComment `json:"review_comments,omitempty"`
	FetchedAt                    string                `json:"fetched_at,omitempty"`
	LastPublishedManagedBodyHash string                `json:"last_published_managed_body_hash,omitempty"`
}

Cache is branch-local mutable GitHub state. Remote resources intentionally stay separate from the append-only local timeline.

func LoadCache

func LoadCache(path, head string) (Cache, error)

LoadCache loads path. A missing file is an empty cache, while malformed or unsupported data is reported so callers can keep running without trusting it.

func NewCache

func NewCache(head string) Cache

NewCache returns an initialized empty cache.

func (Cache) Base

func (c Cache) Base(fallback string) string

Base returns the bound PR base when known, otherwise fallback.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client runs GitHub operations through gh.

func New

func New() Client

New returns a GitHub CLI client.

func (Client) Checkout

func (c Client) Checkout(number int) error

Checkout checks out a pull request using GitHub CLI's native branch handling.

func (Client) Close

func (c Client) Close(number int) error

Close closes a pull request without merging it.

func (Client) Create

func (c Client) Create(base, head, title, bodyFile string, draft bool) (string, error)

Create creates a PR and returns gh's output (normally its URL).

func (Client) DeleteIssueComment added in v0.5.1

func (c Client) DeleteIssueComment(id int64) error

DeleteIssueComment removes a conversation comment by its ID.

func (Client) EditIssueComment added in v0.5.0

func (c Client) EditIssueComment(id int64, body string) error

EditIssueComment updates an existing conversation comment by its ID.

func (Client) Find

func (c Client) Find(number int) (PR, error)

func (Client) FindAllForHead added in v0.5.0

func (c Client) FindAllForHead(head string) ([]PR, error)

FindAllForHead returns every PR (any state) whose head is the given branch.

func (Client) FindChecks added in v0.2.0

func (c Client) FindChecks(number int) (PR, error)

FindChecks loads only the current head revision and its CI rollup.

func (Client) FindForHead

func (c Client) FindForHead(head string) (PR, error)

FindForHead prefers an open PR, then returns the newest PR in any state. When multiple PRs share the same head branch, the newest (highest number) wins.

func (Client) FindOpen

func (c Client) FindOpen(head string) (PR, error)

FindOpen finds the open PR for head. Operational errors are returned as-is; only a successful empty list becomes ErrPRNotFound.

func (Client) FindPreview

func (c Client) FindPreview(number int) (PR, error)

FindPreview loads the expensive preview fields for one PR only.

func (Client) IssueActivities

func (c Client) IssueActivities(number int) ([]Activity, error)

IssueActivities returns non-comment activity from the PR timeline.

func (Client) IssueComments

func (c Client) IssueComments(number int) ([]Comment, error)

IssueComments returns every top-level Conversation comment for a PR.

func (Client) IssueDetail

func (c Client) IssueDetail(number int) IssueDetailResult

IssueDetail loads independent comment and activity collections concurrently.

func (Client) LoadPRDetail

func (c Client) LoadPRDetail(number int) PRDetail

LoadPRDetail loads preview metadata, comments, and activity concurrently.

func (Client) Merge

func (c Client) Merge(number int, headOID string) error

Merge merges a pull request with a merge commit.

func (Client) PostIssueComment added in v0.5.0

func (c Client) PostIssueComment(number int, body string) error

PostIssueComment posts a conversation comment on a pull request.

func (Client) ReviewComments added in v0.5.0

func (c Client) ReviewComments(number int) ([]ReviewThreadComment, error)

ReviewComments returns the inline review comments for a pull request.

func (Client) Reviews added in v0.5.0

func (c Client) Reviews(number int) ([]Review, error)

Reviews returns the submitted reviews for a pull request.

func (Client) SearchPRs

func (c Client) SearchPRs(query, cursor string) (PRPage, error)

SearchPRs returns exactly one page. Callers decide when to request PageInfo.EndCursor.

func (Client) SetStatus added in v0.2.1

func (c Client) SetStatus(pr PR, target string) error

SetStatus changes a pull request to open, closed, or draft.

func (Client) SubmitReview added in v0.2.0

func (c Client) SubmitReview(draft ReviewDraft, event ReviewEvent) error

SubmitReview publishes a complete review and its inline comments atomically.

func (Client) Update

func (c Client) Update(head, title, bodyFile string) error

Update replaces the title and body of an existing PR.

func (Client) UpdateBody added in v0.5.0

func (c Client) UpdateBody(head, bodyFile string) error

UpdateBody replaces only the body of an existing PR, preserving the title.

type Comment

type Comment struct {
	ID        int64  `json:"id"`
	NodeID    string `json:"node_id"`
	Body      string `json:"body"`
	CreatedAt string `json:"created_at"`
	UpdatedAt string `json:"updated_at"`
	HTMLURL   string `json:"html_url"`
	User      struct {
		Login     string `json:"login"`
		AvatarURL string `json:"avatar_url,omitempty"`
	} `json:"user"`
}

Comment is a top-level PR conversation comment.

type IssueDetailResult added in v0.4.1

type IssueDetailResult struct {
	Comments      []Comment
	Activities    []Activity
	CommentsErr   error
	ActivitiesErr error
}

IssueDetailResult bundles the concurrently-loaded comment and activity collections with their independent errors.

type NavigatorCache struct {
	Version       int                    `json:"version"`
	Repository    string                 `json:"repository,omitempty"`
	ViewerLogin   string                 `json:"viewer_login,omitempty"`
	PRs           []PR                   `json:"prs,omitempty"`
	PRsState      string                 `json:"prs_state,omitempty"`
	FetchedStates map[string]bool        `json:"fetched_states,omitempty"`
	Views         map[string]PRViewCache `json:"views,omitempty"`
	Snapshots     map[string]PRSnapshot  `json:"snapshots,omitempty"`
	FetchedAt     string                 `json:"fetched_at,omitempty"`
}

NavigatorCache is repository-wide browse state. Publish conflict state remains in the branch-local Cache and is intentionally absent here.

func LoadNavigatorCache

func LoadNavigatorCache(path string) (NavigatorCache, error)

func NewNavigatorCache

func NewNavigatorCache() NavigatorCache
func (c NavigatorCache) Clone() NavigatorCache

Clone returns a copy that is safe to marshal while the original keeps mutating: the PRs backing array is rewritten in place by preview updates and the maps by SetView/SetSnapshot, but their values are only ever replaced wholesale, so element copies suffice.

func (c *NavigatorCache) PrunePRs()
func (c *NavigatorCache) SetSnapshot(snapshot PRSnapshot)
func (c *NavigatorCache) SetView(name string, prs []PR, total int, fetchedAt string)
func (c NavigatorCache) Snapshot(number int) (PRSnapshot, bool)
func (c NavigatorCache) View(name string) ([]PR, PRViewCache, bool)

type PR

type PR struct {
	Number                int                     `json:"number"`
	URL                   string                  `json:"url"`
	Title                 string                  `json:"title"`
	Body                  string                  `json:"body"`
	State                 string                  `json:"state"`
	BaseRefName           string                  `json:"baseRefName,omitempty"`
	BaseRefOID            string                  `json:"baseRefOid,omitempty"`
	HeadRefName           string                  `json:"headRefName,omitempty"`
	HeadRefOID            string                  `json:"headRefOid,omitempty"`
	IsDraft               bool                    `json:"isDraft,omitempty"`
	IsCrossRepository     bool                    `json:"isCrossRepository,omitempty"`
	Mergeable             string                  `json:"mergeable,omitempty"`
	MergeStateStatus      string                  `json:"mergeStateStatus,omitempty"`
	ReviewDecision        string                  `json:"reviewDecision,omitempty"`
	Additions             int                     `json:"additions,omitempty"`
	Deletions             int                     `json:"deletions,omitempty"`
	ChangedFiles          int                     `json:"changedFiles,omitempty"`
	UpdatedAt             string                  `json:"updatedAt,omitempty"`
	Conversation          []PRConversationComment `json:"comments,omitempty"`
	CommentCount          int                     `json:"commentCount,omitempty"`
	CommitCount           int                     `json:"commitCount,omitempty"`
	Commits               []PRCommit              `json:"commits,omitempty"`
	Checks                []PRCheck               `json:"statusCheckRollup,omitempty"`
	CheckRollupState      string                  `json:"checkRollupState,omitempty"`
	Author                PRUser                  `json:"author,omitempty"`
	CreatedAt             string                  `json:"createdAt,omitempty"`
	Assignees             []PRUser                `json:"assignees,omitempty"`
	Labels                []PRLabel               `json:"labels,omitempty"`
	ReviewRequests        []PRUser                `json:"reviewRequests,omitempty"`
	ViewerReviewRequested bool                    `json:"viewerReviewRequested,omitempty"`
	PreviewLoaded         bool                    `json:"previewLoaded,omitempty"`
}

PR is the remote pull-request state needed by live-pr.

type PRCheck

type PRCheck struct {
	Name         string `json:"name,omitempty"`
	Context      string `json:"context,omitempty"`
	Status       string `json:"status,omitempty"`
	Conclusion   string `json:"conclusion,omitempty"`
	State        string `json:"state,omitempty"`
	WorkflowName string `json:"workflowName,omitempty"`
	StartedAt    string `json:"startedAt,omitempty"`
	CompletedAt  string `json:"completedAt,omitempty"`
}

PRCheck covers both GitHub check runs and legacy status contexts.

type PRCommit

type PRCommit struct {
	OID              string `json:"oid"`
	CommittedDate    string `json:"committedDate,omitempty"`
	MessageHeadline  string `json:"messageHeadline,omitempty"`
	CheckRollupState string `json:"checkRollupState,omitempty"`
}

PRCommit is one pull-request commit with its commit-specific CI rollup.

type PRConversationComment

type PRConversationComment struct {
	Author    PRUser `json:"author"`
	Body      string `json:"body"`
	CreatedAt string `json:"createdAt"`
	URL       string `json:"url,omitempty"`
}

PRConversationComment is compact list-preview conversation metadata.

type PRDetail

type PRDetail struct {
	PR                PR
	Comments          []Comment
	Activities        []Activity
	Reviews           []Review
	ReviewComments    []ReviewThreadComment
	PreviewErr        error
	CommentsErr       error
	ActivitiesErr     error
	ReviewsErr        error
	ReviewCommentsErr error
}

PRDetail is one concurrently loaded pull-request detail snapshot.

type PRLabel

type PRLabel struct {
	Name  string `json:"name"`
	Color string `json:"color"`
}

PRLabel is a GitHub label and its six-digit RGB color.

type PRPage

type PRPage struct {
	Repository  string
	ViewerLogin string
	PRs         []PR
	TotalCount  int
	PageInfo    PageInfo
}

PRPage is one explicitly requested page of lightweight pull-request rows.

type PRSnapshot

type PRSnapshot struct {
	PR         PR         `json:"pr"`
	Comments   []Comment  `json:"comments,omitempty"`
	Activities []Activity `json:"activities,omitempty"`
	FetchedAt  string     `json:"fetched_at,omitempty"`
}

PRSnapshot is cached Conversation data for one browsed pull request.

type PRUser

type PRUser struct {
	Login     string `json:"login"`
	AvatarURL string `json:"avatarUrl,omitempty"`
}

PRUser is a GitHub account attached to PR metadata.

type PRViewCache

type PRViewCache struct {
	Numbers    []int  `json:"numbers,omitempty"`
	TotalCount int    `json:"total_count,omitempty"`
	FetchedAt  string `json:"fetched_at,omitempty"`
}

PRViewCache preserves loaded row order and an exact server count without duplicating PR metadata already stored in NavigatorCache.PRs.

type PageInfo

type PageInfo struct {
	HasNextPage bool   `json:"hasNextPage"`
	StartCursor string `json:"startCursor"`
	EndCursor   string `json:"endCursor"`
}

PageInfo is one GitHub cursor boundary.

type Review added in v0.5.0

type Review struct {
	ID          int64  `json:"id"`
	Body        string `json:"body"`
	State       string `json:"state"`
	SubmittedAt string `json:"submitted_at"`
	User        struct {
		Login     string `json:"login"`
		AvatarURL string `json:"avatar_url,omitempty"`
	} `json:"user"`
}

Review is a submitted pull-request review (approve / request-changes / comment) with its optional summary body.

type ReviewComment added in v0.2.0

type ReviewComment struct {
	Path string `json:"path"`
	Line int    `json:"line"`
	Side string `json:"side"`
	Body string `json:"body"`
}

ReviewComment is one pending inline GitHub review comment.

type ReviewDraft added in v0.2.0

type ReviewDraft struct {
	Version  int             `json:"version"`
	PR       int             `json:"pr"`
	Commit   string          `json:"commit_id"`
	Body     string          `json:"body,omitempty"`
	Comments []ReviewComment `json:"comments,omitempty"`
}

ReviewDraft is a local, unpublished GitHub pull-request review.

func LoadReviewDraft added in v0.2.0

func LoadReviewDraft(path string, pr int, commit string) (ReviewDraft, error)

func NewReviewDraft added in v0.2.0

func NewReviewDraft(pr int, commit string) ReviewDraft

type ReviewEvent added in v0.2.0

type ReviewEvent string

ReviewEvent is one GitHub review verdict.

const (
	ReviewCommentEvent        ReviewEvent = "COMMENT"
	ReviewApproveEvent        ReviewEvent = "APPROVE"
	ReviewRequestChangesEvent ReviewEvent = "REQUEST_CHANGES"
)

type ReviewThreadComment added in v0.5.0

type ReviewThreadComment struct {
	ID        int64  `json:"id"`
	Body      string `json:"body"`
	Path      string `json:"path"`
	Line      int    `json:"line"`
	CreatedAt string `json:"created_at"`
	User      struct {
		Login     string `json:"login"`
		AvatarURL string `json:"avatar_url,omitempty"`
	} `json:"user"`
}

ReviewThreadComment is one inline review comment on a diff line.

Jump to

Keyboard shortcuts

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