Help us shape the future of Go! Take the 2026 Go Developer Survey

store

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package store persists PRs, reviews and the audit trail in SQLite.

Index

Constants

View Source
const (
	Queued  = "queued"
	Running = "running"
	Ready   = "ready" // finished; waiting to be posted
	Posted  = "posted"
	Blocked = "blocked" // never posted: possible secret in output
	Failed  = "failed"
	// Posting is held while a post is in flight, so only one caller posts.
	Posting = "posting"
	// PostUnknown: the post may or may not have reached GitHub (timeout,
	// 5xx, or exit mid-post). Posting again first checks GitHub.
	PostUnknown = "post-unknown"
)

Review states.

View Source
const (
	ApprovalWaiting   = "waiting"   // not yet: CI, draft or a human's requested changes
	ApprovalPending   = "pending"   // qualifies; waiting for the operator (key a)
	ApprovalApproving = "approving" // approval in flight
	ApprovalApproved  = "approved"
	ApprovalNo        = "not eligible" // final for this commit
	ApprovalFailed    = "failed"       // GitHub refused (4xx), e.g. own PR
	ApprovalUnknown   = "unknown"      // may have reached GitHub; check before retrying
)

Approval states (Review.Approval). Empty means approval wasn't considered.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuditEvent

type AuditEvent struct {
	ID       int64
	ReviewID int64 // 0 when not tied to a review
	At       time.Time
	Kind     string
	Message  string
}

type LabelState added in v0.3.0

type LabelState int

LabelState is what Ghost knows about a review label on a PR.

const (
	LabelNever    LabelState = iota // never seen on the PR or added by Ghost
	LabelAdded                      // was on the PR (added by Ghost or seen there)
	LabelOptedOut                   // removed after that: don't review
)

type Review

type Review struct {
	ID           int64
	PRID         int64
	Repo         string
	Number       int
	HeadSHA      string
	Agent        string
	State        string
	Prompt       string
	Verdict      string
	Summary      string
	Comments     string // JSON array of agent.InlineComment
	Error        string
	CostUSD      float64
	Approval     string
	ApprovalNote string
	CreatedAt    time.Time
	FinishedAt   time.Time // set once the review stops running
}

type Store

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

func Open

func Open(path string) (*Store, error)

Open opens (creating if needed) the database at path and applies migrations. The database holds private PR content, so new files are owner-only.

func (*Store) AddAudit

func (s *Store) AddAudit(prID, reviewID int64, kind, msg string) error

func (*Store) ApprovalsFor

func (s *Store) ApprovalsFor(prs []github.PR, states ...string) ([]Review, error)

ApprovalsFor returns the reviews of the given PRs whose approval is in one of states.

func (*Store) Audit

func (s *Store) Audit(repo string, number, limit int) ([]AuditEvent, error)

Audit returns a PR's last limit events, oldest first.

func (*Store) ClaimNext

func (s *Store) ClaimNext() (*Review, error)

ClaimNext marks the oldest queued review running and returns it, or nil. The claim is one UPDATE so it takes SQLite's write lock up front: a read-then-write transaction would fail with SQLITE_BUSY_SNAPSHOT under concurrent writers instead of waiting on busy_timeout.

func (*Store) ClearOptOut added in v0.3.0

func (s *Store) ClearOptOut(prID int64, label string) (bool, error)

ClearOptOut records that the label was added back. It reports whether anything changed.

func (*Store) Close

func (s *Store) Close() error

Close closes the database and releases the single-instance lock.

func (*Store) CountState

func (s *Store) CountState(state string) (int, error)

func (*Store) EnqueueReview

func (s *Store) EnqueueReview(prID int64, headSHA, agentName string, force bool) (int64, bool, error)

EnqueueReview queues a review of headSHA. Unless force is set, it does nothing if any review of that head exists, whatever its state — a failed review may have cost money, so it is not retried automatically.

func (*Store) FailInterrupted

func (s *Store) FailInterrupted(cap float64) ([]Review, error)

FailInterrupted settles reviews a previous process left in flight. A running review may have spent money with no cost report, so it fails and is charged cap (it is not retried automatically); a posting one becomes PostUnknown. It returns the affected reviews.

func (*Store) HeadApproved

func (s *Store) HeadApproved(prID int64, headSHA string, exceptID int64) (bool, error)

HeadApproved reports whether another review of the same PR commit is approved by Ghost, or may be (in flight or unknown).

func (*Store) LabelState added in v0.3.0

func (s *Store) LabelState(prID int64, label string) (LabelState, error)

LabelState reports whether label was ever on the PR, and whether it was removed since. Labels compare case-insensitively.

func (*Store) LatestReview

func (s *Store) LatestReview(repo string, number int) (*Review, error)

LatestReview returns the most recent review of a PR, or nil.

func (*Store) MarkLabeled added in v0.3.0

func (s *Store) MarkLabeled(prID int64, label string) error

MarkLabeled records that label is (or was) on the PR. It never clears an opt-out; use ClearOptOut for that.

func (*Store) MarkOptedOut added in v0.3.0

func (s *Store) MarkOptedOut(prID int64, label string) (bool, error)

MarkOptedOut records that a recorded label was removed. It reports whether anything changed, so the opt-out is audited once.

func (*Store) Reviewed added in v0.3.0

func (s *Store) Reviewed(prID int64, headSHA string) (bool, error)

Reviewed reports whether any review (in any state) exists for the PR at headSHA. Auto-review queues each commit at most once.

func (*Store) SaveReview

func (s *Store) SaveReview(r *Review) error

SaveReview writes a review's mutable fields except its approval, which only changes through compare-and-set (UpdateApproval, TransitionApproval) so a stale in-memory review can't undo a newer decision. Leaving the running state stamps finished_at once, which is when its cost counts toward the day.

func (*Store) SchemaVersion

func (s *Store) SchemaVersion() (int, error)

func (*Store) SetNow

func (s *Store) SetNow(now func() time.Time)

SetNow replaces the clock (tests).

func (*Store) SpendSince

func (s *Store) SpendSince(t time.Time) (float64, error)

SpendSince sums the cost of reviews that finished at or after t.

func (*Store) SupersedeApprovals

func (s *Store) SupersedeApprovals(prID, keepID int64, note string) ([]Review, error)

SupersedeApprovals marks the waiting or pending approvals of a PR's other reviews not eligible (a newer review replaces them) and returns them.

func (*Store) TransitionApproval

func (s *Store) TransitionApproval(id int64, from, to string) (bool, error)

TransitionApproval moves a review's approval state only if it is still from; it reports whether it did.

func (*Store) TransitionState

func (s *Store) TransitionState(id int64, from, to string) (bool, error)

TransitionState moves a review from one state to another only if it is still in from; it reports whether it did.

func (*Store) UpdateApproval

func (s *Store) UpdateApproval(id int64, from, to, note string) (bool, error)

UpdateApproval sets a review's approval state and note only if its approval is still from; it reports whether it did.

func (*Store) UpsertPR

func (s *Store) UpsertPR(pr github.PR) (int64, error)

Jump to

Keyboard shortcuts

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