From 930c0b06fb67f8cd4f3413dcd655d784b2246318 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 12:02:13 +0000 Subject: [PATCH] feat: review GitLab, Gitea and Bitbucket repositories Appwrite hands out Appwrite now sends review and chat jobs for GitLab, Gitea and Bitbucket installations, with the installation's OAuth token. - The token can name the provider's API root, so a self-hosted GitLab or Gitea is reached on its own host instead of GitLab.com or gitea.com. - Bitbucket repositories Appwrite names by their full name, `workspace/slug`, are addressed by that workspace. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01F3M7tuaHpj5WN6awCJZvos Co-authored-by: Torsten Dittmann <1759475+TorstenDittmann@users.noreply.github.com> --- apps/service/src/index.ts | 3 ++- packages/appwrite/src/installation-tokens.ts | 16 +++++++++---- packages/appwrite/src/results.test.ts | 22 +++++++++++++++++ packages/bitbucket/src/client.test.ts | 22 +++++++++++++++++ packages/bitbucket/src/client.ts | 11 +++++---- packages/gitea/src/client.test.ts | 25 ++++++++++++++++++++ packages/gitea/src/client.ts | 20 ++++++++++++---- packages/github/src/tokens.ts | 2 ++ packages/gitlab/src/client.test.ts | 20 ++++++++++++++++ packages/gitlab/src/client.ts | 7 +++++- 10 files changed, 133 insertions(+), 15 deletions(-) diff --git a/apps/service/src/index.ts b/apps/service/src/index.ts index 974ee1c..e5d51b5 100644 --- a/apps/service/src/index.ts +++ b/apps/service/src/index.ts @@ -39,7 +39,8 @@ const tokens = new AppwriteInstallationTokens(appwrite); const clients: Record = { github: new InstallationGitHubClientFactory(tokens, { fetch: metrics.instrumentFetch('github') }), origin: new InstallationOriginClientFactory(tokens, { fetch: metrics.instrumentFetch('origin') }), - // Not handed out by Appwrite yet: these speak to GitLab.com, Bitbucket Cloud and gitea.com. + // GitLab and Gitea default to GitLab.com and gitea.com; a self-hosted installation's token names + // its own API root. gitlab: new InstallationGitLabClientFactory(tokens, { fetch: metrics.instrumentFetch('gitlab') }), bitbucket: new InstallationBitbucketClientFactory(tokens, { fetch: metrics.instrumentFetch('bitbucket') diff --git a/packages/appwrite/src/installation-tokens.ts b/packages/appwrite/src/installation-tokens.ts index 54272d4..4a4da2b 100644 --- a/packages/appwrite/src/installation-tokens.ts +++ b/packages/appwrite/src/installation-tokens.ts @@ -3,8 +3,9 @@ import { AppwriteApi } from './api'; import type { AppwriteConfig } from './config'; /** - * Installation tokens minted by Appwrite, which owns the GitHub App and its private key. Each is - * scoped to the one repository a review is for; the service never holds the key itself. + * Installation tokens minted by Appwrite, which owns the GitHub and Origin apps and their private + * keys, and the OAuth tokens of GitLab, Gitea and Bitbucket installations. Each is scoped to the + * one repository a review is for; the service never holds a key itself. */ export class AppwriteInstallationTokens implements InstallationTokens { private readonly api: AppwriteApi; @@ -14,14 +15,21 @@ export class AppwriteInstallationTokens implements InstallationTokens { } async get(repository: { projectId: string; repositoryId: string }): Promise { - const { token, expiresAt, appId, appSlug } = await this.api.request<{ + const { token, expiresAt, appId, appSlug, endpoint } = await this.api.request<{ token: string; expiresAt: string; appId: string; appSlug: string; + /** A self-hosted GitLab or Gitea's API root. */ + endpoint?: string; }>('POST', `/reviews/repositories/${encodeURIComponent(repository.repositoryId)}/tokens`, { projectId: repository.projectId }); - return { token, expiresAt: new Date(expiresAt), app: { id: appId, slug: appSlug } }; + return { + token, + expiresAt: new Date(expiresAt), + app: { id: appId, slug: appSlug }, + ...(endpoint ? { endpoint } : {}) + }; } } diff --git a/packages/appwrite/src/results.test.ts b/packages/appwrite/src/results.test.ts index e4eb4c3..c005668 100644 --- a/packages/appwrite/src/results.test.ts +++ b/packages/appwrite/src/results.test.ts @@ -344,4 +344,26 @@ describe('AppwriteInstallationTokens', () => { } ]); }); + + test('passes on the API root of a self-hosted provider', async () => { + const tokens = new AppwriteInstallationTokens( + serve(() => + Response.json( + { + token: 'glo_x', + expiresAt: '2026-10-27T13:00:00Z', + appId: '7', + appSlug: 'ada', + provider: 'gitlab', + endpoint: 'https://git.example.com/api/v4' + }, + { status: 201 } + ) + ) + ); + + const token = await tokens.get({ projectId: 'project-1', repositoryId: 'repo-1' }); + + expect(token.endpoint).toBe('https://git.example.com/api/v4'); + }); }); diff --git a/packages/bitbucket/src/client.test.ts b/packages/bitbucket/src/client.test.ts index 88fe3cd..94a54c8 100644 --- a/packages/bitbucket/src/client.test.ts +++ b/packages/bitbucket/src/client.test.ts @@ -366,3 +366,25 @@ describe('BitbucketClient', () => { expect(await github.hasWriteAccess('ada')).toBe(true); }); }); + +describe('InstallationBitbucketClientFactory', () => { + test('reads the workspace from a repository named by its full name', async () => { + const requested: string[] = []; + const apiUrl = serve({ + 'GET /2.0/repositories/acme/rocket': (request) => { + requested.push(request.path); + return Response.json({ + full_name: 'acme/rocket', + links: { html: { href: 'https://bitbucket.org/acme/rocket' }, clone: [] } + }); + } + }); + + const { filesUrl } = await new InstallationBitbucketClientFactory(source, { + apiUrl + }).forRepository({ ...repository, providerRepositoryId: 'acme/rocket', owner: 'Acme Inc' }); + + expect(requested).toEqual(['/2.0/repositories/acme/rocket']); + expect(filesUrl).toBe('https://bitbucket.org/acme/rocket/src'); + }); +}); diff --git a/packages/bitbucket/src/client.ts b/packages/bitbucket/src/client.ts index 4a2c49e..aaaeeac 100644 --- a/packages/bitbucket/src/client.ts +++ b/packages/bitbucket/src/client.ts @@ -80,10 +80,13 @@ export class InstallationBitbucketClientFactory implements GitHubClientFactory { } return current.token; }; - // The repository's `{uuid}` survives a rename; its slug is the fallback. - const path = `/repositories/${encodeURIComponent(repository.owner)}/${encodeURIComponent( - repository.providerRepositoryId || repository.name - )}`; + // The repository's `{uuid}` survives a rename; its slug is the fallback. Appwrite names a + // repository by its full name, `workspace/slug`, which carries the workspace too. + const id = repository.providerRepositoryId || repository.name; + const [workspace, slug] = id.includes('/') + ? [id.slice(0, id.indexOf('/')), id.slice(id.indexOf('/') + 1)] + : [repository.owner, id]; + const path = `/repositories/${encodeURIComponent(workspace)}/${encodeURIComponent(slug)}`; const github = new BitbucketClient( new BitbucketApi(token, this.config), path, diff --git a/packages/gitea/src/client.test.ts b/packages/gitea/src/client.test.ts index eb16197..c82a367 100644 --- a/packages/gitea/src/client.test.ts +++ b/packages/gitea/src/client.test.ts @@ -321,3 +321,28 @@ describe('GiteaClient', () => { expect(await github.hasWriteAccess('nobody')).toBe(false); }); }); + +describe('InstallationGiteaClientFactory', () => { + test('speaks to the self-hosted instance the token names', async () => { + const endpoint = serve({ + [`GET ${BY_ID}`]: () => + Response.json({ + name: 'rocket', + owner: { id: 1, login: 'acme' }, + html_url: 'https://git.example.com/acme/rocket', + clone_url: 'https://git.example.com/acme/rocket.git' + }) + }); + const selfHosted: InstallationTokens = { + async get() { + return { ...(await source.get({ projectId: '', repositoryId: '' })), endpoint }; + } + }; + + const { filesUrl } = await new InstallationGiteaClientFactory(selfHosted, { + apiUrl: 'http://127.0.0.1:9/api/v1' + }).forRepository(repository); + + expect(filesUrl).toBe('https://git.example.com/acme/rocket/src/commit'); + }); +}); diff --git a/packages/gitea/src/client.ts b/packages/gitea/src/client.ts index 31814fe..bf73b10 100644 --- a/packages/gitea/src/client.ts +++ b/packages/gitea/src/client.ts @@ -31,6 +31,11 @@ export interface InstallationToken { expiresAt: Date; /** The bot account: its user id, and the login people mention it by. */ app: { id: string; slug: string }; + /** + * The instance's API root when it is self-hosted, e.g. `https://git.example.com/api/v4`. Wins + * over the configured one, since each installation names its own host. + */ + endpoint?: string; } /** Hands out the bot's token for one repository: the project's `repositories` row. */ @@ -71,7 +76,10 @@ export class InstallationGiteaClientFactory implements GitHubClientFactory { } return current.token; }; - const api = new GiteaApi(token, this.config); + const api = new GiteaApi(token, { + ...this.config, + apiUrl: current.endpoint || this.config.apiUrl + }); // Gitea addresses a repository by owner and name only; its id finds them after a rename. const found = repository.providerRepositoryId ? await api.request( @@ -429,10 +437,12 @@ export class GiteaClient implements GitHubClient { const issues: LinkedIssueRef[] = []; for (const number of numbers) { const issue = await this.api - .request<{ number: number; title: string; body?: string; pull_request?: unknown }>( - 'GET', - `${this.path}/issues/${number}` - ) + .request<{ + number: number; + title: string; + body?: string; + pull_request?: unknown; + }>('GET', `${this.path}/issues/${number}`) .catch(() => null); if (!issue || issue.pull_request) continue; issues.push({ diff --git a/packages/github/src/tokens.ts b/packages/github/src/tokens.ts index e6b69ba..66ca09b 100644 --- a/packages/github/src/tokens.ts +++ b/packages/github/src/tokens.ts @@ -3,6 +3,8 @@ export interface InstallationToken { expiresAt: Date; /** The GitHub App the token is for. */ app: { id: string; slug: string }; + /** The provider's API root, when Appwrite names one: a self-hosted GitLab or Gitea. */ + endpoint?: string; } /** diff --git a/packages/gitlab/src/client.test.ts b/packages/gitlab/src/client.test.ts index 4540f3c..84db1d1 100644 --- a/packages/gitlab/src/client.test.ts +++ b/packages/gitlab/src/client.test.ts @@ -428,3 +428,23 @@ test('contextLines maps unchanged lines to their old numbers', () => { [13, 12] ]); }); + +describe('InstallationGitLabClientFactory', () => { + test('speaks to the self-hosted instance the token names', async () => { + const endpoint = serve({ + [`GET ${PROJECT}`]: () => + Response.json({ id: 42, web_url: 'https://git.example.com/acme/platform/rocket' }) + }); + const selfHosted: InstallationTokens = { + async get() { + return { ...(await source.get({ projectId: '', repositoryId: '' })), endpoint }; + } + }; + + const { filesUrl } = await new InstallationGitLabClientFactory(selfHosted, { + apiUrl: 'http://127.0.0.1:9/api/v4' + }).forRepository(repository); + + expect(filesUrl).toBe('https://git.example.com/acme/platform/rocket/-/blob'); + }); +}); diff --git a/packages/gitlab/src/client.ts b/packages/gitlab/src/client.ts index fc1f601..142bbce 100644 --- a/packages/gitlab/src/client.ts +++ b/packages/gitlab/src/client.ts @@ -33,6 +33,11 @@ export interface InstallationToken { expiresAt: Date; /** The bot account: its user id, and the username people mention it by. */ app: { id: string; slug: string }; + /** + * The instance's API root when it is self-hosted, e.g. `https://git.example.com/api/v4`. Wins + * over the configured one, since each installation names its own host. + */ + endpoint?: string; } /** Hands out the bot's token for one repository: the project's `repositories` row. */ @@ -81,7 +86,7 @@ export class InstallationGitLabClientFactory implements GitHubClientFactory { repository.providerRepositoryId || `${repository.owner}/${repository.name}` ); const github = new GitLabClient( - new GitLabApi(token, this.config), + new GitLabApi(token, { ...this.config, apiUrl: current.endpoint || this.config.apiUrl }), `/projects/${project}`, token, current.app