# Forge Channels

`gitlab()` and `forgejo()` start an Agent from pull request events. A GitLab merge request counts as a pull request, and its `iid` is the number, also in nested groups.

These Channels work like [`github()`](https://vitehub.dev/docs/agents/channels#reconcile-github-pull-requests) for filters, mentions, reconcile triggers, replies, commit statuses, and activity comments. `pullRequest.read(invocation)` returns the request. Its `provider` is `gitlab` or `forgejo`, and `instance` names the Forge instance.

## Add a Channel

```ts [server/agents/reviewer.ts]
import { defineAgent } from 'vite-hub/agent'
import { forgejo, gitlab } from 'vite-hub/agent/channels'

export default defineAgent({
  channels: {
    gitlab: gitlab({
      pullRequest: { reconcile: { mentions: ['@review-bot'] } },
      activity: true,
    }),
    codeberg: forgejo({ pullRequest: true }),
  },
  driver: { model: 'openai/gpt-5.1-mini' },
})
```

`forgejo()` uses Codeberg by default. For self-managed GitLab or Forgejo, set `baseUrl` to the instance root. The Channel adds the API path.

Choose what starts a run:

- `pullRequest`: slash commands.
- `reconcile.mentions`: mentions.
- `reconcile.comments`: comments without a command.
- `reconcile.triggers`: filters and mentions per event. Comment events are `comment`, `review`, and `review_comment`.
- `reconcile: true`: the lifecycle events `opened`, `reopened`, `synchronize`, and `ready_for_review`.

Only GitLab sends `ready_for_review`. Forgejo has no draft-to-ready event, so `forgejo()` rejects it in `reconcile.events`.

`github()` also acts on reconcile runs when a person pushes to the branch or closes the pull request, with `reconcile.headMoves`. `gitlab()` and `forgejo()` do not: they do not pass `closed` events to the trigger, and they cannot always tell a bot push from a person's push. See [When the branch moves or the pull request closes](https://vitehub.dev/docs/agents/channels#when-the-branch-moves-or-the-pull-request-closes).

`pullRequest.ci` is `'report'` (default) or `'wait'`, as on GitHub. See [Hosted CI](https://vitehub.dev/docs/agents/channels#hosted-ci).

The final reply is a pull request comment. `pullRequest.reply: false` turns it off. Delivery effects can add reactions, edit the triggering comment, or report commit statuses. `statusContext` defaults to `ViteHub Agent`. `activity: true` keeps one comment with session links and status, owned by the authenticated account.

## Server Env

Explicit options win over Server Env. If Server Env does not declare a field, the Channel reads the Forge variable. [Forge configuration](https://vitehub.dev/docs/forge/configure#server-env) lists the same fields for `useForge()`.

| Server Env field | Forge variable | Default or use |
| --- | --- | --- |
| `gitlab.baseUrl` | `VITEHUB_GITLAB_BASE_URL` | `https://gitlab.com` |
| `gitlab.token` | `VITEHUB_GITLAB_TOKEN` | Token for metadata and writes. |
| `gitlab.webhookSecret` | `VITEHUB_GITLAB_WEBHOOK_SECRET` | Required webhook secret. |
| `forgejo.baseUrl` | `VITEHUB_FORGEJO_BASE_URL` | `https://codeberg.org` |
| `forgejo.token` | `VITEHUB_FORGEJO_TOKEN` | Token for metadata and writes. |
| `forgejo.webhookSecret` | `VITEHUB_FORGEJO_WEBHOOK_SECRET` | Required webhook secret. |

The matching options are `baseUrl`, `token`, and `webhookSecret`. Each takes a runtime callback. Tokens and secrets also take a Server Env secret with `unseal()`.

## Webhook setup

Set `sync.repositories`, and the sync command creates or updates repository webhooks:

```ts fragment
channels: {
  gitlab: gitlab({
    sync: { repositories: ['platform/team/api'] },
    pullRequest: { reconcile: { mentions: ['@review-bot'] } },
  }),
  codeberg: forgejo({
    sync: { repositories: ['platform/api'] },
    pullRequest: { reconcile: { comments: true } },
  }),
}
```

GitLab paths can include nested groups. Each Channel uses its `baseUrl`, `token`, and `webhookSecret` options, then Server Env. Sync needs a token and webhook secret:

- GitLab: the `api` token scope and Maintainer access to each project.
- Forgejo and Codeberg: the `write:repository` token scope and admin access to each repository.

Deploy the webhook route first. Then run a dry run:

```bash [Terminal]
vitehub channels sync --stage <name> --url <https-origin> --channel <id>
```

Apply the plan after you check it:

```bash [Terminal]
vitehub channels sync --stage <name> --url <https-origin> --channel <id> --apply --confirm-origin <https-origin>
```

The plan shows the current and desired URL, and each repository's hook ID, events, and active state. It lists each create or update. Events follow the pull request and activity features you turn on. Forge event groups can also send related issue events, which the Channel ignores.

The plan never shows tokens or secrets. It lists `webhookSecret` as unverifiable, because Forges do not return it. `--force` resends the secret to existing hooks. Sync never deletes hooks and leaves hooks with other URLs alone. It fails if a repository has duplicate hooks at the desired URL, so remove them first.

Without `sync.repositories`, the plan shows the Channel with no change and an unverifiable entry that names the option. You can still add webhooks by hand. See [Channel Env and sync](https://vitehub.dev/docs/agents/channels#channel-env) and [CLI channel synchronization](https://vitehub.dev/docs/development/cli#synchronize-channel-webhooks) for deploy checks and origin confirmation.

For Forgejo, sync sets JSON payloads and handles its grouped event subscriptions. If GitLab auto-disabled a hook, re-enable it with a successful test request in GitLab before sync. Different deployed webhook URLs can manage hooks on the same repositories.

## Configure webhooks

To set up a webhook by hand, point it at the generated Agent Channel route. Use the same secret as `webhookSecret` or its Server Env field.

| Forge | Events to enable |
| --- | --- |
| GitLab | Comments, Merge request events |
| Forgejo or Codeberg | Pull Request, Pull Request Comment, Review |

GitLab sends its secret in `X-Gitlab-Token`. Forgejo signs the body. A wrong signature or token returns HTTP 401. A missing secret fails with `AGENT_FORGE_WEBHOOK_SECRET_MISSING`, naming the option and env variable. Delivery IDs stop duplicate Invocations. Reconcile runs share the pull request concurrency limit, which defaults to one.

## Limits

- No `pullRequest.workspace` checkout, artifacts, or `authorAssociation` filters. Add a Workspace through the Agent's other Capabilities if needed.
- `webhookSecret: false` is not supported.
- GitLab has approvals, not reviews. An `APPROVE` effect approves the merge request and posts the review text as a separate note. A comment review posts a note. A request-changes review fails with `AGENT_FORGE_REVIEW_UNSUPPORTED`.
- Forgejo has native reviews, but these Channels cannot reach its CI runs, jobs, or check reruns.
- Metadata reads are limited. Change the limits with `maxBodyLength`, `maxCommentBodyLength`, `maxComments`, and `maxFiles` on `pullRequest`. The context reports omitted items and missing metadata.
- After a restart, activity lookup scans the newest 500 comments.

<!-- vitehub-docs:section-links -->
## In this section

- [Start](https://vitehub.dev/raw/docs/agents.md)
- [Define and run](https://vitehub.dev/raw/docs/agents/agent-definitions.md)
- [Instructions and tools](https://vitehub.dev/raw/docs/agents/instructions.md)
- [History and context](https://vitehub.dev/raw/docs/agents/chat-history-sessions.md)
- Channels
  - [Channels](https://vitehub.dev/raw/docs/agents/channels.md)
  - [Forge Channels](https://vitehub.dev/raw/docs/agents/forge-channels.md) (this page)
  - [Gmail Channel](https://vitehub.dev/raw/docs/agents/gmail.md)
  - [Triggers](https://vitehub.dev/raw/docs/agents/triggers.md)
- [Inspect and evaluate](https://vitehub.dev/raw/docs/agents/observability.md)
- [Advanced](https://vitehub.dev/raw/docs/agents/agent-presets.md)
- [Recipes](https://vitehub.dev/raw/docs/agents/support-agent.md)
- [Presets](https://vitehub.dev/raw/docs/agents/presets.md)
- [Capabilities](https://vitehub.dev/raw/docs/agents/capabilities/access.md)

Next: [AGENT_R0053](https://vitehub.dev/raw/docs/agents/diagnostics/agent_r0053.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
