Forgejo and Gitea

Forgejo and Gitea are code platforms: kōjō takes requests there from mentions and from the events you map to skills, answers in comments, and opens pull requests. One adapter serves Forgejo and Gitea, which share an API.

See Surfaces for how Forgejo compares with GitHub and GitLab, and Who may trigger what for whose requests may change code.

Set up

  1. Create a dedicated bot user and an access token for it. Its login is what people mention.

  2. Give the bot write access to the repositories it works on, and make it an administrator of each repository. The forge only tells repository administrators what access other users have, and kōjō needs that to decide who may trigger what.

  3. Add the forge to kojo.yaml. All three keys are required.

    forges:
      forgejo:
        base_url: https://forgejo.example.com
        token: ${FORGEJO_TOKEN}
        webhook_secret: ${FORGEJO_WEBHOOK_SECRET}
  4. In the repository or organization, add a webhook of type Forgejo (or Gitea) with the URL <public_url>/webhooks/forgejo and the secret set to the value of webhook_secret. Enable the events in the table below.

  5. Enable the repositories in the dashboard as forgejo/owner/name.

  6. Run kojo doctor. It checks that the instance answers and accepts the token. A token’s scopes cannot be read back, so a token that lacks the repository or issue scope only shows in the first call that needs it. For each enabled repository the check trigger permissions <repo> fails when kōjō cannot read user permissions there, which on Forgejo and Gitea means the bot is not an administrator of the repository. No mention can be authorized on such a repository.

Events

X-Forgejo-Event or X-Gitea-Event Becomes
issue_comment, pull_request_comment (action created) A mention, when the comment mentions the bot user. On a pull request, any other comment is pr_comment: feedback
pull_request_approved, pull_request_rejected, pull_request_comment (action reviewed, with text) pr_review: feedback, or a mention when the review’s text mentions the bot user
issues (action opened) issue_opened, or a mention when the body mentions the bot user
pull_request (opened, reopened, or the WIP: title prefix removed) pr_opened, or a mention when a newly opened pull request’s body mentions the bot user
pull_request (synchronized) pr_updated
pull_request (closed) pr_closed: the pull request was merged, or closed without merging
status (state failure or error; Gitea 1.24 and later) check_failed
workflow_run (completed with conclusion failure; Gitea 1.25 and later) check_failed
action_run_failure (Forgejo) check_failed
push to a branch A reload of .factory/, when the branch is the default branch

Older servers send nothing for a failed check.

The names above are the values of the event header. A webhook’s own settings name some events differently; created through the API, its events list for everything in the table is issues, issue_comment, pull_request, pull_request_comment, pull_request_sync (commits pushed to a pull request), pull_request_review_approved, pull_request_review_rejected, pull_request_review_comment and push.

A review’s delivery carries the review’s own text only: the comments a reviewer leaves on lines of the diff are in no delivery, so kōjō learns from a review’s summary and from comments on the conversation.

The mention is @ plus the bot user’s login. In the local dev stack the bot is kojo-bot and the demo repository is forgejo/kojo-dev/demo, so there you write @kojo-bot fix this. See Development.

Webhook

The webhook is verified with the hex HMAC-SHA256 of the body in X-Forgejo-Signature or X-Gitea-Signature, compared in constant time.

The status codes kōjō answers a delivery with are the same for every forge: see webhook responses.

Follow-ups

On a pull request kōjō opened, a mention, or a failed check where the repository opts in, starts a follow-up that commits to the pull request’s branch. What it uses on Forgejo and Gitea:

  • The review, for get_review_comments: the text of each submitted review with its verdict (GET /pulls/{n}/reviews) and the comments it left on lines of the diff (GET /pulls/{n}/reviews/{id}/comments, for the newest 50 reviews), with their file, line and diff hunk. A pending or dismissed review is left out with its comments; the comments of a review made on an earlier version of the diff are marked outdated. So a follow-up reads the line comments that no webhook delivers.
  • The checks, for get_check_runs: the commit statuses of the commit, the jobs of Actions among them, with their descriptions. The API kōjō uses keeps no job logs.
  • Which pull request a failed check is about: the pull request an action_run_failure names (#N), or else the branch a workflow_run or an action_run_failure names, among the open pull requests kōjō recorded. A status delivery names only a commit, so it starts no follow-up.
  • The commit: written through the contents API on top of the commit the session cloned. The branch is read first and must point there, and each updated or deleted file names its blob in that commit, so the forge refuses the commit when a push in between changed one of those files.
  • A person’s pull request (commits on a person’s pull request): who added the kojo:commit label is the latest entry of type label in the pull request’s timeline (GET /issues/{n}/timeline) that adds it and that no removal follows, read from at most 10 pages of 50; a longer timeline counts as no adder, with a warning. It counts only when the person who added it has trigger_permission. A pull request whose head repository is another one (a fork) is never written to. A failed check on such a pull request is matched as above, among the repository’s open pull requests whose head is in the repository itself.

Clone credentials

Not short-lived. The forge has no equivalent of an installation token. For clones, kōjō hands the proxy the configured token itself. The token is not scoped to one repository, it can write and it does not expire.

As with GitLab, the token stays at the proxy, and kojo-proxy sets it only on git’s fetch (GET …/info/refs?service=git-upload-pack and POST …/git-upload-pack), outside /api/v1, /api/forgejo and /api/packages. A sandbox cannot use it to push, to call a writing API, or to read the API, where an administrator’s reads include Actions variables and webhooks. Three reads are refused it on every host as well, because they answer with a token that can write: GET /v2/token (a container registry token that can push), GET …/conan/<version>/users/authenticate (a Conan registry token) and GET …/actions/runners/registration-token (a runner registration token, which Forgejo creates on that read; a runner registered with it is sent the repository’s jobs, each with a token that can push). So the forge’s registries cannot be used, and no runner registered, with the clone credential from a sandbox.

With sbx. Docker Sandboxes’ own proxy sets the token, and the sbx driver adds deny rules that refuse a push, a writing API call or a registry token request with a 403 (see Read-only credentials).

Limits

  • The contents API cannot set a file mode. kōjō 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). For a larger push the changed files are unknown.
Scroll to move. + - zoom, 0 fit, Esc close.100%