Documentation
¶
Overview ¶
Package publish runs the publication state machine: gates, envelope, confirmation, one review request, receipt.
Index ¶
- Constants
- Variables
- func ActionRefusal(action, viewer, author string, d *draft.Draft) error
- func ClosedSince(target run.Target, pr github.PullRequest) string
- func DeleteAttempt(dir string) error
- func Earlier(filed []EnvelopeFinding, assessments []draft.Assessment, filedIn draft.FiledIn) []draft.EarlierFinding
- func EditedNotice(rounds []int) string
- func EncodeComments(c Comments) ([]byte, error)
- func EncodePrevious(p Previous) ([]byte, error)
- func Match(reviews []github.Review, attempt Attempt) (github.Review, bool)
- func ReadinessRefusal(d *draft.Draft) error
- func RecaptureCommand(target run.Target) string
- func SaveAttempt(dir string, a *Attempt) error
- func SaveReceipt(dir string, r *Receipt) error
- func StartAction(verdict, viewer, author string, d *draft.Draft) (action, why string)
- func Unavailable(action, viewer, author string, d *draft.Draft) string
- type Attempt
- type BuildInput
- type Comment
- type Comments
- type Confirmation
- type Confirmed
- type Envelope
- type EnvelopeFinding
- type EnvelopeTarget
- type FeedbackComment
- type FeedbackReview
- type FeedbackThread
- type GateInput
- type Gated
- type HeadMoved
- type MovedCommit
- type Options
- type Preview
- type Previous
- type Receipt
- func LoadReceipt(dir string) (Receipt, bool, error)
- func PreviousReceipt(root string, ref run.Ref, viewer, source string) (int, Receipt, error)
- func Reconcile(ctx context.Context, gh github.Client, attempt Attempt) (*Receipt, error)
- func Run(ctx context.Context, opts Options) (receipt Receipt, replayed bool, err error)
- type StickyBuild
Constants ¶
const ( StateInFlight = "in-flight" StateUnknown = "unknown" )
const CommentsSchema = 1
Bump on any field change, so an older loupe refuses the file instead of dropping fields (docs/versioning.md).
const PreviousSchema = 3
Bump on any field change, so an older loupe refuses the file instead of dropping fields (docs/versioning.md).
const RecordSchema = 2
Bump on any field change, so an older loupe refuses the file instead of dropping fields (docs/versioning.md).
Variables ¶
var ( Actions = []string{"comment", "approve", "request-changes"} InlineModes = []string{"none", "blocking", "all"} )
var ErrDeclined = errors.New("publish declined; nothing was sent")
ErrDeclined means the human did not confirm; nothing was sent or written.
Functions ¶
func ActionRefusal ¶
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 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 ¶
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 EncodePrevious ¶
func Match ¶
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 RecaptureCommand ¶
RecaptureCommand captures the run's pull request again with the same source, which reads the previous round afresh.
func SaveAttempt ¶
func SaveReceipt ¶
func StartAction ¶ added in v0.26.0
StartAction is where the publish step's cursor starts: the agent's verdict, unless the human's decisions contradict it or the action is refused. why says in a few words what moved the start off the verdict, and is empty when nothing did.
func Unavailable ¶ added in v0.26.0
Unavailable says in a few words why the publish step refuses action, or "" when it does not. ActionRefusal holds the whole refusal and its fix, which the step shows when the human picks the action anyway. It refuses only an own pull request and an approve over blocking findings.
Types ¶
type Attempt ¶
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 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 ¶
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 ¶
Unattended reports whether the envelope was composed by --unattended, which never records a viewer; an attended publication always does.
type EnvelopeFinding ¶
type EnvelopeTarget ¶
type FeedbackComment ¶
type FeedbackReview ¶
type FeedbackThread ¶
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 ¶
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 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 ¶
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 ¶
LoadReceipt reports found false only when receipt.json does not exist; a damaged file is a record refusal.
func PreviousReceipt ¶
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 ¶
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 ¶
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.