publish

package
v0.25.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 24 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 publish runs the publication state machine: gates, envelope, confirmation, one review request, receipt.

Index

Constants

View Source
const (
	StateInFlight = "in-flight"
	StateUnknown  = "unknown"
)
View Source
const CommentsSchema = 1

Bump on any field change, so an older loupe refuses the file instead of dropping fields (docs/versioning.md).

View Source
const PreviousSchema = 3

Bump on any field change, so an older loupe refuses the file instead of dropping fields (docs/versioning.md).

View Source
const RecordSchema = 2

Bump on any field change, so an older loupe refuses the file instead of dropping fields (docs/versioning.md).

Variables

View Source
var (
	Actions     = []string{"comment", "approve", "request-changes"}
	InlineModes = []string{"none", "blocking", "all"}
)
View Source
var ErrDeclined = errors.New("publish declined; nothing was sent")

ErrDeclined means the human did not confirm; nothing was sent or written.

Functions

func ActionRefusal

func ActionRefusal(action, viewer, author string, d *draft.Draft) error

ActionRefusal is the part of the gates that depends on the chosen action, so a picker can disable an action with the same reason publish would give.

func ClosedSince

func ClosedSince(target run.Target, pr github.PullRequest) string

ClosedSince is how the live pull request differs from an open one at capture: "merged", "closed" or "". The review header and the confirmation both ask it, so they cannot disagree. A run captured closed, which only --allow-closed allows, never warns about the state the human chose to review in; a schema 1 target has no prState, which counts as unknown, not open.

func DeleteAttempt

func DeleteAttempt(dir string) error

func Earlier

func Earlier(filed []EnvelopeFinding, assessments []draft.Assessment, filedIn draft.FiledIn) []draft.EarlierFinding

Earlier is every finding still open before this round: those the previous round assessed as open, in the order it recorded them, which keeps the oldest filing first, then the ones it filed. An open finding therefore rides forward one round at a time, and a round never reads further back than the round before it.

func EditedNotice

func EditedNotice(rounds []int) string

EditedNotice names the rounds of a sticky review edited on GitHub since loupe wrote them, newest first. They are carried as found rather than refused, so the words stay, and this is how the publisher learns of them.

func EncodeComments

func EncodeComments(c Comments) ([]byte, error)

func EncodePrevious

func EncodePrevious(p Previous) ([]byte, error)

func Match

func Match(reviews []github.Review, attempt Attempt) (github.Review, bool)

Match finds the submitted review an attempt created or edited. The marker must be a whole line so a review that quotes it, such as a reply pasting loupe's body, is not taken for the original; a PENDING review was never submitted. A body edited on GitHub can come back with CRLF line endings.

func ReadinessRefusal

func ReadinessRefusal(d *draft.Draft) error

func RecaptureCommand

func RecaptureCommand(target run.Target) string

RecaptureCommand captures the run's pull request again with the same source, which reads the previous round afresh.

func SaveAttempt

func SaveAttempt(dir string, a *Attempt) error

func SaveReceipt

func SaveReceipt(dir string, r *Receipt) error

Types

type Attempt

type Attempt struct {
	Schema    int       `json:"schema"`
	State     string    `json:"state"`
	StartedAt time.Time `json:"startedAt"`
	UpdatedAt time.Time `json:"updatedAt"`
	Envelope  Envelope  `json:"envelope"`
	Confirmed Confirmed `json:"confirmed"`
	LastError string    `json:"lastError,omitempty"`
}

func LoadAttempt

func LoadAttempt(dir string) (Attempt, bool, error)

LoadAttempt reports found false only when attempt.json does not exist; a damaged file is a record refusal.

type BuildInput

type BuildInput struct {
	Target run.Target
	// Round is the published round the body shows; the envelope keeps the capture round, which receipts and run
	// references are keyed by.
	Round  int
	Draft  *draft.Draft
	Viewer string
	Action string
	Inline string
	// Unattended composes the whole publishable set, because nothing accepts a finding in a pipeline.
	Unattended bool
	// PublicationID is minted by the caller, so the reconciliation marker a human approves is the one that is sent.
	PublicationID string
	// Message is the human's own opening prose, typed at the confirmation. It fills the slot the draft's summary
	// fills unattended, and is empty when they typed none.
	Message string
	// Sticky composes a body later rounds edit in place; nil composes an ordinary review.
	Sticky *StickyBuild
	// Note is shown under a sticky round's footer until a later round collapses it. It is not a finding, so the digest
	// never covers it.
	Note string
}

BuildInput is everything a review is composed from. It is a struct rather than a parameter list because PublicationID and Message are adjacent strings the compiler cannot tell apart, and transposing them would publish a UUID as the review's opening prose.

type Comment

type Comment struct {
	Path      string `json:"path"`
	Line      int    `json:"line"`
	Side      string `json:"side"`
	StartLine int    `json:"startLine,omitempty"`
	StartSide string `json:"startSide,omitempty"`
	Body      string `json:"body"`
}

type Comments

type Comments struct {
	Schema          int               `json:"schema"`
	Read            bool              `json:"read"`
	Reason          string            `json:"reason,omitempty"`
	ExcludedReviews int               `json:"excludedReviews,omitempty"`
	Reviews         []FeedbackReview  `json:"reviews,omitempty"`
	Threads         []FeedbackThread  `json:"threads,omitempty"`
	Comments        []FeedbackComment `json:"comments,omitempty"`
}

Comments is the feedback capture read from everyone but the publisher: the pull request's reviews, inline threads and top-level comments when Read, and otherwise the reason they could not be read. The lists are all or nothing, so an agent never reads a failed listing as silence.

func LoadComments

func LoadComments(dir string) (Comments, bool, error)

LoadComments reports found false only when comments.json does not exist, as for a run captured before it did. A damaged file is a record refusal.

func ReadComments

func ReadComments(ctx context.Context, client github.Client, reviews []github.Review, listErr error, owner, repo string,
	number int, viewer, source string) Comments

ReadComments never fails: the feedback is an aid to the reviewer, so a listing that cannot be read is a reason the run records, not a capture that refuses. It leaves out the publisher's own loupe reviews for this source, which show --previous already hands over, and their comments in threads, but keeps everyone's replies to them. It takes capture's one listing of the reviews, and listErr when that listing failed.

type Confirmation

type Confirmation struct {
	// Publish is true only when they confirmed.
	Publish bool
	// Message is the opening prose they typed, empty when they typed none.
	Message string
}

Confirmation is what the human answered at the publish confirmation.

type Confirmed

type Confirmed struct {
	Version      int               `json:"version"`
	Digest       string            `json:"digest"`
	Dispositions map[string]string `json:"dispositions"`
}

Confirmed is what the human saw when they pressed y.

type Envelope

type Envelope struct {
	Target        EnvelopeTarget `json:"target"`
	Viewer        string         `json:"viewer"`
	Action        string         `json:"action"`
	Event         string         `json:"event"`
	CommitID      string         `json:"commitId"`
	DraftVersion  int            `json:"draftVersion"`
	Digest        string         `json:"digest"`
	PublicationID string         `json:"publicationId"`
	Inline        string         `json:"inline"`
	// EditReviewID is the review whose body a sticky round replaces, 0 when the publication creates one.
	EditReviewID int64             `json:"editReviewId,omitempty"`
	Body         string            `json:"body"`
	Comments     []Comment         `json:"comments"`
	Findings     []EnvelopeFinding `json:"findings"`
	// Assessments is the draft's, omitted when there are none so a receipt written before them reads the same.
	Assessments []draft.Assessment `json:"assessments,omitempty"`
}

Envelope is the exact review request that was or may have been sent, plus what a later round needs to read back.

func Build

func Build(in BuildInput) (Envelope, error)

Build composes the review from the accepted findings, or, when unattended, the whole publishable set. It rechecks the allowlist because the draft file may have been edited by hand since the content was filed.

func (Envelope) Unattended

func (e Envelope) Unattended() bool

Unattended reports whether the envelope was composed by --unattended, which never records a viewer; an attended publication always does.

type EnvelopeFinding

type EnvelopeFinding struct {
	ID       string          `json:"id"`
	Title    string          `json:"title"`
	Body     string          `json:"body"`
	Location *draft.Location `json:"location"`
	Label    string          `json:"label"`
	Blocking bool            `json:"blocking"`
}

type EnvelopeTarget

type EnvelopeTarget struct {
	Owner   string `json:"owner"`
	Repo    string `json:"repo"`
	Number  int    `json:"number"`
	HeadSHA string `json:"headSha"`
	Round   int    `json:"round"`
}

type FeedbackComment

type FeedbackComment struct {
	Author    string    `json:"author"`
	Body      string    `json:"body"`
	URL       string    `json:"url"`
	CreatedAt time.Time `json:"createdAt,omitzero"`
}

type FeedbackReview

type FeedbackReview struct {
	ID          int64     `json:"id"`
	Author      string    `json:"author"`
	State       string    `json:"state"`
	Body        string    `json:"body"`
	URL         string    `json:"url"`
	SubmittedAt time.Time `json:"submittedAt,omitzero"`
}

type FeedbackThread

type FeedbackThread struct {
	Path         string            `json:"path"`
	Line         int               `json:"line,omitempty"`
	OriginalLine int               `json:"originalLine,omitempty"`
	Side         string            `json:"side,omitempty"`
	Resolved     bool              `json:"resolved"`
	Outdated     bool              `json:"outdated"`
	Comments     []FeedbackComment `json:"comments"`
}

type GateInput

type GateInput struct {
	IsTerminal bool
	GitHub     github.Client
	Target     run.Target
	Action     string
	Draft      *draft.Draft
	// Unattended skips the gates a pipeline has no human or terminal for: tty, own-pr, the confirmation and readiness.
	Unattended bool
}

type Gated

type Gated struct {
	HeadMoved *HeadMoved
	// Closed is ClosedSince for the pull request the gates read.
	Closed string
}

Gated is what the gates learned that the confirmation shows: a head that only moved forward, and the live state when the pull request is no longer open.

func Gates

func Gates(ctx context.Context, in GateInput) (Gated, error)

Gates refuses in a fixed order so the human always fixes the most fundamental problem first. The token gate comes before any GitHub call, since it needs none. A head that only moved forward is not refused; what it gained is returned for the confirmation to show, with the live state, which never refuses (specs/050-pr-state-awareness).

type HeadMoved

type HeadMoved struct {
	Captured string
	Live     string
	AheadBy  int
	// Commits is the last maxMovedCommits GitHub listed, oldest first, each with a short sha and its subject line. The
	// newest are kept because a merge from the base branch can fill the list with the base branch's older commits.
	Commits []MovedCommit
	// Touched is the ids of publishable located findings on a file the commits changed.
	Touched []string
	// FilesTruncated means GitHub stopped listing changed files at its cap, so Touched can miss findings and the sizes
	// below can be low.
	FilesTruncated bool
	// Files, Additions and Deletions are the push's size, summed over the files GitHub listed, so a human can tell a
	// one-line fix from a rewrite before approving it.
	Files, Additions, Deletions int
}

HeadMoved is what the pull request gained since capture, for the confirmation to show before a review is sent at the captured head.

type MovedCommit

type MovedCommit struct {
	SHA     string
	Subject string
}

type Options

type Options struct {
	Dir    string
	Target run.Target
	// GitHub is called only once no receipt is found, so a replay never needs credentials.
	GitHub func() (github.Client, error)
	// IsTerminal holds when stdin, stdout and, under --json, stderr are terminals.
	IsTerminal bool
	Action     string
	Inline     string
	// Unattended publishes with no terminal, no confirmation and no viewer, from an App installation token.
	Unattended bool
	// RetryUnknown sends again when an attempt's outcome is unknown and no review matches it.
	RetryUnknown bool
	// Sticky edits the publisher's sticky review on the pull request instead of creating a review, or creates it.
	Sticky bool
	// Note is shown on an unattended sticky round while it is the newest. See BuildInput.Note.
	Note string
	// Confirm shows the preview and reports what the human answered: whether they pressed y, and the opening prose
	// they typed. It runs with no lock held.
	Confirm func(Preview) (Confirmation, error)
	Now     func() time.Time
	Getenv  func(string) string
	Stderr  io.Writer
	// HoldSignals is called just before attempt.json is written and the func it returns once the outcome is recorded.
	// Nil holds the signals for real.
	HoldSignals func(signals []os.Signal) (release func())
	// CheckDraft, when set, may refuse the loaded draft. It runs after the terminal rule and before the gates, then
	// again under the lock just before sending, since what it checks can change while the human confirms. It never
	// runs for a replayed publication.
	CheckDraft func(*draft.Draft) error
}

type Preview

type Preview struct {
	// Body, Comments and EnvelopeJSON are the review as it renders with no message, which is what the confirmation
	// opens on.
	Body         string
	Comments     []Comment
	EnvelopeJSON string
	Version      int
	Digest       string
	Dispositions map[string]string
	// HeadMoved is set when the pull request gained commits since capture; the review is still sent at the captured head.
	HeadMoved *HeadMoved
	// Closed is "merged" or "closed" when the pull request is no longer open and was open at capture, which the
	// confirmation names; publish still sends.
	Closed string
	// Edits is the URL of the review whose body this publication replaces, empty when it creates one.
	Edits string
	// EditedIn numbers the rounds of that review edited on GitHub since loupe wrote them that body carries as found,
	// newest first. It takes the body because a longer message can drop one more round for length, so only the body
	// about to be sent can say. Nil when the publication edits nothing.
	EditedIn func(body string) []int
	// Compose rebuilds the review around a message the human typed and returns it with its JSON. The confirmation
	// renders what it returns and publication sends what it returns, so what was approved and what is sent cannot
	// differ. Run always sets it.
	Compose func(message string) (Envelope, string, error)
}

type Previous

type Previous struct {
	Schema    int    `json:"schema"`
	Found     bool   `json:"found"`
	ReviewID  int64  `json:"reviewId,omitempty"`
	ReviewURL string `json:"reviewUrl,omitempty"`
	Round     int    `json:"round,omitempty"`
	// Commit is the round's own head, empty when a sticky body's could not be read back.
	Commit string `json:"commit,omitempty"`
	// PublicationID names the review's current round, which a sticky review's URL alone does not. Empty in a run
	// captured before it was recorded.
	PublicationID string             `json:"publicationId,omitempty"`
	Findings      []EnvelopeFinding  `json:"findings"`
	Assessments   []draft.Assessment `json:"assessments,omitempty"`
	Reason        string             `json:"reason,omitempty"`
	// Unreadable marks a round that is not Found although a loupe review from the publisher exists or may: one that
	// cannot be read back, or a listing that failed.
	Unreadable bool `json:"unreadable,omitempty"`
}

Previous is the round capture read back from GitHub for a run with no earlier local receipt of its publisher's: the publisher's newest loupe review and its findings when Found, and otherwise the reason there is none.

func LoadPrevious

func LoadPrevious(dir string) (Previous, bool, error)

LoadPrevious reports found false only when previous.json does not exist, as for a run captured before it did or after a local receipt of its publisher's. A damaged file is a record refusal.

func ReadPrevious

func ReadPrevious(reviews []github.Review, listErr error, owner, repo string, number int, viewer, source string) Previous

ReadPrevious never fails: the previous round is an aid to the reviewer, so a round that cannot be read is a reason the run records, not a capture that refuses. It never falls back to an older review, which the author has since seen superseded. It takes capture's one listing of the reviews, and listErr when that listing failed.

type Receipt

type Receipt struct {
	Schema    int       `json:"schema"`
	ReviewID  int64     `json:"reviewId"`
	ReviewURL string    `json:"reviewUrl"`
	Action    string    `json:"action"`
	PostedAt  time.Time `json:"postedAt"`
	Envelope  Envelope  `json:"envelope"`
	// Author is the login GitHub returned for the review, recorded because an unattended envelope's Viewer is empty.
	Author string `json:"author,omitempty"`
	// Edited is set when the publication replaced an earlier review's body rather than creating a review.
	Edited bool `json:"edited,omitempty"`
}

func LoadReceipt

func LoadReceipt(dir string) (Receipt, bool, error)

LoadReceipt reports found false only when receipt.json does not exist; a damaged file is a record refusal.

func PreviousReceipt

func PreviousReceipt(root string, ref run.Ref, viewer, source string) (int, Receipt, error)

PreviousReceipt is the earlier local round holding the run's own publisher's newest review, by the rule newestOwn applies to the reviews: a data root two publishers share must not hand one of them findings the other accepted. It skips unpublished rounds because only a receipt records what the pull request author saw.

func Reconcile

func Reconcile(ctx context.Context, gh github.Client, attempt Attempt) (*Receipt, error)

Reconcile returns nil when no review matches. The receipt's postedAt is the attempt's start, the closest time loupe knows to when GitHub recorded the review.

func Run

func Run(ctx context.Context, opts Options) (receipt Receipt, replayed bool, err error)

Run publishes at most one review, or reports replayed when a receipt exists or reconciliation found the review. The lock is released through the gates and the confirmation so agent commands are not blocked while the human reads; after y it is retaken and everything that could have changed is rechecked.

type StickyBuild

type StickyBuild struct {
	Review github.Review
	// Rounds counts this round too.
	Rounds  int
	Earlier []render.Round
}

StickyBuild is the sticky review a round edits, or a zero Review when the round creates it.

Jump to

Keyboard shortcuts

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