Merge pull request #13259 from appwrite/feat-cursor-codebase-vcs-support

feat(vcs): add Codebase (Cursor Origin) provider
This commit is contained in:
Matej Bačo authored and GitHub committed 2026-08-19 16:38:42 +02:00
commit 18998d7968
12 files changed
+788 -8

No files matched your search

+2
View File
@@ -170,5 +170,7 @@ _APP_VCS_GITLAB_WEBHOOK_SECRET=
_APP_VCS_BITBUCKET_CLIENT_ID=
_APP_VCS_BITBUCKET_CLIENT_SECRET=
_APP_VCS_BITBUCKET_WEBHOOK_SECRET=
_APP_VCS_ORIGIN_CLIENT_ID=
_APP_VCS_ORIGIN_PRIVATE_KEY=
_APP_VCS_WEBHOOK_URL=http://appwrite/v1
_APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10
+18
View File
@@ -1392,6 +1392,24 @@ return [
'question' => '',
'filter' => ''
],
[
'name' => '_APP_VCS_ORIGIN_CLIENT_ID',
'description' => 'Origin app client ID. You can find it in your Origin app details on Cursor.',
'introduction' => '2.0.0',
'default' => '',
'required' => false,
'question' => '',
'filter' => ''
],
[
'name' => '_APP_VCS_ORIGIN_PRIVATE_KEY',
'description' => 'Origin app private key. You can generate private keys from your Origin app settings on Cursor.',
'introduction' => '2.0.0',
'default' => '',
'required' => false,
'question' => '',
'filter' => ''
],
[
'name' => '_APP_VCS_WEBHOOK_URL',
'description' => 'Base URL Appwrite advertises to self-hosted VCS providers when registering repository webhooks, if different from the public domain (e.g. a Docker-internal address). Falls back to the public domain when unset.',
+15
View File
@@ -12,6 +12,7 @@ use Utopia\VCS\Adapter\Git\Bitbucket;
use Utopia\VCS\Adapter\Git\Gitea;
use Utopia\VCS\Adapter\Git\GitHub;
use Utopia\VCS\Adapter\Git\GitLab;
use Utopia\VCS\Adapter\Git\Origin;
return [
'github' => [
@@ -62,6 +63,20 @@ return [
'webhookSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_WEBHOOK_SECRET'],
],
],
'origin' => [
'adapter' => Origin::class,
// No 'oauth2': Origin has no OAuth2 user flow. Installs are approved
// on Cursor and confirmed by a signed receipt, a handshake the
// adapter itself owns (getInstallUrl / verifyReceipt).
'variables' => [
// Factory::fromInstallation() reads appId + privateKey; Origin's
// app id doubles as its client id.
'appId' => ['required' => true, 'envVariable' => '_APP_VCS_ORIGIN_CLIENT_ID'],
'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_ORIGIN_PRIVATE_KEY'],
// No webhookSecret: Origin signs deliveries with its own Ed25519
// key, verified against its published JWKS instead of a shared secret.
],
],
'bitbucket' => [
'adapter' => Bitbucket::class,
'oauth2' => fn (string $clientId, string $clientSecret, string $endpoint) => new OAuth2Bitbucket($clientId, $clientSecret, ''),
+1
View File
@@ -114,6 +114,7 @@ const APP_VCS_GITHUB_URL = 'https://github.com/TeamAppwrite';
const APP_VCS_GITEA_EMAIL = 'team@appwrite.io'; // Used to detect Appwrite's own commits
const APP_VCS_GITLAB_EMAIL = 'team@appwrite.io'; // Used to detect Appwrite's own commits
const APP_VCS_BITBUCKET_EMAIL = 'team@appwrite.io'; // Used to detect Appwrite's own commits
const APP_VCS_ORIGIN_EMAIL = 'vcs@utopia.dev'; // Used to detect commits the VCS adapter pushes over Git HTTPS
const APP_BRANDED_EMAIL_BASE_TEMPLATE = 'email-base-styled';
// Embeddings
Generated
+6 -6
View File
@@ -5116,16 +5116,16 @@
},
{
"name": "utopia-php/vcs",
"version": "5.2.1",
"version": "5.2.2",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/vcs.git",
"reference": "ee3928969d6c654fad44fad7f38cc96383b031fa"
"reference": "758eb3ab3c32a607b5aca78b1c9bae4ab1ba9b6e"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/vcs/zipball/ee3928969d6c654fad44fad7f38cc96383b031fa",
"reference": "ee3928969d6c654fad44fad7f38cc96383b031fa",
"url": "https://api.github.com/repos/utopia-php/vcs/zipball/758eb3ab3c32a607b5aca78b1c9bae4ab1ba9b6e",
"reference": "758eb3ab3c32a607b5aca78b1c9bae4ab1ba9b6e",
"shasum": ""
},
"require": {
@@ -5176,10 +5176,10 @@
"vcs"
],
"support": {
"source": "https://github.com/utopia-php/vcs/tree/5.2.1",
"source": "https://github.com/utopia-php/vcs/tree/5.2.2",
"issues": "https://github.com/utopia-php/vcs/issues"
},
"time": "2026-08-19T12:49:54+00:00"
"time": "2026-08-19T14:01:44+00:00"
},
{
"name": "utopia-php/websocket",
+7 -1
View File
@@ -210,6 +210,8 @@ services:
- _APP_VCS_BITBUCKET_CLIENT_ID
- _APP_VCS_BITBUCKET_CLIENT_SECRET
- _APP_VCS_BITBUCKET_WEBHOOK_SECRET
- _APP_VCS_ORIGIN_CLIENT_ID
- _APP_VCS_ORIGIN_PRIVATE_KEY
- _APP_VCS_WEBHOOK_URL
- _APP_MIGRATIONS_FIREBASE_CLIENT_ID
- _APP_MIGRATIONS_FIREBASE_CLIENT_SECRET
@@ -428,6 +430,8 @@ services:
- _APP_VCS_BITBUCKET_CLIENT_ID
- _APP_VCS_BITBUCKET_CLIENT_SECRET
- _APP_VCS_BITBUCKET_WEBHOOK_SECRET
- _APP_VCS_ORIGIN_CLIENT_ID
- _APP_VCS_ORIGIN_PRIVATE_KEY
- _APP_VCS_WEBHOOK_URL
- _APP_BUILDS_VOLUME
- _APP_JOBS_HOST
@@ -708,6 +712,8 @@ services:
- _APP_VCS_BITBUCKET_CLIENT_ID
- _APP_VCS_BITBUCKET_CLIENT_SECRET
- _APP_VCS_BITBUCKET_WEBHOOK_SECRET
- _APP_VCS_ORIGIN_CLIENT_ID
- _APP_VCS_ORIGIN_PRIVATE_KEY
- _APP_VCS_WEBHOOK_URL
- _APP_FUNCTIONS_TIMEOUT
- _APP_SITES_TIMEOUT
@@ -1500,7 +1506,7 @@ services:
options:
max-file: "5"
max-size: 10m
image: ghcr.io/open-runtimes/orchestrator/jobs-service:0.18.0
image: ghcr.io/open-runtimes/orchestrator/jobs-service:1.3.0
restart: unless-stopped
user: root
networks:
@@ -0,0 +1,123 @@
<?php
namespace Appwrite\Platform\Modules\VCS\Http\Origin\Authorize;
use Appwrite\Extend\Exception;
use Appwrite\Platform\Action;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Method;
use Appwrite\SDK\MethodType;
use Appwrite\SDK\Response as SDKResponse;
use Appwrite\Utopia\Response;
use Appwrite\Vcs\Factory as VcsFactory;
use Utopia\Database\Document;
use Utopia\Platform\Scope\HTTP;
use Utopia\System\System;
class Get extends Action
{
use HTTP;
/**
* Everything the integration touches: reading repository metadata,
* cloning sources, pushing templates, deploying from pull requests,
* commenting on them, and reporting build checks.
*/
public const SCOPES = [
'repository:contents:read',
'repository:metadata:read',
'repository:contents:write',
'repository:pull_requests:read',
'repository:pull_requests:write',
'repository:pull_requests:reviews:read',
'repository:pull_requests:reviews:write',
'repository:checks:read',
'repository:checks:write',
];
public static function getName()
{
return 'getVCSOriginAuthorize';
}
public function __construct()
{
$this
->setHttpMethod(Action::HTTP_REQUEST_METHOD_GET)
->setHttpPath('/v1/vcs/origin/authorize')
->desc('Create Origin app installation')
->groups(['api', 'vcs'])
->label('scope', 'vcs.read')
->label('error', APP_VIEWS_DIR . '/general/error.phtml')
->label('sdk', new Method(
namespace: 'vcs',
group: 'installations',
name: 'createOriginInstallation',
description: <<<EOT
Begin Appwrite's Origin app installation to set up version control integration. This endpoint responds with a redirect URL to the Origin app's installation page on Cursor. The Origin app must be configured in your environment for this endpoint to work.
EOT,
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_MOVED_PERMANENTLY,
model: Response::MODEL_NONE,
)
],
contentType: ContentType::HTML,
type: MethodType::WEBAUTH,
// Preview SDK builds show the whole surface, so they do not hide.
hide: System::getEnv('_APP_SDK_PREVIEW', 'disabled') !== 'enabled',
))
->param('success', '', fn ($redirectValidator) => $redirectValidator, 'URL to redirect back to console after a successful installation attempt.', true, ['redirectValidator'])
->param('failure', '', fn ($redirectValidator) => $redirectValidator, 'URL to redirect back to console after a failed installation attempt.', true, ['redirectValidator'])
->inject('response')
->inject('project')
->inject('platform')
->inject('vcsConfigured')
->inject('vcsFactory')
->callback($this->action(...));
}
public function action(
string $success,
string $failure,
Response $response,
Document $project,
array $platform,
callable $vcsConfigured,
VcsFactory $vcsFactory,
) {
if (!$vcsConfigured('origin')) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'This endpoint is not implemented on this server. Please configure VCS (Version Control System) variables in .env file.');
}
$projectId = $project->getId();
// The callback is public and Origin performs no token exchange, so the
// state carries an HMAC binding the redirect targets to this project.
$state = [
'projectId' => $projectId,
'success' => $success,
'failure' => $failure,
'signature' => \hash_hmac('sha256', \json_encode([$projectId, $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')),
];
$protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https';
$callback = $protocol . '://' . ($platform['consoleHostname'] ?? '') . '/v1/vcs/origin/callback';
/** @var \Utopia\VCS\Adapter\Git\Origin $vcs */
$vcs = $vcsFactory->fromProvider('origin');
$installUrl = $vcs->getInstallUrl(
System::getEnv('_APP_VCS_ORIGIN_CLIENT_ID', ''),
self::SCOPES,
$callback,
\json_encode($state) ?: ''
);
$response
->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
->addHeader('Pragma', 'no-cache')
->redirect($installUrl);
}
}
@@ -0,0 +1,186 @@
<?php
namespace Appwrite\Platform\Modules\VCS\Http\Origin\Callback;
use Appwrite\Extend\Exception;
use Appwrite\Platform\Permission as AppwritePermission;
use Appwrite\Utopia\Response;
use Appwrite\Vcs\Factory as VcsFactory;
use Utopia\Console;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Query;
use Utopia\Platform\Action;
use Utopia\Platform\Scope\HTTP;
use Utopia\System\System;
use Utopia\Validator\Text;
class Get extends Action
{
use HTTP;
use AppwritePermission;
public static function getName()
{
return 'getVCSOriginCallback';
}
public function __construct()
{
$this
->setHttpMethod(Action::HTTP_REQUEST_METHOD_GET)
->setHttpPath('/v1/vcs/origin/callback')
->desc('Get installation from Origin app')
->groups(['api', 'vcs'])
->label('scope', 'public')
->label('error', APP_VIEWS_DIR . '/general/error.phtml')
->param('installation_id', '', new Text(256, 0), 'Origin installation ID', true)
->param('installation_receipt', '', new Text(8192, 0), 'Origin installation receipt JWT, signed by Cursor.', true)
->param('state', '', new Text(2048, 0), 'Origin state. Contains info sent when starting the installation flow.', true)
->inject('response')
->inject('dbForPlatform')
->inject('platform')
->inject('vcsFactory')
->callback($this->action(...));
}
public function action(
string $providerInstallationId,
string $installationReceipt,
string $state,
Response $response,
Database $dbForPlatform,
array $platform,
VcsFactory $vcsFactory,
) {
if (empty($installationReceipt)) {
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing installation receipt. Please restart the installation from the Appwrite Console.');
}
// Authenticate the callback before trusting anything else on it: the
// endpoint is public and Origin performs no token exchange, so the
// EdDSA-signed receipt is the only proof the request came from Cursor.
/** @var \Utopia\VCS\Adapter\Git\Origin $vcs */
$vcs = $vcsFactory->fromProvider('origin');
try {
$receiptClaims = $vcs->verifyReceipt($installationReceipt, System::getEnv('_APP_VCS_ORIGIN_CLIENT_ID', ''));
} catch (\Throwable $e) {
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Could not verify the Origin installation receipt. Please restart the installation from the Appwrite Console.');
}
// The receipt's subject is the authoritative installation id; a
// separately passed installation_id may only confirm it.
$receiptInstallationId = \strval($receiptClaims['sub'] ?? '');
if (!empty($providerInstallationId) && $providerInstallationId !== $receiptInstallationId) {
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Receipt subject does not match the installation ID.');
}
$providerInstallationId = $receiptInstallationId;
// Cursor echoes the state given to the install URL as a receipt claim;
// accept a query parameter carrying the same value as a fallback.
$state = !empty($state) ? $state : \strval($receiptClaims['state'] ?? '');
if (empty($state)) {
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing state parameter. Please restart the installation from the Appwrite Console.');
}
$state = \json_decode($state, true) ?? [];
$projectId = $state['projectId'] ?? '';
$redirectFailure = $state['failure'] ?? '';
// The receipt authenticates Cursor, and this HMAC authenticates the
// Appwrite Console that started the flow -- without it anyone could
// attach an installation to an arbitrary projectId.
$signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), System::getEnv('_APP_OPENSSL_KEY_V1', ''));
if (!\hash_equals($signature, $state['signature'] ?? '')) {
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.');
}
$project = $dbForPlatform->getDocument('projects', $projectId);
if ($project->isEmpty()) {
$this->failure($response, $redirectFailure, 'Project with the ID from state could not be found.', Exception::PROJECT_NOT_FOUND);
return;
}
$region = $project->getAttribute('region', 'default');
$protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https';
$hostname = $platform['consoleHostname'] ?? '';
$defaultState = [
'success' => $protocol . '://' . $hostname . "/console/project-$region-$projectId/settings/git-installations",
'failure' => $protocol . '://' . $hostname . "/console/project-$region-$projectId/settings/git-installations",
];
$state = \array_merge($defaultState, \array_filter($state));
$redirectSuccess = $state['success'] ?? '';
$redirectFailure = $state['failure'] ?? '';
if (empty($providerInstallationId)) {
$this->failure($response, $redirectFailure, 'Origin installation ID is missing.');
return;
}
// The receipt names the workspace by its stable id; the owner slug the
// rest of the integration works with comes from the installation itself.
$organization = \strval($receiptClaims['namespace_id'] ?? '');
try {
$adapter = $vcsFactory->fromInstallation(new Document([
'provider' => 'origin',
'providerInstallationId' => $providerInstallationId,
]));
$organization = $adapter->getOwnerName($providerInstallationId);
} catch (\Throwable $e) {
Console::warning('Failed to resolve Origin installation owner: ' . $e->getMessage());
}
$projectInternalId = $project->getSequence();
$installation = $dbForPlatform->findOne('installations', [
Query::equal('providerInstallationId', [$providerInstallationId]),
Query::equal('projectInternalId', [$projectInternalId]),
Query::equal('provider', ['origin']),
]);
if ($installation->isEmpty()) {
$teamId = $project->getAttribute('teamId', '');
$installation = $dbForPlatform->createDocument('installations', new Document([
'$id' => ID::unique(),
'$permissions' => $this->getPermissions($teamId, $projectId),
'providerInstallationId' => $providerInstallationId,
'projectId' => $projectId,
'projectInternalId' => $projectInternalId,
'provider' => 'origin',
'organization' => $organization,
'personal' => false,
]));
} else {
$installation = $dbForPlatform->updateDocument('installations', $installation->getId(), new Document([
'organization' => $organization,
]));
}
$response
->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
->addHeader('Pragma', 'no-cache')
->redirect($redirectSuccess);
}
/**
* Redirect back to the console with an error, or throw when no redirect is available.
*/
private function failure(Response $response, string $redirect, string $error, string $type = Exception::GENERAL_ARGUMENT_INVALID): void
{
if (empty($redirect)) {
throw new Exception($type, $error);
}
$separator = \str_contains($redirect, '?') ? '&' : '?';
$response
->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
->addHeader('Pragma', 'no-cache')
->redirect($redirect . $separator . \http_build_query(['error' => $error]));
}
}
@@ -0,0 +1,395 @@
<?php
namespace Appwrite\Platform\Modules\VCS\Http\Origin\Events;
use Appwrite\Extend\Exception;
use Appwrite\Platform\Action;
use Appwrite\Platform\Modules\VCS\Http\GitHub\Deployment;
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use Appwrite\Vcs\Factory as VcsFactory;
use Utopia\Bus\Bus;
use Utopia\Config\Config;
use Utopia\Console;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Exception\Duplicate;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\DSN\DSN;
use Utopia\Platform\Scope\HTTP;
use Utopia\Span\Span;
use Utopia\System\System;
use Utopia\VCS\Adapter\Git\Origin;
class Create extends Action
{
use HTTP;
use Deployment;
/**
* Origin's active signing keys, cached for the worker's lifetime. The set
* rotates rarely and a delivery that fails on a stale set refetches once.
*
* @var array<string>|null
*/
protected static ?array $signingKeys = null;
public static function getName()
{
return 'createVCSOriginEvent';
}
public function __construct()
{
$this
->setHttpMethod(Action::HTTP_REQUEST_METHOD_POST)
->setHttpPath('/v1/vcs/origin/events')
->desc('Create event')
->groups(['api', 'vcs'])
->label('scope', 'public')
->inject('vcsFactory')
->inject('request')
->inject('response')
->inject('dbForPlatform')
->inject('authorization')
->inject('bus')
->inject('getProjectDB')
->inject('deploymentsFactory')
->inject('platform')
->callback($this->action(...));
}
public function action(
VcsFactory $vcsFactory,
Request $request,
Response $response,
Database $dbForPlatform,
Authorization $authorization,
Bus $bus,
callable $getProjectDB,
callable $deploymentsFactory,
array $platform
) {
/** @var Origin $vcs */
$vcs = $vcsFactory->fromProvider('origin');
$event = $request->getHeaderLine($vcs->getEventHeaderName(), '');
Span::add('vcs.origin.event.name', $event);
$payload = $request->getRawPayload();
$signature = $request->getHeaderLine($vcs->getSignatureHeaderName(), '');
$deliveryId = $request->getHeaderLine('webhook-id', '');
$timestamp = $request->getHeaderLine('webhook-timestamp', '');
// Origin signs the SHA-256 of "<webhook-id>.<webhook-timestamp>.<raw body>"
// with its own Ed25519 key, verified against its published JWKS rather
// than a shared secret. Stale timestamps are replays.
$valid = false;
if (!empty($deliveryId) && \ctype_digit($timestamp) && \abs(\time() - (int) $timestamp) <= 300) {
$signedContent = $deliveryId . '.' . $timestamp . '.' . $payload;
foreach ($this->signingKeys($vcs) as $publicKey) {
if ($vcs->validateWebhookEvent($signedContent, $signature, $publicKey)) {
$valid = true;
break;
}
}
// The key set may have rotated since it was cached.
if (!$valid) {
foreach ($this->signingKeys($vcs, refresh: true) as $publicKey) {
if ($vcs->validateWebhookEvent($signedContent, $signature, $publicKey)) {
$valid = true;
break;
}
}
}
}
Span::add('vcs.origin.event.signature.valid', $valid);
if (!$valid) {
throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'Invalid webhook payload signature. The delivery could not be verified against Origin\'s published signing keys.');
}
// Origin delivers at least once and retries anything not acknowledged
// with a 2xx in time. Processing here is synchronous and can outlast
// that window, so claim the delivery id before doing the work - a
// retry of a delivery already being processed must not deploy again.
// TODO: acknowledge first and process asynchronously instead.
try {
$authorization->skip(fn () => $dbForPlatform->createDocument('vcsCommentLocks', new Document([
'$id' => 'origin-delivery-' . $deliveryId,
])));
} catch (Duplicate) {
$response->json(['events' => [], 'duplicate' => true]);
return;
}
$parsedPayloads = $vcs->getEvents($event, $payload);
foreach ($parsedPayloads as $parsedPayload) {
match (true) {
$event === Origin::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $bus, $getProjectDB, $platform, $deploymentsFactory),
\str_starts_with($event, Origin::EVENT_PULL_REQUEST . '.') => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $bus, $getProjectDB, $platform, $deploymentsFactory),
\str_starts_with($event, Origin::EVENT_INSTALLATION . '.') => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB),
default => null,
};
}
$response->json(['events' => $parsedPayloads]);
}
/**
* Origin's active signing keys, from the adapter, cached for the worker's
* lifetime - the adapter memoizes only per instance, and a new adapter is
* built for every delivery.
*
* @return array<string>
*/
protected function signingKeys(Origin $vcs, bool $refresh = false): array
{
if (!$refresh && self::$signingKeys !== null) {
return self::$signingKeys;
}
$keys = [];
try {
$keys = $vcs->getSigningKeys($refresh);
} catch (\Throwable $e) {
Console::warning('Failed to fetch Origin signing keys: ' . $e->getMessage());
}
// Never cache an empty set - a fetch hiccup would reject deliveries
// until the worker restarts.
if (!empty($keys)) {
self::$signingKeys = $keys;
}
return self::$signingKeys ?? [];
}
protected function handleInstallationEvent(
array $parsedPayload,
Database $dbForPlatform,
Authorization $authorization,
callable $getProjectDB,
) {
if ($parsedPayload['action'] !== 'deleted') {
return;
}
$providerInstallationId = $parsedPayload['installationId'];
$installationCursor = null;
do {
$installationQueries = [
Query::equal('providerInstallationId', [$providerInstallationId]),
Query::equal('provider', ['origin']),
Query::limit(1000),
];
if ($installationCursor !== null) {
$installationQueries[] = Query::cursorAfter($installationCursor);
}
$installations = $authorization->skip(fn () => $dbForPlatform->find('installations', $installationQueries));
foreach ($installations as $installation) {
$projectId = $installation->getAttribute('projectId', '');
$project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId));
if (!$project->isEmpty() && $this->isProjectInCurrentRegion($project)) {
$dbForProject = $getProjectDB($project);
foreach (['functions', 'sites'] as $collection) {
$cursor = null;
do {
$queries = [
Query::equal('installationInternalId', [$installation->getSequence()]),
Query::limit(1000),
];
if ($cursor !== null) {
$queries[] = Query::cursorAfter($cursor);
}
$resources = $authorization->skip(fn () => $dbForProject->find($collection, $queries));
foreach ($resources as $resource) {
$authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), new Document([
'installationId' => '',
'installationInternalId' => '',
'providerRepositoryId' => '',
'providerBranch' => '',
'providerSilentMode' => false,
'providerRootDirectory' => '',
'repositoryId' => '',
'repositoryInternalId' => '',
])));
}
$cursor = count($resources) === 1000 ? $resources[array_key_last($resources)] : null;
} while ($cursor !== null);
}
}
$cursor = null;
do {
$queries = [
Query::equal('installationInternalId', [$installation->getSequence()]),
Query::limit(1000),
];
if ($cursor !== null) {
$queries[] = Query::cursorAfter($cursor);
}
$repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', $queries));
foreach ($repositories as $repository) {
$authorization->skip(fn () => $dbForPlatform->deleteDocument('repositories', $repository->getId()));
}
$cursor = count($repositories) === 1000 ? $repositories[array_key_last($repositories)] : null;
} while ($cursor !== null);
$authorization->skip(fn () => $dbForPlatform->deleteDocument('installations', $installation->getId()));
}
$installationCursor = count($installations) === 1000 ? $installations[array_key_last($installations)] : null;
} while ($installationCursor !== null);
}
private function isProjectInCurrentRegion(Document $project): bool
{
try {
$dsn = new DSN($project->getAttribute('database'));
$databaseName = $dsn->getHost();
} catch (\InvalidArgumentException) {
$databaseName = $project->getAttribute('database');
}
$databases = Config::getParam('pools-database', []);
if (!\in_array($databaseName, $databases)) {
Console::warning("Skipping project {$project->getId()}: database '{$databaseName}' is not part of region " . System::getEnv('_APP_REGION'));
return false;
}
return true;
}
private function handlePushEvent(
array $parsedPayload,
VcsFactory $vcsFactory,
Database $dbForPlatform,
Authorization $authorization,
Bus $bus,
callable $getProjectDB,
array $platform,
callable $deploymentsFactory,
) {
$providerBranchDeleted = $parsedPayload['branchDeleted'] ?? false;
$providerBranch = $parsedPayload['branch'] ?? '';
$providerBranchUrl = $parsedPayload['branchUrl'] ?? '';
$providerRepositoryId = $parsedPayload['repositoryId'] ?? '';
$providerRepositoryName = $parsedPayload['repositoryName'] ?? '';
$providerInstallationId = $parsedPayload['installationId'] ?? '';
$providerRepositoryUrl = $parsedPayload['repositoryUrl'] ?? '';
$providerCommitHash = $parsedPayload['commitHash'] ?? '';
$providerRepositoryOwner = $parsedPayload['owner'] ?? '';
$providerCommitAuthorName = $parsedPayload['headCommitAuthorName'] ?? '';
$providerCommitAuthorEmail = $parsedPayload['headCommitAuthorEmail'] ?? '';
$providerCommitAuthorUrl = $parsedPayload['authorUrl'] ?? '';
$providerCommitMessage = $parsedPayload['headCommitMessage'] ?? '';
$providerCommitUrl = $parsedPayload['headCommitUrl'] ?? '';
Span::add('vcs.origin.event.repo.id', $providerRepositoryId);
Span::add('vcs.origin.event.repo.name', $providerRepositoryName);
Span::add('vcs.origin.event.branch', $providerBranch);
Span::add('vcs.origin.event.installation.id', $providerInstallationId);
$vcs = $vcsFactory->fromInstallation(new Document([
'provider' => 'origin',
'providerInstallationId' => $providerInstallationId,
]));
// Find associated repositories
$repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [
Query::equal('providerRepositoryId', [$providerRepositoryId]),
Query::limit(100),
]));
// Create new deployment only on push (not committed by us) and not when branch is deleted
if (!\in_array($providerCommitAuthorEmail, [APP_VCS_GITHUB_EMAIL, APP_VCS_ORIGIN_EMAIL], true) && !$providerBranchDeleted) {
$providerAffectedFiles = $parsedPayload['affectedFiles'] ?? [];
$this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $bus, $getProjectDB, $platform, $deploymentsFactory);
}
}
private function handlePullRequestEvent(
array $parsedPayload,
VcsFactory $vcsFactory,
Database $dbForPlatform,
Authorization $authorization,
Bus $bus,
callable $getProjectDB,
array $platform,
callable $deploymentsFactory,
) {
$action = $parsedPayload['action'] ?? '';
if ($action == 'opened' || $action == 'reopened' || $action == 'synchronize') {
$providerBranch = $parsedPayload['branch'] ?? '';
$providerBranchUrl = $parsedPayload['branchUrl'] ?? '';
$providerRepositoryId = $parsedPayload['repositoryId'] ?? '';
$providerRepositoryName = $parsedPayload['repositoryName'] ?? '';
$providerInstallationId = $parsedPayload['installationId'] ?? '';
$providerRepositoryUrl = $parsedPayload['repositoryUrl'] ?? '';
$providerPullRequestId = $parsedPayload['pullRequestNumber'] ?? '';
$providerCommitHash = $parsedPayload['commitHash'] ?? '';
$providerRepositoryOwner = $parsedPayload['owner'] ?? '';
$external = $parsedPayload['external'] ?? false;
$providerCommitUrl = $parsedPayload['headCommitUrl'] ?? '';
$providerCommitAuthorUrl = $parsedPayload['authorUrl'] ?? '';
Span::add('vcs.origin.event.repo.id', $providerRepositoryId);
Span::add('vcs.origin.event.repo.name', $providerRepositoryName);
Span::add('vcs.origin.event.branch', $providerBranch);
Span::add('vcs.origin.event.installation.id', $providerInstallationId);
// Ignore sync for non-external. We handle it in push webhook.
// Origin has no fork model, so every pull request is non-external.
if (!$external && $action == 'synchronize') {
return;
}
$vcs = $vcsFactory->fromInstallation(new Document([
'provider' => 'origin',
'providerInstallationId' => $providerInstallationId,
]));
try {
$commitDetails = $vcs->getCommit($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash);
} catch (\Throwable $e) {
Console::warning("Failed to fetch commit '{$providerCommitHash}': " . $e->getMessage());
$commitDetails = [];
}
$providerCommitAuthor = $commitDetails['commitAuthor'] ?? '';
$providerCommitMessage = $commitDetails['commitMessage'] ?? '';
$prFiles = $vcs->getPullRequestFiles($providerRepositoryOwner, $providerRepositoryName, (int) $providerPullRequestId);
$providerAffectedFiles = [
...array_column($prFiles, 'filename'),
// Only renamed files carry a previous filename; skip missing values from other file changes.
...array_filter(array_column($prFiles, 'previousFilename'))
];
$repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [
Query::equal('providerRepositoryId', [$providerRepositoryId]),
Query::orderDesc('$createdAt')
]));
$this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, \strval($providerPullRequestId), $providerAffectedFiles, $external, $dbForPlatform, $authorization, $bus, $getProjectDB, $platform, $deploymentsFactory);
}
// No cleanup on close: Origin pull requests are never external, so no
// authorized-contributor entries accumulate on the repository.
}
}
@@ -25,6 +25,9 @@ use Appwrite\Platform\Modules\VCS\Http\Installations\Repositories\Detections\Cre
use Appwrite\Platform\Modules\VCS\Http\Installations\Repositories\Get as GetRepository;
use Appwrite\Platform\Modules\VCS\Http\Installations\Repositories\XList as ListRepositories;
use Appwrite\Platform\Modules\VCS\Http\Installations\XList as ListInstallations;
use Appwrite\Platform\Modules\VCS\Http\Origin\Authorize\Get as GetOriginAuthorize;
use Appwrite\Platform\Modules\VCS\Http\Origin\Callback\Get as GetOriginCallback;
use Appwrite\Platform\Modules\VCS\Http\Origin\Events\Create as CreateOriginEvent;
use Utopia\Platform\Service;
class Http extends Service
@@ -50,6 +53,10 @@ class Http extends Service
$this->addAction(GetBitbucketAuthorize::getName(), new GetBitbucketAuthorize());
$this->addAction(GetBitbucketCallback::getName(), new GetBitbucketCallback());
// Origin Authorization & Callback
$this->addAction(GetOriginAuthorize::getName(), new GetOriginAuthorize());
$this->addAction(GetOriginCallback::getName(), new GetOriginCallback());
// Installations
$this->addAction(GetInstallation::getName(), new GetInstallation());
$this->addAction(ListInstallations::getName(), new ListInstallations());
@@ -69,5 +76,6 @@ class Http extends Service
$this->addAction(CreateGiteaEvent::getName(), new CreateGiteaEvent());
$this->addAction(CreateGitlabEvent::getName(), new CreateGitlabEvent());
$this->addAction(CreateBitbucketEvent::getName(), new CreateBitbucketEvent());
$this->addAction(CreateOriginEvent::getName(), new CreateOriginEvent());
}
}
+7
View File
@@ -17,6 +17,13 @@ class InstallationTokens
*/
public function refreshForInstallation(Document $installation, Database $dbForPlatform, Factory $vcsFactory): Document
{
// Only personal installations carry OAuth tokens to refresh. An app
// installation (a GitHub App, an Origin app) authenticates per request
// with an app-signed JWT and may have no OAuth2 client to resolve at all.
if (!$installation->getAttribute('personal', false)) {
return $installation;
}
$provider = $installation->getAttribute('provider', 'github');
return $this->refresh($installation, $dbForPlatform, $vcsFactory->oauth2FromProvider($provider));
+20 -1
View File
@@ -31,7 +31,11 @@ final class FactoryTest extends TestCase
foreach ($registry as $key => $entry) {
$this->assertTrue(\is_subclass_of($entry['adapter'], Git::class), "Adapter for '{$key}' must extend Git");
$this->assertIsCallable($entry['oauth2'], "OAuth2 builder for '{$key}' must be callable");
// A provider with an OAuth2 user flow registers a builder; an
// app-only provider (Origin) deliberately registers none.
if (isset($entry['oauth2'])) {
$this->assertIsCallable($entry['oauth2'], "OAuth2 builder for '{$key}' must be callable");
}
$this->assertNotEmpty($entry['variables'], "Variables missing for '{$key}'");
foreach ($entry['variables'] as $name => $variable) {
$this->assertStringStartsWith('_APP_VCS_', $variable['envVariable'] ?? '', "Env variable for '{$name}' missing or invalid for '{$key}'");
@@ -39,6 +43,21 @@ final class FactoryTest extends TestCase
}
}
public function testOriginRegistersNoOAuth2(): void
{
// Origin has no OAuth2 user flow: installs are approved on Cursor and
// confirmed by a signed receipt the adapter verifies itself, so the
// registry deliberately carries no builder and oauth2FromProvider()
// fails loudly instead of handing out a half-configured client.
$registry = Config::getParam('vcs', []);
$this->assertArrayHasKey('origin', $registry);
$this->assertArrayNotHasKey('oauth2', $registry['origin']);
$factory = new Factory($this->cache(), $registry);
$this->expectException(Exception::class);
$factory->oauth2FromProvider('origin');
}
public function testOauth2FromProviderUnknownThrows(): void
{
$factory = new Factory($this->cache(), ['github' => $this->githubEntry()]);