delivery

package
v0.201.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 16 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 delivery hands a worktree job's branch over (GIT-02, design 2026-10-12-delivery-and-cost-estimate-design.md): merge it into the base branch, push it to a remote, or push it and open a pull / merge request on GitHub, GitLab or Gitea / Forgejo. It runs on the machine that owns the worktree — the server for a local job, the worker (through an internal exec job) otherwise — from a self-contained Plan, and never force-pushes.

Index

Constants

View Source
const (
	ModeNone  = "none"
	ModeMerge = "merge"
	ModePush  = "push"
	ModePR    = "pr"
)

Modes.

View Source
const (
	StatePending  = "pending"
	StateMerged   = "merged"
	StatePushed   = "pushed"
	StatePROpened = "pr_opened"
	StateFailed   = "failed"
	StateSkipped  = "skipped"
)

States of a delivery.

View Source
const (
	ProviderAuto   = "auto"
	ProviderGitHub = "github"
	ProviderGitLab = "gitlab"
	ProviderGitea  = "gitea" // Gitea and Forgejo share the API
)

Providers.

Variables

View Source
var (
	ErrMergeConflict = errors.New("worktree merge has conflicts")
	ErrMainNotReady  = errors.New("main checkout is not ready for a merge")
	// ErrIndexLocked: the main checkout's .git/index.lock is still held once the wait
	// elapsed. The lock is NEVER deleted — another git process owns it.
	ErrIndexLocked = errors.New("git index.lock is still held")
)

Merge errors (typed so callers map them to 409).

View Source
var StepPollFloor = time.Second

StepPollFloor is the shortest interval between two platform polls. A package variable so tests can shorten it; the step's own interval comes from its `with` block.

Functions

func BaseBranch

func BaseBranch(ctx context.Context, wtPath string) (main, branch string, err error)

BaseBranch is the branch the main checkout of wtPath's repository is on — where a merge lands and the default target of a pull request.

func CompareURL

func CompareURL(provider string, r Remote, head, base string) string

CompareURL is the platform's web page for opening a request by hand ("" unknown).

func DetectProvider

func DetectProvider(host, override string) string

DetectProvider names the platform of host: github.com and gitlab.com by name, a self-hosted instance when its host says so (gitlab.*, gitea.*, forgejo.*, codeberg.org); "" when it cannot tell (set provider explicitly).

func GitOut

func GitOut(ctx context.Context, dir string, args ...string) (string, error)

GitOut runs git in dir and returns its trimmed stdout; a failure carries git's stderr.

func MainCheckout

func MainCheckout(ctx context.Context, wtPath string) (string, error)

MainCheckout returns the main working tree of the repository a linked worktree belongs to.

func MergeBranch

func MergeBranch(ctx context.Context, wtPath, branch string, squash bool) (main, base string, err error)

MergeBranch merges branch (checked out in the worktree wtPath) into its repository's main checkout: --no-ff with a merge commit, or --squash plus one commit. The main checkout must have no tracked changes and be on a named branch; a conflict is fully undone before the typed error is returned, and uncommitted .gofer/tracker/ changes survive the undo. It returns the main checkout and the branch merged into.

func Push

func Push(ctx context.Context, dir, remote, src, dst, proxy string) error

Push pushes local ref src to refs/heads/dst on remote — never forced: a remote branch that moved elsewhere is an error naming the rejection. A non-empty proxy overrides git's http.proxy for this push only.

func RemoteURL added in v0.181.0

func RemoteURL(ctx context.Context, dir, remote string) (string, error)

RemoteURL is the URL a remote points at (`git remote get-url`), for parsing the platform and repository out of it. An empty remote means origin.

func ResolveToken

func ResolveToken(ctx context.Context, provider, override string, env Env) string

ResolveToken reads the platform token on this machine: the configured / default variables, else the logged-in CLI (`gh auth token`, `glab auth token`).

func TokenVars

func TokenVars(provider, override string) []string

TokenVars are the environment variables a provider's token is read from.

func ValidMode

func ValidMode(m string) bool

ValidMode reports whether m is a delivery mode ("" = unset).

Types

type APIError

type APIError struct {
	Status int
	Body   string
}

APIError is a non-2xx answer from the platform (Body trimmed).

func (*APIError) Error

func (e *APIError) Error() string

type Client

type Client struct {
	Provider string
	Remote   Remote
	Token    string
	HTTP     *http.Client
	// APIBase overrides the derived API root (tests, unusual installs).
	APIBase string
}

Client opens requests on one platform.

func (*Client) Open

func (c *Client) Open(ctx context.Context, req PRRequest) (PR, error)

Open opens the request, or returns the one already open for the same head (a retried delivery must not open a second one).

type Env

type Env struct {
	Lookup func(string) (string, bool)
	// RunTokenCLI runs a token helper (`gh auth token`); nil = exec it.
	RunTokenCLI func(ctx context.Context, name string, args ...string) (string, error)
	HTTPBase    string // API base override (tests)
}

Env lets the caller supply the environment and the CLI token helpers (tests).

type MergeConflictError

type MergeConflictError struct {
	Files []string
}

MergeConflictError reports a conflict after the main checkout has been restored.

func (*MergeConflictError) Error

func (e *MergeConflictError) Error() string

func (*MergeConflictError) Unwrap

func (e *MergeConflictError) Unwrap() error

type PR

type PR struct {
	URL     string
	Number  int
	Existed bool // an open request for the same head was found and reused
}

PR is an opened (or already open) request.

type PRRequest

type PRRequest struct {
	Title  string
	Body   string
	Head   string // the pushed branch
	Base   string // target branch
	Draft  bool
	Labels []string
}

PRRequest is what a pull / merge request is opened with.

type Plan

type Plan struct {
	Mode         string   `json:"mode"`
	JobID        string   `json:"job_id"`
	Worktree     string   `json:"worktree"`      // the job's worktree (absolute, on the executing machine)
	Branch       string   `json:"branch"`        // the local branch (gofer/<job>)
	RemoteBranch string   `json:"remote_branch"` // pushed as
	Remote       string   `json:"remote"`
	Base         string   `json:"base,omitempty"` // "" = the main checkout's branch
	AllowPush    bool     `json:"allow_push"`
	PushBase     bool     `json:"push_base,omitempty"`
	Squash       bool     `json:"squash,omitempty"`
	Cleanup      bool     `json:"cleanup,omitempty"`
	Draft        bool     `json:"draft,omitempty"`
	Labels       []string `json:"labels,omitempty"`
	Provider     string   `json:"provider,omitempty"`  // auto | github | gitlab | gitea
	TokenEnv     string   `json:"token_env,omitempty"` // variable NAME; "" = the provider's defaults
	Proxy        string   `json:"proxy,omitempty"`     // push + API proxy; "" = git's http.proxy, then the environment
	Title        string   `json:"title"`
	Body         string   `json:"body"`
}

Plan is one delivery, self-contained so it can be carried to the machine that owns the worktree (an internal exec job reads it as JSON on stdin).

type RefResult added in v0.181.0

type RefResult struct {
	Ref    string `json:"ref"`
	Kind   string `json:"kind"` // branch | tag
	Remote string `json:"remote"`
	SHA    string `json:"sha"`
}

RefResult is one ref pushed and verified against the remote.

func PushRefs added in v0.181.0

func PushRefs(ctx context.Context, dir, remote string, refs []string, proxy string) ([]RefResult, error)

PushRefs pushes every ref to remote and verifies the remote now holds the same sha. A name that resolves to a local branch is pushed to refs/heads/<name>, a local tag to refs/tags/<name>. Nothing is ever forced: a remote ref that moved elsewhere makes git reject the push and the rejection is returned as-is. After the push every ref is read back with `git ls-remote`, so a "successful" push that did not land (a hook, a mirror) is a failure here rather than a silently wrong release.

type Remote

type Remote struct {
	Host string // host[:port] of the web / API endpoint
	Path string // owner/repo (GitLab: group/sub/repo), without .git
}

Remote is a parsed git remote URL.

func ParseRemote

func ParseRemote(raw string) (Remote, error)

ParseRemote reads https://host/owner/repo(.git), ssh://git@host[:port]/owner/repo and scp-style git@host:owner/repo. An ssh port is dropped (the API is on https).

type Result

type Result struct {
	Mode       string `json:"mode"`
	State      string `json:"state"`
	Branch     string `json:"branch,omitempty"`
	Remote     string `json:"remote,omitempty"`
	Base       string `json:"base,omitempty"`
	Provider   string `json:"provider,omitempty"`
	PRURL      string `json:"pr_url,omitempty"`
	PRNumber   int    `json:"pr_number,omitempty"`
	CompareURL string `json:"compare_url,omitempty"`
	Note       string `json:"note,omitempty"`
	Error      string `json:"error,omitempty"`
	At         int64  `json:"at"`
}

Result is what a delivery did.

func Execute

func Execute(ctx context.Context, p Plan, env Env) Result

Execute performs plan. It never force-pushes; any failure is a Result with State failed and the reason (the error return is for programming errors only).

type WaitAssetsOptions added in v0.181.0

type WaitAssetsOptions struct {
	Tag         string
	Assets      []string // globs of asset file names
	TimeoutMin  int
	IntervalSec int
	Remote      string
	Env         Env
}

WaitAssetsOptions is what `gofer/wait-assets` asks for.

type WaitAssetsResult added in v0.181.0

type WaitAssetsResult struct {
	Provider string   `json:"provider"`
	Tag      string   `json:"tag"`
	URL      string   `json:"url,omitempty"`
	Assets   []string `json:"assets"`
}

WaitAssetsResult is what the step reports.

func WaitAssets added in v0.181.0

func WaitAssets(ctx context.Context, dir string, o WaitAssetsOptions) (WaitAssetsResult, error)

WaitAssets polls the release of tag until every glob matches an uploaded asset.

type WaitCIOptions added in v0.181.0

type WaitCIOptions struct {
	Ref          string   // branch, tag or sha whose checks to wait for
	Required     []string // check / workflow run names that MUST pass ("" = every check must pass)
	CodeScanning bool     // also fail on code scanning alerts reported for this ref
	TimeoutMin   int      // default 40
	IntervalSec  int      // default 20, never below StepPollFloor
	Remote       string   // "" = origin
	Env          Env      // HTTPBase / Lookup overrides (tests)
}

WaitCIOptions is what `gofer/wait-ci` asks for.

type WaitCIResult added in v0.181.0

type WaitCIResult struct {
	Provider string   `json:"provider"`
	Ref      string   `json:"ref"`
	Checks   []string `json:"checks,omitempty"`
	Alerts   int      `json:"code_scanning_alerts"`
}

WaitCIResult is what the step reports.

func WaitCI added in v0.181.0

func WaitCI(ctx context.Context, dir string, o WaitCIOptions) (WaitCIResult, error)

WaitCI polls the platform until every check of ref has finished. It fails as soon as a required check fails, and it fails with the checks still running when the timeout elapses, so the workflow log names what was waited for.

Jump to

Keyboard shortcuts

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