{"modulePath":"github.com/giantswarm/gitops-commit","version":"v0.14.0-rc.6","isLatest":false,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package commitmodel\n\nPackage commitmodel lands rendered files on the Git host 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 (commit/github), and commit/committest is the in-process Remote for tests.\n\nThe module holds no token. A Remote is built from the token the caller supplies (for the managers: the caller's user token of the manager's App), and a token the host refuses surfaces as ErrAuth carrying the HTTP status; the callers turn it into their sign-in answer. Nothing is retried with another token, nothing is logged, and no error carries file content.\n\n### Reading one revision {#hdr-Reading_one_revision}\n\nReadFile 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 \\*committest.Remote (Remote has none of the three):\n\n\tsha, err := gh.Head(ctx, repo, \"main\")\n\tpaths, err := gh.ListFiles(ctx, repo, sha, dir)\n\tfor _, p := range paths {\n\t\tcontent, err := gh.ReadFile(ctx, repo, sha, p)\n\t}\n\n## Constants\n\n```go\nconst (\n\tReviewNone             ReviewState = \"none\"\n\tReviewApproved         ReviewState = \"approved\"\n\tReviewChangesRequested ReviewState = \"changes_requested\"\n\n\t// ChecksNone: the head carries no statuses and no check runs; nothing to wait for.\n\tChecksNone    CheckState = \"none\"\n\tChecksPending CheckState = \"pending\"\n\tChecksSuccess CheckState = \"success\"\n\tChecksFailure CheckState = \"failure\"\n)\n```\n\n```go\nconst (\n\tOpCreateBranch    = \"create branch\"\n\tOpCommit          = \"commit\"\n\tOpOpenPullRequest = \"open pull request\"\n\tOpStatus          = \"pull request status\"\n\tOpMerge           = \"merge\"\n\tOpApprove         = \"approve pull request\"\n\tOpFindPullRequest = \"find pull request\"\n\tOpFindByPrefix    = \"find pull requests by prefix\"\n\tOpEnableAutoMerge = \"enable auto-merge\"\n\tOpClose           = \"close pull request\"\n\tOpRevert          = \"revert pull request\"\n\tOpReadFile        = \"read file\"\n\tOpListFiles       = \"list files\"\n\tOpHead            = \"read branch head\"\n\tOpMergedFiles     = \"list merged files\"\n\tOpReadRepository  = \"read repository\"\n)\n```\nOperation names of the remote calls, carried by AuthError and by committest.Remote.Fail.\n\n## Variables\n\n```go\nvar (\n\t// ErrAuth: the remote refused the caller's token (401 or 403). The callers\n\t// answer with their sign-in flow; the module neither retries nor substitutes.\n\tErrAuth = errors.New(\"token refused\")\n\t// ErrInvalidRequest: the request lacks a branch or a title.\n\tErrInvalidRequest = errors.New(\"request needs a branch and a title\")\n\t// ErrNoFiles: a change carries no files.\n\tErrNoFiles = errors.New(\"no files to commit\")\n\t// ErrConflictingBase: two changes for one repository name different branches.\n\tErrConflictingBase = errors.New(\"one repository, two base branches\")\n\t// ErrDuplicatePath: two changes for one repository write the same path.\n\tErrDuplicatePath = errors.New(\"one path in two changes\")\n\t// ErrStaleBranch: the existing branch is behind its base and base does not\n\t// merge into it; nothing is committed onto a head that lacks the base.\n\tErrStaleBranch = errors.New(\"branch behind its base and the base does not merge into it\")\n\t// ErrNotApproved: the caller has not said approved.\n\tErrNotApproved = errors.New(\"merge refused: not approved\")\n\t// ErrChecksPending: a status or check run on the head has not finished.\n\tErrChecksPending = errors.New(\"merge refused: checks pending\")\n\t// ErrChecksFailed: a status or check run on the head failed.\n\tErrChecksFailed = errors.New(\"merge refused: checks failed\")\n\t// ErrHeadMoved: the head is no longer the commit the caller saw.\n\tErrHeadMoved = errors.New(\"merge refused: the head moved since the pull request was seen\")\n\t// ErrNotMerged: the pull request is not merged; only a merged pull request\n\t// can be reverted or have its merged files listed.\n\tErrNotMerged = errors.New(\"the pull request is not merged\")\n\t// ErrNothingToRevert: the merged pull request changed no files.\n\tErrNothingToRevert = errors.New(\"revert refused: the pull request changed no files\")\n\t// ErrFileNotFound: the branch or commit carries no file at the path, or\n\t// the commit SHA a read or a list names is not in the repository.\n\tErrFileNotFound = errors.New(\"no such file\")\n)\n```\n\n## Functions\n\n```go\nfunc Merge(ctx context.Context, remote Remote, pr PullRequest, approved bool) error\n```\nMerge 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.\n\n```go\nfunc Removes(changes []FileChange, path string) bool\n```\nRemoves reports whether changes take path away: a removal of path, or a rename from it.\n\n## Types\n\n```go\ntype AuthError struct {\n\tOp     string\n\tStatus int\n}\n```\nAuthError is ErrAuth with the status the remote answered and the operation it refused. errors.Is(err, ErrAuth) matches it.\n\n```go\nfunc (e *AuthError) Error() string\n```\n\n```go\nfunc (e *AuthError) Unwrap() error\n```\nUnwrap makes errors.Is(err, ErrAuth) true.\n\n```go\nfunc (e *AuthError) Write() bool\n```\nWrite reports whether the operation is a recognized repository write. Unknown operations return false, which does not establish read access.\n\n```go\ntype Change struct {\n\tLocation provenance.Location\n\tFiles    map[string][]byte\n}\n```\nChange 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.\n\n```go\ntype ChangeKind string\n```\nChangeKind is what a merged pull request did to one file.\n\n```go\nconst (\n\tFileAdded    ChangeKind = \"added\"\n\tFileModified ChangeKind = \"modified\"\n\tFileRemoved  ChangeKind = \"removed\"\n\t// FileRenamed: the file moved from FileChange.PreviousPath to\n\t// FileChange.Path; on GitHub its content may have changed too. The\n\t// previous path is gone from the base after the merge although no\n\t// FileRemoved names it: Removes counts it.\n\tFileRenamed ChangeKind = \"renamed\"\n)\n```\n\n```go\ntype CheckState string\n```\nCheckState is the rollup of the commit statuses and check runs on the head.\n\n```go\ntype FileChange struct {\n\tPath         string\n\tKind         ChangeKind\n\tPreviousPath string\n}\n```\nFileChange 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.\n\n```go\ntype HeadReader interface {\n\t// Head returns the SHA of the commit at the head of branch.\n\tHead(ctx context.Context, repo Repository, branch string) (string, error)\n}\n```\nHeadReader 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 committest.Remote implement it.\n\n```go\ntype Lister interface {\n\t// ListFiles returns the repository-relative paths of every file under\n\t// dir at ref, recursively and sorted; none when ref carries no such\n\t// directory. A commit SHA the repository does not have is\n\t// ErrFileNotFound.\n\tListFiles(ctx context.Context, repo Repository, ref, dir string) ([]string, error)\n}\n```\nLister 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 committest.Remote implement it. ref is read as Reader reads it.\n\n```go\ntype MergedFilesLister interface {\n\t// MergedFiles returns the files the merged pull request added, modified,\n\t// removed or renamed, sorted by path; ErrNotMerged when the pull request\n\t// is open or closed without a merge. Only pr.Repository and pr.Number are\n\t// read.\n\tMergedFiles(ctx context.Context, pr PullRequest) ([]FileChange, error)\n}\n```\nMergedFilesLister 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 committest.Remote implement it.\n\n```go\ntype PrefixFinder interface {\n\t// FindPullRequestsByPrefix returns every open pull request of repo whose\n\t// head branch is in repo itself (never a fork's branch of that name) and\n\t// starts with prefix, sorted by number; none when there is no such pull\n\t// request. An empty prefix is ErrInvalidRequest.\n\t//\n\t// A prefix matches every branch that starts with it: end it with a\n\t// separator that the family's own name cannot carry, so that \"org-a-\"\n\t// does not also find the branches of a family \"org-a-prod-\". The result\n\t// is not a snapshot: on GitHub, past one page of open pull requests, one\n\t// that closes during the read can hide another one from it.\n\tFindPullRequestsByPrefix(ctx context.Context, repo Repository, prefix string) ([]PullRequest, error)\n}\n```\nPrefixFinder 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 committest.Remote implement it.\n\n```go\ntype PullRequest struct {\n\tRepository Repository\n\tNumber     int\n\tURL        string\n\tHead       string\n\tBase       string\n\tHeadSHA    string\n}\n```\nPullRequest identifies a pull request the module opened or found open. HeadSHA is the commit the caller saw; Merge refuses when the head has moved.\n\n```go\nfunc Open(ctx context.Context, remote Remote, req Request, changes []Change) ([]PullRequest, error)\n```\nOpen 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.\n\n```go\ntype Reader interface {\n\t// ReadFile returns the content of path at ref, or ErrFileNotFound: no\n\t// file at path, or ref a commit SHA the repository does not have.\n\tReadFile(ctx context.Context, repo Repository, ref, path string) ([]byte, error)\n}\n```\nReader 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 committest.Remote implement it.\n\nref 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.\n\n```go\ntype Remote interface {\n\t// CreateBranch creates branch from base. A branch that exists is brought\n\t// up to date: base is merged into it when it is behind (a merge commit,\n\t// history never rewritten), or ErrStaleBranch names the branch and its\n\t// open pull request when base does not merge into it. The branch's head\n\t// contains base when CreateBranch returns nil.\n\tCreateBranch(ctx context.Context, repo Repository, branch, base string) error\n\t// Commit adds one commit with files to branch; a path whose content is\n\t// nil is removed.\n\tCommit(ctx context.Context, repo Repository, branch, message string, files map[string][]byte) error\n\t// OpenPullRequest opens head against base, or returns the open pull request head already has.\n\tOpenPullRequest(ctx context.Context, repo Repository, head, base, title, body string) (PullRequest, error)\n\t// Status reads the pull request's head, merged flag, review and check state.\n\tStatus(ctx context.Context, pr PullRequest) (Status, error)\n\t// Approve submits an approving review on the pull request with the caller's identity —\n\t// the identity the remote was built with. GitHub refuses a review by the\n\t// pull request's own author; callers check the author before the call.\n\tApprove(ctx context.Context, pr PullRequest, body string) error\n\t// Merge merges the pull request with the caller's identity, refusing when the head is not pr.HeadSHA.\n\tMerge(ctx context.Context, pr PullRequest) error\n\t// FindPullRequest returns the open pull request whose head is head, and whether there is one.\n\tFindPullRequest(ctx context.Context, repo Repository, head string) (PullRequest, bool, error)\n\t// OpenDraftPullRequest is OpenPullRequest with the pull request opened as a draft.\n\tOpenDraftPullRequest(ctx context.Context, repo Repository, head, base, title, body string) (PullRequest, error)\n\t// EnableAutoMerge arms the remote's auto-merge on the pull request with the\n\t// repository's merge method, so it lands once its reviews and checks pass.\n\t// The caller's choice, never the module's default.\n\tEnableAutoMerge(ctx context.Context, pr PullRequest) error\n\t// Close closes the pull request without merging and, when asked, deletes\n\t// its head branch; a closed pull request is left as it is.\n\tClose(ctx context.Context, pr PullRequest, deleteBranch bool) error\n\t// Revert opens a pull request undoing a merged one: the merged change's\n\t// files are inverted in one commit — added files removed, changed and\n\t// removed files restored — except the paths in overrides, which take the\n\t// given content. The commit lands on the head of the revert branch, which\n\t// is created at the base's tip or brought up to date with it as\n\t// CreateBranch does (ErrStaleBranch naming the branch and its pull request\n\t// when the base does not merge in); a second revert adds a commit to the\n\t// open revert pull request. History is never rewritten.\n\tRevert(ctx context.Context, pr PullRequest, body string, overrides map[string][]byte) (PullRequest, error)\n}\n```\nRemote is the Git host the package talks to: github.Remote in production, committest.Remote in tests. Every operation acts with the token the Remote was built from.\n\n```go\ntype Repository = provenance.Repository\n```\nRepository is the GitHub repository a change lands in.\n\n```go\ntype Request struct {\n\tBranch string\n\tTitle  string\n\tBody   string\n}\n```\nRequest names one run: its head branch, its commit message and pull request title, and the pull request body — the caller's text, carried verbatim.\n\n```go\ntype ReviewState string\n```\nReviewState is the pull request's review rollup: the latest stance of every reviewer, approved when at least one approves and none requests changes.\n\n```go\ntype Status struct {\n\tHeadSHA string\n\tMerged  bool\n\tReview  ReviewState\n\tChecks  CheckState\n}\n```\nStatus is the pull request's state as the remote reports it.\n\n","path":"github.com/giantswarm/gitops-commit/internal/commitmodel","name":"commitmodel","synopsis":"Package commitmodel lands rendered files on the Git host 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.","isRedistributable":true}
