forge

package
v0.14.0-rc.9 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 8 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 forge is the Git host the module writes to. It lands rendered files with the caller's identity: a branch, one commit per repository, the pull request carrying the caller's text, and the merge once the caller says approved and the checks are green. It holds what every host shares; each host's Remote lives in its own package (forge/github), and forge/forgetest is the in-process Remote for tests.

A Remote uses the Git host credential supplied by the caller. The package does not obtain credentials or manage sign-in. Authentication and permission refusals surface as ErrAuth carrying the HTTP status; the caller decides how to respond. Nothing is retried with another identity, nothing is logged, and no error carries file content.

Reading one revision

ReadFile and ListFiles take a branch name or a commit SHA. With a branch name each call reads the branch's head when it runs, so a list and the reads after it can see two revisions when a commit lands between them: a file the commit moved reads as ErrFileNotFound, and its new path is not in the list. To read one consistent revision, resolve the head once and pass the SHA to every call. gh is a value that implements HeadReader, Lister and Reader, such as *github.Remote or *forgetest.Remote (Remote has none of the three):

sha, err := gh.Head(ctx, repo, "main")
paths, err := gh.ListFiles(ctx, repo, sha, dir)
for _, p := range paths {
	content, err := gh.ReadFile(ctx, repo, sha, p)
}

Index

Constants

View Source
const (
	ReviewNone             ReviewState = "none"
	ReviewApproved         ReviewState = "approved"
	ReviewChangesRequested ReviewState = "changes_requested"

	// ChecksNone: the head carries no statuses and no check runs; nothing to wait for.
	ChecksNone    CheckState = "none"
	ChecksPending CheckState = "pending"
	ChecksSuccess CheckState = "success"
	ChecksFailure CheckState = "failure"
)
View Source
const (
	OpCreateBranch    = "create branch"
	OpCommit          = "commit"
	OpOpenPullRequest = "open pull request"
	OpStatus          = "pull request status"
	OpMerge           = "merge"
	OpApprove         = "approve pull request"
	OpFindPullRequest = "find pull request"
	OpFindByPrefix    = "find pull requests by prefix"
	OpEnableAutoMerge = "enable auto-merge"
	OpClose           = "close pull request"
	OpRevert          = "revert pull request"
	OpReadFile        = "read file"
	OpListFiles       = "list files"
	OpHead            = "read branch head"
	OpMergedFiles     = "list merged files"
	OpReadRepository  = "read repository"
)

Operation names of the remote calls, carried by AuthError and by forgetest.Remote.Fail.

Variables

View Source
var (
	// ErrAuth: the remote refused authentication or permission (401 or 403).
	// The module neither retries nor substitutes an identity.
	ErrAuth = errors.New("token refused")
	// ErrInvalidRequest: the request lacks a branch or a title.
	ErrInvalidRequest = errors.New("request needs a branch and a title")
	// ErrNoFiles: a change carries no files.
	ErrNoFiles = errors.New("no files to commit")
	// ErrConflictingBase: two changes for one repository name different branches.
	ErrConflictingBase = errors.New("one repository, two base branches")
	// ErrDuplicatePath: two changes for one repository write the same path.
	ErrDuplicatePath = errors.New("one path in two changes")
	// ErrStaleBranch: the existing branch is behind its base and base does not
	// merge into it; nothing is committed onto a head that lacks the base.
	ErrStaleBranch = errors.New("branch behind its base and the base does not merge into it")
	// ErrNotApproved: the caller has not said approved.
	ErrNotApproved = errors.New("merge refused: not approved")
	// ErrChecksPending: a status or check run on the head has not finished.
	ErrChecksPending = errors.New("merge refused: checks pending")
	// ErrChecksFailed: a status or check run on the head failed.
	ErrChecksFailed = errors.New("merge refused: checks failed")
	// ErrHeadMoved: the head is no longer the commit the caller saw.
	ErrHeadMoved = errors.New("merge refused: the head moved since the pull request was seen")
	// ErrNotMerged: the pull request is not merged; only a merged pull request
	// can be reverted or have its merged files listed.
	ErrNotMerged = errors.New("the pull request is not merged")
	// ErrNothingToRevert: the merged pull request changed no files.
	ErrNothingToRevert = errors.New("revert refused: the pull request changed no files")
	// ErrFileNotFound: the branch or commit carries no file at the path, or
	// the commit SHA a read or a list names is not in the repository.
	ErrFileNotFound = errors.New("no such file")
)
View Source
var ErrPullRequestOpen = errors.New("the object already has an open pull request")

ErrPullRequestOpen indicates that an object already has an open pull request.

Functions

func Merge

func Merge(ctx context.Context, remote Remote, pr PullRequest, approved bool) error

Merge merges the pull request with the caller's identity. It is refused before the caller says approved (ErrNotApproved, no remote call), before every check on the head is green (ErrChecksPending, ErrChecksFailed; a head without checks has nothing to wait for) and when the head is not the commit the caller saw (ErrHeadMoved). A merged pull request is left as it is. GitHub's own rules — the approving review that satisfies enforce_admins and rulesets — apply at the merge call; no administrative path exists.

func Removes

func Removes(changes []FileChange, path string) bool

Removes reports whether changes take path away: a removal of path, or a rename from it.

Types

type AuthError

type AuthError struct {
	Op     string
	Status int
}

AuthError is ErrAuth with the status the remote answered and the operation it refused. errors.Is(err, ErrAuth) matches it.

func (*AuthError) Error

func (e *AuthError) Error() string

func (*AuthError) Unwrap

func (e *AuthError) Unwrap() error

Unwrap makes errors.Is(err, ErrAuth) true.

func (*AuthError) Write

func (e *AuthError) Write() bool

Write reports whether the operation is a recognized repository write. Unknown operations return false, which does not establish read access.

type Change

type Change struct {
	Location provenance.Location
	Files    map[string][]byte
}

Change is the set of files for one location: the repository, the base branch and the repository-relative paths the files land at (the keys of what sopsenc.Encrypt returns). Changes for one repository become one commit. A path whose content is nil is removed from the repository.

type ChangeKind

type ChangeKind string

ChangeKind is what a merged pull request did to one file.

const (
	FileAdded    ChangeKind = "added"
	FileModified ChangeKind = "modified"
	FileRemoved  ChangeKind = "removed"
	// FileRenamed: the file moved from FileChange.PreviousPath to
	// FileChange.Path; on GitHub its content may have changed too. The
	// previous path is gone from the base after the merge although no
	// FileRemoved names it: Removes counts it.
	FileRenamed ChangeKind = "renamed"
)

type CheckState

type CheckState string

CheckState is the rollup of the commit statuses and check runs on the head.

type FileChange

type FileChange struct {
	Path         string
	Kind         ChangeKind
	PreviousPath string
}

FileChange is one file a merged pull request changed: its repository-relative path, the kind of change and, for FileRenamed only, the path it had before.

type HeadReader

type HeadReader interface {
	// Head returns the SHA of the commit at the head of branch.
	Head(ctx context.Context, repo Repository, branch string) (string, error)
}

HeadReader resolves a branch's head to its commit SHA, so that a caller reads one revision: Head once, then every ListFiles and ReadFile at the SHA it returns. A commit that lands on the branch after Head changes nothing those calls read. github.Remote and forgetest.Remote implement it.

type Lister

type Lister interface {
	// ListFiles returns the repository-relative paths of every file under
	// dir at ref, recursively and sorted; none when ref carries no such
	// directory. An empty dir lists the repository root. A commit SHA the
	// repository does not have is ErrFileNotFound.
	ListFiles(ctx context.Context, repo Repository, ref, dir string) ([]string, error)
}

Lister lists a directory of a repository at a branch or a commit: what a caller removes as a whole (a workload cluster's directory) or checks is there before it writes into it. github.Remote and forgetest.Remote implement it. ref is read as Reader reads it.

type MergedFilesLister

type MergedFilesLister interface {
	// MergedFiles returns the files the merged pull request added, modified,
	// removed or renamed, sorted by path; ErrNotMerged when the pull request
	// is open or closed without a merge. Only pr.Repository and pr.Number are
	// read.
	MergedFiles(ctx context.Context, pr PullRequest) ([]FileChange, error)
}

MergedFilesLister lists what a merged pull request changed: what a caller checks before it acts on a merge — that this merge, and not another one, removed a given file. github.Remote and forgetest.Remote implement it.

type OneRemote

type OneRemote interface {
	Remote
	PrefixFinder
}

OneRemote can find competing pull requests as well as open a new one.

type PrefixFinder

type PrefixFinder interface {
	// FindPullRequestsByPrefix returns every open pull request of repo whose
	// head branch is in repo itself (never a fork's branch of that name) and
	// starts with prefix, sorted by number; none when there is no such pull
	// request. An empty prefix is ErrInvalidRequest.
	//
	// A prefix matches every branch that starts with it: end it with a
	// separator that the family's own name cannot carry, so that "org-a-"
	// does not also find the branches of a family "org-a-prod-". The result
	// is not a snapshot: on GitHub, past one page of open pull requests, one
	// that closes during the read can hide another one from it.
	FindPullRequestsByPrefix(ctx context.Context, repo Repository, prefix string) ([]PullRequest, error)
}

PrefixFinder finds the open pull requests of a family of branches: what a caller checks before it writes when each write gets its own branch, a stable prefix and a suffix of its own, and a second write must wait while a pull request of the family is open. github.Remote and forgetest.Remote implement it.

type PullRequest

type PullRequest struct {
	Repository Repository
	Number     int
	URL        string
	Head       string
	Base       string
	HeadSHA    string
}

PullRequest identifies a pull request the module opened or found open. HeadSHA is the commit the caller saw; Merge refuses when the head has moved.

func Open

func Open(ctx context.Context, remote Remote, req Request, changes []Change) ([]PullRequest, error)

Open lands the changes: for every repository one branch from its base, one commit with all of that repository's files, one pull request with the caller's title and body. A branch an earlier run left behind is brought up to date with its base first, so files composed from the base's current content never land on a head that lacks it (ErrStaleBranch when the base does not merge in). Repositories are handled in name order; on an error the pull requests opened so far are returned with it.

func OpenOne

func OpenOne(ctx context.Context, remote OneRemote, object string, request Request, changes []Change) (PullRequest, error)

OpenOne opens a pull request only when object has no open pull request in the repository. object is a stable branch-name prefix without underscores; request.Branch is ignored. Rendered files are never joined into an existing pull request because their source revision may differ from its head.

The lookup and creation are not atomic. A second lookup detects competing creates and closes this call's pull request when an older one exists. Lookup failure after creation returns the created handle with an error so the caller can recover without blindly opening another pull request.

type PullRequestConflictError

type PullRequestConflictError struct {
	Existing   PullRequest
	Created    PullRequest
	CleanupErr error
}

PullRequestConflictError identifies the existing pull request and any newly created competitor. CleanupErr is nonnil if closing the competitor failed.

func (*PullRequestConflictError) Error

func (e *PullRequestConflictError) Error() string

func (*PullRequestConflictError) Unwrap

func (e *PullRequestConflictError) Unwrap() error

type Reader

type Reader interface {
	// ReadFile returns the content of path at ref, or ErrFileNotFound: no
	// file at path, or ref a commit SHA the repository does not have.
	ReadFile(ctx context.Context, repo Repository, ref, path string) ([]byte, error)
}

Reader reads a repository's files at a branch or a commit: what a caller renders against before it lands a change — the kustomization.yaml it extends, the .sops.yaml it encrypts for, whether a file it would remove exists. github.Remote and forgetest.Remote implement it.

ref is a branch name, read at the branch's head when the call runs, or a full commit SHA (from HeadReader.Head), read at that commit. Two calls with a branch name can see two revisions when a commit lands between them; a caller that lists a directory and reads its files resolves the head once with Head and passes the SHA to every call.

type Remote

type Remote interface {
	// CreateBranch creates branch from base. A branch that exists is brought
	// up to date: base is merged into it when it is behind (a merge commit,
	// history never rewritten), or ErrStaleBranch names the branch and its
	// open pull request when base does not merge into it. The branch's head
	// contains base when CreateBranch returns nil.
	CreateBranch(ctx context.Context, repo Repository, branch, base string) error
	// Commit adds one commit with files to branch; a path whose content is
	// nil is removed.
	Commit(ctx context.Context, repo Repository, branch, message string, files map[string][]byte) error
	// OpenPullRequest opens head against base, or returns the open pull request head already has.
	OpenPullRequest(ctx context.Context, repo Repository, head, base, title, body string) (PullRequest, error)
	// Status reads the pull request's head, merged flag, review and check state.
	Status(ctx context.Context, pr PullRequest) (Status, error)
	// Approve submits an approving review on the pull request with the caller's identity —
	// the identity the remote was built with. GitHub refuses a review by the
	// pull request's own author; callers check the author before the call.
	Approve(ctx context.Context, pr PullRequest, body string) error
	// Merge merges the pull request with the caller's identity, refusing when the head is not pr.HeadSHA.
	Merge(ctx context.Context, pr PullRequest) error
	// FindPullRequest returns the open pull request whose head is head, and whether there is one.
	FindPullRequest(ctx context.Context, repo Repository, head string) (PullRequest, bool, error)
	// OpenDraftPullRequest is OpenPullRequest with the pull request opened as a draft.
	OpenDraftPullRequest(ctx context.Context, repo Repository, head, base, title, body string) (PullRequest, error)
	// EnableAutoMerge arms the remote's auto-merge on the pull request with the
	// repository's merge method, so it lands once its reviews and checks pass.
	// The caller's choice, never the module's default.
	EnableAutoMerge(ctx context.Context, pr PullRequest) error
	// Close closes the pull request without merging and, when asked, deletes
	// its head branch; a closed pull request is left as it is.
	Close(ctx context.Context, pr PullRequest, deleteBranch bool) error
	// Revert opens a pull request undoing a merged one: the merged change's
	// files are inverted in one commit — added files removed, changed and
	// removed files restored — except the paths in overrides, which take the
	// given content. The commit lands on the head of the revert branch, which
	// is created at the base's tip or brought up to date with it as
	// CreateBranch does (ErrStaleBranch naming the branch and its pull request
	// when the base does not merge in); a second revert adds a commit to the
	// open revert pull request. History is never rewritten.
	Revert(ctx context.Context, pr PullRequest, body string, overrides map[string][]byte) (PullRequest, error)
}

Remote is the Git host the package talks to: github.Remote in production, forgetest.Remote in tests. Every operation acts with the token the Remote was built from.

type Repository

type Repository = provenance.Repository

Repository is the GitHub repository a change lands in.

type RepositoryAccess

type RepositoryAccess interface {
	CanReadRepository(context.Context, Repository) (bool, error)
}

RepositoryAccess checks whether the remote's identity can read a repository.

type Request

type Request struct {
	Branch string
	Title  string
	Body   string
}

Request names one run: its head branch, its commit message and pull request title, and the pull request body — the caller's text, carried verbatim.

type ReviewState

type ReviewState string

ReviewState is the pull request's review rollup: the latest stance of every reviewer, approved when at least one approves and none requests changes.

type Status

type Status struct {
	HeadSHA string
	Merged  bool
	Review  ReviewState
	Checks  CheckState
}

Status is the pull request's state as the remote reports it.

Directories

Path Synopsis
internal
remote
Package remote holds what the forge.Remote implementations share: the names and messages of the branches, commits and pull requests they make.
Package remote holds what the forge.Remote implementations share: the names and messages of the branches, commits and pull requests they make.

Jump to

Keyboard shortcuts

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