Documentation
¶
Overview ¶
Package officership is what lets an officer SHIP: push branches, merge its own pull requests and tag releases in one repository, with every write decided in muster-server (docs/officers.md "Shipping officers").
- pushgate.go: the connector-proxy write filter (a connectors.Gate) an instance's every GitHub request passes through;
- gates.go: officer_merge_pr's and officer_tag_release's gates, all read fresh from GitHub and HQ at call time;
- lint.go: the destructive-DDL lint on migration files;
- rollout.go: the post-release watch and the automatic rollback.
🔴 THE REPOSITORY ENFORCES NOTHING, SO THIS PACKAGE IS THE BRANCH PROTECTION. A private repository on a plan without branch protection or rulesets lets any `contents: write` token push to its base branch, push any tag and merge with red checks. The officer's write token therefore never leaves muster: the instance's requests reach GitHub only through the push gate, and merges and tags are made here, after the gates, with muster's own token.
Index ¶
- Constants
- Variables
- func BotLogin(slug string) string
- func IsRollbackTag(message string) bool
- func SensitiveFiles(patterns []string, files []File) []string
- type CheckRun
- type Comment
- type Compare
- type File
- type Finding
- type Gates
- type GitHub
- type HQ
- type MergeOutcome
- type PR
- type PushGate
- func (g *PushGate) Admit(r *http.Request, host, rest string) (*http.Request, error)
- func (g *PushGate) Connector() string
- func (g *PushGate) Credential(ctx context.Context, caller connectors.Caller, host, rest string) (string, bool, error)
- func (g *PushGate) Describe() connectors.Connector
- func (g *PushGate) Rejected(caller connectors.Caller)
- type REST
- func (g *REST) AppSlug(ctx context.Context) (string, error)
- func (g *REST) BranchSHA(ctx context.Context, branch string) (string, error)
- func (g *REST) CheckRuns(ctx context.Context, sha, name string) ([]CheckRun, error)
- func (g *REST) Commit(ctx context.Context, sha string) (string, []string, error)
- func (g *REST) Compare(ctx context.Context, base, head string) (Compare, error)
- func (g *REST) CreateCommit(ctx context.Context, message, tree string, parents []string) (string, error)
- func (g *REST) CreateRef(ctx context.Context, ref, sha string) error
- func (g *REST) CreateTag(ctx context.Context, name, message, sha string, t Tagger) (string, error)
- func (g *REST) FileAt(ctx context.Context, path, ref string) ([]byte, error)
- func (g *REST) Merge(ctx context.Context, n int, sha string) (string, error)
- func (g *REST) MergeInto(ctx context.Context, base, head, message string) (string, error)
- func (g *REST) OpenPR(ctx context.Context, title, head, base, body string) (int, error)
- func (g *REST) PR(ctx context.Context, n int) (PR, error)
- func (g *REST) PRComments(ctx context.Context, n int) ([]Comment, error)
- func (g *REST) PRFiles(ctx context.Context, n int) ([]File, error)
- func (g *REST) PRsForCommit(ctx context.Context, sha string) ([]PR, error)
- func (g *REST) Statuses(ctx context.Context, sha string) (map[string]string, error)
- func (g *REST) TagObject(ctx context.Context, sha string) (TagObject, error)
- func (g *REST) Tags(ctx context.Context) ([]TagRef, error)
- type Refusal
- type TagObject
- type TagOptions
- type TagOutcome
- type TagRef
- type Tagger
- type Version
- type WatchStatus
- type Watcher
- func (w *Watcher) InFlight() (string, bool)
- func (w *Watcher) LiveVersion(ctx context.Context) (string, error)
- func (w *Watcher) Resume(ctx context.Context)
- func (w *Watcher) Start(ctx context.Context, rel TagOutcome, itemID string) error
- func (w *Watcher) Status() (cur *WatchStatus, history []WatchStatus)
- func (w *Watcher) Wait()
Constants ¶
const ( GateStage = "stage" GateHQEnabled = "hq_enabled" GatePR = "pull_request" GateBase = "base" GateHeadBranch = "head_branch" GateAuthor = "author" GateMergeable = "mergeable" GateUpToDate = "up_to_date" GateChecks = "checks" GateReview = "review" GateHold = "hold" GateMigrations = "migrations" GateNotice = "notice" GateMerge = "merge" GateTags = "tags" GateAncestor = "ancestor" GateInFlight = "in_flight" GateRate = "rate" GateQuietHours = "quiet_hours" GateMainCI = "main_ci" GateBackup = "backup" GateTagCreate = "tag_create" // GateUntestedMerge refuses a release while the base's head is an // officer merge that raced the base (MergeOutcome.UntestedTree). GateUntestedMerge = "untested_merge" )
Gate names, as a refusal and the call log name them.
const ( VerdictWatching = "watching" VerdictHealthy = "healthy" VerdictNotLive = "not_live" VerdictUnhealthy = "unhealthy" VerdictSuperseded = "superseded" VerdictAborted = "aborted" )
Watch verdicts.
Variables ¶
var ErrMergeConflict = errors.New("officership: merge conflict")
ErrMergeConflict is a merge GitHub could not make cleanly (409).
var ErrRefExists = errors.New("officership: the ref already exists")
ErrRefExists is a create of a ref that already exists (GitHub's 422): the compare-and-swap against a concurrent tag or branch.
Functions ¶
func IsRollbackTag ¶
IsRollbackTag reports whether an annotated tag's message carries the rollback trailer (a whole line, exactly).
func SensitiveFiles ¶
SensitiveFiles are the changed paths (a rename's old path included) that match any pattern, sorted and unique.
Types ¶
type Comment ¶
Comment is any human-or-bot text on a pull request: a conversation comment, a review body or a review comment.
type Compare ¶
type Compare struct {
Status string // ahead | behind | diverged | identical
AheadBy int
BehindBy int
Files []File
Commits []string // shas, oldest first
}
Compare is GET compare/{base}...{head}.
type File ¶
type File struct {
Path string
Previous string // a rename's old path
Status string // added | modified | removed | renamed | copied | changed | unchanged
}
File is one changed file of a pull request or a comparison.
type Finding ¶
Finding is one destructive statement in a migration file.
func LintMigration ¶
LintMigration returns every destructive statement in one migration file. An empty result is a pass.
type Gates ¶
type Gates struct {
OfficerID string
Repo string
Ship *officers.Ship
GH GitHub
HQ HQ
Now func() time.Time
// InFlight reports a release the rollout watch still owns (its tag).
InFlight func() (string, bool)
// LiveVersion reads the version the service reports now (the rollout's
// health URL).
LiveVersion func(ctx context.Context) (string, error)
// contains filtered or unexported fields
}
Gates are officer_merge_pr's and officer_tag_release's checks for one officer. Every input is read from GitHub and HQ at call time; nothing is cached between calls.
func (*Gates) MergePR ¶
MergePR squash-merges the officer's own pull request n if, and only if, every merge gate passes now. A refusal is a *Refusal naming the gate; on a refusal nothing was written (a sensitive-path notice may have been posted only when every OTHER gate had passed).
The gates, in order:
- stage — the officer's stage allows merging;
- hq_enabled — HQ answers and says the officer is enabled (fail closed);
- base / head_branch / author — open, not draft, into the base branch, from <prefix>… in the same repository, authored by the officer App's bot;
- mergeable — GitHub says mergeable (unknown is "call again");
- up_to_date — the head contains the base's head (behind_by 0);
- checks — every required check is success on the head commit (a commit status, or a completed check run);
- review — the newest review check run on the head is completed+success (neutral fails; from review_app when set);
- hold — no human wrote /hold on the pull request;
- migrations — no destructive DDL in an added or changed migration, and no migration removed or renamed;
- notice — if a sensitive path changed, HQ accepted the notice;
- merge — GitHub squash-merges with the head pinned to the checked sha.
func (*Gates) TagRelease ¶
func (g *Gates) TagRelease(ctx context.Context, opt TagOptions) (TagOutcome, error)
TagRelease creates the next minor release tag (annotated) on the base branch's head if, and only if, every release gate passes now:
- stage — the officer's stage allows releasing;
- hq_enabled — HQ says the officer is enabled (fail closed);
- tags / ancestor — the highest vX.Y.Z tag T is an ancestor of the base's head H, and H is ahead of it (else nothing to release);
- in_flight — no release the rollout watch still owns, and the service reports T's version (the previous release is live);
- rate — fewer than releases_per_day officer tags in the last 24 h, and none in the last min_release_gap;
- quiet_hours — not inside them;
- main_ci — the required checks are success for H's exact tree;
- migrations / backup — no destructive DDL in a migration added in T..H, and, when one was added, HQ's backup preflight passes;
- tag_create — GitHub creates refs/tags/<next>; a tag pushed by someone else first makes this fail, and the tool reports it.
type GitHub ¶
type GitHub interface {
AppSlug(ctx context.Context) (string, error)
PR(ctx context.Context, n int) (PR, error)
PRFiles(ctx context.Context, n int) ([]File, error)
Compare(ctx context.Context, base, head string) (Compare, error)
// Statuses is the latest commit status per context on sha.
Statuses(ctx context.Context, sha string) (map[string]string, error)
CheckRuns(ctx context.Context, sha, name string) ([]CheckRun, error)
PRComments(ctx context.Context, n int) ([]Comment, error)
FileAt(ctx context.Context, path, ref string) ([]byte, error)
BranchSHA(ctx context.Context, branch string) (string, error)
Tags(ctx context.Context) ([]TagRef, error)
TagObject(ctx context.Context, sha string) (TagObject, error)
// Commit is a commit's tree and parents.
Commit(ctx context.Context, sha string) (tree string, parents []string, err error)
PRsForCommit(ctx context.Context, sha string) ([]PR, error)
Merge(ctx context.Context, n int, sha string) (string, error)
CreateTag(ctx context.Context, name, message, sha string, t Tagger) (string, error)
CreateRef(ctx context.Context, ref, sha string) error
CreateCommit(ctx context.Context, message, tree string, parents []string) (string, error)
MergeInto(ctx context.Context, base, head, message string) (string, error)
OpenPR(ctx context.Context, title, head, base, body string) (int, error)
}
GitHub is every GitHub read and write the gates and the watcher make, for ONE repository. The REST implementation is REST; the tests use a fake.
type HQ ¶
type HQ interface {
// Enabled is HQ's on/off switch for the officer (GET /officers/{id}).
Enabled(ctx context.Context) (bool, error)
// PostNotice posts the sensitive-path notice (POST to path under hq_url).
PostNotice(ctx context.Context, path string, n officers.Notice) error
// Preflight is HQ's database backup health (POST /officers/{id}/preflight).
Preflight(ctx context.Context) (officers.Preflight, error)
}
HQ is what the gates ask HQ, with the officer's own token.
type MergeOutcome ¶
type MergeOutcome struct {
PR int `json:"pr"`
HeadSHA string `json:"head_sha"`
MergeSHA string `json:"merge_sha"`
Passed []string `json:"gates_passed"`
Noticed []string `json:"sensitive_paths_noticed,omitempty"`
// UntestedTree: the merge landed on a base that moved after the gates
// read it, so the base now holds a tree no CI run saw (Warning says so).
UntestedTree bool `json:"untested_tree,omitempty"`
Warning string `json:"warning,omitempty"`
}
MergeOutcome is what officer_merge_pr did.
type PR ¶
type PR struct {
Number int
State string
Draft bool
Merged bool
Mergeable *bool
Author string
AuthorType string
HeadRef string
HeadSHA string
HeadRepo string
BaseRef string
BaseRepo string
Title string
MergeCommitSHA string
}
PR is the part of a pull request the gates read.
type PushGate ¶
type PushGate struct {
// Repo is the officer's repository (owner/name).
Repo string
// Ship is the officer's policy (Base, BranchPrefix).
Ship *officers.Ship
// GitHost and APIHost are the hosts served (github.com, api.github.com).
GitHost, APIHost string
// Cred mints the credential for an admitted request (the officer App's
// connectors.Virtual Credential: its token for Repo, anonymous elsewhere).
Cred func(ctx context.Context, caller connectors.Caller, host, rest string) (string, bool, error)
// OnRejected drops a cached credential after a 401.
OnRejected func(caller connectors.Caller)
// GH reads a pull request (PATCH's own-PR check) and the App's slug.
GH GitHub
}
PushGate is a shipping officer's connectors.Gate: the write filter every one of its instance's GitHub requests passes through in muster's connector proxy, and the source of the (write-capable) credential an admitted request carries.
🔴 IT IS AN ALLOWLIST. A read (GET, HEAD) is forwarded. A write is forwarded only when it is one of these, in the officer's own repository:
- git push (POST …/git-receive-pack) whose EVERY ref update targets refs/heads/<prefix>… — so never the base branch, never a tag;
- git fetch (POST …/git-upload-pack), which writes nothing;
- POST /repos/{repo}/pulls with head <prefix>… and base = the base branch;
- PATCH /repos/{repo}/pulls/{n} of the officer's OWN pull request (its App's bot authored it, its head is <prefix>…), title/body/state only;
- POST /repos/{repo}/issues/{n}/comments on its OWN pull request.
Every other method, path and host shape is refused — the merge endpoint, the git/refs and git/tags endpoints, releases, the contents API (which commits to any branch), the merges API, GraphQL (which can do all of those), and every DELETE. Merges and tags are made by officer_merge_pr and officer_tag_release in muster-server, after their gates.
func (*PushGate) Credential ¶
func (g *PushGate) Credential(ctx context.Context, caller connectors.Caller, host, rest string) (string, bool, error)
Credential implements connectors.Gate.
func (*PushGate) Describe ¶
func (g *PushGate) Describe() connectors.Connector
Describe implements connectors.Gate: the GitHub preset's shape.
func (*PushGate) Rejected ¶
func (g *PushGate) Rejected(caller connectors.Caller)
Rejected implements connectors.Gate.
type REST ¶
type REST struct {
Base string // API base, no trailing slash
Repo string // owner/name
Token func(ctx context.Context) (string, error)
// Slug reads the App's slug (GET /app, as the App).
Slug func(ctx context.Context) (string, error)
HTTP *http.Client
}
REST is GitHub over GitHub's REST API for Repo, authenticated by Token (the officer App's installation token, minted per call by muster).
func (*REST) Compare ¶
Compare implements GitHub. 🔴 GitHub lists at most 300 files and 250 commits in one comparison; a comparison at either cap is refused rather than read as complete.
func (*REST) CreateCommit ¶
func (g *REST) CreateCommit(ctx context.Context, message, tree string, parents []string) (string, error)
CreateCommit implements GitHub.
func (*REST) Merge ¶
Merge implements GitHub: a squash merge that GitHub refuses (409) unless the head is still sha — the match-head-commit guard.
func (*REST) MergeInto ¶
MergeInto implements GitHub (POST merges: head merged into branch base). A conflict is ErrMergeConflict.
func (*REST) PRComments ¶
PRComments implements GitHub: conversation comments, review bodies and review comments, every page.
func (*REST) PRsForCommit ¶
PRsForCommit implements GitHub.
type Refusal ¶
Refusal is a gate that did not pass. Nothing was written.
type TagObject ¶
type TagObject struct {
SHA string
Tagger string
TaggedAt time.Time
TargetSHA string
TargetType string
Message string
}
TagObject is an annotated tag.
type TagOptions ¶
type TagOptions struct {
// Rollback is the rollout watch's own release of a revert: it skips the
// in-flight, rate, quiet-hours and HQ-enabled gates (the release being
// rolled back is the one in flight, and HQ may be what is down) and keeps
// every other.
Rollback bool
}
TagOptions are officer_tag_release's inputs.
type TagOutcome ¶
type TagOutcome struct {
Tag string `json:"tag"`
SHA string `json:"sha"`
Previous string `json:"previous"`
PreviousSHA string `json:"previous_sha"`
TaggedAt time.Time `json:"tagged_at"`
Passed []string `json:"gates_passed"`
Migrations []string `json:"migrations,omitempty"`
}
TagOutcome is what officer_tag_release did.
type TagRef ¶
type TagRef struct {
Name string
ObjectSHA string
ObjectType string // commit (lightweight) | tag (annotated)
}
TagRef is one refs/tags/* ref: its name and the object it points at.
type Version ¶
type Version struct{ Major, Minor, Patch int }
Version is a vMAJOR.MINOR.PATCH tag.
func ParseVersion ¶
ParseVersion reads a release tag; ok is false for anything else.
type WatchStatus ¶
type WatchStatus struct {
Tag string `json:"tag"`
SHA string `json:"sha"`
Previous string `json:"previous"`
ItemID string `json:"item_id,omitempty"`
TaggedAt time.Time `json:"tagged_at"`
Phase string `json:"phase"`
Verdict string `json:"verdict"`
LiveAt time.Time `json:"live_at,omitempty"`
RevertPR int `json:"revert_pr,omitempty"`
RollbackTag string `json:"rollback_tag,omitempty"`
Escalated bool `json:"escalated"`
// Rollback is true for the watch of a ROLLBACK release: it is never itself
// rolled back.
Rollback bool `json:"rollback"`
Events []string `json:"events"`
Done bool `json:"done"`
}
WatchStatus is one release's watch, as officer_rollout_status reports it.
type Watcher ¶
type Watcher struct {
OfficerID string
Repo string
Ship *officers.Ship
GH GitHub
Gates *Gates
HTTP *http.Client
Now func() time.Time
// Sleep waits d or until ctx ends (tests replace it).
Sleep func(ctx context.Context, d time.Duration) error
Logf func(format string, args ...any)
// contains filtered or unexported fields
}
Watcher watches one officer's releases, one at a time.
func (*Watcher) LiveVersion ¶
LiveVersion is the version the service reports now (Gates.LiveVersion).
func (*Watcher) Resume ¶
Resume restarts the watch of the newest release after a muster restart, when that release is the officer's own and still inside its watch window. A restart therefore costs at most the probes it missed, not the watch.
func (*Watcher) Start ¶
Start begins watching a release in the background (ctx ends it). A watch already in flight is refused: officer_tag_release's in-flight gate makes that unreachable except by a rollback, which watches inline.
func (*Watcher) Status ¶
func (w *Watcher) Status() (cur *WatchStatus, history []WatchStatus)
Status is the current (or last) watch and the earlier ones of this process.