forgejo

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0 Imports: 27 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 forgejo is the forge adapter for Forgejo and Gitea, which share one API (config.Forgejo.BaseURL is the instance root).

Auth model

The adapter uses one access token of a dedicated bot user, sent as "Authorization: token <t>" to the API v1. That user's login is what people mention. The bot needs write access to the repositories it works on, and has to be an administrator of a repository for Permission to work: the forge only tells repository administrators what access other users have.

Webhooks are verified with the hex HMAC-SHA256 of the body in X-Forgejo-Signature (or X-Gitea-Signature), in constant time, before anything is decoded. A Forge without a webhook secret rejects every delivery.

Writes

CreateBranch and CommitFiles refuse any branch that is not kojo/*. CommitFiles uses the contents API, so no git binary and no repository hook runs in the core: one commit for all files through the "change files" endpoint, or one commit per file on a server too old to have it (Gitea before 1.20). It accepts regular files only and no path inside .git. With forge.WithExpectedHead it writes only while the branch is at that commit, and replaces the blobs of that commit, so the forge refuses the commit when a push in between changed one of the files.

Limitations

  • CloneCredentials cannot return a short-lived or read-only credential: the forge has no equivalent of an installation token. It returns the configured token itself, which can write and does not expire (Expires is zero). The egress proxy is then the only thing between a sandbox and a push; keep the allowlist of a Forgejo skill to what it needs. Git and the API are one host and both accept Basic auth, so it is one credential.
  • The contents API cannot set a file mode. CommitFiles refuses to create a file with mode 100755 rather than commit it without the executable bit; an updated file keeps the mode it has.
  • A push payload carries only a few commits (five by default). Event.ChangedPaths is nil when a push has more, which means "unknown", not "nothing changed".
  • Failed checks arrive as a "status" delivery (Gitea 1.24 and later), a "workflow_run" delivery (Gitea 1.25 and later) or an "action_run_failure" delivery (Forgejo). Older servers send nothing for a failed check.
  • ListRepos does not fill RepoInfo.HeadSHA. GetRepo does.
  • ListPullRequests reads GET /pulls with sort=recentupdate. That list has no since filter, so the pages stop at the first pull request updated before it, and no merged state, so closed and merged are its "closed" told apart by "merged". BaseSHA is merge_base, the merge base the forge last computed; base.sha is the base branch's head at the time of the call and is not used. MergeCommitSHA is merge_commit_sha: what the merge put on the base branch (the head for a fast-forward).
  • Mentions inside quoted text or code blocks count as mentions.
  • MergePR takes a pull request out of draft (the "WIP:" title prefix) before it merges it.

Index

Constants

View Source
const Name = "forgejo"

Name is the forge name of this adapter.

Variables

This section is empty.

Functions

This section is empty.

Types

type Forge

type Forge struct {
	// contains filtered or unexported fields
}

Forge is a Forgejo or Gitea instance reached with one access token. It is safe for concurrent use.

func New

func New(cfg config.Forgejo, opts ...Option) (*Forge, error)

New builds the adapter from the deployment config. It makes no request.

func (*Forge) BotLogin

func (f *Forge) BotLogin(ctx context.Context) (string, error)

BotLogin is the login of the token's user.

func (*Forge) BranchProtection

func (f *Forge) BranchProtection(ctx context.Context, repo forge.Repo, name string) (*forge.Protection, error)

BranchProtection describes the protection rule that applies to a branch, glob rules included. A branch that does not exist is not protected.

func (*Forge) Check

func (f *Forge) Check(ctx context.Context) error

Check verifies that the instance answers and accepts the token. It returns one joined error naming every problem. A token's scopes cannot be read back, so a token that authenticates but lacks the repository or issue scope only shows in the first call that needs it.

func (*Forge) CheckRuns

func (f *Forge) CheckRuns(ctx context.Context, repo forge.Repo, sha string) ([]forge.CheckRun, error)

CheckRuns lists the commit statuses of a commit, the jobs of Forgejo and Gitea Actions among them, each with its description: the forge keeps no other output a token can read. A warning counts as success, as it does for ChecksStatus.

func (*Forge) ChecksStatus

func (f *Forge) ChecksStatus(ctx context.Context, repo forge.Repo, sha string) (forge.ChecksState, error)

ChecksStatus is the combined commit status of a commit, which includes the jobs of Forgejo and Gitea Actions. A warning counts as success.

func (*Forge) CloneCredentials

func (f *Forge) CloneCredentials(_ context.Context, repo forge.Repo) ([]sandbox.HostCredential, error)

CloneCredentials returns the configured token as a Basic credential for the instance's host, which serves both git over HTTPS and the API.

The token is neither short-lived nor read-only: the forge cannot mint a narrower one on demand (see the package documentation). Expires is zero because the token does not expire.

func (*Forge) Comment

func (f *Forge) Comment(ctx context.Context, repo forge.Repo, number int, body string) (*forge.Comment, error)

Comment posts a comment on an issue or pull request.

func (*Forge) CommitFiles

func (f *Forge) CommitFiles(ctx context.Context, repo forge.Repo, branch, message string, changes []forge.FileChange) (string, error)

CommitFiles writes the changes on top of branch, which must be kojo/* and exist, and returns the SHA of the last commit it made: one commit for all files, or one per file on a server without the multi-file endpoint.

It looks at the branch to choose between create and update and to find the blob each update or delete replaces; when the branch changed in between, the forge refuses the commit. With forge.WithExpectedHead it starts only when the branch is at that commit and reads the blobs there, so a push that landed after the branch was read and changed one of the files makes the forge refuse the commit, and that is forge.ErrHeadMoved too; a push that changed other files stays, under the new commit. The one-file-at-a-time fallback of an old server checks each file the same way. The contents API has no file mode: creating a file with mode 100755 is refused, and an updated file keeps the mode it has.

func (*Forge) CreateBranch

func (f *Forge) CreateBranch(ctx context.Context, repo forge.Repo, name, fromSHA string) error

CreateBranch creates the branch name at fromSHA. name must be kojo/*. It fails if the branch exists.

func (*Forge) GetFile

func (f *Forge) GetFile(ctx context.Context, repo forge.Repo, ref, filePath string) ([]byte, error)

GetFile reads one file at ref (a branch, tag or commit; empty means the default branch).

func (*Forge) GetIssue

func (f *Forge) GetIssue(ctx context.Context, repo forge.Repo, number int) (*forge.Issue, error)

GetIssue reads an issue. A pull request number returns its issue side, with IsPR set.

func (*Forge) GetPR

func (f *Forge) GetPR(ctx context.Context, repo forge.Repo, number int) (*forge.PullRequest, error)

GetPR reads a pull request.

func (*Forge) GetPRDiff

func (f *Forge) GetPRDiff(ctx context.Context, repo forge.Repo, number int) (string, error)

GetPRDiff returns the unified diff of a pull request.

func (*Forge) GetRepo

func (f *Forge) GetRepo(ctx context.Context, repo forge.Repo) (*forge.RepoInfo, error)

GetRepo describes a repository and the head of its default branch. HeadSHA is empty for a repository without commits.

func (*Forge) ListComments

func (f *Forge) ListComments(ctx context.Context, repo forge.Repo, number int) ([]forge.Comment, error)

ListComments lists the comments of an issue or pull request, oldest first. The forge returns them in one answer.

func (*Forge) ListFiles

func (f *Forge) ListFiles(ctx context.Context, repo forge.Repo, ref, dir string) ([]string, error)

ListFiles lists the files under dir at ref, recursively and sorted, as paths relative to the repository root. Submodules are left out. A ref that does not exist is forge.ErrNotFound; a dir that does not exist is an empty list.

func (*Forge) ListIssues

func (f *Forge) ListIssues(ctx context.Context, repo forge.Repo, opts forge.ListIssuesOptions) ([]forge.Issue, error)

ListIssues lists issues, newest first, without pull requests.

func (*Forge) ListPullRequests

func (f *Forge) ListPullRequests(ctx context.Context, repo forge.Repo, opts forge.ListPullRequestsOptions) ([]forge.PullRequest, error)

ListPullRequests lists pull requests, the most recently updated first (sort=recentupdate). The forge's list knows neither a merged state nor a since filter: closed and merged are its "closed", told apart by "merged", and the pages stop at the first pull request updated before opts.Since.

func (*Forge) ListRepos

func (f *Forge) ListRepos(ctx context.Context) ([]forge.RepoInfo, error)

ListRepos lists the repositories the token's user owns or was given access to. HeadSHA is not filled.

func (*Forge) ListReviewComments

func (f *Forge) ListReviewComments(ctx context.Context, repo forge.Repo, number int) ([]forge.ReviewComment, error)

ListReviewComments lists the review of a pull request, oldest first: the text of each submitted review and the comments on lines of its diff, which the forge files under the review they were submitted with. A dismissed review is left out with its comments; the comments of a review made on an earlier version of the diff (stale) are marked outdated.

func (*Forge) MergePR

func (f *Forge) MergePR(ctx context.Context, repo forge.Repo, number int, method, headSHA string) error

MergePR merges a pull request. method is merge (the default), squash or rebase. A draft is taken out of draft first, by removing the WIP prefix from its title.

func (*Forge) Name

func (f *Forge) Name() string

Name is "forgejo", for Gitea too.

func (*Forge) OpenPR

func (f *Forge) OpenPR(ctx context.Context, repo forge.Repo, req forge.PRRequest) (*forge.PullRequest, error)

OpenPR opens a pull request from req.Head into req.Base. A draft gets the "WIP: " title prefix, which is how the forge marks one.

func (*Forge) ParseWebhook

func (f *Forge) ParseWebhook(r *http.Request, body []byte) (*forge.Event, error)

ParseWebhook verifies the HMAC of a delivery and normalizes it.

It returns forge.ErrBadSignature when the signature is missing or wrong, and forge.ErrIgnored for an event or action kōjō does not handle, a comment on an issue that does not mention the bot and anything the bot did itself. A failed check, a closed pull request and a push are reported whoever caused them, except a push by the bot to one of its own kojo/* branches.

func (*Forge) Permission

func (f *Forge) Permission(ctx context.Context, repo forge.Repo, user string) (forge.Permission, error)

Permission returns a user's access to a repository. A user the forge does not know has PermissionNone. The forge has no triage or maintain level.

The forge only answers this for a repository administrator, so the bot has to be one; otherwise the error is forge.ErrUnauthorized.

func (*Forge) UpdatePR

func (f *Forge) UpdatePR(ctx context.Context, repo forge.Repo, number int, title, body string) error

UpdatePR changes a pull request's title and description with one PATCH. An empty title or body is left out, so that one stays as it is; with both empty nothing is sent. The forge reads a draft from the title, so a new title keeps the pull request's draft state: it gets the "WIP: " prefix on a draft and loses any WIP prefix on a pull request that is ready. That takes a read first; a new description alone is one request.

type Option

type Option func(*Forge)

Option configures New.

func WithClock

func WithClock(now func() time.Time) Option

WithClock sets the clock. The default is time.Now.

func WithHTTPClient

func WithHTTPClient(c *http.Client) Option

WithHTTPClient sets the HTTP client. The default has a 30 second timeout.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger. The default is slog.Default.

Jump to

Keyboard shortcuts

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