delivery

package
v0.149.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 14 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")
)

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

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. 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 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 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).

Jump to

Keyboard shortcuts

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