Documentation
¶
Overview ¶
Package remote drives GitHub operations (currently PR review) through the user's `gh` CLI. It performs no HTTPS calls itself — `gh` handles auth, host resolution, and the REST round-trips. See ADR-0016.
Index ¶
- Variables
- func BuildCommentBody(f render.Finding, whoami string) string
- func BuildReviewBody(v Verdict, whoami string) string
- func BuildUnanchoredSection(findings []render.Finding) string
- func CommentText(f render.Finding, whoami string) string
- func EnsureGH() error
- func FetchCompareDiff(ctx context.Context, r Runner, slug, base, head string) (string, error)
- func FetchDiff(ctx context.Context, r Runner, id, repo string) (string, error)
- func FetchLastOID(ctx context.Context, r Runner, id, repo string) (string, error)
- func FetchLastReviewedOID(ctx context.Context, r Runner, slug string, number int) (string, error)
- func NormalizeText(s string) string
- func ParseReviewedMarker(body string) (string, bool)
- func PostComment(ctx context.Context, r Runner, c CommentRequest) error
- func ReplyToComment(ctx context.Context, r Runner, slug string, number int, commentID int64, ...) error
- func ResolveThread(ctx context.Context, r Runner, nodeID string) error
- func ReviewedMarker(oid string) string
- func SubmitReview(ctx context.Context, r Runner, id, repo string, v Verdict, body string) error
- func UnresolveThread(ctx context.Context, r Runner, nodeID string) error
- func UpdateComment(ctx context.Context, r Runner, slug string, commentID int64, body string) error
- func ValidFingerprint(fp string) bool
- func Whoami(ctx context.Context, r Runner) (string, error)
- func WithReviewedMarker(body, oid string) string
- type Class
- type Comment
- type CommentRequest
- type Compare
- type CompareCommit
- type Group
- type Marker
- type PRMeta
- type Result
- type Runner
- type SyncParams
- type Thread
- type Verdict
- type Warning
Constants ¶
This section is empty.
Variables ¶
var ( // ErrGHMissing — the `gh` binary is not on PATH. ErrGHMissing = errors.New("remote: gh binary not found on PATH") // ErrSelfPR — the authenticated user is the PR author (GitHub blocks // self-approval; we reject before fetching the diff). ErrSelfPR = errors.New("remote: cannot review your own pull request") // ErrTooVolatile — the PR head changed twice during review; aborted. ErrTooVolatile = errors.New("remote: pull request changed during review") )
Sentinel errors let the CLI layer map failures to i18n catalog keys without string-matching. Wrap with %w where extra context helps.
Functions ¶
func BuildCommentBody ¶
BuildCommentBody renders one finding as an inline review comment body. Fixed English (ADR-0016 §10):
[SEVERITY] - Title Description 💡 Suggestion @whoami by #commitbrief
A finding that carries a fingerprint also gets the identity marker on its own trailing line (ADR-0046); the visible text is unchanged.
func BuildReviewBody ¶
BuildReviewBody returns the fixed-English review body for a verdict (ADR-0016 §6.4).
func BuildUnanchoredSection ¶ added in v1.2.1
BuildUnanchoredSection renders the findings that fell back to the review body. Returns "" when there are none so callers can append it unconditionally.
func CommentText ¶ added in v1.20.0
CommentText renders the visible part of a finding's comment (no marker).
func EnsureGH ¶
func EnsureGH() error
EnsureGH reports ErrGHMissing when the `gh` binary is not on PATH.
func FetchCompareDiff ¶ added in v1.20.0
FetchCompareDiff returns the unified diff of base...head. Large comparisons make the API fail; the caller then falls back to the full PR diff.
func FetchLastOID ¶
FetchLastOID re-reads only the commits to detect a head change cheaply between the diff fetch and the review submission (race check).
func FetchLastReviewedOID ¶ added in v1.20.0
FetchLastReviewedOID returns the head OID recorded in the newest review the viewer authored that carries the K8 marker, looking at the PR's last 100 reviews only. An empty OID with a nil error means there is none.
func NormalizeText ¶ added in v1.20.0
NormalizeText prepares comment text for the "is the body the same?" comparison: CRLF -> LF, trailing whitespace trimmed per line and overall.
func ParseReviewedMarker ¶ added in v1.20.0
ParseReviewedMarker extracts the head OID from a review body. When a body holds several markers the last one wins.
func PostComment ¶
func PostComment(ctx context.Context, r Runner, c CommentRequest) error
PostComment posts a single inline review comment via the REST API. Side is chosen by the caller from the parsed diff (RIGHT for added / context lines, LEFT for removed ones); a finding whose line is outside the diff is filtered out upstream and never reaches here, so a 422 is now an unexpected GitHub error rather than the routine hallucinated-line case (ADR-0016 §9).
func ReplyToComment ¶ added in v1.20.0
func ReplyToComment(ctx context.Context, r Runner, slug string, number int, commentID int64, body string) error
ReplyToComment posts a reply into the thread rooted at commentID.
func ResolveThread ¶ added in v1.20.0
ResolveThread marks a review thread resolved.
func ReviewedMarker ¶ added in v1.20.0
ReviewedMarker returns the hidden marker recording that a review covered the PR up to head commit oid.
func SubmitReview ¶
SubmitReview submits the review-level verdict via `gh pr review`.
func UnresolveThread ¶ added in v1.20.0
UnresolveThread reopens a resolved review thread.
func UpdateComment ¶ added in v1.20.0
UpdateComment replaces the body of one review comment (REST PATCH).
func ValidFingerprint ¶ added in v1.20.0
ValidFingerprint reports whether fp is a 64-char lowercase hex digest, the only shape the identity marker accepts.
func WithReviewedMarker ¶ added in v1.20.0
WithReviewedMarker appends the K8 marker for oid to a review body. An empty oid leaves the body unchanged.
Types ¶
type Class ¶ added in v1.20.0
type Class int
Class is how a thread CommitBrief owns is currently classified (S1-S9).
const ( // ClassActive is an open thread CommitBrief manages (S1). ClassActive Class = iota // ClassHeld is an open thread a human reopened or marked to keep (S2-S5). ClassHeld // ClassClosedByUs is a thread CommitBrief resolved and nobody touched since (S6). ClassClosedByUs // ClassClosedByHuman is a closed thread a human decided on (S7-S9). ClassClosedByHuman )
type CommentRequest ¶
type CommentRequest struct {
RepoSlug string
PRNumber int
CommitID string
Path string
Line int
Side string
Body string
}
CommentRequest is one inline comment to POST. RepoSlug is the PR's baseRepository ("owner/name", cross-fork correctness); CommitID is the head OID the diff was fetched at. Side is "RIGHT" (new file) or "LEFT" (old file); empty defaults to RIGHT.
type Compare ¶ added in v1.20.0
type Compare struct {
Status string `json:"status"`
TotalCommits int `json:"total_commits"`
Commits []CompareCommit `json:"commits"`
}
Compare is the part of GET /repos/{o}/{r}/compare/{base}...{head} that the incremental review needs.
func FetchCompare ¶ added in v1.20.0
FetchCompare compares two commits through the REST API. The commit list is paginated by GitHub, so every page is read and merged.
func (Compare) Ahead ¶ added in v1.20.0
Ahead reports whether head is strictly ahead of base, i.e. base is an ancestor of head (no force-push in between).
type CompareCommit ¶ added in v1.20.0
type CompareCommit struct {
SHA string `json:"sha"`
Parents []struct {
SHA string `json:"sha"`
} `json:"parents"`
}
CompareCommit is one commit of a comparison; Parents has more than one entry for a merge commit.
type Group ¶ added in v1.20.0
type Group struct {
FP string
Rep render.Finding
Members []render.Finding
Severity render.Severity
Side string
Anchored bool
Text string
}
Group is every finding that shares one fingerprint, folded into a single comment. Rep is the finding the comment is anchored on (lowest anchorable line); Text is the rendered comment without the identity marker. A group with an empty FP is unmanaged: it never matches a thread and is always posted fresh, like before comment sync existed.
type Marker ¶ added in v1.20.0
Marker is a parsed identity marker. Attrs holds every attribute token other than `v=1`, in document order — unknown keys and values included — so a rewrite preserves what it does not understand.
func NewMarker ¶ added in v1.20.0
NewMarker returns a marker with no attributes (a thread CommitBrief manages and has not closed).
func SplitMarker ¶ added in v1.20.0
SplitMarker separates a comment body into its text and identity marker. ok is false when the body carries no valid v=1 marker. text is normalized for comparison (CRLF -> LF, trailing whitespace trimmed).
func (Marker) String ¶ added in v1.20.0
String renders the marker exactly as it is stored in a comment body.
type PRMeta ¶
type PRMeta struct {
Number int `json:"number"`
Author owner `json:"author"`
URL string `json:"url"`
HeadRepository repoRef `json:"headRepository"`
Commits []commit `json:"commits"`
}
PRMeta is the subset of `gh pr view --json ...` that remote pr consumes.
Note: gh does not expose a baseRepository field on PRs — the base repo is derived from the PR URL (which always points at the base repo) via BaseSlug.
func FetchPRMeta ¶
FetchPRMeta runs `gh pr view <id> --json number,author,url,headRepository,commits`.
func (PRMeta) AuthorLogin ¶
AuthorLogin returns the PR author's GitHub login.
type Result ¶ added in v1.20.0
type Result struct {
Posted int
PostFailed int
Updated int
Unchanged int
Resolved int
Replied int
Held int
// StaleOpen counts duplicate (non-canonical) threads that could not be
// resolved and were left open.
StaleOpen int
// StaleReason says why the duplicates stayed open: "permission" when the
// viewer may not resolve (or record), otherwise the first API error.
StaleReason string
// Unanchored are findings that could not be given an inline comment and
// must be listed in the review body instead.
Unanchored []render.Finding
Warnings []Warning
// OpenOurs counts CommitBrief threads that were already on the PR and are
// still open once the sync is done: untouched, held and out-of-scope
// ones, and threads left open by a failed resolve. Comments this run
// posted are not counted: the verdict already weighs the findings they
// carry. An incremental verdict that approves must not ignore OpenOurs
// (ADR-0046 §A.7b).
OpenOurs int
}
Result is the outcome of one Sync run.
type Runner ¶
Runner executes a `gh` invocation and returns its stdout. Production uses execRunner; tests inject a fake to avoid live network and to script sequential responses (race-retry, failure paths).
type SyncParams ¶ added in v1.20.0
type SyncParams struct {
Runner Runner
Slug string
PRNumber int
CommitID string
Threads []Thread
Groups []Group
Select func([]render.Finding) []render.Finding
// InScope reports whether a thread's file was part of this run's review.
// A nil InScope means the whole PR was reviewed. Threads outside the
// scope are never touched: a narrowed run (--file/--dir/--exclude-*) says
// nothing about files it did not look at (ADR-0046 §Update 2026-10-18).
InScope func(path string) bool
// Incremental marks a --since-last-review run: only the delta since the
// last review was re-reviewed, so a finding missing from Groups proves
// nothing about code outside that delta. In that mode a thread whose
// finding is gone is resolved only when it is active, outdated and its
// file is in InDiff (ADR-0046 §A.7b); every other thread is left alone.
Incremental bool
// InDiff reports whether a file is part of the incremental diff. It is
// consulted only when Incremental is set; nil then means no file is.
InDiff func(path string) bool
}
SyncParams is the input of Sync. Select applies the loop cap (ADR-0016 §6) to the findings that need a brand-new inline comment.
type Thread ¶ added in v1.20.0
type Thread struct {
NodeID string
Path string
Resolved bool
Outdated bool
CanResolve bool
CanUnresolve bool
CanReply bool
Root Comment
Recent []Comment
}
Thread is a review thread snapshot (GraphQL `reviewThreads`). Root is the first comment (it carries the identity marker); Recent is the tail of the thread, oldest first, so its last element is the thread's latest comment.
func FetchThreads ¶ added in v1.20.0
FetchThreads reads every review thread of a PR through GraphQL. `gh api graphql --paginate` emits one JSON document per page, back to back, so the output is decoded as a stream. Empty output is an empty snapshot.