From 99f6cdd3f6b9d5d77bdf5b3b1aa00cd43a74e3d7 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 8 Jul 2026 19:27:56 +0530 Subject: [PATCH 01/52] refactor: replace gitHub DI resource with adapter-driven Vcs\Factory Replaces the hardcoded 'gitHub' resource (new GitHub($cache)) with a 'vcsFactory' resource backed by Appwrite\Vcs\Factory and a provider registry (app/config/vcs.php), mirroring the databaseFactory pattern. - Factory only builds adapters (no DB access): fromInstallation() returns a ready adapter, fromProvider() an uninitialized one for webhook validation and app callbacks. - initializeVariables() is called with both credential sets (app credentials + personal tokens); each adapter uses the set its auth scheme needs, so no auth-type branching exists anywhere. - Endpoints keep identical behavior: same routes, owner resolution via adapter->getOwnerName(), GitHub-specific URLs/headers untouched. - Server-side branch search stays GitHub-only behind an instanceof guard until adapters gain listBranches signature parity upstream. --- app/config/vcs.php | 23 ++++ app/controllers/mock.php | 13 +- app/init/configs.php | 1 + app/init/resources.php | 4 +- .../Platform/Modules/Compute/Base.php | 28 ++-- .../Modules/Console/Http/Variables/Get.php | 16 ++- .../Http/Deployments/Template/Create.php | 11 +- .../Functions/Http/Deployments/Vcs/Create.php | 8 +- .../Functions/Http/Functions/Create.php | 8 +- .../Functions/Http/Functions/Update.php | 8 +- .../Modules/Functions/Workers/Builds.php | 46 +++---- .../Http/Deployments/Template/Create.php | 11 +- .../Sites/Http/Deployments/Vcs/Create.php | 8 +- .../Modules/Sites/Http/Sites/Update.php | 8 +- .../Http/GitHub/Authorize/External/Update.php | 23 ++-- .../Modules/VCS/Http/GitHub/Callback/Get.php | 13 +- .../Modules/VCS/Http/GitHub/Deployment.php | 34 ++--- .../Modules/VCS/Http/GitHub/Events/Create.php | 34 ++--- .../Repositories/Branches/XList.php | 21 +-- .../Repositories/Contents/Get.php | 16 +-- .../Installations/Repositories/Create.php | 14 +- .../Repositories/Detections/Create.php | 25 ++-- .../Http/Installations/Repositories/Get.php | 21 ++- .../Http/Installations/Repositories/XList.php | 29 ++-- src/Appwrite/Vcs/Factory.php | 126 ++++++++++++++++++ tests/unit/Vcs/FactoryTest.php | 117 ++++++++++++++++ 26 files changed, 459 insertions(+), 207 deletions(-) create mode 100644 app/config/vcs.php create mode 100644 src/Appwrite/Vcs/Factory.php create mode 100644 tests/unit/Vcs/FactoryTest.php diff --git a/app/config/vcs.php b/app/config/vcs.php new file mode 100644 index 0000000000..1f76c4d86e --- /dev/null +++ b/app/config/vcs.php @@ -0,0 +1,23 @@ + [ + 'enabled' => true, + 'adapter' => GitHub::class, + 'envVariables' => [ + 'APP_NAME' => '_APP_VCS_GITHUB_APP_NAME', + 'PRIVATE_KEY' => '_APP_VCS_GITHUB_PRIVATE_KEY', + 'APP_ID' => '_APP_VCS_GITHUB_APP_ID', + 'CLIENT_ID' => '_APP_VCS_GITHUB_CLIENT_ID', + 'CLIENT_SECRET' => '_APP_VCS_GITHUB_CLIENT_SECRET', + 'WEBHOOK_SECRET' => '_APP_VCS_GITHUB_WEBHOOK_SECRET', + ], + 'requiredEnvVariables' => ['APP_NAME', 'PRIVATE_KEY', 'APP_ID', 'CLIENT_ID', 'CLIENT_SECRET'], + ], +]; diff --git a/app/controllers/mock.php b/app/controllers/mock.php index fcb17da717..a2170ab24f 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -5,6 +5,7 @@ global $utopia, $request, $response; use Appwrite\Extend\Exception; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; @@ -18,7 +19,6 @@ use Utopia\Locale\Locale; use Utopia\System\System; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; Http::get('/v1/mock/tests/general/oauth2') ->desc('OAuth Login') @@ -227,11 +227,11 @@ Http::get('/v1/mock/github/callback') ->label('docs', false) ->param('providerInstallationId', '', new UID(), 'GitHub installation ID') ->param('projectId', '', new UID(), 'Project ID of the project where app is to be installed') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('project') ->inject('response') ->inject('dbForPlatform') - ->action(function (string $providerInstallationId, string $projectId, GitHub $github, Document $project, Response $response, Database $dbForPlatform) { + ->action(function (string $providerInstallationId, string $projectId, Factory $vcsFactory, Document $project, Response $response, Database $dbForPlatform) { $isDevelopment = System::getEnv('_APP_ENV', 'development') === 'development'; if (!$isDevelopment) { @@ -249,10 +249,9 @@ Http::get('/v1/mock/github/callback') throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing provider installation ID'); } - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); - $owner = $github->getOwnerName($providerInstallationId); + $vcs = $vcsFactory->fromProvider('github'); + $vcs->initializeVariables($providerInstallationId, System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'), System::getEnv('_APP_VCS_GITHUB_APP_ID')); + $owner = $vcs->getOwnerName($providerInstallationId); $projectInternalId = $project->getSequence(); diff --git a/app/init/configs.php b/app/init/configs.php index f45ee1a7fe..c335329457 100644 --- a/app/init/configs.php +++ b/app/init/configs.php @@ -15,6 +15,7 @@ Config::load('auth', __DIR__ . '/../config/auth.php', $configAdapter); Config::load('protocols', __DIR__ . '/../config/protocols.php', $configAdapter); Config::load('errors', __DIR__ . '/../config/errors.php', $configAdapter); Config::load('oAuthProviders', __DIR__ . '/../config/oAuthProviders.php', $configAdapter); +Config::load('vcs', __DIR__ . '/../config/vcs.php', $configAdapter); Config::load('sdks', __DIR__ . '/../config/sdks.php', $configAdapter); Config::load('platform', __DIR__ . '/../config/platform.php', $configAdapter); Config::load('console', __DIR__ . '/../config/console.php', $configAdapter); diff --git a/app/init/resources.php b/app/init/resources.php index 019bdf1e34..dba6816701 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -17,6 +17,7 @@ use Appwrite\Event\Publisher\Screenshot as ScreenshotPublisher; use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl; +use Appwrite\Vcs\Factory as VcsFactory; use Executor\Executor; use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimitRedis; use Utopia\Cache\Adapter\Pool as CachePool; @@ -45,7 +46,6 @@ use Utopia\Storage\Storage; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; use Utopia\Telemetry\Adapter\None as NoTelemetry; -use Utopia\VCS\Adapter\Git\GitHub as VcsGitHub; global $register; global $container; @@ -346,7 +346,7 @@ $container->set('servers', function () { $container->set('promiseAdapter', fn ($register) => $register->get('promiseAdapter'), ['register']); -$container->set('gitHub', fn (Cache $cache) => new VcsGitHub($cache), ['cache']); +$container->set('vcsFactory', fn (Cache $cache) => new VcsFactory($cache), ['cache']); $container->set('plan', fn () => []); diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index 41463859f3..a50ae92f11 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -20,7 +20,7 @@ use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Http\Adapter\Swoole\Request; use Utopia\System\System; -use Utopia\VCS\Adapter\Git\GitHub; +use Utopia\VCS\Adapter\Git; use Utopia\VCS\Exception\RepositoryNotFound; class Base extends Action @@ -67,21 +67,18 @@ class Base extends Action return $allowedSpecifications[0]; } - public function redeployVcsFunction(Request $request, Document $function, Document $project, Document $installation, Database $dbForProject, BuildPublisher $publisherForBuilds, Document $template, GitHub $github, bool $activate, array $platform = [], string $referenceType = 'branch', string $reference = ''): Document + public function redeployVcsFunction(Request $request, Document $function, Document $project, Document $installation, Database $dbForProject, BuildPublisher $publisherForBuilds, Document $template, Git $vcs, bool $activate, array $platform = [], string $referenceType = 'branch', string $reference = ''): Document { $deploymentId = ID::unique(); $entrypoint = $function->getAttribute('entrypoint', ''); $providerInstallationId = $installation->getAttribute('providerInstallationId', ''); - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); if (empty($providerInstallationId)) { throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); - $owner = $github->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId); $providerRepositoryId = $function->getAttribute('providerRepositoryId', ''); try { - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $repositoryName = $vcs->getRepositoryName($providerRepositoryId); if (empty($repositoryName)) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } @@ -98,13 +95,13 @@ class Base extends Action $providerBranch = empty($reference) ? $function->getAttribute('providerBranch', 'main') : $reference; $branchUrl = "https://github.com/$owner/$repositoryName/tree/$providerBranch"; try { - $commitDetails = $github->getLatestCommit($owner, $repositoryName, $providerBranch); + $commitDetails = $vcs->getLatestCommit($owner, $repositoryName, $providerBranch); } catch (\Throwable $error) { // Ignore; deployment can continue } } elseif ($referenceType === 'commit') { try { - $commitDetails = $github->getCommit($owner, $repositoryName, $reference); + $commitDetails = $vcs->getCommit($owner, $repositoryName, $reference); } catch (\Throwable $error) { // Ignore; deployment can continue } @@ -163,20 +160,17 @@ class Base extends Action return $deployment; } - public function redeployVcsSite(Request $request, Document $site, Document $project, Document $installation, Database $dbForProject, Database $dbForPlatform, BuildPublisher $publisherForBuilds, Document $template, GitHub $github, bool $activate, Authorization $authorization, array $platform, string $referenceType = 'branch', string $reference = ''): Document + public function redeployVcsSite(Request $request, Document $site, Document $project, Document $installation, Database $dbForProject, Database $dbForPlatform, BuildPublisher $publisherForBuilds, Document $template, Git $vcs, bool $activate, Authorization $authorization, array $platform, string $referenceType = 'branch', string $reference = ''): Document { $deploymentId = ID::unique(); $providerInstallationId = $installation->getAttribute('providerInstallationId', ''); - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); if (empty($providerInstallationId)) { throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); - $owner = $github->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId); $providerRepositoryId = $site->getAttribute('providerRepositoryId', ''); try { - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $repositoryName = $vcs->getRepositoryName($providerRepositoryId); if (empty($repositoryName)) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } @@ -193,13 +187,13 @@ class Base extends Action $providerBranch = empty($reference) ? $site->getAttribute('providerBranch', 'main') : $reference; $branchUrl = "https://github.com/$owner/$repositoryName/tree/$providerBranch"; try { - $commitDetails = $github->getLatestCommit($owner, $repositoryName, $providerBranch); + $commitDetails = $vcs->getLatestCommit($owner, $repositoryName, $providerBranch); } catch (\Throwable $error) { // Ignore; deployment can continue } } elseif ($referenceType === 'commit') { try { - $commitDetails = $github->getCommit($owner, $repositoryName, $reference); + $commitDetails = $vcs->getCommit($owner, $repositoryName, $reference); } catch (\Throwable $error) { // Ignore; deployment can continue } diff --git a/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php index d39049a409..f1beaeb276 100644 --- a/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php +++ b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php @@ -7,6 +7,7 @@ use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Domains\Domain; @@ -46,14 +47,19 @@ class Get extends Action ], contentType: ContentType::JSON )) + ->inject('vcsFactory') ->inject('response') ->inject('platform') ->inject('dbForProject') ->callback($this->action(...)); } - public function action(Response $response, array $platform, Database $dbForProject) - { + public function action( + Factory $vcsFactory, + Response $response, + array $platform, + Database $dbForProject + ) { $validator = new Domain(System::getEnv('_APP_DOMAIN_TARGET_CNAME')); $isCNAMEValid = !empty(System::getEnv('_APP_DOMAIN_TARGET_CNAME', '')) && $validator->isKnown() && !$validator->isTest(); @@ -66,11 +72,7 @@ class Get extends Action $isDomainEnabled = $isAAAAValid || $isAValid || $isCNAMEValid; - $isVcsEnabled = !empty(System::getEnv('_APP_VCS_GITHUB_APP_NAME', '')) - && !empty(System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY', '')) - && !empty(System::getEnv('_APP_VCS_GITHUB_APP_ID', '')) - && !empty(System::getEnv('_APP_VCS_GITHUB_CLIENT_ID', '')) - && !empty(System::getEnv('_APP_VCS_GITHUB_CLIENT_SECRET', '')); + $isVcsEnabled = !empty($vcsFactory->getProviders()); $isAssistantEnabled = !empty(System::getEnv('_APP_ASSISTANT_OPENAI_API_KEY', '')); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php index e710da13cc..77606ba334 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php @@ -12,6 +12,8 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; +use Utopia\VCS\Adapter\Git; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -25,7 +27,6 @@ use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; class Create extends Base { @@ -80,7 +81,7 @@ class Create extends Base ->inject('queueForEvents') ->inject('project') ->inject('publisherForBuilds') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -101,7 +102,7 @@ class Create extends Base Event $queueForEvents, Document $project, BuildPublisher $publisherForBuilds, - GitHub $github, + Factory $vcsFactory, Authorization $authorization, array $platform ) { @@ -134,7 +135,7 @@ class Create extends Base dbForProject: $dbForProject, publisherForBuilds: $publisherForBuilds, template: $template, - github: $github, + vcs: $vcsFactory->fromInstallation($installation), activate: $activate, platform: $platform, referenceType: $type, @@ -170,7 +171,7 @@ class Create extends Base 'providerRepositoryOwner' => $owner, 'providerRepositoryUrl' => $repositoryUrl, 'providerBranchUrl' => $branchUrl, - 'providerBranch' => $type == GitHub::CLONE_TYPE_BRANCH ? $reference : '', + 'providerBranch' => $type == Git::CLONE_TYPE_BRANCH ? $reference : '', 'type' => 'vcs', 'activate' => $activate, ])); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php index 65919f328c..5fe33354dd 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php @@ -11,6 +11,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\UID; @@ -20,7 +21,6 @@ use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; class Create extends Base { @@ -73,7 +73,7 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('publisherForBuilds') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('platform') ->callback($this->action(...)); } @@ -90,7 +90,7 @@ class Create extends Base Document $project, Event $queueForEvents, BuildPublisher $publisherForBuilds, - GitHub $github, + Factory $vcsFactory, array $platform, ) { $function = $dbForProject->getDocument('functions', $functionId); @@ -115,7 +115,7 @@ class Create extends Base dbForProject: $dbForProject, publisherForBuilds: $publisherForBuilds, template: $template, - github: $github, + vcs: $vcsFactory->fromInstallation($installation), activate: $activate, platform: $platform, reference: $reference, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php index 9de42c369c..bbb6ecf265 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -20,6 +20,7 @@ use Appwrite\Task\Validator\Cron; use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model\Rule; +use Appwrite\Vcs\Factory; use Utopia\Abuse\Abuse; use Utopia\Config\Config; use Utopia\Database\Database; @@ -41,7 +42,6 @@ use Utopia\Validator\Boolean; use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; class Create extends Base { @@ -128,7 +128,7 @@ class Create extends Base ->inject('publisherForFunctions') ->inject('dbForPlatform') ->inject('request') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -172,7 +172,7 @@ class Create extends Base FunctionPublisher $publisherForFunctions, Database $dbForPlatform, Request $request, - GitHub $github, + Factory $vcsFactory, Authorization $authorization, array $platform ) { @@ -344,7 +344,7 @@ class Create extends Base dbForProject: $dbForProject, publisherForBuilds: $publisherForBuilds, template: $template, - github: $github, + vcs: $vcsFactory->fromInstallation($installation), activate: true, platform: $platform, reference: $providerBranch, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php index 9f9e10ddca..f275a15aee 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -13,6 +13,7 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Task\Validator\Cron; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Executor\Executor; use Utopia\Config\Config; use Utopia\Database\Database; @@ -34,7 +35,6 @@ use Utopia\Validator\Nullable; use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; class Update extends Base { @@ -112,7 +112,7 @@ class Update extends Base ->inject('queueForEvents') ->inject('publisherForBuilds') ->inject('dbForPlatform') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('executor') ->inject('authorization') ->inject('platform') @@ -149,7 +149,7 @@ class Update extends Base Event $queueForEvents, BuildPublisher $publisherForBuilds, Database $dbForPlatform, - GitHub $github, + Factory $vcsFactory, Executor $executor, Authorization $authorization, array $platform @@ -309,7 +309,7 @@ class Update extends Base // Redeploy logic if (!$isConnected && !empty($providerRepositoryId)) { - $this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $github, true, $platform); + $this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsFactory->fromInstallation($installation), true, $platform); } // Inform scheduler if function is still active diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index f1ec93bc86..2f6153fd90 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -16,12 +16,12 @@ use Appwrite\Filter\BranchDomain as BranchDomainFilter; use Appwrite\Usage\Context; use Appwrite\Utopia\Response\Model\Deployment; use Appwrite\Vcs\Comment; +use Appwrite\Vcs\Factory; use Exception; use Executor\Exception as ExecutorException; use Executor\Exception\Timeout as ExecutorTimeout; use Executor\Executor; use Swoole\Coroutine as Co; -use Utopia\Cache\Cache; use Utopia\Config\Config; use Utopia\Console; use Utopia\Database\Database; @@ -43,7 +43,7 @@ use Utopia\Span\Span; use Utopia\Storage\Device; use Utopia\Storage\Device\Local; use Utopia\System\System; -use Utopia\VCS\Adapter\Git\GitHub; +use Utopia\VCS\Adapter\Git; class Builds extends Action { @@ -90,7 +90,7 @@ class Builds extends Action ->inject('queueForRealtime') ->inject('usage') ->inject('publisherForUsage') - ->inject('cache') + ->inject('vcsFactory') ->inject('dbForProject') ->inject('deviceForFunctions') ->inject('deviceForSites') @@ -116,7 +116,7 @@ class Builds extends Action Realtime $queueForRealtime, Context $usage, UsagePublisher $publisherForUsage, - Cache $cache, + Factory $vcsFactory, Database $dbForProject, Device $deviceForFunctions, Device $deviceForSites, @@ -146,7 +146,7 @@ class Builds extends Action switch ($type) { case BUILD_TYPE_DEPLOYMENT: case BUILD_TYPE_RETRY: - $github = new GitHub($cache); + $vcs = $vcsFactory->fromProvider('github'); $this->buildDeployment( $deviceForFunctions, $deviceForSites, @@ -160,7 +160,7 @@ class Builds extends Action $publisherForUsage, $dbForPlatform, $dbForProject, - $github, + $vcs, $project, $resource, $deployment, @@ -196,7 +196,7 @@ class Builds extends Action UsagePublisher $publisherForUsage, Database $dbForPlatform, Database $dbForProject, - GitHub $github, + Git $vcs, Document $project, Document $resource, Document $deployment, @@ -317,7 +317,7 @@ class Builds extends Action $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); try { - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs->initializeVariables($providerInstallationId, $privateKey, $githubAppId); } catch (\Exception $e) { if ($e->getCode() === 404 && $resource->getAttribute('installationId', '') === $installationId) { @@ -350,7 +350,7 @@ class Builds extends Action // Clone template repo $tmpTemplateDirectory = '/tmp/builds/' . $deploymentId . '-template'; - $gitCloneCommandForTemplate = $github->generateCloneCommand($templateOwnerName, $templateRepositoryName, $templateReferenceValue, $templateReferenceType, $tmpTemplateDirectory, $templateRootDirectory); + $gitCloneCommandForTemplate = $vcs->generateCloneCommand($templateOwnerName, $templateRepositoryName, $templateReferenceValue, $templateReferenceType, $tmpTemplateDirectory, $templateRootDirectory); $exit = Console::execute($gitCloneCommandForTemplate, '', $stdout, $stderr); @@ -408,8 +408,8 @@ class Builds extends Action $rootDirectory = \ltrim($rootDirectory, '.'); $rootDirectory = \ltrim($rootDirectory, '/'); - $owner = $github->getOwnerName($providerInstallationId); - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $owner = $vcs->getOwnerName($providerInstallationId); + $repositoryName = $vcs->getRepositoryName($providerRepositoryId); $cloneOwner = $deployment->getAttribute('providerRepositoryOwner', $owner); $cloneRepository = $deployment->getAttribute('providerRepositoryName', $repositoryName); @@ -418,13 +418,13 @@ class Builds extends Action $commitHash = $deployment->getAttribute('providerCommitHash', ''); $cloneVersion = $branchName; - $cloneType = GitHub::CLONE_TYPE_BRANCH; + $cloneType = Git::CLONE_TYPE_BRANCH; if (! empty($commitHash)) { $cloneVersion = $commitHash; - $cloneType = GitHub::CLONE_TYPE_COMMIT; + $cloneType = Git::CLONE_TYPE_COMMIT; } - $gitCloneCommand = $github->generateCloneCommand($cloneOwner, $cloneRepository, $cloneVersion, $cloneType, $tmpDirectory, $rootDirectory); + $gitCloneCommand = $vcs->generateCloneCommand($cloneOwner, $cloneRepository, $cloneVersion, $cloneType, $tmpDirectory, $rootDirectory); $stdout = ''; $stderr = ''; @@ -470,7 +470,7 @@ class Builds extends Action // Clone template repo $tmpTemplateDirectory = '/tmp/builds/' . $deploymentId . '/template'; - $gitCloneCommandForTemplate = $github->generateCloneCommand($templateOwnerName, $templateRepositoryName, $templateReferenceValue, $templateReferenceType, $tmpTemplateDirectory, $templateRootDirectory); + $gitCloneCommandForTemplate = $vcs->generateCloneCommand($templateOwnerName, $templateRepositoryName, $templateReferenceValue, $templateReferenceType, $tmpTemplateDirectory, $templateRootDirectory); $exit = Console::execute($gitCloneCommandForTemplate, '', $stdout, $stderr); if ($exit !== 0) { @@ -569,7 +569,7 @@ class Builds extends Action Span::add('build.source_size', $deployment->getAttribute('sourceSize')); - $this->runGitAction('processing', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); + $this->runGitAction('processing', $vcs, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); } Span::add('timings.source', \round(\microtime(true) - $phaseStart, 3)); @@ -598,7 +598,7 @@ class Builds extends Action ->trigger(); if ($isVcsEnabled) { - $this->runGitAction('building', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); + $this->runGitAction('building', $vcs, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); } $deploymentModel = new Deployment(); @@ -986,7 +986,7 @@ class Builds extends Action $resource = $this->updateLatestDeployment($dbForProject, $resource); if ($isVcsEnabled) { - $this->runGitAction('ready', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); + $this->runGitAction('ready', $vcs, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); } /** Set auto deploy */ @@ -1235,7 +1235,7 @@ class Builds extends Action ->trigger(); if ($isVcsEnabled) { - $this->runGitAction('failed', $github, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform, true); + $this->runGitAction('failed', $vcs, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform, true); } // Let the worker harness record internal errors via the span and logger. @@ -1470,7 +1470,7 @@ class Builds extends Action */ protected function runGitAction( string $status, - GitHub $github, + Git $vcs, string $providerCommitHash, string $owner, string $repositoryName, @@ -1525,7 +1525,7 @@ class Builds extends Action default => throw new \Exception('Invalid resource type') }; - $github->updateCommitStatus($repositoryName, $providerCommitHash, $owner, $state, $message, $providerTargetUrl, $name); + $vcs->updateCommitStatus($repositoryName, $providerCommitHash, $owner, $state, $message, $providerTargetUrl, $name); } if (! empty($commentId)) { @@ -1569,9 +1569,9 @@ class Builds extends Action } $comment = new Comment($platform); - $comment->parseComment($github->getComment($owner, $repositoryName, $commentId)); + $comment->parseComment($vcs->getComment($owner, $repositoryName, $commentId)); $comment->addBuild($project, $resource, $resourceType, $status, $deployment->getId(), ['type' => 'logs'], $previewUrl); - $github->updateComment($owner, $repositoryName, $commentId, $comment->generateComment()); + $vcs->updateComment($owner, $repositoryName, $commentId, $comment->generateComment()); } finally { $dbForPlatform->deleteDocument('vcsCommentLocks', $commentId); } diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index aebac55154..21b294d842 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php @@ -12,6 +12,8 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; +use Utopia\VCS\Adapter\Git; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -26,7 +28,6 @@ use Utopia\System\System; use Utopia\Validator\Boolean; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; class Create extends Base { @@ -81,7 +82,7 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('publisherForBuilds') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -102,7 +103,7 @@ class Create extends Base Document $project, Event $queueForEvents, BuildPublisher $publisherForBuilds, - GitHub $github, + Factory $vcsFactory, Authorization $authorization, array $platform ) { @@ -135,7 +136,7 @@ class Create extends Base dbForPlatform: $dbForPlatform, publisherForBuilds: $publisherForBuilds, template: $template, - github: $github, + vcs: $vcsFactory->fromInstallation($installation), activate: $activate, authorization: $authorization, platform: $platform @@ -178,7 +179,7 @@ class Create extends Base 'providerRepositoryOwner' => $owner, 'providerRepositoryUrl' => $repositoryUrl, 'providerBranchUrl' => $branchUrl, - 'providerBranch' => $type == GitHub::CLONE_TYPE_BRANCH ? $reference : '', + 'providerBranch' => $type == Git::CLONE_TYPE_BRANCH ? $reference : '', 'adapter' => $site->getAttribute('adapter', ''), 'fallbackFile' => $site->getAttribute('fallbackFile', ''), 'type' => 'vcs', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php index 16e4be4b36..986788bd0c 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php @@ -11,6 +11,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; @@ -21,7 +22,6 @@ use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; class Create extends Base { @@ -74,7 +74,7 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('publisherForBuilds') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -92,7 +92,7 @@ class Create extends Base Document $project, Event $queueForEvents, BuildPublisher $publisherForBuilds, - GitHub $github, + Factory $vcsFactory, Authorization $authorization, array $platform ) { @@ -119,7 +119,7 @@ class Create extends Base dbForPlatform: $dbForPlatform, publisherForBuilds: $publisherForBuilds, template: $template, - github: $github, + vcs: $vcsFactory->fromInstallation($installation), activate: $activate, authorization: $authorization, reference: $reference, diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php index 532c92a428..6604354436 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php @@ -11,6 +11,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Executor\Executor; use Utopia\Config\Config; use Utopia\Database\Database; @@ -29,7 +30,6 @@ use Utopia\Validator\Nullable; use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; class Update extends Base { @@ -108,7 +108,7 @@ class Update extends Base ->inject('queueForEvents') ->inject('publisherForBuilds') ->inject('dbForPlatform') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('executor') ->inject('platform') ->callback($this->action(...)); @@ -145,7 +145,7 @@ class Update extends Base Event $queueForEvents, BuildPublisher $publisherForBuilds, Database $dbForPlatform, - GitHub $github, + Factory $vcsFactory, Executor $executor, array $platform ) { @@ -309,7 +309,7 @@ class Update extends Base // Redeploy logic if (!$isConnected && !empty($providerRepositoryId)) { - $this->redeployVcsFunction($request, $site, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $github, true, $platform); + $this->redeployVcsFunction($request, $site, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsFactory->fromInstallation($installation), true, $platform); } $queueForEvents->setParam('siteId', $site->getId()); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php index 993740c61a..461a9d3b5c 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php @@ -10,14 +10,13 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Platform\Scope\HTTP; -use Utopia\System\System; use Utopia\Validator\Text; -use Utopia\VCS\Adapter\Git\GitHub; use Utopia\VCS\Exception\RepositoryNotFound; class Update extends Action @@ -54,7 +53,7 @@ class Update extends Action ->param('installationId', '', new Text(256), 'Installation Id') ->param('repositoryId', '', new Text(256), 'VCS Repository Id') ->param('providerPullRequestId', '', new Text(256), 'GitHub Pull Request Id') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('response') ->inject('project') ->inject('dbForPlatform') @@ -69,7 +68,7 @@ class Update extends Action string $installationId, string $repositoryId, string $providerPullRequestId, - GitHub $github, + Factory $vcsFactory, Response $response, Document $project, Database $dbForPlatform, @@ -101,22 +100,20 @@ class Update extends Action $repository = $authorization->skip(fn () => $dbForPlatform->updateDocument('repositories', $repository->getId(), new Document(['providerPullRequestIds' => $providerPullRequestIds]))); - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs = $vcsFactory->fromInstallation($installation); $repositories = [$repository]; $providerRepositoryId = $repository->getAttribute('providerRepositoryId'); try { - $providerRepositoryName = $github->getRepositoryName($providerRepositoryId); + $providerRepositoryName = $vcs->getRepositoryName($providerRepositoryId); } catch (RepositoryNotFound $e) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } - $owner = $github->getOwnerName($providerInstallationId); - $pullRequestResponse = $github->getPullRequest($owner, $providerRepositoryName, $providerPullRequestId); + $owner = $vcs->getOwnerName($providerInstallationId); + $pullRequestResponse = $vcs->getPullRequest($owner, $providerRepositoryName, $providerPullRequestId); $providerRepositoryUrl = $pullRequestResponse['head']['repo']['html_url'] ?? ''; $providerRepositoryOwner = $pullRequestResponse['head']['repo']['owner']['login'] ?? ''; @@ -124,20 +121,20 @@ class Update extends Action $providerBranchUrl = "$providerRepositoryUrl/tree/$providerBranch"; $providerCommitHash = $pullRequestResponse['head']['sha'] ?? ''; - $commitDetails = $github->getCommit($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash); + $commitDetails = $vcs->getCommit($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash); $providerCommitMessage = $commitDetails["commitMessage"] ?? ''; $providerCommitUrl = $commitDetails["commitUrl"] ?? ''; $providerCommitAuthor = $commitDetails["commitAuthor"] ?? ''; $providerCommitAuthorUrl = $commitDetails["commitAuthorUrl"] ?? ''; - $prFiles = $github->getPullRequestFiles($owner, $providerRepositoryName, $providerPullRequestId); + $prFiles = $vcs->getPullRequestFiles($owner, $providerRepositoryName, $providerPullRequestId); $providerAffectedFiles = [ ...array_column($prFiles, 'filename'), // Only renamed files include previous_filename; skip missing values from other file changes. ...array_filter(array_column($prFiles, 'previous_filename')) ]; - $this->createGitDeployments($github, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, $providerPullRequestId, $providerAffectedFiles, true, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); + $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, $providerPullRequestId, $providerAffectedFiles, true, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); $response->noContent(); } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index c5a8d8f43f..1f2029e86d 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -6,6 +6,7 @@ use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Appwrite\Extend\Exception; use Appwrite\Platform\Permission as AppwritePermission; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; @@ -15,7 +16,6 @@ use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; use Utopia\Validator\Text; -use Utopia\VCS\Adapter\Git\GitHub; class Get extends Action { @@ -40,7 +40,7 @@ class Get extends Action ->param('setup_action', '', new Text(256, 0), 'GitHub setup action type', true) ->param('state', '', new Text(2048), 'GitHub state. Contains info sent when starting authorization flow.', true) ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) - ->inject('gitHub') + ->inject('vcsFactory') ->inject('project') ->inject('response') ->inject('dbForPlatform') @@ -53,7 +53,7 @@ class Get extends Action string $setupAction, string $state, string $code, - GitHub $github, + Factory $vcsFactory, Document $project, Response $response, Database $dbForPlatform, @@ -101,10 +101,9 @@ class Get extends Action // Create / Update installation if (!empty($providerInstallationId)) { - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); - $owner = $github->getOwnerName($providerInstallationId); + $vcs = $vcsFactory->fromProvider('github'); + $vcs->initializeVariables($providerInstallationId, System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'), System::getEnv('_APP_VCS_GITHUB_APP_ID')); + $owner = $vcs->getOwnerName($providerInstallationId); $projectInternalId = $project->getSequence(); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php index d312cc3a56..adde2750ca 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php @@ -23,13 +23,13 @@ use Utopia\Span\Span; use Utopia\System\System; use Utopia\Validator\Contains; use Utopia\Validator\Globstar; -use Utopia\VCS\Adapter\Git\GitHub; +use Utopia\VCS\Adapter\Git; use Utopia\VCS\Exception\RepositoryNotFound; trait Deployment { protected function createGitDeployments( - GitHub $github, + Git $vcs, string $providerInstallationId, array $repositories, string $providerBranch, @@ -144,9 +144,9 @@ trait Deployment $activate = true; } - $owner = $github->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId); try { - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $repositoryName = $vcs->getRepositoryName($providerRepositoryId); } catch (RepositoryNotFound $e) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } @@ -233,10 +233,10 @@ trait Deployment // Wrap in try/finally to ensure lock file gets deleted try { $comment = new Comment($platform); - $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); + $comment->parseComment($vcs->getComment($owner, $repositoryName, $latestCommentId)); $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, $commentPreviewUrl); - $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); + $latestCommentId = \strval($vcs->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); } catch (\Throwable $e) { Console::warning("Failed to update PR comment '{$latestCommentId}': " . $e->getMessage()); } finally { @@ -246,7 +246,7 @@ trait Deployment } else { $comment = new Comment($platform); $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, $commentPreviewUrl); - $latestCommentId = \strval($github->createComment($owner, $repositoryName, $providerPullRequestId, $comment->generateComment())); + $latestCommentId = \strval($vcs->createComment($owner, $repositoryName, $providerPullRequestId, $comment->generateComment())); if (!empty($latestCommentId)) { $teamId = $project->getAttribute('teamId', ''); @@ -306,10 +306,10 @@ trait Deployment // Wrap in try/finally to ensure lock file gets deleted try { $comment = new Comment($platform); - $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); + $comment->parseComment($vcs->getComment($owner, $repositoryName, $latestCommentId)); $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, ''); - $latestCommentId = \strval($github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); + $latestCommentId = \strval($vcs->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment())); } catch (\Throwable $e) { Console::warning("Failed to update PR comment '{$latestCommentId}': " . $e->getMessage()); } finally { @@ -327,12 +327,12 @@ trait Deployment $providerRepositoryId = $repository->getAttribute('providerRepositoryId'); try { - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $repositoryName = $vcs->getRepositoryName($providerRepositoryId); } catch (RepositoryNotFound $e) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } - $owner = $github->getOwnerName($providerInstallationId); - $github->updateCommitStatus($repositoryName, $providerCommitHash, $owner, 'pending', $message, $authorizeUrl, $name); + $owner = $vcs->getOwnerName($providerInstallationId); + $vcs->updateCommitStatus($repositoryName, $providerCommitHash, $owner, 'pending', $message, $authorizeUrl, $name); continue; } @@ -526,9 +526,9 @@ trait Deployment if (!empty($previewUrl)) { $comment = new Comment($platform); - $comment->parseComment($github->getComment($owner, $repositoryName, $latestCommentId)); + $comment->parseComment($vcs->getComment($owner, $repositoryName, $latestCommentId)); $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, $previewUrl); - $github->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment()); + $vcs->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment()); } } catch (\Throwable $e) { Console::warning("Failed to update PR comment '{$latestCommentId}': " . $e->getMessage()); @@ -547,14 +547,14 @@ trait Deployment $providerRepositoryId = $repository->getAttribute('providerRepositoryId'); try { - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $repositoryName = $vcs->getRepositoryName($providerRepositoryId); } catch (RepositoryNotFound $e) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } - $owner = $github->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId); $providerTargetUrl = $protocol . '://' . $hostname . "/console/project-$region-$projectId/$resourceCollection/$resourceType-$resourceId"; - $github->updateCommitStatus($repositoryName, $providerCommitHash, $owner, 'pending', $message, $providerTargetUrl, $name); + $vcs->updateCommitStatus($repositoryName, $providerCommitHash, $owner, 'pending', $message, $providerTargetUrl, $name); } $queueName = $this->getBuildQueueName($project, $dbForPlatform, $authorization); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 53f67f34b0..75d45d8e6a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -8,6 +8,7 @@ use Appwrite\Platform\Action; use Appwrite\Platform\Modules\VCS\Http\GitHub\Deployment; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; @@ -16,6 +17,7 @@ use Utopia\Database\Validator\Authorization; use Utopia\Platform\Scope\HTTP; use Utopia\Span\Span; use Utopia\System\System; +use Utopia\VCS\Adapter\Git; use Utopia\VCS\Adapter\Git\GitHub; class Create extends Action @@ -36,7 +38,7 @@ class Create extends Action ->desc('Create event') ->groups(['api', 'vcs']) ->label('scope', 'public') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('request') ->inject('response') ->inject('dbForPlatform') @@ -48,7 +50,7 @@ class Create extends Action } public function action( - GitHub $github, + Factory $vcsFactory, Request $request, Response $response, Database $dbForPlatform, @@ -59,6 +61,8 @@ class Create extends Action ) { $this->preprocessEvent($request); + $vcs = $vcsFactory->fromProvider('github'); + $event = $request->getHeaderLine('x-github-event', ''); Span::add('vcs.github.event.name', $event); @@ -66,7 +70,7 @@ class Create extends Action $signature = $request->getHeaderLine('x-hub-signature-256', ''); $secretKey = System::getEnv('_APP_VCS_GITHUB_WEBHOOK_SECRET', ''); - $valid = empty($secretKey) ? true : $github->validateWebhookEvent($payload, $signature, $secretKey); + $valid = empty($secretKey) ? true : $vcs->validateWebhookEvent($payload, $signature, $secretKey); Span::add('vcs.github.event.signature.valid', $valid); if (!$valid) { @@ -75,12 +79,12 @@ class Create extends Action $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $parsedPayload = $github->getEvent($event, $payload); + $parsedPayload = $vcs->getEvent($event, $payload); match ($event) { - $github::EVENT_INSTALLATION => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), - $github::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $githubAppId, $privateKey, $github, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), - $github::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $privateKey, $githubAppId, $github, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + GitHub::EVENT_INSTALLATION => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), + GitHub::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $githubAppId, $privateKey, $vcs, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + GitHub::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $privateKey, $githubAppId, $vcs, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), default => null, }; @@ -181,7 +185,7 @@ class Create extends Action array $parsedPayload, string $githubAppId, string $privateKey, - GitHub $github, + Git $vcs, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, @@ -208,7 +212,7 @@ class Create extends Action Span::add('vcs.github.event.branch', $providerBranch); Span::add('vcs.github.event.installation.id', $providerInstallationId); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs->initializeVariables($providerInstallationId, $privateKey, $githubAppId); // Find associated repositories $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ @@ -219,7 +223,7 @@ class Create extends Action // Create new deployment only on push (not committed by us) and not when branch is deleted if ($providerCommitAuthorEmail !== APP_VCS_GITHUB_EMAIL && !$providerBranchDeleted) { $providerAffectedFiles = $parsedPayload['affectedFiles'] ?? []; - $this->createGitDeployments($github, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); + $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); } } @@ -227,7 +231,7 @@ class Create extends Action array $parsedPayload, string $privateKey, string $githubAppId, - GitHub $github, + Git $vcs, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, @@ -260,10 +264,10 @@ class Create extends Action return; } - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs->initializeVariables($providerInstallationId, $privateKey, $githubAppId); try { - $commitDetails = $github->getCommit($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash); + $commitDetails = $vcs->getCommit($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash); } catch (\Throwable $e) { Console::warning("Failed to fetch commit '{$providerCommitHash}': " . $e->getMessage()); $commitDetails = []; @@ -271,7 +275,7 @@ class Create extends Action $providerCommitAuthor = $commitDetails["commitAuthor"] ?? ''; $providerCommitMessage = $commitDetails["commitMessage"] ?? ''; - $prFiles = $github->getPullRequestFiles($providerRepositoryOwner, $providerRepositoryName, $providerPullRequestId); + $prFiles = $vcs->getPullRequestFiles($providerRepositoryOwner, $providerRepositoryName, $providerPullRequestId); $providerAffectedFiles = [ ...array_column($prFiles, 'filename'), // Only renamed files include previous_filename; skip missing values from other file changes. @@ -283,7 +287,7 @@ class Create extends Action Query::orderDesc('$createdAt') ])); - $this->createGitDeployments($github, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, $providerPullRequestId, $providerAffectedFiles, $external, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); + $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, $providerPullRequestId, $providerAffectedFiles, $external, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); } elseif ($action == "closed") { // Allowed external contributions cleanup diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php index c1a86c5597..2c8c4e2093 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php @@ -9,14 +9,14 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Database\Validator\Queries\Branches; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; +use Utopia\VCS\Adapter\Git\GitHub; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Query as QueryException; use Utopia\Database\Query; use Utopia\Platform\Scope\HTTP; -use Utopia\System\System; use Utopia\Validator\Text; -use Utopia\VCS\Adapter\Git\GitHub; use Utopia\VCS\Exception\RepositoryNotFound; class XList extends Action @@ -54,7 +54,7 @@ class XList extends Action ->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('queries', [], new Branches(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit, offset, cursorAfter, and cursorBefore', true) - ->inject('gitHub') + ->inject('vcsFactory') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -65,7 +65,7 @@ class XList extends Action string $providerRepositoryId, string $search, array $queries, - GitHub $github, + Factory $vcsFactory, Response $response, Database $dbForPlatform ) { @@ -82,13 +82,11 @@ class XList extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs = $vcsFactory->fromInstallation($installation); - $owner = $github->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId); try { - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $repositoryName = $vcs->getRepositoryName($providerRepositoryId); if (empty($repositoryName)) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } @@ -96,7 +94,10 @@ class XList extends Action throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } - $branches = $github->listBranches($owner, $repositoryName, search: $search); + // Server-side branch search is GitHub-specific until the adapters gain signature parity upstream. + $branches = $vcs instanceof GitHub + ? $vcs->listBranches($owner, $repositoryName, search: $search) + : $vcs->listBranches($owner, $repositoryName); $total = \count($branches); [ diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php index 89b38e7b79..598ec60a55 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php @@ -8,10 +8,10 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Platform\Scope\HTTP; -use Utopia\System\System; use Utopia\Validator\Text; use Utopia\VCS\Adapter\Git\GitHub; use Utopia\VCS\Exception\RepositoryNotFound; @@ -51,7 +51,7 @@ class Get extends Action ->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('providerRootDirectory', '', new Text(256, 0), 'Path to get contents of nested directory', true) ->param('providerReference', '', new Text(256, 0), 'Git reference (branch, tag, commit) to get contents from', true) - ->inject('gitHub') + ->inject('vcsFactory') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -62,7 +62,7 @@ class Get extends Action string $providerRepositoryId, string $providerRootDirectory, string $providerReference, - GitHub $github, + Factory $vcsFactory, Response $response, Database $dbForPlatform ) { @@ -73,13 +73,11 @@ class Get extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs = $vcsFactory->fromInstallation($installation); - $owner = $github->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId); try { - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $repositoryName = $vcs->getRepositoryName($providerRepositoryId); if (empty($repositoryName)) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } @@ -87,7 +85,7 @@ class Get extends Action throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } - $contents = $github->listRepositoryContents($owner, $repositoryName, $providerRootDirectory, $providerReference); + $contents = $vcs->listRepositoryContents($owner, $repositoryName, $providerRootDirectory, $providerReference); $vcsContents = []; foreach ($contents as $content) { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php index 1918e454a4..99fc01726a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -9,6 +9,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; @@ -17,7 +18,6 @@ use Utopia\Platform\Scope\HTTP; use Utopia\System\System; use Utopia\Validator\Boolean; use Utopia\Validator\Text; -use Utopia\VCS\Adapter\Git\GitHub; class Create extends Action { @@ -53,7 +53,7 @@ class Create extends Action ->param('installationId', '', new Text(256), 'Installation Id') ->param('name', '', new Text(256), 'Repository name (slug)') ->param('private', '', new Boolean(false), 'Mark repository public or private') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('user') ->inject('response') ->inject('dbForPlatform') @@ -64,7 +64,7 @@ class Create extends Action string $installationId, string $name, bool $private, - GitHub $github, + Factory $vcsFactory, Document $user, Response $response, Database $dbForPlatform @@ -128,13 +128,11 @@ class Create extends Action } } else { $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); - $owner = $github->getOwnerName($providerInstallationId); + $vcs = $vcsFactory->fromInstallation($installation); + $owner = $vcs->getOwnerName($providerInstallationId); try { - $repository = $github->createRepository($owner, $name, $private); + $repository = $vcs->createRepository($owner, $name, $private); } catch (Exception $exception) { throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, "GitHub failed to process the request: " . $exception->getMessage()); } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php index 4da5699a4e..9300e107eb 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php @@ -8,6 +8,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Swoole\Coroutine\WaitGroup; use Utopia\Config\Adapters\Dotenv as ConfigDotenv; use Utopia\Config\Config; @@ -48,10 +49,8 @@ use Utopia\Detector\Detector\Runtime; use Utopia\Detector\Detector\Strategy; use Utopia\Platform\Enum; use Utopia\Platform\Scope\HTTP; -use Utopia\System\System; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; use Utopia\VCS\Exception\FileNotFound; use Utopia\VCS\Exception\RepositoryNotFound; @@ -94,7 +93,7 @@ class Create extends Action ->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('type', '', new WhiteList(['runtime', 'framework']), 'Detector type. Must be one of the following: runtime, framework', enum: new Enum(name: 'VCSDetectionType')) ->param('providerRootDirectory', '', new Text(256, 0), 'Path to Root Directory', true) - ->inject('gitHub') + ->inject('vcsFactory') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -105,7 +104,7 @@ class Create extends Action string $providerRepositoryId, string $type, string $providerRootDirectory, - GitHub $github, + Factory $vcsFactory, Response $response, Database $dbForPlatform ) { @@ -116,13 +115,11 @@ class Create extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs = $vcsFactory->fromInstallation($installation); - $owner = $github->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId); try { - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $repositoryName = $vcs->getRepositoryName($providerRepositoryId); if (empty($repositoryName)) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } @@ -130,9 +127,9 @@ class Create extends Action throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } - $files = $github->listRepositoryContents($owner, $repositoryName, $providerRootDirectory); + $files = $vcs->listRepositoryContents($owner, $repositoryName, $providerRootDirectory); $files = \array_column($files, 'name'); - $languages = $github->listRepositoryLanguages($owner, $repositoryName); + $languages = $vcs->listRepositoryLanguages($owner, $repositoryName); $detector = new Packager(); foreach ($files as $file) { @@ -149,7 +146,7 @@ class Create extends Action if ($type === 'framework') { $packages = ''; try { - $contentResponse = $github->getRepositoryContent($owner, $repositoryName, \rtrim($providerRootDirectory, '/') . '/package.json'); + $contentResponse = $vcs->getRepositoryContent($owner, $repositoryName, \rtrim($providerRootDirectory, '/') . '/package.json'); $packages = $contentResponse['content'] ?? ''; } catch (FileNotFound $e) { // Continue detection without package.json @@ -279,9 +276,9 @@ class Create extends Action } $wg->add(); - go(function () use ($github, $owner, $repositoryName, $providerRootDirectory, $file, $wg, &$envs) { + go(function () use ($vcs, $owner, $repositoryName, $providerRootDirectory, $file, $wg, &$envs) { try { - $contentResponse = $github->getRepositoryContent($owner, $repositoryName, \rtrim($providerRootDirectory, '/') . '/' . $file); + $contentResponse = $vcs->getRepositoryContent($owner, $repositoryName, \rtrim($providerRootDirectory, '/') . '/' . $file); $envFile = $contentResponse['content'] ?? ''; $configAdapter = new ConfigDotenv(); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php index ec135dc96e..0d1e867305 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php @@ -8,12 +8,11 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Platform\Scope\HTTP; -use Utopia\System\System; use Utopia\Validator\Text; -use Utopia\VCS\Adapter\Git\GitHub; use Utopia\VCS\Exception\RepositoryNotFound; class Get extends Action @@ -49,7 +48,7 @@ class Get extends Action )) ->param('installationId', '', new Text(256), 'Installation Id') ->param('providerRepositoryId', '', new Text(256), 'Repository Id') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -58,7 +57,7 @@ class Get extends Action public function action( string $installationId, string $providerRepositoryId, - GitHub $github, + Factory $vcsFactory, Response $response, Database $dbForPlatform ) { @@ -69,13 +68,11 @@ class Get extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs = $vcsFactory->fromInstallation($installation); - $owner = $github->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId); try { - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $repositoryName = $vcs->getRepositoryName($providerRepositoryId); if (empty($repositoryName)) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } @@ -83,12 +80,12 @@ class Get extends Action throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } - $repository = $github->getRepository($owner, $repositoryName); + $repository = $vcs->getRepository($owner, $repositoryName); - $authorized = $github->hasAccessToAllRepositories(); + $authorized = $vcs->hasAccessToAllRepositories(); if (!$authorized) { try { - $installationRepository = $github->getInstallationRepository($repositoryName); + $installationRepository = $vcs->getInstallationRepository($repositoryName); if (!empty($installationRepository)) { $authorized = true; } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php index 0163492233..bcc1a2782f 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php @@ -8,6 +8,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; use Swoole\Coroutine\WaitGroup; use Utopia\Config\Adapters\Dotenv as ConfigDotenv; use Utopia\Config\Config; @@ -52,10 +53,8 @@ use Utopia\Detector\Detector\Runtime; use Utopia\Detector\Detector\Strategy; use Utopia\Platform\Enum; use Utopia\Platform\Scope\HTTP; -use Utopia\System\System; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; use Utopia\VCS\Exception\FileNotFound; use function Swoole\Coroutine\batch; @@ -98,7 +97,7 @@ class XList extends Action ->param('type', '', new WhiteList(['runtime', 'framework']), 'Detector type. Must be one of the following: runtime, framework', enum: new Enum(name: 'VCSDetectionType')) ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) - ->inject('gitHub') + ->inject('vcsFactory') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -109,7 +108,7 @@ class XList extends Action string $type, string $search, array $queries, - GitHub $github, + Factory $vcsFactory, Response $response, Database $dbForPlatform ) { @@ -124,9 +123,7 @@ class XList extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs = $vcsFactory->fromInstallation($installation); $queries = Query::parseQueries($queries); $limitQuery = current(array_filter($queries, fn ($query) => $query->getMethod() === Query::TYPE_LIMIT)); @@ -140,8 +137,8 @@ class XList extends Action } $page = ($offset / $limit) + 1; - $owner = $github->getOwnerName($providerInstallationId); - ['items' => $repos, 'total' => $total] = $github->searchRepositories($owner, $page, $limit, $search); + $owner = $vcs->getOwnerName($providerInstallationId); + ['items' => $repos, 'total' => $total] = $vcs->searchRepositories($owner, $page, $limit, $search); $repos = \array_map(function ($repo) use ($installation) { $repo['id'] = \strval($repo['id'] ?? ''); @@ -153,9 +150,9 @@ class XList extends Action return $repo; }, $repos); - $repos = batch(\array_map(function ($repo) use ($type, $github) { - return function () use ($repo, $type, $github) { - $files = $github->listRepositoryContents($repo['organization'], $repo['name'], ''); + $repos = batch(\array_map(function ($repo) use ($type, $vcs) { + return function () use ($repo, $type, $vcs) { + $files = $vcs->listRepositoryContents($repo['organization'], $repo['name'], ''); $files = \array_column($files, 'name'); $detector = new Packager(); @@ -173,7 +170,7 @@ class XList extends Action if ($type === 'framework') { $packages = ''; try { - $contentResponse = $github->getRepositoryContent($repo['organization'], $repo['name'], 'package.json'); + $contentResponse = $vcs->getRepositoryContent($repo['organization'], $repo['name'], 'package.json'); $packages = $contentResponse['content'] ?? ''; } catch (FileNotFound $e) { // Continue detection without package.json @@ -215,7 +212,7 @@ class XList extends Action } $repo['framework'] = $framework; } else { - $languages = $github->listRepositoryLanguages($repo['organization'], $repo['name']); + $languages = $vcs->listRepositoryLanguages($repo['organization'], $repo['name']); $strategies = [ new Strategy(Strategy::FILEMATCH), @@ -276,9 +273,9 @@ class XList extends Action } $wg->add(); - go(function () use ($github, $repo, $file, $wg, &$envs) { + go(function () use ($vcs, $repo, $file, $wg, &$envs) { try { - $contentResponse = $github->getRepositoryContent($repo['organization'], $repo['name'], $file); + $contentResponse = $vcs->getRepositoryContent($repo['organization'], $repo['name'], $file); $envFile = $contentResponse['content'] ?? ''; $configAdapter = new ConfigDotenv(); diff --git a/src/Appwrite/Vcs/Factory.php b/src/Appwrite/Vcs/Factory.php new file mode 100644 index 0000000000..edd7f5e939 --- /dev/null +++ b/src/Appwrite/Vcs/Factory.php @@ -0,0 +1,126 @@ +> + */ + protected array $registry = []; + + /** + * @param array>|null $registry Override, defaults to the `vcs` config. + */ + public function __construct( + protected Cache $cache, + ?array $registry = null, + ) { + $registry ??= Config::getParam('vcs', []); + + foreach ($registry as $key => $entry) { + if (!($entry['enabled'] ?? false)) { + continue; + } + + $this->registry[$key] = $entry; + } + } + + /** + * Keys of providers that are enabled and fully configured. + * + * @return array + */ + public function getProviders(): array + { + return \array_values(\array_filter( + \array_keys($this->registry), + fn (string $key) => $this->isConfigured($key), + )); + } + + /** + * Whether all required environment variables are set for a provider. + */ + public function isConfigured(string $key): bool + { + if (!isset($this->registry[$key])) { + return false; + } + + foreach ($this->registry[$key]['requiredEnvVariables'] ?? [] as $name) { + if (empty($this->getEnv($key, $name))) { + return false; + } + } + + return true; + } + + /** + * Uninitialized adapter for provider-level operations that need no + * installation credentials (webhook signature validation, payload parsing). + */ + public function fromProvider(string $key): Git + { + if (!isset($this->registry[$key])) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unsupported VCS provider: ' . $key); + } + + $adapter = new ($this->registry[$key]['adapter'])($this->cache); + + $endpoint = $this->getEnv($key, 'ENDPOINT'); + if (!empty($endpoint) && \method_exists($adapter, 'setEndpoint')) { + $adapter->setEndpoint(\rtrim($endpoint, '/')); + } + + return $adapter; + } + + /** + * Initialized adapter for an installation. App credentials and personal + * tokens are both always passed; each adapter uses the set its + * authentication scheme needs and ignores the other. + */ + public function fromInstallation(Document $installation): Git + { + if ($installation->isEmpty()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + + $provider = $installation->getAttribute('provider', 'github'); + $adapter = $this->fromProvider($provider); + + $adapter->initializeVariables( + $installation->getAttribute('providerInstallationId', ''), + $this->getEnv($provider, 'PRIVATE_KEY'), + $this->getEnv($provider, 'APP_ID'), + $installation->getAttribute('personalAccessToken', ''), + $installation->getAttribute('personalRefreshToken', ''), + ); + + return $adapter; + } + + protected function getEnv(string $key, string $name): string + { + $variable = $this->registry[$key]['envVariables'][$name] ?? ''; + + return empty($variable) ? '' : System::getEnv($variable, ''); + } +} diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php new file mode 100644 index 0000000000..80d7f12918 --- /dev/null +++ b/tests/unit/Vcs/FactoryTest.php @@ -0,0 +1,117 @@ +assertNotEmpty($registry); + + foreach ($registry as $key => $entry) { + $this->assertTrue(\is_subclass_of($entry['adapter'], Git::class), "Adapter for '{$key}' must extend Git"); + $this->assertNotEmpty($entry['requiredEnvVariables'], "Required env variables missing for '{$key}'"); + foreach ($entry['requiredEnvVariables'] as $required) { + $this->assertStringStartsWith('_APP_VCS_', $entry['envVariables'][$required] ?? '', "Env variable for required key '{$required}' missing or invalid for '{$key}'"); + } + } + } + + public function testDisabledProvidersAreNotRegistered(): void + { + $factory = new Factory($this->cache(), [ + 'github' => $this->githubEntry(), + 'legacy' => ['enabled' => false] + $this->githubEntry(), + ]); + + $this->expectException(Exception::class); + $factory->fromProvider('legacy'); + } + + public function testFromProviderUnknownThrows(): void + { + $factory = new Factory($this->cache(), ['github' => $this->githubEntry()]); + + $this->expectException(Exception::class); + $factory->fromProvider('bitbucket'); + } + + public function testFromProviderBuildsAdapter(): void + { + $factory = new Factory($this->cache(), ['github' => $this->githubEntry()]); + + $this->assertInstanceOf(GitHub::class, $factory->fromProvider('github')); + } + + public function testFromInstallationEmptyDocumentThrows(): void + { + $factory = new Factory($this->cache(), ['github' => $this->githubEntry()]); + + $this->expectException(Exception::class); + $factory->fromInstallation(new Document()); + } + + public function testIsConfigured(): void + { + $entry = [ + 'enabled' => true, + 'adapter' => GitHub::class, + 'envVariables' => ['TOKEN' => '_APP_VCS_TEST_TOKEN'], + 'requiredEnvVariables' => ['TOKEN'], + ]; + $factory = new Factory($this->cache(), ['test' => $entry]); + + $this->assertFalse($factory->isConfigured('test')); + $this->assertFalse($factory->isConfigured('unknown')); + $this->assertSame([], $factory->getProviders()); + + \putenv('_APP_VCS_TEST_TOKEN=secret'); + $this->assertTrue($factory->isConfigured('test')); + $this->assertSame(['test'], $factory->getProviders()); + } + + protected function cache(): Cache + { + return new Cache(new None()); + } + + /** + * @return array + */ + protected function githubEntry(): array + { + return [ + 'enabled' => true, + 'adapter' => GitHub::class, + 'envVariables' => [ + 'APP_NAME' => '_APP_VCS_GITHUB_APP_NAME', + 'PRIVATE_KEY' => '_APP_VCS_GITHUB_PRIVATE_KEY', + 'APP_ID' => '_APP_VCS_GITHUB_APP_ID', + 'CLIENT_ID' => '_APP_VCS_GITHUB_CLIENT_ID', + 'CLIENT_SECRET' => '_APP_VCS_GITHUB_CLIENT_SECRET', + ], + 'requiredEnvVariables' => ['APP_NAME', 'PRIVATE_KEY', 'APP_ID', 'CLIENT_ID', 'CLIENT_SECRET'], + ]; + } +} From b58c45debe2966a33c66ca653cf994199c3bb51f Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 9 Jul 2026 11:48:32 +0530 Subject: [PATCH 02/52] refactor: extract InstallationTokens and RepositoryWebhooks services MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extracts the OAuth2 token refresh-and-persist logic that was inlined in Repositories/Create.php into Appwrite\Vcs\InstallationTokens, and adds Appwrite\Vcs\RepositoryWebhooks for idempotent per-repository webhook creation. Both are single-purpose, non-static, DI-registered services (no DB writes hang off Factory) so a self-hosted OAuth2 provider can wire into them without re-deriving this logic. RepositoryWebhooks has no caller yet — GitHub App delivery needs no per-repo webhook — it lands here so the follow-up Gitea PR only wires config and routes. --- app/init/resources.php | 4 + .../Http/Deployments/Template/Create.php | 2 +- .../Http/Deployments/Template/Create.php | 2 +- .../Repositories/Branches/XList.php | 2 +- .../Installations/Repositories/Create.php | 41 +---- src/Appwrite/Vcs/InstallationTokens.php | 68 ++++++++ src/Appwrite/Vcs/RepositoryWebhooks.php | 50 ++++++ tests/unit/Vcs/InstallationTokensTest.php | 156 ++++++++++++++++++ tests/unit/Vcs/RepositoryWebhooksTest.php | 58 +++++++ 9 files changed, 346 insertions(+), 37 deletions(-) create mode 100644 src/Appwrite/Vcs/InstallationTokens.php create mode 100644 src/Appwrite/Vcs/RepositoryWebhooks.php create mode 100644 tests/unit/Vcs/InstallationTokensTest.php create mode 100644 tests/unit/Vcs/RepositoryWebhooksTest.php diff --git a/app/init/resources.php b/app/init/resources.php index dba6816701..180afd21bd 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -18,6 +18,8 @@ use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl; use Appwrite\Vcs\Factory as VcsFactory; +use Appwrite\Vcs\InstallationTokens; +use Appwrite\Vcs\RepositoryWebhooks; use Executor\Executor; use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimitRedis; use Utopia\Cache\Adapter\Pool as CachePool; @@ -347,6 +349,8 @@ $container->set('servers', function () { $container->set('promiseAdapter', fn ($register) => $register->get('promiseAdapter'), ['register']); $container->set('vcsFactory', fn (Cache $cache) => new VcsFactory($cache), ['cache']); +$container->set('installationTokens', fn () => new InstallationTokens(), []); +$container->set('repositoryWebhooks', fn () => new RepositoryWebhooks(), []); $container->set('plan', fn () => []); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php index 77606ba334..cad0336071 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php @@ -13,7 +13,6 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory; -use Utopia\VCS\Adapter\Git; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -27,6 +26,7 @@ use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; +use Utopia\VCS\Adapter\Git; class Create extends Base { diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index 21b294d842..bb31f56d80 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php @@ -13,7 +13,6 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory; -use Utopia\VCS\Adapter\Git; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -28,6 +27,7 @@ use Utopia\System\System; use Utopia\Validator\Boolean; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; +use Utopia\VCS\Adapter\Git; class Create extends Base { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php index 2c8c4e2093..f8810c5f49 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php @@ -10,13 +10,13 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Database\Validator\Queries\Branches; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory; -use Utopia\VCS\Adapter\Git\GitHub; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Query as QueryException; use Utopia\Database\Query; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Text; +use Utopia\VCS\Adapter\Git\GitHub; use Utopia\VCS\Exception\RepositoryNotFound; class XList extends Action diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php index 99fc01726a..26ce96a68e 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -10,8 +10,8 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory; +use Appwrite\Vcs\InstallationTokens; use Utopia\Database\Database; -use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Query; use Utopia\Platform\Scope\HTTP; @@ -54,6 +54,7 @@ class Create extends Action ->param('name', '', new Text(256), 'Repository name (slug)') ->param('private', '', new Boolean(false), 'Mark repository public or private') ->inject('vcsFactory') + ->inject('installationTokens') ->inject('user') ->inject('response') ->inject('dbForPlatform') @@ -65,6 +66,7 @@ class Create extends Action string $name, bool $private, Factory $vcsFactory, + InstallationTokens $installationTokens, Document $user, Response $response, Database $dbForPlatform @@ -78,11 +80,8 @@ class Create extends Action if ($installation->getAttribute('personal', false) === true) { $oauth2 = new OAuth2Github(System::getEnv('_APP_VCS_GITHUB_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITHUB_CLIENT_SECRET', ''), ""); - $accessToken = $installation->getAttribute('personalAccessToken'); - $refreshToken = $installation->getAttribute('personalRefreshToken'); - $accessTokenExpiry = $installation->getAttribute('personalAccessTokenExpiry'); - - if (empty($accessToken) || empty($refreshToken) || empty($accessTokenExpiry)) { + $identity = null; + if (empty($installation->getAttribute('personalAccessToken')) || empty($installation->getAttribute('personalRefreshToken')) || empty($installation->getAttribute('personalAccessTokenExpiry'))) { $identity = $dbForPlatform->findOne('identities', [ Query::equal('provider', ['github']), Query::equal('userInternalId', [$user->getSequence()]), @@ -90,36 +89,10 @@ class Create extends Action if ($identity->isEmpty()) { throw new Exception(Exception::USER_IDENTITY_NOT_FOUND); } - - $accessToken = $accessToken ?? $identity->getAttribute('providerAccessToken'); - $refreshToken = $refreshToken ?? $identity->getAttribute('providerRefreshToken'); - $accessTokenExpiry = $accessTokenExpiry ?? $identity->getAttribute('providerAccessTokenExpiry'); } - $isExpired = new \DateTime($accessTokenExpiry) < new \DateTime('now'); - if ($isExpired) { - $oauth2->refreshTokens($refreshToken); - - $accessToken = $oauth2->getAccessToken(''); - $refreshToken = $oauth2->getRefreshToken(''); - - $verificationId = $oauth2->getUserID($accessToken); - - if (empty($verificationId)) { - throw new Exception(Exception::GENERAL_RATE_LIMIT_EXCEEDED, "Another request is currently refreshing OAuth token. Please try again."); - } - - $installation = $installation - ->setAttribute('personalAccessToken', $accessToken) - ->setAttribute('personalRefreshToken', $refreshToken) - ->setAttribute('personalAccessTokenExpiry', DateTime::addSeconds(new \DateTime(), (int)$oauth2->getAccessTokenExpiry(''))); - - $dbForPlatform->updateDocument('installations', $installation->getId(), new Document([ - 'personalAccessToken' => $installation->getAttribute('personalAccessToken'), - 'personalRefreshToken' => $installation->getAttribute('personalRefreshToken'), - 'personalAccessTokenExpiry' => $installation->getAttribute('personalAccessTokenExpiry'), - ])); - } + $installation = $installationTokens->refresh($installation, $dbForPlatform, $oauth2, $identity); + $accessToken = $installation->getAttribute('personalAccessToken'); try { $repository = $oauth2->createRepository($accessToken, $name, $private); diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php new file mode 100644 index 0000000000..cb8506f99b --- /dev/null +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -0,0 +1,68 @@ +getAttribute('personalAccessToken'); + $refreshToken = $installation->getAttribute('personalRefreshToken'); + $accessTokenExpiry = $installation->getAttribute('personalAccessTokenExpiry'); + + if ($identity !== null) { + $accessToken = $accessToken ?? $identity->getAttribute('providerAccessToken'); + $refreshToken = $refreshToken ?? $identity->getAttribute('providerRefreshToken'); + $accessTokenExpiry = $accessTokenExpiry ?? $identity->getAttribute('providerAccessTokenExpiry'); + } + + $installation = $installation->setAttribute('personalAccessToken', $accessToken); + + if (!empty($accessTokenExpiry) && new \DateTime($accessTokenExpiry) >= new \DateTime('now')) { + return $installation; + } + + $oauth2->refreshTokens($refreshToken); + + $accessToken = $oauth2->getAccessToken(''); + $refreshToken = $oauth2->getRefreshToken(''); + $verificationId = $oauth2->getUserID($accessToken); + + if (empty($verificationId)) { + throw new Exception(Exception::GENERAL_RATE_LIMIT_EXCEEDED, 'Another request is currently refreshing OAuth token. Please try again.'); + } + + $installation = $installation + ->setAttribute('personalAccessToken', $accessToken) + ->setAttribute('personalRefreshToken', $refreshToken) + ->setAttribute('personalAccessTokenExpiry', DateTime::addSeconds(new \DateTime(), (int)$oauth2->getAccessTokenExpiry(''))); + + $dbForPlatform->updateDocument('installations', $installation->getId(), new Document([ + 'personalAccessToken' => $installation->getAttribute('personalAccessToken'), + 'personalRefreshToken' => $installation->getAttribute('personalRefreshToken'), + 'personalAccessTokenExpiry' => $installation->getAttribute('personalAccessTokenExpiry'), + ])); + + return $installation; + } +} diff --git a/src/Appwrite/Vcs/RepositoryWebhooks.php b/src/Appwrite/Vcs/RepositoryWebhooks.php new file mode 100644 index 0000000000..a39a679bc8 --- /dev/null +++ b/src/Appwrite/Vcs/RepositoryWebhooks.php @@ -0,0 +1,50 @@ +count('repositories', [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::equal('providerRepositoryId', [$providerRepositoryId]), + ], 2); + + if ($connections > 1) { + return; + } + + try { + $adapter->createWebhook($owner, $repositoryName, $url, $secret); + } catch (\Throwable $error) { + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, 'Failed to create repository webhook: ' . $error->getMessage()); + } + } +} diff --git a/tests/unit/Vcs/InstallationTokensTest.php b/tests/unit/Vcs/InstallationTokensTest.php new file mode 100644 index 0000000000..a6da0af674 --- /dev/null +++ b/tests/unit/Vcs/InstallationTokensTest.php @@ -0,0 +1,156 @@ +createMock(Database::class); + } + + public function testUnexpiredTokenIsNotRefreshed(): void + { + $installation = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'valid-token', + 'personalRefreshToken' => 'valid-refresh', + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), 3600), + ]); + + $oauth2 = $this->fakeOAuth2(refreshCalls: 0); + + $result = (new InstallationTokens())->refresh($installation, $this->db(), $oauth2); + + $this->assertSame('valid-token', $result->getAttribute('personalAccessToken')); + $this->assertSame(0, $oauth2->refreshCalls); + } + + public function testExpiredTokenIsRefreshedAndPersisted(): void + { + $db = $this->createMock(Database::class); + + $db->expects($this->once()) + ->method('updateDocument') + ->with('installations', 'installation1', $this->callback(function (Document $update) { + return $update->getAttribute('personalAccessToken') === 'fresh-token' + && $update->getAttribute('personalRefreshToken') === 'fresh-refresh'; + })) + ->willReturnArgument(2); + + $installation = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'stale-token', + 'personalRefreshToken' => 'stale-refresh', + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), -3600), + ]); + + $oauth2 = $this->fakeOAuth2(refreshCalls: 1); + + $result = (new InstallationTokens())->refresh($installation, $db, $oauth2); + + $this->assertSame('fresh-token', $result->getAttribute('personalAccessToken')); + $this->assertSame(1, $oauth2->refreshCalls); + } + + public function testFallsBackToIdentityWhenInstallationHasNoTokens(): void + { + $installation = new Document(['$id' => 'installation1']); + $identity = new Document([ + 'providerAccessToken' => 'identity-token', + 'providerRefreshToken' => 'identity-refresh', + 'providerAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), 3600), + ]); + + $oauth2 = $this->fakeOAuth2(refreshCalls: 0); + + $result = (new InstallationTokens())->refresh($installation, $this->db(), $oauth2, $identity); + + $this->assertSame('identity-token', $result->getAttribute('personalAccessToken')); + } + + public function testFailedRefreshThrows(): void + { + $installation = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'stale-token', + 'personalRefreshToken' => 'stale-refresh', + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), -3600), + ]); + + $oauth2 = $this->fakeOAuth2(refreshCalls: 1, emptyUserId: true); + + $this->expectException(Exception::class); + (new InstallationTokens())->refresh($installation, $this->db(), $oauth2); + } + + protected function fakeOAuth2(int $refreshCalls, bool $emptyUserId = false) + { + return new class ($emptyUserId) extends OAuth2 { + public int $refreshCalls = 0; + protected array $tokens = []; + + public function __construct(protected bool $emptyUserId) + { + parent::__construct('id', 'secret', ''); + } + + public function getName(): string + { + return 'fake'; + } + + public function getLoginURL(): string + { + return ''; + } + + protected function getTokens(string $code): array + { + return $this->tokens; + } + + public function refreshTokens(string $refreshToken): array + { + $this->refreshCalls++; + $this->tokens = [ + 'access_token' => 'fresh-token', + 'refresh_token' => 'fresh-refresh', + 'expires_in' => 3600, + ]; + + return $this->tokens; + } + + public function getUserID(string $accessToken): string + { + return $this->emptyUserId ? '' : 'user1'; + } + + public function getUserEmail(string $accessToken): string + { + return ''; + } + + public function isEmailVerified(string $accessToken): bool + { + return true; + } + + public function getUserName(string $accessToken): string + { + return ''; + } + }; + } +} diff --git a/tests/unit/Vcs/RepositoryWebhooksTest.php b/tests/unit/Vcs/RepositoryWebhooksTest.php new file mode 100644 index 0000000000..6f5a546ee1 --- /dev/null +++ b/tests/unit/Vcs/RepositoryWebhooksTest.php @@ -0,0 +1,58 @@ +createMock(Database::class); + $db->expects($this->once())->method('count')->willReturn(1); + + $adapter = $this->createMock(Git::class); + $adapter->expects($this->once()) + ->method('createWebhook') + ->with('owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret') + ->willReturn(1); + + $installation = new Document(['$sequence' => 1]); + + (new RepositoryWebhooks())->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret'); + } + + public function testSkipsWhenRepositoryAlreadyConnected(): void + { + $db = $this->createMock(Database::class); + $db->expects($this->once())->method('count')->willReturn(2); + + $adapter = $this->createMock(Git::class); + $adapter->expects($this->never())->method('createWebhook'); + + $installation = new Document(['$sequence' => 1]); + + (new RepositoryWebhooks())->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret'); + } + + public function testAdapterFailureIsWrapped(): void + { + $db = $this->createMock(Database::class); + $db->method('count')->willReturn(1); + + $adapter = $this->createMock(Git::class); + $adapter->method('createWebhook')->willThrowException(new \RuntimeException('provider unreachable')); + + $installation = new Document(['$sequence' => 1]); + + $this->expectException(Exception::class); + (new RepositoryWebhooks())->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret'); + } +} From 53e5c72be63989c83b1eb611d56ff6c0c0dc207d Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 9 Jul 2026 13:36:20 +0530 Subject: [PATCH 03/52] refactor: use adapter URL builders instead of hardcoded GitHub URLs Points composer at the utopia-php/vcs branch (utopia-php/vcs#118, not yet merged) that adds getRepositoryUrl/getBranchUrl/getCommitUrl to the Git adapter, and replaces the hardcoded https://github.com/... string building in Compute\Base and the Builds worker with calls to the adapter. Temporary dev-branch pin until #118 releases. --- composer.json | 2 +- composer.lock | 26 ++++++++++++------- .../Platform/Modules/Compute/Base.php | 12 ++++----- .../Http/Deployments/Template/Create.php | 2 +- .../Modules/Functions/Workers/Builds.php | 2 +- .../Http/Deployments/Template/Create.php | 2 +- .../Repositories/Branches/XList.php | 2 +- 7 files changed, 28 insertions(+), 20 deletions(-) diff --git a/composer.json b/composer.json index 1f231eddbb..9f1f394ec8 100644 --- a/composer.json +++ b/composer.json @@ -88,7 +88,7 @@ "utopia-php/storage": "2.*", "utopia-php/system": "0.10.*", "utopia-php/telemetry": "^0.4", - "utopia-php/vcs": "4.*", + "utopia-php/vcs": "dev-feat/appwrite-provider-parity as 4.99.99", "utopia-php/websocket": "1.0.*", "matomo/device-detector": "6.4.*", "dragonmantank/cron-expression": "3.4.*", diff --git a/composer.lock b/composer.lock index 8bad4622a2..21de26a0b5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "091a71ba9518a11bacfa660d4df558ba", + "content-hash": "8aefd65f2026fdfc298af8867d4a5b0b", "packages": [ { "name": "adhocore/jwt", @@ -5124,16 +5124,16 @@ }, { "name": "utopia-php/vcs", - "version": "4.2.4", + "version": "dev-feat/appwrite-provider-parity", "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "f19c61daa1e9594734d85ae25b3e90ba2b350d33" + "reference": "c6e3769aa9bdc87334b38a60620e5d96cf4ef760" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/f19c61daa1e9594734d85ae25b3e90ba2b350d33", - "reference": "f19c61daa1e9594734d85ae25b3e90ba2b350d33", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/c6e3769aa9bdc87334b38a60620e5d96cf4ef760", + "reference": "c6e3769aa9bdc87334b38a60620e5d96cf4ef760", "shasum": "" }, "require": { @@ -5167,9 +5167,9 @@ ], "support": { "issues": "https://github.com/utopia-php/vcs/issues", - "source": "https://github.com/utopia-php/vcs/tree/4.2.4" + "source": "https://github.com/utopia-php/vcs/tree/feat/appwrite-provider-parity" }, - "time": "2026-06-25T13:27:19+00:00" + "time": "2026-07-09T07:55:13+00:00" }, { "name": "utopia-php/websocket", @@ -8354,11 +8354,19 @@ "time": "2026-05-30T17:09:26+00:00" } ], - "aliases": [], + "aliases": [ + { + "package": "utopia-php/vcs", + "version": "dev-feat/appwrite-provider-parity", + "alias": "4.99.99", + "alias_normalized": "4.99.99.0" + } + ], "minimum-stability": "dev", "stability-flags": { "utopia-php/http": 5, - "utopia-php/platform": 5 + "utopia-php/platform": 5, + "utopia-php/vcs": 20 }, "prefer-stable": true, "prefer-lowest": false, diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index a50ae92f11..b19014a71d 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -93,7 +93,7 @@ class Base extends Action // TODO: Support tag in future if ($referenceType === 'branch') { $providerBranch = empty($reference) ? $function->getAttribute('providerBranch', 'main') : $reference; - $branchUrl = "https://github.com/$owner/$repositoryName/tree/$providerBranch"; + $branchUrl = $vcs->getBranchUrl($owner, $repositoryName, $providerBranch); try { $commitDetails = $vcs->getLatestCommit($owner, $repositoryName, $providerBranch); } catch (\Throwable $error) { @@ -110,10 +110,10 @@ class Base extends Action // Goal is to set providerBranch, so build worker knows what to clone as base // Without this, clone command would be cloning empty branch, and failing $providerBranch = $function->getAttribute('providerBranch', 'main'); - $branchUrl = "https://github.com/$owner/$repositoryName/tree/$providerBranch"; + $branchUrl = $vcs->getBranchUrl($owner, $repositoryName, $providerBranch); } - $repositoryUrl = "https://github.com/$owner/$repositoryName"; + $repositoryUrl = $vcs->getRepositoryUrl($owner, $repositoryName); $deployment = $dbForProject->createDocument('deployments', new Document([ '$id' => $deploymentId, @@ -185,7 +185,7 @@ class Base extends Action // TODO: Support tag in future if ($referenceType === 'branch') { $providerBranch = empty($reference) ? $site->getAttribute('providerBranch', 'main') : $reference; - $branchUrl = "https://github.com/$owner/$repositoryName/tree/$providerBranch"; + $branchUrl = $vcs->getBranchUrl($owner, $repositoryName, $providerBranch); try { $commitDetails = $vcs->getLatestCommit($owner, $repositoryName, $providerBranch); } catch (\Throwable $error) { @@ -202,10 +202,10 @@ class Base extends Action // Goal is to set providerBranch, so build worker knows what to clone as base // Without this, clone command would be cloning empty branch, and failing $providerBranch = $site->getAttribute('providerBranch', 'main'); - $branchUrl = "https://github.com/$owner/$repositoryName/tree/$providerBranch"; + $branchUrl = $vcs->getBranchUrl($owner, $repositoryName, $providerBranch); } - $repositoryUrl = "https://github.com/$owner/$repositoryName"; + $repositoryUrl = $vcs->getRepositoryUrl($owner, $repositoryName); $commands = []; if (!empty($site->getAttribute('installCommand', ''))) { diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php index 77606ba334..cad0336071 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php @@ -13,7 +13,6 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory; -use Utopia\VCS\Adapter\Git; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -27,6 +26,7 @@ use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; +use Utopia\VCS\Adapter\Git; class Create extends Base { diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 2f6153fd90..2e0eda5caa 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -504,7 +504,7 @@ class Builds extends Action $deployment->setAttribute('providerCommitAuthorUrl', APP_VCS_GITHUB_URL); $deployment->setAttribute('providerCommitAuthor', APP_VCS_GITHUB_USERNAME); $deployment->setAttribute('providerCommitMessage', "Create '" . $resource->getAttribute('name', '') . "' function"); - $deployment->setAttribute('providerCommitUrl', "https://github.com/$cloneOwner/$cloneRepository/commit/$providerCommitHash"); + $deployment->setAttribute('providerCommitUrl', $vcs->getCommitUrl($cloneOwner, $cloneRepository, $providerCommitHash)); $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ 'providerCommitHash' => $deployment->getAttribute('providerCommitHash'), 'providerCommitAuthorUrl' => $deployment->getAttribute('providerCommitAuthorUrl'), diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index 21b294d842..bb31f56d80 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php @@ -13,7 +13,6 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory; -use Utopia\VCS\Adapter\Git; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -28,6 +27,7 @@ use Utopia\System\System; use Utopia\Validator\Boolean; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; +use Utopia\VCS\Adapter\Git; class Create extends Base { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php index 2c8c4e2093..f8810c5f49 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php @@ -10,13 +10,13 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Database\Validator\Queries\Branches; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory; -use Utopia\VCS\Adapter\Git\GitHub; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Query as QueryException; use Utopia\Database\Query; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Text; +use Utopia\VCS\Adapter\Git\GitHub; use Utopia\VCS\Exception\RepositoryNotFound; class XList extends Action From 8439c90bc16332010eaab20a96b534293f6335f0 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 9 Jul 2026 15:17:53 +0530 Subject: [PATCH 04/52] feat: add Gitea VCS provider Adds Gitea as the first additional VCS provider on the Factory/ InstallationTokens/RepositoryWebhooks foundation from the previous two PRs, using OAuth2 (personal installations) rather than a GitHub-App- style flow. - app/config/vcs.php: gitea registry entry (adapter, env vars). - Appwrite\Auth\OAuth2\Gitea: OAuth2 protocol implementation. - VCS\Http\Authorize\Base / Callback\Base: generic OAuth2 install flow for any OAuth2-based provider, no registry lookup for OAuth2 itself -- each provider's own subclass constructs its own OAuth2 client and picks the browser vs API endpoint, matching the polymorphism-over- branching direction from review (no shared 'createOAuth2' factory). - VCS\Http\Gitea\{Authorize,Callback,Events}: thin per-provider subclasses. - RepositoryWebhooks now takes Factory in its constructor and gates on the adapter's own requiresRepositoryWebhook() capability, so callers invoke it unconditionally on every repository connection without knowing which providers need one; also derives the webhook URL and secret internally instead of taking them as caller-supplied params. - Wired RepositoryWebhooks::ensure() into the four endpoints that actually create a repository connection (Sites/Functions Create and Update) -- the two endpoints that only trigger a deployment against an already-connected repo need no change. - V25 migration + collection resize: personalAccessToken/ personalRefreshToken 256 -> 2048 chars (Gitea's OAuth2 tokens run longer than GitHub's). - Env vars, docker-compose passthrough, mock endpoint for E2E test setup, and _APP_VCS_PROVIDERS exposed via Console Variables so the console knows which providers are actually configured. Gitea's Events webhook handler resolves a fresh adapter per repository connection (via Factory::fromInstallation) rather than one adapter for the whole batch like GitHub App does, since each OAuth2 installation carries its own personal token -- repositories matching the same providerRepositoryId can belong to different Appwrite installations. Known limitation: owner resolution for org-owned Gitea repositories during webhook processing falls back to the token owner's own login (correct for personal repos, the common case); a real fix needs the installation's stored organization threaded through the shared Deployment trait, which the existing GitHub-only createGitDeployments signature doesn't carry today. --- .env | 6 + app/config/collections/platform.php | 4 +- app/config/variables.php | 54 ++++ app/config/vcs.php | 12 + app/controllers/mock.php | 56 ++++ app/init/resources.php | 2 +- docker-compose.yml | 12 + src/Appwrite/Auth/OAuth2/Gitea.php | 213 +++++++++++++++ src/Appwrite/Migration/Version/V25.php | 13 + .../Modules/Console/Http/Variables/Get.php | 1 + .../Functions/Http/Functions/Create.php | 8 + .../Functions/Http/Functions/Update.php | 8 + .../Modules/Sites/Http/Sites/Create.php | 14 +- .../Modules/Sites/Http/Sites/Update.php | 8 + .../Modules/VCS/Http/Authorize/Base.php | 111 ++++++++ .../Modules/VCS/Http/Callback/Base.php | 182 +++++++++++++ .../Modules/VCS/Http/Gitea/Authorize/Get.php | 43 +++ .../Modules/VCS/Http/Gitea/Callback/Get.php | 39 +++ .../Modules/VCS/Http/Gitea/Events/Create.php | 252 ++++++++++++++++++ .../Platform/Modules/VCS/Services/Http.php | 8 + .../Response/Model/ConsoleVariables.php | 7 + src/Appwrite/Vcs/Factory.php | 8 + src/Appwrite/Vcs/RepositoryWebhooks.php | 47 +++- tests/unit/Vcs/RepositoryWebhooksTest.php | 65 +++-- 24 files changed, 1146 insertions(+), 27 deletions(-) create mode 100644 src/Appwrite/Auth/OAuth2/Gitea.php create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Gitea/Authorize/Get.php create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Gitea/Callback/Get.php create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php diff --git a/.env b/.env index 63c856cd8d..2d813f024c 100644 --- a/.env +++ b/.env @@ -150,4 +150,10 @@ _APP_VCS_GITHUB_CLIENT_ID= _APP_VCS_GITHUB_CLIENT_SECRET= _APP_VCS_GITHUB_PRIVATE_KEY=disabled _APP_VCS_GITHUB_WEBHOOK_SECRET= +_APP_VCS_GITEA_ENDPOINT= +_APP_VCS_GITEA_BROWSER_ENDPOINT= +_APP_VCS_GITEA_CLIENT_ID= +_APP_VCS_GITEA_CLIENT_SECRET= +_APP_VCS_GITEA_WEBHOOK_SECRET= +_APP_VCS_WEBHOOK_URL= _APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 2aff20aee5..9fd0667d17 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1842,7 +1842,7 @@ $platformCollections = [ '$id' => ID::custom('personalAccessToken'), 'type' => Database::VAR_STRING, 'format' => '', - 'size' => 256, + 'size' => 2048, 'signed' => true, 'required' => false, 'default' => null, @@ -1864,7 +1864,7 @@ $platformCollections = [ '$id' => ID::custom('personalRefreshToken'), 'type' => Database::VAR_STRING, 'format' => '', - 'size' => 256, + 'size' => 2048, 'signed' => true, 'required' => false, 'default' => null, diff --git a/app/config/variables.php b/app/config/variables.php index 2082868183..a98b6422e3 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -1210,6 +1210,60 @@ return [ 'question' => '', 'filter' => '' ], + [ + 'name' => '_APP_VCS_GITEA_ENDPOINT', + 'description' => 'URL of your self-hosted Gitea instance, reachable from the Appwrite server (e.g. for API calls and webhook token exchange).', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], + [ + 'name' => '_APP_VCS_GITEA_BROWSER_ENDPOINT', + 'description' => 'URL of your Gitea instance reachable from the browser, if different from _APP_VCS_GITEA_ENDPOINT (e.g. when Appwrite and Gitea are on the same Docker network but Gitea is exposed to the browser on a different host). Falls back to _APP_VCS_GITEA_ENDPOINT when unset.', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], + [ + 'name' => '_APP_VCS_GITEA_CLIENT_ID', + 'description' => 'Gitea OAuth2 application client ID. You can generate one in your Gitea instance under Settings > Applications.', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], + [ + 'name' => '_APP_VCS_GITEA_CLIENT_SECRET', + 'description' => 'Gitea OAuth2 application client secret. You can generate one in your Gitea instance under Settings > Applications.', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], + [ + 'name' => '_APP_VCS_GITEA_WEBHOOK_SECRET', + 'description' => 'Secret used to validate incoming Gitea webhook payloads.', + '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.', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], ], ], [ diff --git a/app/config/vcs.php b/app/config/vcs.php index 1f76c4d86e..1f22e474d9 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -4,6 +4,7 @@ * VCS provider registry, read by Appwrite\Vcs\Factory. */ +use Utopia\VCS\Adapter\Git\Gitea; use Utopia\VCS\Adapter\Git\GitHub; return [ @@ -20,4 +21,15 @@ return [ ], 'requiredEnvVariables' => ['APP_NAME', 'PRIVATE_KEY', 'APP_ID', 'CLIENT_ID', 'CLIENT_SECRET'], ], + 'gitea' => [ + 'enabled' => true, + 'adapter' => Gitea::class, + 'envVariables' => [ + 'ENDPOINT' => '_APP_VCS_GITEA_ENDPOINT', + 'CLIENT_ID' => '_APP_VCS_GITEA_CLIENT_ID', + 'CLIENT_SECRET' => '_APP_VCS_GITEA_CLIENT_SECRET', + 'WEBHOOK_SECRET' => '_APP_VCS_GITEA_WEBHOOK_SECRET', + ], + 'requiredEnvVariables' => ['ENDPOINT', 'CLIENT_ID', 'CLIENT_SECRET'], + ], ]; diff --git a/app/controllers/mock.php b/app/controllers/mock.php index a2170ab24f..f265a365d6 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -281,6 +281,62 @@ Http::get('/v1/mock/github/callback') ]); }); +Http::get('/v1/mock/gitea/callback') + ->desc('Create installation document using Gitea OAuth2 tokens') + ->groups(['mock', 'api', 'vcs']) + ->label('scope', 'public') + ->label('docs', false) + ->param('projectId', '', new UID(), 'Project ID of the project where Gitea is to be installed') + ->param('giteaUserId', '', new Text(256), 'Gitea user ID') + ->param('organization', '', new Text(256), 'Gitea username or organization') + ->param('accessToken', '', new Text(2048), 'Gitea access token') + ->inject('response') + ->inject('dbForPlatform') + ->action(function (string $projectId, string $giteaUserId, string $organization, string $accessToken, Response $response, Database $dbForPlatform) { + $isDevelopment = System::getEnv('_APP_ENV', 'development') === 'development'; + + if (!$isDevelopment) { + throw new Exception(Exception::GENERAL_NOT_IMPLEMENTED); + } + + $project = $dbForPlatform->getDocument('projects', $projectId); + + if ($project->isEmpty()) { + throw new Exception(Exception::PROJECT_NOT_FOUND, 'Project with the ID from state could not be found.'); + } + + if (empty($giteaUserId) || empty($organization) || empty($accessToken)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing Gitea installation details'); + } + + $projectInternalId = $project->getSequence(); + $teamId = $project->getAttribute('teamId', ''); + + $installation = $dbForPlatform->createDocument('installations', new Document([ + '$id' => ID::unique(), + '$permissions' => [ + Permission::read(Role::team(ID::custom($teamId))), + Permission::update(Role::team(ID::custom($teamId), 'owner')), + Permission::update(Role::team(ID::custom($teamId), 'developer')), + Permission::delete(Role::team(ID::custom($teamId), 'owner')), + Permission::delete(Role::team(ID::custom($teamId), 'developer')), + ], + 'providerInstallationId' => $giteaUserId, + 'projectId' => $projectId, + 'projectInternalId' => $projectInternalId, + 'provider' => 'gitea', + 'organization' => $organization, + 'personal' => true, + 'personalAccessToken' => $accessToken, + 'personalRefreshToken' => '', + 'personalAccessTokenExpiry' => null, + ])); + + $response->json([ + 'installationId' => $installation->getId(), + ]); + }); + Http::shutdown() ->groups(['mock']) ->inject('response') diff --git a/app/init/resources.php b/app/init/resources.php index 180afd21bd..d02ed42c5a 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -350,7 +350,7 @@ $container->set('promiseAdapter', fn ($register) => $register->get('promiseAdapt $container->set('vcsFactory', fn (Cache $cache) => new VcsFactory($cache), ['cache']); $container->set('installationTokens', fn () => new InstallationTokens(), []); -$container->set('repositoryWebhooks', fn () => new RepositoryWebhooks(), []); +$container->set('repositoryWebhooks', fn (VcsFactory $vcsFactory) => new RepositoryWebhooks($vcsFactory), ['vcsFactory']); $container->set('plan', fn () => []); diff --git a/docker-compose.yml b/docker-compose.yml index 1b5163e1e5..cdbdbce214 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -192,6 +192,12 @@ services: - _APP_VCS_GITHUB_WEBHOOK_SECRET - _APP_VCS_GITHUB_CLIENT_SECRET - _APP_VCS_GITHUB_CLIENT_ID + - _APP_VCS_GITEA_ENDPOINT + - _APP_VCS_GITEA_BROWSER_ENDPOINT + - _APP_VCS_GITEA_CLIENT_ID + - _APP_VCS_GITEA_CLIENT_SECRET + - _APP_VCS_GITEA_WEBHOOK_SECRET + - _APP_VCS_WEBHOOK_URL - _APP_MIGRATIONS_FIREBASE_CLIENT_ID - _APP_MIGRATIONS_FIREBASE_CLIENT_SECRET - _APP_ASSISTANT_OPENAI_API_KEY @@ -472,6 +478,12 @@ services: - _APP_VCS_GITHUB_APP_NAME - _APP_VCS_GITHUB_PRIVATE_KEY - _APP_VCS_GITHUB_APP_ID + - _APP_VCS_GITEA_ENDPOINT + - _APP_VCS_GITEA_BROWSER_ENDPOINT + - _APP_VCS_GITEA_CLIENT_ID + - _APP_VCS_GITEA_CLIENT_SECRET + - _APP_VCS_GITEA_WEBHOOK_SECRET + - _APP_VCS_WEBHOOK_URL - _APP_FUNCTIONS_TIMEOUT - _APP_SITES_TIMEOUT - _APP_COMPUTE_BUILD_TIMEOUT diff --git a/src/Appwrite/Auth/OAuth2/Gitea.php b/src/Appwrite/Auth/OAuth2/Gitea.php new file mode 100644 index 0000000000..af82dd440f --- /dev/null +++ b/src/Appwrite/Auth/OAuth2/Gitea.php @@ -0,0 +1,213 @@ +endpoint = \rtrim($endpoint, '/'); + } + + /** + * @return string + */ + public function getName(): string + { + return 'gitea'; + } + + /** + * @return string + */ + public function getLoginURL(): string + { + return $this->endpoint . '/login/oauth/authorize?' . \http_build_query([ + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'response_type' => 'code', + 'scope' => \implode(' ', $this->getScopes()), + 'state' => \json_encode($this->state) + ]); + } + + /** + * @param string $code + * + * @return array + */ + protected function getTokens(string $code): array + { + if (empty($this->tokens)) { + $response = $this->request( + 'POST', + $this->endpoint . '/login/oauth/access_token', + ['Content-Type: application/json'], + \json_encode([ + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'grant_type' => 'authorization_code', + 'redirect_uri' => $this->callback, + 'code' => $code + ]) + ); + + $this->tokens = \json_decode($response, true) ?? []; + } + + return $this->tokens; + } + + /** + * @param string $refreshToken + * + * @return array + */ + public function refreshTokens(string $refreshToken): array + { + $response = $this->request( + 'POST', + $this->endpoint . '/login/oauth/access_token', + ['Content-Type: application/json'], + \json_encode([ + 'client_id' => $this->appID, + 'client_secret' => $this->appSecret, + 'grant_type' => 'refresh_token', + 'refresh_token' => $refreshToken + ]) + ); + + $this->tokens = \json_decode($response, true) ?? []; + + if (empty($this->tokens['refresh_token'])) { + $this->tokens['refresh_token'] = $refreshToken; + } + + return $this->tokens; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserID(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return \strval($user['id'] ?? ''); + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['email'] ?? ''; + } + + /** + * @param string $accessToken + * + * @return bool + */ + public function isEmailVerified(string $accessToken): bool + { + $user = $this->getUser($accessToken); + + return $user['verified'] ?? false; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserName(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['full_name'] ?? ''; + } + + /** + * @param string $accessToken + * + * @return string + */ + public function getUserSlug(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['login'] ?? ''; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken) + { + if (empty($this->user)) { + $this->user = \json_decode($this->request('GET', $this->endpoint . '/api/v1/user', ['Authorization: token ' . $accessToken]), true) ?? []; + + // Gitea reports the primary email's verification separately + $emails = \json_decode($this->request('GET', $this->endpoint . '/api/v1/user/emails', ['Authorization: token ' . $accessToken]), true); + + foreach (\is_array($emails) ? $emails : [] as $email) { + if (($email['primary'] ?? false) === true) { + $this->user['email'] = $email['email'] ?? ($this->user['email'] ?? ''); + $this->user['verified'] = $email['verified'] ?? false; + break; + } + } + } + + return $this->user; + } + + public function createRepository(string $accessToken, string $repositoryName, bool $private): array + { + $repository = $this->request('POST', $this->endpoint . '/api/v1/user/repos', ['Authorization: token ' . $accessToken, 'Content-Type: application/json'], \json_encode([ + 'name' => $repositoryName, + 'private' => $private + ])); + + return \json_decode($repository, true) ?? []; + } +} diff --git a/src/Appwrite/Migration/Version/V25.php b/src/Appwrite/Migration/Version/V25.php index 800b9b451f..6ee9cc8ab9 100644 --- a/src/Appwrite/Migration/Version/V25.php +++ b/src/Appwrite/Migration/Version/V25.php @@ -63,6 +63,19 @@ class V25 extends Migration } $this->dbForProject->purgeCachedCollection($id); break; + + case 'installations': + if ($collectionType === 'console') { + foreach (['personalAccessToken', 'personalRefreshToken'] as $attribute) { + try { + $this->dbForProject->updateAttribute($id, $attribute, size: 2048); + } catch (Throwable $th) { + Console::warning("Failed to resize attribute \"{$attribute}\" in collection {$id}: {$th->getMessage()}"); + } + } + } + $this->dbForProject->purgeCachedCollection($id); + break; } } } diff --git a/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php index f1beaeb276..0c58da109c 100644 --- a/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php +++ b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php @@ -88,6 +88,7 @@ class Get extends Action '_APP_COMPUTE_SIZE_LIMIT' => +System::getEnv('_APP_COMPUTE_SIZE_LIMIT'), '_APP_USAGE_STATS' => System::getEnv('_APP_USAGE_STATS'), '_APP_VCS_ENABLED' => $isVcsEnabled, + '_APP_VCS_PROVIDERS' => $vcsFactory->getProviders(), '_APP_DOMAIN_ENABLED' => $isDomainEnabled, '_APP_ASSISTANT_ENABLED' => $isAssistantEnabled, '_APP_DOMAIN_SITES' => $platform['sitesDomain'], diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php index bbb6ecf265..d12d652749 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -21,6 +21,7 @@ use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model\Rule; use Appwrite\Vcs\Factory; +use Appwrite\Vcs\RepositoryWebhooks; use Utopia\Abuse\Abuse; use Utopia\Config\Config; use Utopia\Database\Database; @@ -129,6 +130,7 @@ class Create extends Base ->inject('dbForPlatform') ->inject('request') ->inject('vcsFactory') + ->inject('repositoryWebhooks') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -173,6 +175,7 @@ class Create extends Base Database $dbForPlatform, Request $request, Factory $vcsFactory, + RepositoryWebhooks $repositoryWebhooks, Authorization $authorization, array $platform ) { @@ -303,6 +306,11 @@ class Create extends Base 'providerPullRequestIds' => [] ])); + $providerAdapter = $vcsFactory->fromInstallation($installation); + $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); + $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); + $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); + $function->setAttribute('repositoryId', $repository->getId()); $function->setAttribute('repositoryInternalId', $repository->getSequence()); } diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php index f275a15aee..0ebfd9e665 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -14,6 +14,7 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Task\Validator\Cron; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory; +use Appwrite\Vcs\RepositoryWebhooks; use Executor\Executor; use Utopia\Config\Config; use Utopia\Database\Database; @@ -113,6 +114,7 @@ class Update extends Base ->inject('publisherForBuilds') ->inject('dbForPlatform') ->inject('vcsFactory') + ->inject('repositoryWebhooks') ->inject('executor') ->inject('authorization') ->inject('platform') @@ -150,6 +152,7 @@ class Update extends Base BuildPublisher $publisherForBuilds, Database $dbForPlatform, Factory $vcsFactory, + RepositoryWebhooks $repositoryWebhooks, Executor $executor, Authorization $authorization, array $platform @@ -243,6 +246,11 @@ class Update extends Base $repositoryId = $repository->getId(); $repositoryInternalId = $repository->getSequence(); + + $providerAdapter = $vcsFactory->fromInstallation($installation); + $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); + $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); + $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); } $live = true; diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php index 3c51c57420..617acb95e4 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php @@ -11,6 +11,8 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory; +use Appwrite\Vcs\RepositoryWebhooks; use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; @@ -102,6 +104,8 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('dbForPlatform') + ->inject('vcsFactory') + ->inject('repositoryWebhooks') ->callback($this->action(...)); } @@ -133,7 +137,9 @@ class Create extends Base Database $dbForProject, Document $project, Event $queueForEvents, - Database $dbForPlatform + Database $dbForPlatform, + Factory $vcsFactory, + RepositoryWebhooks $repositoryWebhooks ) { if (!empty($adapter)) { $configFramework = Config::getParam('frameworks')[$framework] ?? []; @@ -215,6 +221,12 @@ class Create extends Base 'providerPullRequestIds' => [] ]); $repository = $dbForPlatform->createDocument('repositories', $repository); + + $providerAdapter = $vcsFactory->fromInstallation($installation); + $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); + $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); + $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); + $site->setAttribute('repositoryId', $repository->getId()); $site->setAttribute('repositoryInternalId', $repository->getSequence()); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php index 6604354436..eada5634d8 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php @@ -12,6 +12,7 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory; +use Appwrite\Vcs\RepositoryWebhooks; use Executor\Executor; use Utopia\Config\Config; use Utopia\Database\Database; @@ -109,6 +110,7 @@ class Update extends Base ->inject('publisherForBuilds') ->inject('dbForPlatform') ->inject('vcsFactory') + ->inject('repositoryWebhooks') ->inject('executor') ->inject('platform') ->callback($this->action(...)); @@ -146,6 +148,7 @@ class Update extends Base BuildPublisher $publisherForBuilds, Database $dbForPlatform, Factory $vcsFactory, + RepositoryWebhooks $repositoryWebhooks, Executor $executor, array $platform ) { @@ -241,6 +244,11 @@ class Update extends Base $repository = $dbForPlatform->createDocument('repositories', $repository); $repositoryId = $repository->getId(); $repositoryInternalId = $repository->getSequence(); + + $providerAdapter = $vcsFactory->fromInstallation($installation); + $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); + $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); + $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); } $live = true; diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php new file mode 100644 index 0000000000..eeea2c19cd --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php @@ -0,0 +1,111 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) + ->setHttpPath('/v1/vcs/' . $key . '/authorize') + ->desc('Create ' . $name . ' 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: 'create' . $name . 'Installation', + description: '/docs/references/vcs/create-' . $key . '-installation.md', + auth: [AuthType::ADMIN], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_MOVED_PERMANENTLY, + model: Response::MODEL_NONE, + ) + ], + contentType: ContentType::HTML, + type: MethodType::WEBAUTH, + hide: true, + )) + ->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('vcsFactory') + ->inject('response') + ->inject('project') + ->inject('platform') + ->callback($this->action(...)); + } + + public function action( + string $success, + string $failure, + Factory $vcsFactory, + Response $response, + Document $project, + array $platform + ) { + $key = static::getProvider(); + + if (!$vcsFactory->isConfigured($key)) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, static::getProviderName() . ' provider is not configured. Please configure VCS (Version Control System) variables in .env file.'); + } + + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; + $hostname = $platform['consoleHostname'] ?? ''; + $callback = $protocol . '://' . $hostname . '/v1/vcs/' . $key . '/callback'; + + $oauth2 = $this->createOAuth2($callback, [ + 'projectId' => $project->getId(), + 'success' => $success, + 'failure' => $failure, + ]); + + $response + ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') + ->addHeader('Pragma', 'no-cache') + ->redirect($oauth2->getLoginURL()); + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php new file mode 100644 index 0000000000..1c8ab3e3ad --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php @@ -0,0 +1,182 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) + ->setHttpPath('/v1/vcs/' . $key . '/callback') + ->desc('Get installation and authorization from ' . $key) + ->groups(['api', 'vcs']) + ->label('scope', 'public') + ->label('error', APP_VIEWS_DIR . '/general/error.phtml') + ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) + ->param('state', '', new Text(2048), 'OAuth2 state. Contains info sent when starting authorization flow.', true) + ->inject('response') + ->inject('dbForPlatform') + ->inject('platform') + ->callback($this->action(...)); + } + + public function action( + string $code, + string $state, + Response $response, + Database $dbForPlatform, + array $platform + ) { + $key = static::getProvider(); + + 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) ?? []; + $redirectFailure = $state['failure'] ?? ''; + $projectId = $state['projectId'] ?? ''; + + $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, $state); + $redirectSuccess = $state['success'] ?? ''; + $redirectFailure = $state['failure'] ?? ''; + + if (empty($code)) { + $this->failure($response, $redirectFailure, 'OAuth2 authorization code is missing.'); + return; + } + + $callback = $protocol . '://' . $hostname . '/v1/vcs/' . $key . '/callback'; + $oauth2 = $this->createOAuth2($callback); + + $accessToken = $oauth2->getAccessToken($code); + $refreshToken = $oauth2->getRefreshToken($code); + $accessTokenExpiry = DateTime::addSeconds(new \DateTime(), \intval($oauth2->getAccessTokenExpiry($code))); + + if (empty($accessToken)) { + $this->failure($response, $redirectFailure, 'Failed to exchange authorization code for an access token.'); + return; + } + + $providerInstallationId = $oauth2->getUserID($accessToken); + $owner = \method_exists($oauth2, 'getUserSlug') ? $oauth2->getUserSlug($accessToken) : ''; + + if (empty($providerInstallationId) || empty($owner)) { + $this->failure($response, $redirectFailure, 'Failed to get user information from ' . static::getProviderName() . '.'); + return; + } + + $projectInternalId = $project->getSequence(); + + $installation = $dbForPlatform->findOne('installations', [ + Query::equal('providerInstallationId', [$providerInstallationId]), + Query::equal('projectInternalId', [$projectInternalId]), + Query::equal('provider', [$key]), + ]); + + 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' => $key, + 'organization' => $owner, + 'personal' => true, + 'personalRefreshToken' => $refreshToken, + 'personalAccessToken' => $accessToken, + 'personalAccessTokenExpiry' => $accessTokenExpiry, + ])); + } else { + $installation = $dbForPlatform->updateDocument('installations', $installation->getId(), new Document([ + 'organization' => $owner, + 'personal' => true, + 'personalRefreshToken' => $refreshToken, + 'personalAccessToken' => $accessToken, + 'personalAccessTokenExpiry' => $accessTokenExpiry, + ])); + } + + $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. + */ + protected 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])); + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Authorize/Get.php new file mode 100644 index 0000000000..e77d2267ec --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Authorize/Get.php @@ -0,0 +1,43 @@ +setEndpoint($browserEndpoint); + + return $oauth2; + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Callback/Get.php new file mode 100644 index 0000000000..09590ab1bb --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Callback/Get.php @@ -0,0 +1,39 @@ +setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', '')); + + return $oauth2; + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php new file mode 100644 index 0000000000..17f02c577a --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -0,0 +1,252 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_POST) + ->setHttpPath('/v1/vcs/gitea/events') + ->desc('Create event') + ->groups(['api', 'vcs']) + ->label('scope', 'public') + ->inject('vcsFactory') + ->inject('request') + ->inject('response') + ->inject('dbForPlatform') + ->inject('authorization') + ->inject('getProjectDB') + ->inject('publisherForBuilds') + ->inject('platform') + ->callback($this->action(...)); + } + + public function action( + Factory $vcsFactory, + Request $request, + Response $response, + Database $dbForPlatform, + Authorization $authorization, + callable $getProjectDB, + BuildPublisher $publisherForBuilds, + array $platform + ) { + $vcs = $vcsFactory->fromProvider('gitea'); + + $event = $request->getHeaderLine($vcs->getEventHeaderName(), ''); + Span::add('vcs.gitea.event.name', $event); + + $payload = $request->getRawPayload(); + $signature = $request->getHeaderLine($vcs->getSignatureHeaderName(), ''); + $secretKey = System::getEnv('_APP_VCS_GITEA_WEBHOOK_SECRET', ''); + + $valid = empty($secretKey) ? true : $vcs->validateWebhookEvent($payload, $signature, $secretKey); + Span::add('vcs.gitea.event.signature.valid', $valid); + + if (!$valid) { + throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'Invalid webhook payload signature. Please make sure the webhook secret has same value in your Gitea repository settings and in the _APP_VCS_GITEA_WEBHOOK_SECRET environment variable'); + } + + $parsedPayload = $vcs->getEvent($event, $payload); + + match ($event) { + 'push' => $this->handlePushEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + 'pull_request' => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + default => null, + }; + + $response->json($parsedPayload); + } + + /** + * Resolves an authenticated adapter for a repository connection's own + * installation. Unlike GitHub App, where one adapter serves every + * repository under an installation, Gitea's OAuth2 installations each + * carry their own personal token — repositories matching the same + * providerRepositoryId can belong to different Appwrite installations. + */ + private function resolveAdapterForRepository(Document $repository, Factory $vcsFactory, Database $dbForPlatform, Authorization $authorization): ?Git + { + $installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', ''))); + + if ($installation->isEmpty()) { + return null; + } + + try { + return $vcsFactory->fromInstallation($installation); + } catch (\Throwable $error) { + Console::warning("Failed to resolve Gitea adapter for installation '{$installation->getId()}': " . $error->getMessage()); + return null; + } + } + + private function handlePushEvent( + array $parsedPayload, + Factory $vcsFactory, + Database $dbForPlatform, + Authorization $authorization, + BuildPublisher $publisherForBuilds, + callable $getProjectDB, + array $platform, + ) { + $providerBranchDeleted = $parsedPayload['branchDeleted'] ?? false; + $providerBranch = $parsedPayload['branch'] ?? ''; + $providerBranchUrl = $parsedPayload['branchUrl'] ?? ''; + $providerRepositoryId = $parsedPayload['repositoryId'] ?? ''; + $providerRepositoryName = $parsedPayload['repositoryName'] ?? ''; + $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.gitea.event.repo.id', $providerRepositoryId); + Span::add('vcs.gitea.event.repo.name', $providerRepositoryName); + Span::add('vcs.gitea.event.branch', $providerBranch); + + if ($providerCommitAuthorEmail === APP_VCS_GITHUB_EMAIL || $providerBranchDeleted) { + return; + } + + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ + Query::equal('providerRepositoryId', [$providerRepositoryId]), + Query::limit(100), + ])); + + $providerAffectedFiles = $parsedPayload['affectedFiles'] ?? []; + + foreach ($repositories as $repository) { + $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $dbForPlatform, $authorization); + + if ($adapter === null) { + continue; + } + + $providerInstallationId = $repository->getAttribute('installationId', ''); + + $this->createGitDeployments($adapter, $providerInstallationId, [$repository], $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); + } + } + + private function handlePullRequestEvent( + array $parsedPayload, + Factory $vcsFactory, + Database $dbForPlatform, + Authorization $authorization, + BuildPublisher $publisherForBuilds, + callable $getProjectDB, + array $platform, + ) { + $action = $parsedPayload['action'] ?? ''; + + if ($action === 'opened' || $action === 'reopened' || $action === 'synchronize') { + $providerBranch = $parsedPayload['branch'] ?? ''; + $providerBranchUrl = $parsedPayload['branchUrl'] ?? ''; + $providerRepositoryId = $parsedPayload['repositoryId'] ?? ''; + $providerRepositoryName = $parsedPayload['repositoryName'] ?? ''; + $providerRepositoryUrl = $parsedPayload['repositoryUrl'] ?? ''; + $providerPullRequestId = $parsedPayload['pullRequestNumber'] ?? ''; + $providerCommitHash = $parsedPayload['commitHash'] ?? ''; + $providerRepositoryOwner = $parsedPayload['owner'] ?? ''; + $external = $parsedPayload['external'] ?? true; + $providerCommitUrl = $parsedPayload['headCommitUrl'] ?? ''; + $providerCommitAuthorUrl = $parsedPayload['authorUrl'] ?? ''; + + Span::add('vcs.gitea.event.repo.id', $providerRepositoryId); + Span::add('vcs.gitea.event.repo.name', $providerRepositoryName); + Span::add('vcs.gitea.event.branch', $providerBranch); + + // Ignore sync for non-external. We handle it in the push webhook. + if (!$external && $action === 'synchronize') { + return; + } + + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ + Query::equal('providerRepositoryId', [$providerRepositoryId]), + Query::orderDesc('$createdAt'), + Query::limit(100), + ])); + + foreach ($repositories as $repository) { + $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $dbForPlatform, $authorization); + + if ($adapter === null) { + continue; + } + + $providerInstallationId = $repository->getAttribute('installationId', ''); + + try { + $commitDetails = $adapter->getCommit($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash); + } catch (\Throwable $e) { + Console::warning("Failed to fetch commit '{$providerCommitHash}': " . $e->getMessage()); + $commitDetails = []; + } + $providerCommitAuthor = $commitDetails['commitAuthor'] ?? ''; + $providerCommitMessage = $commitDetails['commitMessage'] ?? ''; + + $prFiles = $adapter->getPullRequestFiles($providerRepositoryOwner, $providerRepositoryName, $providerPullRequestId); + $providerAffectedFiles = [ + ...array_column($prFiles, 'filename'), + ...array_filter(array_column($prFiles, 'previous_filename')), + ]; + + $this->createGitDeployments($adapter, $providerInstallationId, [$repository], $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, $providerPullRequestId, $providerAffectedFiles, $external, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); + } + } elseif ($action === 'closed') { + $providerRepositoryId = $parsedPayload['repositoryId'] ?? ''; + $providerPullRequestId = $parsedPayload['pullRequestNumber'] ?? ''; + $external = $parsedPayload['external'] ?? true; + + if (!$external) { + return; + } + + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ + Query::equal('providerRepositoryId', [$providerRepositoryId]), + Query::orderDesc('$createdAt'), + Query::limit(100), + ])); + + foreach ($repositories as $repository) { + $providerPullRequestIds = $repository->getAttribute('providerPullRequestIds', []); + + if (\in_array($providerPullRequestId, $providerPullRequestIds)) { + $providerPullRequestIds = \array_diff($providerPullRequestIds, [$providerPullRequestId]); + $authorization->skip(fn () => $dbForPlatform->updateDocument('repositories', $repository->getId(), new Document(['providerPullRequestIds' => $providerPullRequestIds]))); + } + } + } + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Services/Http.php b/src/Appwrite/Platform/Modules/VCS/Services/Http.php index ae766ad00e..dfdd6c8a5f 100644 --- a/src/Appwrite/Platform/Modules/VCS/Services/Http.php +++ b/src/Appwrite/Platform/Modules/VCS/Services/Http.php @@ -2,6 +2,9 @@ namespace Appwrite\Platform\Modules\VCS\Services; +use Appwrite\Platform\Modules\VCS\Http\Gitea\Authorize\Get as GetGiteaAuthorize; +use Appwrite\Platform\Modules\VCS\Http\Gitea\Callback\Get as GetGiteaCallback; +use Appwrite\Platform\Modules\VCS\Http\Gitea\Events\Create as CreateGiteaEvent; use Appwrite\Platform\Modules\VCS\Http\GitHub\Authorize\External\Update as UpdateExternalDeployment; use Appwrite\Platform\Modules\VCS\Http\GitHub\Authorize\Get as GetGitHubAuthorize; use Appwrite\Platform\Modules\VCS\Http\GitHub\Callback\Get as GetGitHubCallback; @@ -28,6 +31,10 @@ class Http extends Service $this->addAction(GetGitHubCallback::getName(), new GetGitHubCallback()); $this->addAction(UpdateExternalDeployment::getName(), new UpdateExternalDeployment()); + // Gitea Authorization & Callback + $this->addAction(GetGiteaAuthorize::getName(), new GetGiteaAuthorize()); + $this->addAction(GetGiteaCallback::getName(), new GetGiteaCallback()); + // Installations $this->addAction(GetInstallation::getName(), new GetInstallation()); $this->addAction(ListInstallations::getName(), new ListInstallations()); @@ -43,5 +50,6 @@ class Http extends Service // Events $this->addAction(CreateGitHubEvent::getName(), new CreateGitHubEvent()); + $this->addAction(CreateGiteaEvent::getName(), new CreateGiteaEvent()); } } diff --git a/src/Appwrite/Utopia/Response/Model/ConsoleVariables.php b/src/Appwrite/Utopia/Response/Model/ConsoleVariables.php index 5ca01ca010..ab179d04ee 100644 --- a/src/Appwrite/Utopia/Response/Model/ConsoleVariables.php +++ b/src/Appwrite/Utopia/Response/Model/ConsoleVariables.php @@ -64,6 +64,13 @@ class ConsoleVariables extends Model 'default' => false, 'example' => true, ]) + ->addRule('_APP_VCS_PROVIDERS', [ + 'type' => self::TYPE_STRING, + 'description' => 'List of configured VCS providers.', + 'default' => [], + 'example' => ['github'], + 'array' => true, + ]) ->addRule('_APP_DOMAIN_ENABLED', [ 'type' => self::TYPE_BOOLEAN, 'description' => 'Defines if main domain is configured. If so, custom domains can be created.', diff --git a/src/Appwrite/Vcs/Factory.php b/src/Appwrite/Vcs/Factory.php index edd7f5e939..3a6bb687a8 100644 --- a/src/Appwrite/Vcs/Factory.php +++ b/src/Appwrite/Vcs/Factory.php @@ -117,6 +117,14 @@ class Factory return $adapter; } + /** + * Webhook secret configured for a provider, empty if none is set. + */ + public function getWebhookSecret(string $key): string + { + return $this->getEnv($key, 'WEBHOOK_SECRET'); + } + protected function getEnv(string $key, string $name): string { $variable = $this->registry[$key]['envVariables'][$name] ?? ''; diff --git a/src/Appwrite/Vcs/RepositoryWebhooks.php b/src/Appwrite/Vcs/RepositoryWebhooks.php index a39a679bc8..905634a493 100644 --- a/src/Appwrite/Vcs/RepositoryWebhooks.php +++ b/src/Appwrite/Vcs/RepositoryWebhooks.php @@ -6,21 +6,29 @@ use Appwrite\Extend\Exception; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; +use Utopia\System\System; use Utopia\VCS\Adapter\Git; /** * Creates per-repository webhooks for self-hosted providers. * - * GitHub App deliveries need no per-repository webhook, so no endpoint calls - * this yet; it exists so a self-hosted OAuth2 provider (e.g. Gitea) can wire - * into it without inventing its own idempotency check. + * Callers invoke this unconditionally on every repository connection; it + * no-ops for providers whose adapter reports no per-repository webhook is + * needed (e.g. GitHub App, which delivers events platform-wide), so no + * endpoint needs to know which providers require one. */ class RepositoryWebhooks { + public function __construct( + protected Factory $vcsFactory, + ) { + } + /** - * Creates a webhook for $owner/$repositoryName unless a `repositories` - * document already exists for this installation + provider repository - * (an earlier connection already went through this path). + * Creates a webhook for $owner/$repositoryName unless the adapter's + * provider doesn't need one, or a `repositories` document already + * exists for this installation + provider repository (an earlier + * connection already went through this path). */ public function ensure( Git $adapter, @@ -29,9 +37,11 @@ class RepositoryWebhooks string $providerRepositoryId, string $owner, string $repositoryName, - string $url, - string $secret, ): void { + if (!$adapter->requiresRepositoryWebhook()) { + return; + } + $connections = $dbForPlatform->count('repositories', [ Query::equal('installationInternalId', [$installation->getSequence()]), Query::equal('providerRepositoryId', [$providerRepositoryId]), @@ -41,10 +51,31 @@ class RepositoryWebhooks return; } + $provider = $installation->getAttribute('provider', 'github'); + $url = $this->buildWebhookUrl($provider); + $secret = $this->vcsFactory->getWebhookSecret($provider); + try { $adapter->createWebhook($owner, $repositoryName, $url, $secret); } catch (\Throwable $error) { throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, 'Failed to create repository webhook: ' . $error->getMessage()); } } + + protected function buildWebhookUrl(string $provider): string + { + $endpoint = System::getEnv('_APP_VCS_WEBHOOK_URL', ''); + if (empty($endpoint)) { + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; + $endpoint = $protocol . '://' . System::getEnv('_APP_DOMAIN', 'localhost'); + } + + // Accept the base with or without a trailing /v1 (both are used in practice). + $endpoint = \rtrim($endpoint, '/'); + if (\str_ends_with($endpoint, '/v1')) { + $endpoint = \substr($endpoint, 0, -3); + } + + return $endpoint . '/v1/vcs/' . $provider . '/events'; + } } diff --git a/tests/unit/Vcs/RepositoryWebhooksTest.php b/tests/unit/Vcs/RepositoryWebhooksTest.php index 6f5a546ee1..40f3e5db3d 100644 --- a/tests/unit/Vcs/RepositoryWebhooksTest.php +++ b/tests/unit/Vcs/RepositoryWebhooksTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Tests\Unit\Vcs; use Appwrite\Extend\Exception; +use Appwrite\Vcs\Factory; use Appwrite\Vcs\RepositoryWebhooks; use PHPUnit\Framework\TestCase; use Utopia\Database\Database; @@ -13,46 +14,80 @@ use Utopia\VCS\Adapter\Git; final class RepositoryWebhooksTest extends TestCase { + public function testSkipsWhenAdapterDoesNotRequireWebhook(): void + { + $adapter = $this->createMock(Git::class); + $adapter->method('requiresRepositoryWebhook')->willReturn(false); + $adapter->expects($this->never())->method('createWebhook'); + + $db = $this->createMock(Database::class); + $db->expects($this->never())->method('count'); + + $installation = new Document(['$sequence' => 1, 'provider' => 'github']); + + $this->ensure($adapter, $installation, $db, $this->factory('secret')); + } + public function testCreatesWebhookWhenNoExistingConnection(): void { - $db = $this->createMock(Database::class); - $db->expects($this->once())->method('count')->willReturn(1); - $adapter = $this->createMock(Git::class); + $adapter->method('requiresRepositoryWebhook')->willReturn(true); $adapter->expects($this->once()) ->method('createWebhook') ->with('owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret') ->willReturn(1); - $installation = new Document(['$sequence' => 1]); + $db = $this->createMock(Database::class); + $db->expects($this->once())->method('count')->willReturn(1); - (new RepositoryWebhooks())->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret'); + $installation = new Document(['$sequence' => 1, 'provider' => 'gitea']); + + $this->ensure($adapter, $installation, $db, $this->factory('secret')); } public function testSkipsWhenRepositoryAlreadyConnected(): void { + $adapter = $this->createMock(Git::class); + $adapter->method('requiresRepositoryWebhook')->willReturn(true); + $adapter->expects($this->never())->method('createWebhook'); + $db = $this->createMock(Database::class); $db->expects($this->once())->method('count')->willReturn(2); - $adapter = $this->createMock(Git::class); - $adapter->expects($this->never())->method('createWebhook'); + $installation = new Document(['$sequence' => 1, 'provider' => 'gitea']); - $installation = new Document(['$sequence' => 1]); - - (new RepositoryWebhooks())->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret'); + $this->ensure($adapter, $installation, $db, $this->factory('secret')); } public function testAdapterFailureIsWrapped(): void { + $adapter = $this->createMock(Git::class); + $adapter->method('requiresRepositoryWebhook')->willReturn(true); + $adapter->method('createWebhook')->willThrowException(new \RuntimeException('provider unreachable')); + $db = $this->createMock(Database::class); $db->method('count')->willReturn(1); - $adapter = $this->createMock(Git::class); - $adapter->method('createWebhook')->willThrowException(new \RuntimeException('provider unreachable')); - - $installation = new Document(['$sequence' => 1]); + $installation = new Document(['$sequence' => 1, 'provider' => 'gitea']); $this->expectException(Exception::class); - (new RepositoryWebhooks())->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret'); + $this->ensure($adapter, $installation, $db, $this->factory('secret')); + } + + protected function ensure(Git $adapter, Document $installation, Database $db, Factory $vcsFactory): void + { + \putenv('_APP_VCS_WEBHOOK_URL=https://example.com'); + + (new RepositoryWebhooks($vcsFactory))->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo'); + + \putenv('_APP_VCS_WEBHOOK_URL'); + } + + protected function factory(string $secret): Factory + { + $factory = $this->createMock(Factory::class); + $factory->method('getWebhookSecret')->willReturn($secret); + + return $factory; } } From 6c47bcd0672a3406ac5f9841f98c6209ed91963a Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 9 Jul 2026 15:27:00 +0530 Subject: [PATCH 05/52] feat: add Factory::getWebhookSecret Belongs on Factory (the registry-backed service), not on the Gitea PR that was consuming it -- moving it here so RepositoryWebhooks (PR2) can depend on it without a layering violation. --- src/Appwrite/Vcs/Factory.php | 8 ++++++++ tests/unit/Vcs/FactoryTest.php | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Appwrite/Vcs/Factory.php b/src/Appwrite/Vcs/Factory.php index edd7f5e939..3a6bb687a8 100644 --- a/src/Appwrite/Vcs/Factory.php +++ b/src/Appwrite/Vcs/Factory.php @@ -117,6 +117,14 @@ class Factory return $adapter; } + /** + * Webhook secret configured for a provider, empty if none is set. + */ + public function getWebhookSecret(string $key): string + { + return $this->getEnv($key, 'WEBHOOK_SECRET'); + } + protected function getEnv(string $key, string $name): string { $variable = $this->registry[$key]['envVariables'][$name] ?? ''; diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index 80d7f12918..c1a6989335 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -91,6 +91,17 @@ final class FactoryTest extends TestCase $this->assertSame(['test'], $factory->getProviders()); } + public function testGetWebhookSecret(): void + { + $factory = new Factory($this->cache(), ['github' => $this->githubEntry()]); + + $this->assertSame('', $factory->getWebhookSecret('github')); + + \putenv('_APP_VCS_GITHUB_WEBHOOK_SECRET=hunter2'); + $this->assertSame('hunter2', $factory->getWebhookSecret('github')); + \putenv('_APP_VCS_GITHUB_WEBHOOK_SECRET'); + } + protected function cache(): Cache { return new Cache(new None()); @@ -110,6 +121,7 @@ final class FactoryTest extends TestCase 'APP_ID' => '_APP_VCS_GITHUB_APP_ID', 'CLIENT_ID' => '_APP_VCS_GITHUB_CLIENT_ID', 'CLIENT_SECRET' => '_APP_VCS_GITHUB_CLIENT_SECRET', + 'WEBHOOK_SECRET' => '_APP_VCS_GITHUB_WEBHOOK_SECRET', ], 'requiredEnvVariables' => ['APP_NAME', 'PRIVATE_KEY', 'APP_ID', 'CLIENT_ID', 'CLIENT_SECRET'], ]; From 616f846eeddb3d37b6a9e706bf595183a1eff7b7 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 9 Jul 2026 15:31:12 +0530 Subject: [PATCH 06/52] fix: address review findings on InstallationTokens and RepositoryWebhooks - InstallationTokens: the expiry guard had flipped semantics from the original code it was extracted from -- a missing/null expiry now triggered an unconditional refresh (and could throw GENERAL_RATE_LIMIT_EXCEEDED for a still-valid non-expiring token) instead of being treated as not-expired. Restored the original semantics explicitly and added a regression test. - RepositoryWebhooks: moved the redesign that was accidentally committed on the Gitea PR back to where it belongs -- constructor now takes Factory, gates on the adapter's requiresRepositoryWebhook() capability, and derives the webhook URL/secret internally instead of taking them as caller-supplied params. Documented the calling convention the idempotency check relies on (repository document must be persisted before calling ensure()). - Removed the misleading unused constructor param on the test's fake OAuth2 double. - Applied Rector's WithCallbackIdenticalToStandaloneAssertsRector fix to the updateDocument callback assertion (CI refactor:check gate). --- app/init/resources.php | 2 +- src/Appwrite/Vcs/InstallationTokens.php | 7 ++- src/Appwrite/Vcs/RepositoryWebhooks.php | 53 +++++++++++++++--- tests/unit/Vcs/InstallationTokensTest.php | 32 ++++++++--- tests/unit/Vcs/RepositoryWebhooksTest.php | 65 +++++++++++++++++------ 5 files changed, 127 insertions(+), 32 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index 180afd21bd..d02ed42c5a 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -350,7 +350,7 @@ $container->set('promiseAdapter', fn ($register) => $register->get('promiseAdapt $container->set('vcsFactory', fn (Cache $cache) => new VcsFactory($cache), ['cache']); $container->set('installationTokens', fn () => new InstallationTokens(), []); -$container->set('repositoryWebhooks', fn () => new RepositoryWebhooks(), []); +$container->set('repositoryWebhooks', fn (VcsFactory $vcsFactory) => new RepositoryWebhooks($vcsFactory), ['vcsFactory']); $container->set('plan', fn () => []); diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php index cb8506f99b..5d2f902ab5 100644 --- a/src/Appwrite/Vcs/InstallationTokens.php +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -38,7 +38,12 @@ class InstallationTokens $installation = $installation->setAttribute('personalAccessToken', $accessToken); - if (!empty($accessTokenExpiry) && new \DateTime($accessTokenExpiry) >= new \DateTime('now')) { + // A missing expiry means the provider issued a non-expiring token + // (or none was recorded yet) -- treat it as not expired rather than + // forcing a refresh on every call. + $isExpired = !empty($accessTokenExpiry) && new \DateTime($accessTokenExpiry) < new \DateTime('now'); + + if (!$isExpired) { return $installation; } diff --git a/src/Appwrite/Vcs/RepositoryWebhooks.php b/src/Appwrite/Vcs/RepositoryWebhooks.php index a39a679bc8..f13b64242a 100644 --- a/src/Appwrite/Vcs/RepositoryWebhooks.php +++ b/src/Appwrite/Vcs/RepositoryWebhooks.php @@ -6,21 +6,35 @@ use Appwrite\Extend\Exception; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; +use Utopia\System\System; use Utopia\VCS\Adapter\Git; /** * Creates per-repository webhooks for self-hosted providers. * - * GitHub App deliveries need no per-repository webhook, so no endpoint calls - * this yet; it exists so a self-hosted OAuth2 provider (e.g. Gitea) can wire - * into it without inventing its own idempotency check. + * Callers invoke this unconditionally on every repository connection; it + * no-ops for providers whose adapter reports no per-repository webhook is + * needed (e.g. GitHub App, which delivers events platform-wide), so no + * endpoint needs to know which providers require one. */ class RepositoryWebhooks { + public function __construct( + protected Factory $vcsFactory, + ) { + } + /** - * Creates a webhook for $owner/$repositoryName unless a `repositories` - * document already exists for this installation + provider repository - * (an earlier connection already went through this path). + * Creates a webhook for $owner/$repositoryName unless the adapter's + * provider doesn't need one, or a `repositories` document already + * exists for this installation + provider repository (an earlier + * connection already went through this path). + * + * Callers must persist the current repository connection's document + * (via createDocument) before calling this — the idempotency check + * counts existing `repositories` rows for this installation + + * providerRepositoryId and expects to see itself already included + * (count 1 on first connection, 2+ on any repeat). */ public function ensure( Git $adapter, @@ -29,9 +43,11 @@ class RepositoryWebhooks string $providerRepositoryId, string $owner, string $repositoryName, - string $url, - string $secret, ): void { + if (!$adapter->requiresRepositoryWebhook()) { + return; + } + $connections = $dbForPlatform->count('repositories', [ Query::equal('installationInternalId', [$installation->getSequence()]), Query::equal('providerRepositoryId', [$providerRepositoryId]), @@ -41,10 +57,31 @@ class RepositoryWebhooks return; } + $provider = $installation->getAttribute('provider', 'github'); + $url = $this->buildWebhookUrl($provider); + $secret = $this->vcsFactory->getWebhookSecret($provider); + try { $adapter->createWebhook($owner, $repositoryName, $url, $secret); } catch (\Throwable $error) { throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, 'Failed to create repository webhook: ' . $error->getMessage()); } } + + protected function buildWebhookUrl(string $provider): string + { + $endpoint = System::getEnv('_APP_VCS_WEBHOOK_URL', ''); + if (empty($endpoint)) { + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; + $endpoint = $protocol . '://' . System::getEnv('_APP_DOMAIN', 'localhost'); + } + + // Accept the base with or without a trailing /v1 (both are used in practice). + $endpoint = \rtrim($endpoint, '/'); + if (\str_ends_with($endpoint, '/v1')) { + $endpoint = \substr($endpoint, 0, -3); + } + + return $endpoint . '/v1/vcs/' . $provider . '/events'; + } } diff --git a/tests/unit/Vcs/InstallationTokensTest.php b/tests/unit/Vcs/InstallationTokensTest.php index a6da0af674..f661ca771b 100644 --- a/tests/unit/Vcs/InstallationTokensTest.php +++ b/tests/unit/Vcs/InstallationTokensTest.php @@ -28,7 +28,24 @@ final class InstallationTokensTest extends TestCase 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), 3600), ]); - $oauth2 = $this->fakeOAuth2(refreshCalls: 0); + $oauth2 = $this->fakeOAuth2(); + + $result = (new InstallationTokens())->refresh($installation, $this->db(), $oauth2); + + $this->assertSame('valid-token', $result->getAttribute('personalAccessToken')); + $this->assertSame(0, $oauth2->refreshCalls); + } + + public function testMissingExpiryIsNotRefreshed(): void + { + $installation = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'valid-token', + 'personalRefreshToken' => 'valid-refresh', + 'personalAccessTokenExpiry' => null, + ]); + + $oauth2 = $this->fakeOAuth2(); $result = (new InstallationTokens())->refresh($installation, $this->db(), $oauth2); @@ -43,8 +60,9 @@ final class InstallationTokensTest extends TestCase $db->expects($this->once()) ->method('updateDocument') ->with('installations', 'installation1', $this->callback(function (Document $update) { - return $update->getAttribute('personalAccessToken') === 'fresh-token' - && $update->getAttribute('personalRefreshToken') === 'fresh-refresh'; + $this->assertSame('fresh-token', $update->getAttribute('personalAccessToken')); + $this->assertSame('fresh-refresh', $update->getAttribute('personalRefreshToken')); + return true; })) ->willReturnArgument(2); @@ -55,7 +73,7 @@ final class InstallationTokensTest extends TestCase 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), -3600), ]); - $oauth2 = $this->fakeOAuth2(refreshCalls: 1); + $oauth2 = $this->fakeOAuth2(); $result = (new InstallationTokens())->refresh($installation, $db, $oauth2); @@ -72,7 +90,7 @@ final class InstallationTokensTest extends TestCase 'providerAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), 3600), ]); - $oauth2 = $this->fakeOAuth2(refreshCalls: 0); + $oauth2 = $this->fakeOAuth2(); $result = (new InstallationTokens())->refresh($installation, $this->db(), $oauth2, $identity); @@ -88,13 +106,13 @@ final class InstallationTokensTest extends TestCase 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), -3600), ]); - $oauth2 = $this->fakeOAuth2(refreshCalls: 1, emptyUserId: true); + $oauth2 = $this->fakeOAuth2(emptyUserId: true); $this->expectException(Exception::class); (new InstallationTokens())->refresh($installation, $this->db(), $oauth2); } - protected function fakeOAuth2(int $refreshCalls, bool $emptyUserId = false) + protected function fakeOAuth2(bool $emptyUserId = false) { return new class ($emptyUserId) extends OAuth2 { public int $refreshCalls = 0; diff --git a/tests/unit/Vcs/RepositoryWebhooksTest.php b/tests/unit/Vcs/RepositoryWebhooksTest.php index 6f5a546ee1..40f3e5db3d 100644 --- a/tests/unit/Vcs/RepositoryWebhooksTest.php +++ b/tests/unit/Vcs/RepositoryWebhooksTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Tests\Unit\Vcs; use Appwrite\Extend\Exception; +use Appwrite\Vcs\Factory; use Appwrite\Vcs\RepositoryWebhooks; use PHPUnit\Framework\TestCase; use Utopia\Database\Database; @@ -13,46 +14,80 @@ use Utopia\VCS\Adapter\Git; final class RepositoryWebhooksTest extends TestCase { + public function testSkipsWhenAdapterDoesNotRequireWebhook(): void + { + $adapter = $this->createMock(Git::class); + $adapter->method('requiresRepositoryWebhook')->willReturn(false); + $adapter->expects($this->never())->method('createWebhook'); + + $db = $this->createMock(Database::class); + $db->expects($this->never())->method('count'); + + $installation = new Document(['$sequence' => 1, 'provider' => 'github']); + + $this->ensure($adapter, $installation, $db, $this->factory('secret')); + } + public function testCreatesWebhookWhenNoExistingConnection(): void { - $db = $this->createMock(Database::class); - $db->expects($this->once())->method('count')->willReturn(1); - $adapter = $this->createMock(Git::class); + $adapter->method('requiresRepositoryWebhook')->willReturn(true); $adapter->expects($this->once()) ->method('createWebhook') ->with('owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret') ->willReturn(1); - $installation = new Document(['$sequence' => 1]); + $db = $this->createMock(Database::class); + $db->expects($this->once())->method('count')->willReturn(1); - (new RepositoryWebhooks())->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret'); + $installation = new Document(['$sequence' => 1, 'provider' => 'gitea']); + + $this->ensure($adapter, $installation, $db, $this->factory('secret')); } public function testSkipsWhenRepositoryAlreadyConnected(): void { + $adapter = $this->createMock(Git::class); + $adapter->method('requiresRepositoryWebhook')->willReturn(true); + $adapter->expects($this->never())->method('createWebhook'); + $db = $this->createMock(Database::class); $db->expects($this->once())->method('count')->willReturn(2); - $adapter = $this->createMock(Git::class); - $adapter->expects($this->never())->method('createWebhook'); + $installation = new Document(['$sequence' => 1, 'provider' => 'gitea']); - $installation = new Document(['$sequence' => 1]); - - (new RepositoryWebhooks())->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret'); + $this->ensure($adapter, $installation, $db, $this->factory('secret')); } public function testAdapterFailureIsWrapped(): void { + $adapter = $this->createMock(Git::class); + $adapter->method('requiresRepositoryWebhook')->willReturn(true); + $adapter->method('createWebhook')->willThrowException(new \RuntimeException('provider unreachable')); + $db = $this->createMock(Database::class); $db->method('count')->willReturn(1); - $adapter = $this->createMock(Git::class); - $adapter->method('createWebhook')->willThrowException(new \RuntimeException('provider unreachable')); - - $installation = new Document(['$sequence' => 1]); + $installation = new Document(['$sequence' => 1, 'provider' => 'gitea']); $this->expectException(Exception::class); - (new RepositoryWebhooks())->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret'); + $this->ensure($adapter, $installation, $db, $this->factory('secret')); + } + + protected function ensure(Git $adapter, Document $installation, Database $db, Factory $vcsFactory): void + { + \putenv('_APP_VCS_WEBHOOK_URL=https://example.com'); + + (new RepositoryWebhooks($vcsFactory))->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo'); + + \putenv('_APP_VCS_WEBHOOK_URL'); + } + + protected function factory(string $secret): Factory + { + $factory = $this->createMock(Factory::class); + $factory->method('getWebhookSecret')->willReturn($secret); + + return $factory; } } From b5c7e2829ed93ad46c60799e52055dc4bbba4e73 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 9 Jul 2026 18:16:05 +0530 Subject: [PATCH 07/52] style: trim InstallationTokens docblocks to what earns its place The class and method docblocks mostly restated what the name/signature already said. Kept only the non-obvious parts: why this is separate from Factory, why the identity fallback exists, and the expiry-guard semantics (already a standalone comment). --- src/Appwrite/Vcs/InstallationTokens.php | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php index 5d2f902ab5..44f9d10c27 100644 --- a/src/Appwrite/Vcs/InstallationTokens.php +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -8,22 +8,12 @@ use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; -/** - * Refreshes and persists an installation's personal OAuth2 tokens. - * - * Only relevant for personal (OAuth2-authenticated) installations. Owns the - * one database write this concern needs, so adapter construction (Factory) - * stays free of persistence. - */ +// Owns the one database write OAuth2 token refresh needs, so Factory stays +// free of persistence. class InstallationTokens { - /** - * Returns the installation with a valid, unexpired access token attached. - * Falls back to $identity's tokens when the installation has none yet - * (first repository action after connecting a personal installation). - * Refreshes and persists to `installations` only when the token is - * actually expired. - */ + // Falls back to $identity's tokens when the installation has none yet -- + // the first repository action after connecting a personal installation. public function refresh(Document $installation, Database $dbForPlatform, OAuth2 $oauth2, ?Document $identity = null): Document { $accessToken = $installation->getAttribute('personalAccessToken'); From 587073d9368ff5c1b8fb64ea4cc1853dfbcf1231 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 9 Jul 2026 18:29:01 +0530 Subject: [PATCH 08/52] fix: address Greptile findings on Gitea webhook wiring and OAuth2 scope - Widened Gitea OAuth2 scopes from read:user alone to also include read:repository, write:repository, read:organization. The resulting token is reused for every VCS operation on the installation (list repos, read contents, create webhooks), not just login, so read:user alone would fail with 403s on nearly every call. - Made WEBHOOK_SECRET required for Gitea (added to requiredEnvVariables in app/config/vcs.php) and changed the validation guard in Gitea/Events/Create.php to fail closed instead of open when it's unset. GitHub App deliveries stay validated independently via a signed JWT even without a webhook secret configured; Gitea has no equivalent fallback, so an empty secret there meant no verification at all. - Fixed cross-provider repository ID collision: Gitea and GitHub repository IDs are independent numeric sequences and can collide. repositories has no provider attribute to filter the query on, so resolveAdapterForRepository() now checks the matched installation's own provider and skips anything that isn't 'gitea', instead of silently building a mismatched adapter for a same-ID GitHub repo. - Fixed the orphaned-repository-document bug across all four connect paths (Functions/Sites Create/Update): getOwnerName/getRepositoryName/ ensure() now only run when the adapter's requiresRepositoryWebhook() is true (GitHub's is false, so this also removes two previously unconditional GitHub API round-trips on every GitHub connect), and any failure during that block now deletes the just-created repositories document before rethrowing -- otherwise a failed first attempt left a row behind that made every retry's connection count look like a repeat, silently skipping webhook creation forever. --- app/config/vcs.php | 5 ++++- src/Appwrite/Auth/OAuth2/Gitea.php | 6 ++++++ .../Modules/Functions/Http/Functions/Create.php | 13 ++++++++++--- .../Modules/Functions/Http/Functions/Update.php | 13 ++++++++++--- .../Platform/Modules/Sites/Http/Sites/Create.php | 16 +++++++++++++--- .../Platform/Modules/Sites/Http/Sites/Update.php | 13 ++++++++++--- .../Modules/VCS/Http/Gitea/Events/Create.php | 14 +++++++++++++- 7 files changed, 66 insertions(+), 14 deletions(-) diff --git a/app/config/vcs.php b/app/config/vcs.php index 1f22e474d9..787fb07ecc 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -30,6 +30,9 @@ return [ 'CLIENT_SECRET' => '_APP_VCS_GITEA_CLIENT_SECRET', 'WEBHOOK_SECRET' => '_APP_VCS_GITEA_WEBHOOK_SECRET', ], - 'requiredEnvVariables' => ['ENDPOINT', 'CLIENT_ID', 'CLIENT_SECRET'], + // WEBHOOK_SECRET is required (unlike GitHub's) because a Gitea webhook + // has no other verification layer -- GitHub App deliveries also carry + // a signed JWT Appwrite always validates independently of the secret. + 'requiredEnvVariables' => ['ENDPOINT', 'CLIENT_ID', 'CLIENT_SECRET', 'WEBHOOK_SECRET'], ], ]; diff --git a/src/Appwrite/Auth/OAuth2/Gitea.php b/src/Appwrite/Auth/OAuth2/Gitea.php index af82dd440f..dd649047d1 100644 --- a/src/Appwrite/Auth/OAuth2/Gitea.php +++ b/src/Appwrite/Auth/OAuth2/Gitea.php @@ -27,8 +27,14 @@ class Gitea extends OAuth2 /** * @var array */ + // The resulting personal token is reused for every VCS operation on the + // installation (list/create repos, read contents, create webhooks, + // comment on PRs) via Factory::fromInstallation(), not just login. protected array $scopes = [ 'read:user', + 'read:repository', + 'write:repository', + 'read:organization', ]; /** diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php index d12d652749..613efe402c 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -307,9 +307,16 @@ class Create extends Base ])); $providerAdapter = $vcsFactory->fromInstallation($installation); - $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); - $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); - $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); + if ($providerAdapter->requiresRepositoryWebhook()) { + try { + $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); + $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); + $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); + } catch (\Throwable $error) { + $dbForPlatform->deleteDocument('repositories', $repository->getId()); + throw $error; + } + } $function->setAttribute('repositoryId', $repository->getId()); $function->setAttribute('repositoryInternalId', $repository->getSequence()); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php index 0ebfd9e665..7418dc809a 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -248,9 +248,16 @@ class Update extends Base $repositoryInternalId = $repository->getSequence(); $providerAdapter = $vcsFactory->fromInstallation($installation); - $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); - $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); - $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); + if ($providerAdapter->requiresRepositoryWebhook()) { + try { + $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); + $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); + $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); + } catch (\Throwable $error) { + $dbForPlatform->deleteDocument('repositories', $repository->getId()); + throw $error; + } + } } $live = true; diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php index 617acb95e4..d6e00b39e5 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php @@ -223,9 +223,19 @@ class Create extends Base $repository = $dbForPlatform->createDocument('repositories', $repository); $providerAdapter = $vcsFactory->fromInstallation($installation); - $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); - $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); - $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); + if ($providerAdapter->requiresRepositoryWebhook()) { + try { + $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); + $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); + $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); + } catch (\Throwable $error) { + // Don't leave an orphaned repositories document behind -- it + // would make the next retry's connection count look like a + // repeat, silently skipping webhook creation. + $dbForPlatform->deleteDocument('repositories', $repository->getId()); + throw $error; + } + } $site->setAttribute('repositoryId', $repository->getId()); $site->setAttribute('repositoryInternalId', $repository->getSequence()); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php index eada5634d8..83de5f3c2f 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php @@ -246,9 +246,16 @@ class Update extends Base $repositoryInternalId = $repository->getSequence(); $providerAdapter = $vcsFactory->fromInstallation($installation); - $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); - $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); - $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); + if ($providerAdapter->requiresRepositoryWebhook()) { + try { + $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); + $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); + $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); + } catch (\Throwable $error) { + $dbForPlatform->deleteDocument('repositories', $repository->getId()); + throw $error; + } + } } $live = true; diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index 17f02c577a..33fe0b3e10 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -67,7 +67,11 @@ class Create extends Action $signature = $request->getHeaderLine($vcs->getSignatureHeaderName(), ''); $secretKey = System::getEnv('_APP_VCS_GITEA_WEBHOOK_SECRET', ''); - $valid = empty($secretKey) ? true : $vcs->validateWebhookEvent($payload, $signature, $secretKey); + // Fail closed, unlike GitHub App deliveries: those also carry a + // signed JWT Appwrite always validates, so an empty webhook secret + // there still leaves a real check in place. Gitea has no such + // fallback -- an empty secret here would mean no verification at all. + $valid = !empty($secretKey) && $vcs->validateWebhookEvent($payload, $signature, $secretKey); Span::add('vcs.gitea.event.signature.valid', $valid); if (!$valid) { @@ -100,6 +104,14 @@ class Create extends Action return null; } + // `repositories` has no provider attribute -- providerRepositoryId is + // provider-scoped numeric IDs from independent servers, so a GitHub + // and a Gitea repository can collide on the same ID. Filter by the + // matched installation's own provider instead of the query. + if ($installation->getAttribute('provider', 'github') !== 'gitea') { + return null; + } + try { return $vcsFactory->fromInstallation($installation); } catch (\Throwable $error) { From 9818456e91b99002f0b91969d344f557f45d7c0b Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 9 Jul 2026 18:58:22 +0530 Subject: [PATCH 09/52] refactor: inject resolved vcs adapters via closures instead of Factory Actions now receive vcsForInstallation/vcsForProvider closures that resolve an adapter for one call, matching the getProjectDB/getLogsDB pattern, instead of injecting Factory and calling it inside the action body. --- app/controllers/mock.php | 7 +++---- app/init/resources.php | 4 ++++ .../Modules/Functions/Http/Deployments/Template/Create.php | 7 +++---- .../Modules/Functions/Http/Deployments/Vcs/Create.php | 7 +++---- .../Platform/Modules/Functions/Http/Functions/Create.php | 7 +++---- .../Platform/Modules/Functions/Http/Functions/Update.php | 7 +++---- src/Appwrite/Platform/Modules/Functions/Workers/Builds.php | 7 +++---- .../Modules/Sites/Http/Deployments/Template/Create.php | 7 +++---- .../Platform/Modules/Sites/Http/Deployments/Vcs/Create.php | 7 +++---- src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php | 7 +++---- .../Modules/VCS/Http/GitHub/Authorize/External/Update.php | 7 +++---- .../Platform/Modules/VCS/Http/GitHub/Callback/Get.php | 7 +++---- .../Platform/Modules/VCS/Http/GitHub/Events/Create.php | 7 +++---- .../VCS/Http/Installations/Repositories/Branches/XList.php | 7 +++---- .../VCS/Http/Installations/Repositories/Contents/Get.php | 7 +++---- .../Modules/VCS/Http/Installations/Repositories/Create.php | 7 +++---- .../Http/Installations/Repositories/Detections/Create.php | 7 +++---- .../Modules/VCS/Http/Installations/Repositories/Get.php | 7 +++---- .../Modules/VCS/Http/Installations/Repositories/XList.php | 7 +++---- 19 files changed, 58 insertions(+), 72 deletions(-) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index a2170ab24f..2e5e4db6f6 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -5,7 +5,6 @@ global $utopia, $request, $response; use Appwrite\Extend\Exception; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; @@ -227,11 +226,11 @@ Http::get('/v1/mock/github/callback') ->label('docs', false) ->param('providerInstallationId', '', new UID(), 'GitHub installation ID') ->param('projectId', '', new UID(), 'Project ID of the project where app is to be installed') - ->inject('vcsFactory') + ->inject('vcsForProvider') ->inject('project') ->inject('response') ->inject('dbForPlatform') - ->action(function (string $providerInstallationId, string $projectId, Factory $vcsFactory, Document $project, Response $response, Database $dbForPlatform) { + ->action(function (string $providerInstallationId, string $projectId, callable $vcsForProvider, Document $project, Response $response, Database $dbForPlatform) { $isDevelopment = System::getEnv('_APP_ENV', 'development') === 'development'; if (!$isDevelopment) { @@ -249,7 +248,7 @@ Http::get('/v1/mock/github/callback') throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing provider installation ID'); } - $vcs = $vcsFactory->fromProvider('github'); + $vcs = $vcsForProvider('github'); $vcs->initializeVariables($providerInstallationId, System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'), System::getEnv('_APP_VCS_GITHUB_APP_ID')); $owner = $vcs->getOwnerName($providerInstallationId); diff --git a/app/init/resources.php b/app/init/resources.php index dba6816701..e5fa068d01 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -348,6 +348,10 @@ $container->set('promiseAdapter', fn ($register) => $register->get('promiseAdapt $container->set('vcsFactory', fn (Cache $cache) => new VcsFactory($cache), ['cache']); +$container->set('vcsForInstallation', fn (VcsFactory $vcsFactory) => fn (Document $installation) => $vcsFactory->fromInstallation($installation), ['vcsFactory']); + +$container->set('vcsForProvider', fn (VcsFactory $vcsFactory) => fn (string $provider) => $vcsFactory->fromProvider($provider), ['vcsFactory']); + $container->set('plan', fn () => []); $container->set('smsRates', fn () => []); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php index cad0336071..0aa7b762ec 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php @@ -12,7 +12,6 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -81,7 +80,7 @@ class Create extends Base ->inject('queueForEvents') ->inject('project') ->inject('publisherForBuilds') - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -102,7 +101,7 @@ class Create extends Base Event $queueForEvents, Document $project, BuildPublisher $publisherForBuilds, - Factory $vcsFactory, + callable $vcsForInstallation, Authorization $authorization, array $platform ) { @@ -135,7 +134,7 @@ class Create extends Base dbForProject: $dbForProject, publisherForBuilds: $publisherForBuilds, template: $template, - vcs: $vcsFactory->fromInstallation($installation), + vcs: $vcsForInstallation($installation), activate: $activate, platform: $platform, referenceType: $type, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php index 5fe33354dd..739bb2c52f 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php @@ -11,7 +11,6 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\UID; @@ -73,7 +72,7 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('publisherForBuilds') - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('platform') ->callback($this->action(...)); } @@ -90,7 +89,7 @@ class Create extends Base Document $project, Event $queueForEvents, BuildPublisher $publisherForBuilds, - Factory $vcsFactory, + callable $vcsForInstallation, array $platform, ) { $function = $dbForProject->getDocument('functions', $functionId); @@ -115,7 +114,7 @@ class Create extends Base dbForProject: $dbForProject, publisherForBuilds: $publisherForBuilds, template: $template, - vcs: $vcsFactory->fromInstallation($installation), + vcs: $vcsForInstallation($installation), activate: $activate, platform: $platform, reference: $reference, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php index bbb6ecf265..a6ecba76b1 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -20,7 +20,6 @@ use Appwrite\Task\Validator\Cron; use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model\Rule; -use Appwrite\Vcs\Factory; use Utopia\Abuse\Abuse; use Utopia\Config\Config; use Utopia\Database\Database; @@ -128,7 +127,7 @@ class Create extends Base ->inject('publisherForFunctions') ->inject('dbForPlatform') ->inject('request') - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -172,7 +171,7 @@ class Create extends Base FunctionPublisher $publisherForFunctions, Database $dbForPlatform, Request $request, - Factory $vcsFactory, + callable $vcsForInstallation, Authorization $authorization, array $platform ) { @@ -344,7 +343,7 @@ class Create extends Base dbForProject: $dbForProject, publisherForBuilds: $publisherForBuilds, template: $template, - vcs: $vcsFactory->fromInstallation($installation), + vcs: $vcsForInstallation($installation), activate: true, platform: $platform, reference: $providerBranch, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php index f275a15aee..f4345bd2f7 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -13,7 +13,6 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Task\Validator\Cron; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Executor\Executor; use Utopia\Config\Config; use Utopia\Database\Database; @@ -112,7 +111,7 @@ class Update extends Base ->inject('queueForEvents') ->inject('publisherForBuilds') ->inject('dbForPlatform') - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('executor') ->inject('authorization') ->inject('platform') @@ -149,7 +148,7 @@ class Update extends Base Event $queueForEvents, BuildPublisher $publisherForBuilds, Database $dbForPlatform, - Factory $vcsFactory, + callable $vcsForInstallation, Executor $executor, Authorization $authorization, array $platform @@ -309,7 +308,7 @@ class Update extends Base // Redeploy logic if (!$isConnected && !empty($providerRepositoryId)) { - $this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsFactory->fromInstallation($installation), true, $platform); + $this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsForInstallation($installation), true, $platform); } // Inform scheduler if function is still active diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 2e0eda5caa..f194e1026d 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -16,7 +16,6 @@ use Appwrite\Filter\BranchDomain as BranchDomainFilter; use Appwrite\Usage\Context; use Appwrite\Utopia\Response\Model\Deployment; use Appwrite\Vcs\Comment; -use Appwrite\Vcs\Factory; use Exception; use Executor\Exception as ExecutorException; use Executor\Exception\Timeout as ExecutorTimeout; @@ -90,7 +89,7 @@ class Builds extends Action ->inject('queueForRealtime') ->inject('usage') ->inject('publisherForUsage') - ->inject('vcsFactory') + ->inject('vcsForProvider') ->inject('dbForProject') ->inject('deviceForFunctions') ->inject('deviceForSites') @@ -116,7 +115,7 @@ class Builds extends Action Realtime $queueForRealtime, Context $usage, UsagePublisher $publisherForUsage, - Factory $vcsFactory, + callable $vcsForProvider, Database $dbForProject, Device $deviceForFunctions, Device $deviceForSites, @@ -146,7 +145,7 @@ class Builds extends Action switch ($type) { case BUILD_TYPE_DEPLOYMENT: case BUILD_TYPE_RETRY: - $vcs = $vcsFactory->fromProvider('github'); + $vcs = $vcsForProvider('github'); $this->buildDeployment( $deviceForFunctions, $deviceForSites, diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index bb31f56d80..49fcd497c3 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php @@ -12,7 +12,6 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -82,7 +81,7 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('publisherForBuilds') - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -103,7 +102,7 @@ class Create extends Base Document $project, Event $queueForEvents, BuildPublisher $publisherForBuilds, - Factory $vcsFactory, + callable $vcsForInstallation, Authorization $authorization, array $platform ) { @@ -136,7 +135,7 @@ class Create extends Base dbForPlatform: $dbForPlatform, publisherForBuilds: $publisherForBuilds, template: $template, - vcs: $vcsFactory->fromInstallation($installation), + vcs: $vcsForInstallation($installation), activate: $activate, authorization: $authorization, platform: $platform diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php index 986788bd0c..98d544cd37 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php @@ -11,7 +11,6 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; @@ -74,7 +73,7 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('publisherForBuilds') - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -92,7 +91,7 @@ class Create extends Base Document $project, Event $queueForEvents, BuildPublisher $publisherForBuilds, - Factory $vcsFactory, + callable $vcsForInstallation, Authorization $authorization, array $platform ) { @@ -119,7 +118,7 @@ class Create extends Base dbForPlatform: $dbForPlatform, publisherForBuilds: $publisherForBuilds, template: $template, - vcs: $vcsFactory->fromInstallation($installation), + vcs: $vcsForInstallation($installation), activate: $activate, authorization: $authorization, reference: $reference, diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php index 6604354436..219415d08c 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php @@ -11,7 +11,6 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Executor\Executor; use Utopia\Config\Config; use Utopia\Database\Database; @@ -108,7 +107,7 @@ class Update extends Base ->inject('queueForEvents') ->inject('publisherForBuilds') ->inject('dbForPlatform') - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('executor') ->inject('platform') ->callback($this->action(...)); @@ -145,7 +144,7 @@ class Update extends Base Event $queueForEvents, BuildPublisher $publisherForBuilds, Database $dbForPlatform, - Factory $vcsFactory, + callable $vcsForInstallation, Executor $executor, array $platform ) { @@ -309,7 +308,7 @@ class Update extends Base // Redeploy logic if (!$isConnected && !empty($providerRepositoryId)) { - $this->redeployVcsFunction($request, $site, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsFactory->fromInstallation($installation), true, $platform); + $this->redeployVcsFunction($request, $site, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsForInstallation($installation), true, $platform); } $queueForEvents->setParam('siteId', $site->getId()); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php index 461a9d3b5c..71ea874c60 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php @@ -10,7 +10,6 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; @@ -53,7 +52,7 @@ class Update extends Action ->param('installationId', '', new Text(256), 'Installation Id') ->param('repositoryId', '', new Text(256), 'VCS Repository Id') ->param('providerPullRequestId', '', new Text(256), 'GitHub Pull Request Id') - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('response') ->inject('project') ->inject('dbForPlatform') @@ -68,7 +67,7 @@ class Update extends Action string $installationId, string $repositoryId, string $providerPullRequestId, - Factory $vcsFactory, + callable $vcsForInstallation, Response $response, Document $project, Database $dbForPlatform, @@ -101,7 +100,7 @@ class Update extends Action $repository = $authorization->skip(fn () => $dbForPlatform->updateDocument('repositories', $repository->getId(), new Document(['providerPullRequestIds' => $providerPullRequestIds]))); $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsFactory->fromInstallation($installation); + $vcs = $vcsForInstallation($installation); $repositories = [$repository]; $providerRepositoryId = $repository->getAttribute('providerRepositoryId'); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 1f2029e86d..29c2723c98 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -6,7 +6,6 @@ use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Appwrite\Extend\Exception; use Appwrite\Platform\Permission as AppwritePermission; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; @@ -40,7 +39,7 @@ class Get extends Action ->param('setup_action', '', new Text(256, 0), 'GitHub setup action type', true) ->param('state', '', new Text(2048), 'GitHub state. Contains info sent when starting authorization flow.', true) ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) - ->inject('vcsFactory') + ->inject('vcsForProvider') ->inject('project') ->inject('response') ->inject('dbForPlatform') @@ -53,7 +52,7 @@ class Get extends Action string $setupAction, string $state, string $code, - Factory $vcsFactory, + callable $vcsForProvider, Document $project, Response $response, Database $dbForPlatform, @@ -101,7 +100,7 @@ class Get extends Action // Create / Update installation if (!empty($providerInstallationId)) { - $vcs = $vcsFactory->fromProvider('github'); + $vcs = $vcsForProvider('github'); $vcs->initializeVariables($providerInstallationId, System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'), System::getEnv('_APP_VCS_GITHUB_APP_ID')); $owner = $vcs->getOwnerName($providerInstallationId); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 75d45d8e6a..7edccfd1eb 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -8,7 +8,6 @@ use Appwrite\Platform\Action; use Appwrite\Platform\Modules\VCS\Http\GitHub\Deployment; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; @@ -38,7 +37,7 @@ class Create extends Action ->desc('Create event') ->groups(['api', 'vcs']) ->label('scope', 'public') - ->inject('vcsFactory') + ->inject('vcsForProvider') ->inject('request') ->inject('response') ->inject('dbForPlatform') @@ -50,7 +49,7 @@ class Create extends Action } public function action( - Factory $vcsFactory, + callable $vcsForProvider, Request $request, Response $response, Database $dbForPlatform, @@ -61,7 +60,7 @@ class Create extends Action ) { $this->preprocessEvent($request); - $vcs = $vcsFactory->fromProvider('github'); + $vcs = $vcsForProvider('github'); $event = $request->getHeaderLine('x-github-event', ''); Span::add('vcs.github.event.name', $event); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php index f8810c5f49..dde4255855 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php @@ -9,7 +9,6 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Database\Validator\Queries\Branches; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Query as QueryException; @@ -54,7 +53,7 @@ class XList extends Action ->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('queries', [], new Branches(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit, offset, cursorAfter, and cursorBefore', true) - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -65,7 +64,7 @@ class XList extends Action string $providerRepositoryId, string $search, array $queries, - Factory $vcsFactory, + callable $vcsForInstallation, Response $response, Database $dbForPlatform ) { @@ -82,7 +81,7 @@ class XList extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsFactory->fromInstallation($installation); + $vcs = $vcsForInstallation($installation); $owner = $vcs->getOwnerName($providerInstallationId); try { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php index 598ec60a55..0d95e9e3e9 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php @@ -8,7 +8,6 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Platform\Scope\HTTP; @@ -51,7 +50,7 @@ class Get extends Action ->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('providerRootDirectory', '', new Text(256, 0), 'Path to get contents of nested directory', true) ->param('providerReference', '', new Text(256, 0), 'Git reference (branch, tag, commit) to get contents from', true) - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -62,7 +61,7 @@ class Get extends Action string $providerRepositoryId, string $providerRootDirectory, string $providerReference, - Factory $vcsFactory, + callable $vcsForInstallation, Response $response, Database $dbForPlatform ) { @@ -73,7 +72,7 @@ class Get extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsFactory->fromInstallation($installation); + $vcs = $vcsForInstallation($installation); $owner = $vcs->getOwnerName($providerInstallationId); try { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php index 99fc01726a..eda58745c5 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -9,7 +9,6 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; @@ -53,7 +52,7 @@ class Create extends Action ->param('installationId', '', new Text(256), 'Installation Id') ->param('name', '', new Text(256), 'Repository name (slug)') ->param('private', '', new Boolean(false), 'Mark repository public or private') - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('user') ->inject('response') ->inject('dbForPlatform') @@ -64,7 +63,7 @@ class Create extends Action string $installationId, string $name, bool $private, - Factory $vcsFactory, + callable $vcsForInstallation, Document $user, Response $response, Database $dbForPlatform @@ -128,7 +127,7 @@ class Create extends Action } } else { $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsFactory->fromInstallation($installation); + $vcs = $vcsForInstallation($installation); $owner = $vcs->getOwnerName($providerInstallationId); try { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php index 9300e107eb..16e125c732 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php @@ -8,7 +8,6 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Swoole\Coroutine\WaitGroup; use Utopia\Config\Adapters\Dotenv as ConfigDotenv; use Utopia\Config\Config; @@ -93,7 +92,7 @@ class Create extends Action ->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('type', '', new WhiteList(['runtime', 'framework']), 'Detector type. Must be one of the following: runtime, framework', enum: new Enum(name: 'VCSDetectionType')) ->param('providerRootDirectory', '', new Text(256, 0), 'Path to Root Directory', true) - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -104,7 +103,7 @@ class Create extends Action string $providerRepositoryId, string $type, string $providerRootDirectory, - Factory $vcsFactory, + callable $vcsForInstallation, Response $response, Database $dbForPlatform ) { @@ -115,7 +114,7 @@ class Create extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsFactory->fromInstallation($installation); + $vcs = $vcsForInstallation($installation); $owner = $vcs->getOwnerName($providerInstallationId); try { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php index 0d1e867305..086f3a3e51 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php @@ -8,7 +8,6 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Platform\Scope\HTTP; @@ -48,7 +47,7 @@ class Get extends Action )) ->param('installationId', '', new Text(256), 'Installation Id') ->param('providerRepositoryId', '', new Text(256), 'Repository Id') - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -57,7 +56,7 @@ class Get extends Action public function action( string $installationId, string $providerRepositoryId, - Factory $vcsFactory, + callable $vcsForInstallation, Response $response, Database $dbForPlatform ) { @@ -68,7 +67,7 @@ class Get extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsFactory->fromInstallation($installation); + $vcs = $vcsForInstallation($installation); $owner = $vcs->getOwnerName($providerInstallationId); try { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php index bcc1a2782f..54b7fc7941 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php @@ -8,7 +8,6 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Swoole\Coroutine\WaitGroup; use Utopia\Config\Adapters\Dotenv as ConfigDotenv; use Utopia\Config\Config; @@ -97,7 +96,7 @@ class XList extends Action ->param('type', '', new WhiteList(['runtime', 'framework']), 'Detector type. Must be one of the following: runtime, framework', enum: new Enum(name: 'VCSDetectionType')) ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -108,7 +107,7 @@ class XList extends Action string $type, string $search, array $queries, - Factory $vcsFactory, + callable $vcsForInstallation, Response $response, Database $dbForPlatform ) { @@ -123,7 +122,7 @@ class XList extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsFactory->fromInstallation($installation); + $vcs = $vcsForInstallation($installation); $queries = Query::parseQueries($queries); $limitQuery = current(array_filter($queries, fn ($query) => $query->getMethod() === Query::TYPE_LIMIT)); From 797111dee4c279d27c8cc4054501cb7474d3b094 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 9 Jul 2026 19:05:21 +0530 Subject: [PATCH 10/52] refactor: migrate Gitea webhook wiring to closure-resolved vcs adapters Sites/Create and the Gitea events handler were the two remaining call sites still injecting Factory directly; they now use vcsForInstallation/vcsForProvider like everything else in the stack. --- .../Modules/Sites/Http/Sites/Create.php | 7 +++--- .../Modules/VCS/Http/Gitea/Events/Create.php | 25 ++++++++++--------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php index d6e00b39e5..3f8d35f78a 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php @@ -11,7 +11,6 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Appwrite\Vcs\RepositoryWebhooks; use Utopia\Config\Config; use Utopia\Database\Database; @@ -104,7 +103,7 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('dbForPlatform') - ->inject('vcsFactory') + ->inject('vcsForInstallation') ->inject('repositoryWebhooks') ->callback($this->action(...)); } @@ -138,7 +137,7 @@ class Create extends Base Document $project, Event $queueForEvents, Database $dbForPlatform, - Factory $vcsFactory, + callable $vcsForInstallation, RepositoryWebhooks $repositoryWebhooks ) { if (!empty($adapter)) { @@ -222,7 +221,7 @@ class Create extends Base ]); $repository = $dbForPlatform->createDocument('repositories', $repository); - $providerAdapter = $vcsFactory->fromInstallation($installation); + $providerAdapter = $vcsForInstallation($installation); if ($providerAdapter->requiresRepositoryWebhook()) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index 33fe0b3e10..1d1dd99dbf 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -8,7 +8,6 @@ use Appwrite\Platform\Action; use Appwrite\Platform\Modules\VCS\Http\GitHub\Deployment; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; @@ -37,7 +36,8 @@ class Create extends Action ->desc('Create event') ->groups(['api', 'vcs']) ->label('scope', 'public') - ->inject('vcsFactory') + ->inject('vcsForProvider') + ->inject('vcsForInstallation') ->inject('request') ->inject('response') ->inject('dbForPlatform') @@ -49,7 +49,8 @@ class Create extends Action } public function action( - Factory $vcsFactory, + callable $vcsForProvider, + callable $vcsForInstallation, Request $request, Response $response, Database $dbForPlatform, @@ -58,7 +59,7 @@ class Create extends Action BuildPublisher $publisherForBuilds, array $platform ) { - $vcs = $vcsFactory->fromProvider('gitea'); + $vcs = $vcsForProvider('gitea'); $event = $request->getHeaderLine($vcs->getEventHeaderName(), ''); Span::add('vcs.gitea.event.name', $event); @@ -81,8 +82,8 @@ class Create extends Action $parsedPayload = $vcs->getEvent($event, $payload); match ($event) { - 'push' => $this->handlePushEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), - 'pull_request' => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + 'push' => $this->handlePushEvent($parsedPayload, $vcsForInstallation, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + 'pull_request' => $this->handlePullRequestEvent($parsedPayload, $vcsForInstallation, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), default => null, }; @@ -96,7 +97,7 @@ class Create extends Action * carry their own personal token — repositories matching the same * providerRepositoryId can belong to different Appwrite installations. */ - private function resolveAdapterForRepository(Document $repository, Factory $vcsFactory, Database $dbForPlatform, Authorization $authorization): ?Git + private function resolveAdapterForRepository(Document $repository, callable $vcsForInstallation, Database $dbForPlatform, Authorization $authorization): ?Git { $installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', ''))); @@ -113,7 +114,7 @@ class Create extends Action } try { - return $vcsFactory->fromInstallation($installation); + return $vcsForInstallation($installation); } catch (\Throwable $error) { Console::warning("Failed to resolve Gitea adapter for installation '{$installation->getId()}': " . $error->getMessage()); return null; @@ -122,7 +123,7 @@ class Create extends Action private function handlePushEvent( array $parsedPayload, - Factory $vcsFactory, + callable $vcsForInstallation, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, @@ -159,7 +160,7 @@ class Create extends Action $providerAffectedFiles = $parsedPayload['affectedFiles'] ?? []; foreach ($repositories as $repository) { - $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $dbForPlatform, $authorization); + $adapter = $this->resolveAdapterForRepository($repository, $vcsForInstallation, $dbForPlatform, $authorization); if ($adapter === null) { continue; @@ -173,7 +174,7 @@ class Create extends Action private function handlePullRequestEvent( array $parsedPayload, - Factory $vcsFactory, + callable $vcsForInstallation, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, @@ -211,7 +212,7 @@ class Create extends Action ])); foreach ($repositories as $repository) { - $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $dbForPlatform, $authorization); + $adapter = $this->resolveAdapterForRepository($repository, $vcsForInstallation, $dbForPlatform, $authorization); if ($adapter === null) { continue; From 38ca920351ac82097515228a5a0113ea5161d330 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 14:02:44 +0530 Subject: [PATCH 11/52] refactor: use verifySignature() instead of validateWebhookEvent() Bumps utopia-php/vcs to the branch's latest commit, which replaces the bool-returning validateWebhookEvent() with a throwing verifySignature(). Preserves the existing fail-open behavior when no webhook secret is configured. --- composer.lock | 8 ++++---- .../Platform/Modules/VCS/Http/GitHub/Events/Create.php | 10 +++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/composer.lock b/composer.lock index 7d1e10552b..e0e584ab5c 100644 --- a/composer.lock +++ b/composer.lock @@ -5137,12 +5137,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "c3bc979e355bb1c1c8600082bb375af10740c785" + "reference": "9e079babfc027d9e9694b0038bd74b53b036df8e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/c3bc979e355bb1c1c8600082bb375af10740c785", - "reference": "c3bc979e355bb1c1c8600082bb375af10740c785", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/9e079babfc027d9e9694b0038bd74b53b036df8e", + "reference": "9e079babfc027d9e9694b0038bd74b53b036df8e", "shasum": "" }, "require": { @@ -5178,7 +5178,7 @@ "issues": "https://github.com/utopia-php/vcs/issues", "source": "https://github.com/utopia-php/vcs/tree/feat/appwrite-provider-parity" }, - "time": "2026-07-09T09:03:44+00:00" + "time": "2026-07-10T08:25:28+00:00" }, { "name": "utopia-php/websocket", diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 7edccfd1eb..e263d55c81 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -18,6 +18,7 @@ use Utopia\Span\Span; use Utopia\System\System; use Utopia\VCS\Adapter\Git; use Utopia\VCS\Adapter\Git\GitHub; +use Utopia\VCS\Exception\SignatureVerificationException; class Create extends Action { @@ -69,7 +70,14 @@ class Create extends Action $signature = $request->getHeaderLine('x-hub-signature-256', ''); $secretKey = System::getEnv('_APP_VCS_GITHUB_WEBHOOK_SECRET', ''); - $valid = empty($secretKey) ? true : $vcs->validateWebhookEvent($payload, $signature, $secretKey); + $valid = true; + if (!empty($secretKey)) { + try { + $vcs->verifySignature($payload, $signature, $secretKey); + } catch (SignatureVerificationException) { + $valid = false; + } + } Span::add('vcs.github.event.signature.valid', $valid); if (!$valid) { From ccd0ba5d3b6964fe92bd526ecaecba339f447337 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 14:04:03 +0530 Subject: [PATCH 12/52] refactor: use verifySignature() in Gitea event handler Preserves the existing fail-closed behavior (empty secret = invalid) now that the library throws instead of returning a bool. --- .../Platform/Modules/VCS/Http/Gitea/Events/Create.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index 1d1dd99dbf..2c07f63ba9 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -17,6 +17,7 @@ use Utopia\Platform\Scope\HTTP; use Utopia\Span\Span; use Utopia\System\System; use Utopia\VCS\Adapter\Git; +use Utopia\VCS\Exception\SignatureVerificationException; class Create extends Action { @@ -72,7 +73,14 @@ class Create extends Action // signed JWT Appwrite always validates, so an empty webhook secret // there still leaves a real check in place. Gitea has no such // fallback -- an empty secret here would mean no verification at all. - $valid = !empty($secretKey) && $vcs->validateWebhookEvent($payload, $signature, $secretKey); + $valid = false; + if (!empty($secretKey)) { + try { + $vcs->verifySignature($payload, $signature, $secretKey); + $valid = true; + } catch (SignatureVerificationException) { + } + } Span::add('vcs.gitea.event.signature.valid', $valid); if (!$valid) { From 780608de79b9a188ec0d19a50e3fd026488a47be Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 15:07:48 +0530 Subject: [PATCH 13/52] revert: use validateWebhookEvent() instead of verifySignature() Bumps utopia-php/vcs to the branch's latest commit, which reverts verifySignature back to validateWebhookEvent -- reusing the existing bool-returning method rather than introducing an unnecessary breaking API change for a purely internal DRY improvement. --- composer.lock | 8 ++++---- .../Platform/Modules/VCS/Http/GitHub/Events/Create.php | 10 +--------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/composer.lock b/composer.lock index e0e584ab5c..848d524519 100644 --- a/composer.lock +++ b/composer.lock @@ -5137,12 +5137,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "9e079babfc027d9e9694b0038bd74b53b036df8e" + "reference": "27fc470e16e379ac6904e06f48f3a2cbebea9c54" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/9e079babfc027d9e9694b0038bd74b53b036df8e", - "reference": "9e079babfc027d9e9694b0038bd74b53b036df8e", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/27fc470e16e379ac6904e06f48f3a2cbebea9c54", + "reference": "27fc470e16e379ac6904e06f48f3a2cbebea9c54", "shasum": "" }, "require": { @@ -5178,7 +5178,7 @@ "issues": "https://github.com/utopia-php/vcs/issues", "source": "https://github.com/utopia-php/vcs/tree/feat/appwrite-provider-parity" }, - "time": "2026-07-10T08:25:28+00:00" + "time": "2026-07-10T09:32:00+00:00" }, { "name": "utopia-php/websocket", diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index e263d55c81..7edccfd1eb 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -18,7 +18,6 @@ use Utopia\Span\Span; use Utopia\System\System; use Utopia\VCS\Adapter\Git; use Utopia\VCS\Adapter\Git\GitHub; -use Utopia\VCS\Exception\SignatureVerificationException; class Create extends Action { @@ -70,14 +69,7 @@ class Create extends Action $signature = $request->getHeaderLine('x-hub-signature-256', ''); $secretKey = System::getEnv('_APP_VCS_GITHUB_WEBHOOK_SECRET', ''); - $valid = true; - if (!empty($secretKey)) { - try { - $vcs->verifySignature($payload, $signature, $secretKey); - } catch (SignatureVerificationException) { - $valid = false; - } - } + $valid = empty($secretKey) ? true : $vcs->validateWebhookEvent($payload, $signature, $secretKey); Span::add('vcs.github.event.signature.valid', $valid); if (!$valid) { From 27840460704175d74c72da5617061bfe0c575925 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 15:09:02 +0530 Subject: [PATCH 14/52] refactor: rename requiresRepositoryWebhook to hasPerRepositoryWebhooks Matches the library's rename -- the method describes the provider's own webhook delivery model, not a consumer's requirement. --- src/Appwrite/Vcs/RepositoryWebhooks.php | 2 +- tests/unit/Vcs/RepositoryWebhooksTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Vcs/RepositoryWebhooks.php b/src/Appwrite/Vcs/RepositoryWebhooks.php index f13b64242a..269dacaa24 100644 --- a/src/Appwrite/Vcs/RepositoryWebhooks.php +++ b/src/Appwrite/Vcs/RepositoryWebhooks.php @@ -44,7 +44,7 @@ class RepositoryWebhooks string $owner, string $repositoryName, ): void { - if (!$adapter->requiresRepositoryWebhook()) { + if (!$adapter->hasPerRepositoryWebhooks()) { return; } diff --git a/tests/unit/Vcs/RepositoryWebhooksTest.php b/tests/unit/Vcs/RepositoryWebhooksTest.php index 40f3e5db3d..881b22b5fb 100644 --- a/tests/unit/Vcs/RepositoryWebhooksTest.php +++ b/tests/unit/Vcs/RepositoryWebhooksTest.php @@ -17,7 +17,7 @@ final class RepositoryWebhooksTest extends TestCase public function testSkipsWhenAdapterDoesNotRequireWebhook(): void { $adapter = $this->createMock(Git::class); - $adapter->method('requiresRepositoryWebhook')->willReturn(false); + $adapter->method('hasPerRepositoryWebhooks')->willReturn(false); $adapter->expects($this->never())->method('createWebhook'); $db = $this->createMock(Database::class); @@ -31,7 +31,7 @@ final class RepositoryWebhooksTest extends TestCase public function testCreatesWebhookWhenNoExistingConnection(): void { $adapter = $this->createMock(Git::class); - $adapter->method('requiresRepositoryWebhook')->willReturn(true); + $adapter->method('hasPerRepositoryWebhooks')->willReturn(true); $adapter->expects($this->once()) ->method('createWebhook') ->with('owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret') @@ -48,7 +48,7 @@ final class RepositoryWebhooksTest extends TestCase public function testSkipsWhenRepositoryAlreadyConnected(): void { $adapter = $this->createMock(Git::class); - $adapter->method('requiresRepositoryWebhook')->willReturn(true); + $adapter->method('hasPerRepositoryWebhooks')->willReturn(true); $adapter->expects($this->never())->method('createWebhook'); $db = $this->createMock(Database::class); @@ -62,7 +62,7 @@ final class RepositoryWebhooksTest extends TestCase public function testAdapterFailureIsWrapped(): void { $adapter = $this->createMock(Git::class); - $adapter->method('requiresRepositoryWebhook')->willReturn(true); + $adapter->method('hasPerRepositoryWebhooks')->willReturn(true); $adapter->method('createWebhook')->willThrowException(new \RuntimeException('provider unreachable')); $db = $this->createMock(Database::class); From 18505c812b6f2ed0c8d87c3ce40e55bdb2e8777e Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 15:10:30 +0530 Subject: [PATCH 15/52] revert: use validateWebhookEvent() and hasPerRepositoryWebhooks() Matches the library revert -- Gitea event handling goes back to the bool-returning validateWebhookEvent(), and the four repo-connect webhook-wiring call sites pick up the hasPerRepositoryWebhooks rename. --- .../Modules/Functions/Http/Functions/Create.php | 2 +- .../Modules/Functions/Http/Functions/Update.php | 2 +- .../Platform/Modules/Sites/Http/Sites/Create.php | 2 +- .../Platform/Modules/Sites/Http/Sites/Update.php | 2 +- .../Platform/Modules/VCS/Http/Gitea/Events/Create.php | 10 +--------- 5 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php index 45198c149e..6ec997f7ca 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -306,7 +306,7 @@ class Create extends Base ])); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->requiresRepositoryWebhook()) { + if ($providerAdapter->hasPerRepositoryWebhooks()) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php index 1ff5e0cbab..ce4715c854 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -247,7 +247,7 @@ class Update extends Base $repositoryInternalId = $repository->getSequence(); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->requiresRepositoryWebhook()) { + if ($providerAdapter->hasPerRepositoryWebhooks()) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php index 3f8d35f78a..c29cca3c5d 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php @@ -222,7 +222,7 @@ class Create extends Base $repository = $dbForPlatform->createDocument('repositories', $repository); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->requiresRepositoryWebhook()) { + if ($providerAdapter->hasPerRepositoryWebhooks()) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php index b6f514812d..e2a1103c2a 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php @@ -245,7 +245,7 @@ class Update extends Base $repositoryInternalId = $repository->getSequence(); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->requiresRepositoryWebhook()) { + if ($providerAdapter->hasPerRepositoryWebhooks()) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index 2c07f63ba9..1d1dd99dbf 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -17,7 +17,6 @@ use Utopia\Platform\Scope\HTTP; use Utopia\Span\Span; use Utopia\System\System; use Utopia\VCS\Adapter\Git; -use Utopia\VCS\Exception\SignatureVerificationException; class Create extends Action { @@ -73,14 +72,7 @@ class Create extends Action // signed JWT Appwrite always validates, so an empty webhook secret // there still leaves a real check in place. Gitea has no such // fallback -- an empty secret here would mean no verification at all. - $valid = false; - if (!empty($secretKey)) { - try { - $vcs->verifySignature($payload, $signature, $secretKey); - $valid = true; - } catch (SignatureVerificationException) { - } - } + $valid = !empty($secretKey) && $vcs->validateWebhookEvent($payload, $signature, $secretKey); Span::add('vcs.gitea.event.signature.valid', $valid); if (!$valid) { From 877bc71376b031b6ab356f32748aac429df7ee59 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 15:24:55 +0530 Subject: [PATCH 16/52] refactor: rename to supportsRepositoryWebhooks per review Matches the library's rename to a pure capability statement -- RepositoryWebhooks::ensure() is the one deciding what to do with that fact (skip when unsupported), not the adapter. --- composer.lock | 8 ++++---- src/Appwrite/Vcs/RepositoryWebhooks.php | 10 +++++----- tests/unit/Vcs/RepositoryWebhooksTest.php | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/composer.lock b/composer.lock index 848d524519..667764cc29 100644 --- a/composer.lock +++ b/composer.lock @@ -5137,12 +5137,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "27fc470e16e379ac6904e06f48f3a2cbebea9c54" + "reference": "48094a79a3e445a08af5f2c887bc11abfae1c4b0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/27fc470e16e379ac6904e06f48f3a2cbebea9c54", - "reference": "27fc470e16e379ac6904e06f48f3a2cbebea9c54", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/48094a79a3e445a08af5f2c887bc11abfae1c4b0", + "reference": "48094a79a3e445a08af5f2c887bc11abfae1c4b0", "shasum": "" }, "require": { @@ -5178,7 +5178,7 @@ "issues": "https://github.com/utopia-php/vcs/issues", "source": "https://github.com/utopia-php/vcs/tree/feat/appwrite-provider-parity" }, - "time": "2026-07-10T09:32:00+00:00" + "time": "2026-07-10T09:51:03+00:00" }, { "name": "utopia-php/websocket", diff --git a/src/Appwrite/Vcs/RepositoryWebhooks.php b/src/Appwrite/Vcs/RepositoryWebhooks.php index 269dacaa24..4a6dd0302d 100644 --- a/src/Appwrite/Vcs/RepositoryWebhooks.php +++ b/src/Appwrite/Vcs/RepositoryWebhooks.php @@ -13,9 +13,9 @@ use Utopia\VCS\Adapter\Git; * Creates per-repository webhooks for self-hosted providers. * * Callers invoke this unconditionally on every repository connection; it - * no-ops for providers whose adapter reports no per-repository webhook is - * needed (e.g. GitHub App, which delivers events platform-wide), so no - * endpoint needs to know which providers require one. + * no-ops for providers whose adapter doesn't support per-repository webhooks + * (e.g. GitHub App, which delivers events platform-wide instead), so no + * endpoint needs its own per-provider branching. */ class RepositoryWebhooks { @@ -26,7 +26,7 @@ class RepositoryWebhooks /** * Creates a webhook for $owner/$repositoryName unless the adapter's - * provider doesn't need one, or a `repositories` document already + * provider doesn't support one, or a `repositories` document already * exists for this installation + provider repository (an earlier * connection already went through this path). * @@ -44,7 +44,7 @@ class RepositoryWebhooks string $owner, string $repositoryName, ): void { - if (!$adapter->hasPerRepositoryWebhooks()) { + if (!$adapter->supportsRepositoryWebhooks()) { return; } diff --git a/tests/unit/Vcs/RepositoryWebhooksTest.php b/tests/unit/Vcs/RepositoryWebhooksTest.php index 881b22b5fb..e4db0b4c60 100644 --- a/tests/unit/Vcs/RepositoryWebhooksTest.php +++ b/tests/unit/Vcs/RepositoryWebhooksTest.php @@ -17,7 +17,7 @@ final class RepositoryWebhooksTest extends TestCase public function testSkipsWhenAdapterDoesNotRequireWebhook(): void { $adapter = $this->createMock(Git::class); - $adapter->method('hasPerRepositoryWebhooks')->willReturn(false); + $adapter->method('supportsRepositoryWebhooks')->willReturn(false); $adapter->expects($this->never())->method('createWebhook'); $db = $this->createMock(Database::class); @@ -31,7 +31,7 @@ final class RepositoryWebhooksTest extends TestCase public function testCreatesWebhookWhenNoExistingConnection(): void { $adapter = $this->createMock(Git::class); - $adapter->method('hasPerRepositoryWebhooks')->willReturn(true); + $adapter->method('supportsRepositoryWebhooks')->willReturn(true); $adapter->expects($this->once()) ->method('createWebhook') ->with('owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret') @@ -48,7 +48,7 @@ final class RepositoryWebhooksTest extends TestCase public function testSkipsWhenRepositoryAlreadyConnected(): void { $adapter = $this->createMock(Git::class); - $adapter->method('hasPerRepositoryWebhooks')->willReturn(true); + $adapter->method('supportsRepositoryWebhooks')->willReturn(true); $adapter->expects($this->never())->method('createWebhook'); $db = $this->createMock(Database::class); @@ -62,7 +62,7 @@ final class RepositoryWebhooksTest extends TestCase public function testAdapterFailureIsWrapped(): void { $adapter = $this->createMock(Git::class); - $adapter->method('hasPerRepositoryWebhooks')->willReturn(true); + $adapter->method('supportsRepositoryWebhooks')->willReturn(true); $adapter->method('createWebhook')->willThrowException(new \RuntimeException('provider unreachable')); $db = $this->createMock(Database::class); From 3cc049282fc636b934344a4c8c6a24e821e964f2 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 15:25:44 +0530 Subject: [PATCH 17/52] refactor: rename to supportsRepositoryWebhooks per review --- .../Platform/Modules/Functions/Http/Functions/Create.php | 2 +- .../Platform/Modules/Functions/Http/Functions/Update.php | 2 +- src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php | 2 +- src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php index 6ec997f7ca..a8216e5328 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -306,7 +306,7 @@ class Create extends Base ])); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->hasPerRepositoryWebhooks()) { + if ($providerAdapter->supportsRepositoryWebhooks()) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php index ce4715c854..e8eadbc9dc 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -247,7 +247,7 @@ class Update extends Base $repositoryInternalId = $repository->getSequence(); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->hasPerRepositoryWebhooks()) { + if ($providerAdapter->supportsRepositoryWebhooks()) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php index c29cca3c5d..dff6225fa3 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php @@ -222,7 +222,7 @@ class Create extends Base $repository = $dbForPlatform->createDocument('repositories', $repository); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->hasPerRepositoryWebhooks()) { + if ($providerAdapter->supportsRepositoryWebhooks()) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php index e2a1103c2a..587e2d756d 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php @@ -245,7 +245,7 @@ class Update extends Base $repositoryInternalId = $repository->getSequence(); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->hasPerRepositoryWebhooks()) { + if ($providerAdapter->supportsRepositoryWebhooks()) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); From 2d3f9fa0d4f67e6dff0d017333e3ac9d0681e3c0 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 17:40:48 +0530 Subject: [PATCH 18/52] refactor: read webhook scopes array instead of a single boolean Matches the library's getSupportedWebhookScopes() -- prefers installation-scope delivery when the adapter reports it, falling back to a per-repository webhook otherwise. Behavior for GitHub/Gitea is unchanged; this is forward-compatible now that GitHub can technically report both scopes. --- composer.lock | 8 +++---- src/Appwrite/Vcs/RepositoryWebhooks.php | 29 ++++++++++++++++------- tests/unit/Vcs/RepositoryWebhooksTest.php | 24 +++++++++++++++---- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/composer.lock b/composer.lock index 667764cc29..8be5b8a016 100644 --- a/composer.lock +++ b/composer.lock @@ -5137,12 +5137,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "48094a79a3e445a08af5f2c887bc11abfae1c4b0" + "reference": "3044bca222095c44d1f110d8bc6f513ee081dd89" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/48094a79a3e445a08af5f2c887bc11abfae1c4b0", - "reference": "48094a79a3e445a08af5f2c887bc11abfae1c4b0", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/3044bca222095c44d1f110d8bc6f513ee081dd89", + "reference": "3044bca222095c44d1f110d8bc6f513ee081dd89", "shasum": "" }, "require": { @@ -5178,7 +5178,7 @@ "issues": "https://github.com/utopia-php/vcs/issues", "source": "https://github.com/utopia-php/vcs/tree/feat/appwrite-provider-parity" }, - "time": "2026-07-10T09:51:03+00:00" + "time": "2026-07-10T12:06:13+00:00" }, { "name": "utopia-php/websocket", diff --git a/src/Appwrite/Vcs/RepositoryWebhooks.php b/src/Appwrite/Vcs/RepositoryWebhooks.php index 4a6dd0302d..388ce4196b 100644 --- a/src/Appwrite/Vcs/RepositoryWebhooks.php +++ b/src/Appwrite/Vcs/RepositoryWebhooks.php @@ -12,10 +12,11 @@ use Utopia\VCS\Adapter\Git; /** * Creates per-repository webhooks for self-hosted providers. * - * Callers invoke this unconditionally on every repository connection; it - * no-ops for providers whose adapter doesn't support per-repository webhooks - * (e.g. GitHub App, which delivers events platform-wide instead), so no - * endpoint needs its own per-provider branching. + * Callers invoke this unconditionally on every repository connection. + * Prefers installation-scope delivery when the adapter supports it (e.g. + * GitHub App, where events already arrive platform-wide) and only falls + * back to creating a per-repository webhook otherwise, so no endpoint + * needs its own per-provider branching. */ class RepositoryWebhooks { @@ -25,10 +26,11 @@ class RepositoryWebhooks } /** - * Creates a webhook for $owner/$repositoryName unless the adapter's - * provider doesn't support one, or a `repositories` document already - * exists for this installation + provider repository (an earlier - * connection already went through this path). + * Creates a webhook for $owner/$repositoryName unless the adapter + * already delivers events at the installation level, or a + * `repositories` document already exists for this installation + + * provider repository (an earlier connection already went through this + * path). * * Callers must persist the current repository connection's document * (via createDocument) before calling this — the idempotency check @@ -44,7 +46,16 @@ class RepositoryWebhooks string $owner, string $repositoryName, ): void { - if (!$adapter->supportsRepositoryWebhooks()) { + $scopes = $adapter->getSupportedWebhookScopes(); + + // Installation-scope delivery is preferred when available -- it + // already covers every repository the integration has access to, + // so a separate per-repository webhook would be redundant. + if (\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $scopes, true)) { + return; + } + + if (!\in_array(Git::WEBHOOK_SCOPE_REPOSITORY, $scopes, true)) { return; } diff --git a/tests/unit/Vcs/RepositoryWebhooksTest.php b/tests/unit/Vcs/RepositoryWebhooksTest.php index e4db0b4c60..e1775d82c5 100644 --- a/tests/unit/Vcs/RepositoryWebhooksTest.php +++ b/tests/unit/Vcs/RepositoryWebhooksTest.php @@ -14,10 +14,24 @@ use Utopia\VCS\Adapter\Git; final class RepositoryWebhooksTest extends TestCase { - public function testSkipsWhenAdapterDoesNotRequireWebhook(): void + public function testSkipsWhenAdapterHasNoWebhookScopes(): void { $adapter = $this->createMock(Git::class); - $adapter->method('supportsRepositoryWebhooks')->willReturn(false); + $adapter->method('getSupportedWebhookScopes')->willReturn([]); + $adapter->expects($this->never())->method('createWebhook'); + + $db = $this->createMock(Database::class); + $db->expects($this->never())->method('count'); + + $installation = new Document(['$sequence' => 1, 'provider' => 'github']); + + $this->ensure($adapter, $installation, $db, $this->factory('secret')); + } + + public function testPrefersInstallationScopeOverRepository(): void + { + $adapter = $this->createMock(Git::class); + $adapter->method('getSupportedWebhookScopes')->willReturn([Git::WEBHOOK_SCOPE_INSTALLATION, Git::WEBHOOK_SCOPE_REPOSITORY]); $adapter->expects($this->never())->method('createWebhook'); $db = $this->createMock(Database::class); @@ -31,7 +45,7 @@ final class RepositoryWebhooksTest extends TestCase public function testCreatesWebhookWhenNoExistingConnection(): void { $adapter = $this->createMock(Git::class); - $adapter->method('supportsRepositoryWebhooks')->willReturn(true); + $adapter->method('getSupportedWebhookScopes')->willReturn([Git::WEBHOOK_SCOPE_REPOSITORY]); $adapter->expects($this->once()) ->method('createWebhook') ->with('owner', 'repo', 'https://example.com/v1/vcs/gitea/events', 'secret') @@ -48,7 +62,7 @@ final class RepositoryWebhooksTest extends TestCase public function testSkipsWhenRepositoryAlreadyConnected(): void { $adapter = $this->createMock(Git::class); - $adapter->method('supportsRepositoryWebhooks')->willReturn(true); + $adapter->method('getSupportedWebhookScopes')->willReturn([Git::WEBHOOK_SCOPE_REPOSITORY]); $adapter->expects($this->never())->method('createWebhook'); $db = $this->createMock(Database::class); @@ -62,7 +76,7 @@ final class RepositoryWebhooksTest extends TestCase public function testAdapterFailureIsWrapped(): void { $adapter = $this->createMock(Git::class); - $adapter->method('supportsRepositoryWebhooks')->willReturn(true); + $adapter->method('getSupportedWebhookScopes')->willReturn([Git::WEBHOOK_SCOPE_REPOSITORY]); $adapter->method('createWebhook')->willThrowException(new \RuntimeException('provider unreachable')); $db = $this->createMock(Database::class); From 160515290fd5ed6add3ca30a35097bf3817335f2 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 17:43:50 +0530 Subject: [PATCH 19/52] refactor: check webhook scopes array at repo-connect call sites Matches the array-based API -- skip resolving owner/repo and calling RepositoryWebhooks::ensure() when the adapter reports installation-scope delivery, same as before for GitHub/Gitea, but now forward-compatible if GitHub's array grows to include repository scope too. --- .../Platform/Modules/Functions/Http/Functions/Create.php | 3 ++- .../Platform/Modules/Functions/Http/Functions/Update.php | 3 ++- src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php | 3 ++- src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php index a8216e5328..c765759c94 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -42,6 +42,7 @@ use Utopia\Validator\Boolean; use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; +use Utopia\VCS\Adapter\Git; class Create extends Base { @@ -306,7 +307,7 @@ class Create extends Base ])); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->supportsRepositoryWebhooks()) { + if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php index e8eadbc9dc..4b9c922a7f 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -35,6 +35,7 @@ use Utopia\Validator\Nullable; use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; +use Utopia\VCS\Adapter\Git; class Update extends Base { @@ -247,7 +248,7 @@ class Update extends Base $repositoryInternalId = $repository->getSequence(); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->supportsRepositoryWebhooks()) { + if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php index dff6225fa3..76dd2ba083 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php @@ -26,6 +26,7 @@ use Utopia\Validator\Boolean; use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; +use Utopia\VCS\Adapter\Git; class Create extends Base { @@ -222,7 +223,7 @@ class Create extends Base $repository = $dbForPlatform->createDocument('repositories', $repository); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->supportsRepositoryWebhooks()) { + if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php index 587e2d756d..a6a0605524 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php @@ -30,6 +30,7 @@ use Utopia\Validator\Nullable; use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; +use Utopia\VCS\Adapter\Git; class Update extends Base { @@ -245,7 +246,7 @@ class Update extends Base $repositoryInternalId = $repository->getSequence(); $providerAdapter = $vcsForInstallation($installation); - if ($providerAdapter->supportsRepositoryWebhooks()) { + if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { try { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); From e94a95d58752a5ee2e836e22e1ed7f631347c352 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 17:51:33 +0530 Subject: [PATCH 20/52] chore: bump utopia-php/vcs to pick up createWebhook fix --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 848d524519..eecd220c71 100644 --- a/composer.lock +++ b/composer.lock @@ -5137,12 +5137,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "27fc470e16e379ac6904e06f48f3a2cbebea9c54" + "reference": "f785974cb61238675a1269f9f262f77245c6d634" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/27fc470e16e379ac6904e06f48f3a2cbebea9c54", - "reference": "27fc470e16e379ac6904e06f48f3a2cbebea9c54", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/f785974cb61238675a1269f9f262f77245c6d634", + "reference": "f785974cb61238675a1269f9f262f77245c6d634", "shasum": "" }, "require": { @@ -5178,7 +5178,7 @@ "issues": "https://github.com/utopia-php/vcs/issues", "source": "https://github.com/utopia-php/vcs/tree/feat/appwrite-provider-parity" }, - "time": "2026-07-10T09:32:00+00:00" + "time": "2026-07-10T12:16:02+00:00" }, { "name": "utopia-php/websocket", From b10c746cb7180b30078e14b08b495776b03de4f6 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 18:15:35 +0530 Subject: [PATCH 21/52] fix: guard the closed-PR handler against cross-provider collisions The opened/reopened/synchronize paths already skip repositories whose installation isn't gitea (via resolveAdapterForRepository), but the closed-PR path updated providerPullRequestIds on any matching providerRepositoryId regardless of provider. Since GitHub and Gitea both assign short sequential repo/PR numbers, a Gitea PR close could silently strip a GitHub repository's tracked PR ID. Extracts the provider check into isGiteaRepository() and applies it in both paths. --- .../Modules/VCS/Http/Gitea/Events/Create.php | 29 +++++-- .../Vcs/GiteaClosedPullRequestEventTest.php | 80 +++++++++++++++++++ 2 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 tests/unit/Vcs/GiteaClosedPullRequestEventTest.php diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index 1d1dd99dbf..cb1c8199e1 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -97,21 +97,30 @@ class Create extends Action * carry their own personal token — repositories matching the same * providerRepositoryId can belong to different Appwrite installations. */ - private function resolveAdapterForRepository(Document $repository, callable $vcsForInstallation, Database $dbForPlatform, Authorization $authorization): ?Git + /** + * `repositories` has no provider attribute -- providerRepositoryId is + * provider-scoped numeric IDs from independent servers, so a GitHub + * and a Gitea repository can collide on the same ID. Filter by the + * matched installation's own provider instead of the query. + */ + private function isGiteaRepository(Document $repository, Database $dbForPlatform, Authorization $authorization): bool { $installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', ''))); if ($installation->isEmpty()) { + return false; + } + + return $installation->getAttribute('provider', 'github') === 'gitea'; + } + + private function resolveAdapterForRepository(Document $repository, callable $vcsForInstallation, Database $dbForPlatform, Authorization $authorization): ?Git + { + if (!$this->isGiteaRepository($repository, $dbForPlatform, $authorization)) { return null; } - // `repositories` has no provider attribute -- providerRepositoryId is - // provider-scoped numeric IDs from independent servers, so a GitHub - // and a Gitea repository can collide on the same ID. Filter by the - // matched installation's own provider instead of the query. - if ($installation->getAttribute('provider', 'github') !== 'gitea') { - return null; - } + $installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', ''))); try { return $vcsForInstallation($installation); @@ -253,6 +262,10 @@ class Create extends Action ])); foreach ($repositories as $repository) { + if (!$this->isGiteaRepository($repository, $dbForPlatform, $authorization)) { + continue; + } + $providerPullRequestIds = $repository->getAttribute('providerPullRequestIds', []); if (\in_array($providerPullRequestId, $providerPullRequestIds)) { diff --git a/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php b/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php new file mode 100644 index 0000000000..2cb0417c76 --- /dev/null +++ b/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php @@ -0,0 +1,80 @@ +handler = new Create(); + } + + public function testClosedEventOnlyUpdatesGiteaRepositories(): void + { + // A GitHub repo and a Gitea repo can share the same numeric + // providerRepositoryId since each is scoped to its own server. + $giteaRepository = new Document([ + '$id' => 'repo-gitea', + 'installationId' => 'install-gitea', + 'providerRepositoryId' => '5', + 'providerPullRequestIds' => [5], + ]); + + $githubRepository = new Document([ + '$id' => 'repo-github', + 'installationId' => 'install-github', + 'providerRepositoryId' => '5', + 'providerPullRequestIds' => [5], + ]); + + $giteaInstallation = new Document(['$id' => 'install-gitea', 'provider' => 'gitea']); + $githubInstallation = new Document(['$id' => 'install-github', 'provider' => 'github']); + + $dbForPlatform = $this->createMock(Database::class); + $authorization = $this->createMock(Authorization::class); + + $dbForPlatform->method('find')->willReturn([$giteaRepository, $githubRepository]); + + $dbForPlatform->method('getDocument') + ->willReturnCallback(fn (string $collection, string $id): Document => match ($id) { + 'install-gitea' => $giteaInstallation, + 'install-github' => $githubInstallation, + default => new Document(), + }); + + $authorization->method('skip')->willReturnCallback(fn (callable $fn): mixed => $fn()); + + $dbForPlatform->expects($this->once()) + ->method('updateDocument') + ->with('repositories', 'repo-gitea', $this->callback(function (Document $doc) { + $this->assertSame([], array_values($doc->getAttribute('providerPullRequestIds'))); + return true; + })); + + $publisherForBuilds = $this->createMock(BuildPublisher::class); + + $this->callHandler('handlePullRequestEvent', [ + 'action' => 'closed', + 'repositoryId' => '5', + 'pullRequestNumber' => 5, + 'external' => true, + ], fn () => null, $dbForPlatform, $authorization, $publisherForBuilds, fn () => null, []); + } + + private function callHandler(string $method, mixed ...$arguments): mixed + { + return (new ReflectionMethod($this->handler, $method))->invoke($this->handler, ...$arguments); + } +} From f926ba26270b8be680e1075abeada9afac25a565 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 18:19:30 +0530 Subject: [PATCH 22/52] fix: throw a clear error when no refresh token is on file Previously an expired token with no refresh token (both installation and identity fallback empty) would fall through to refreshTokens(''), fail with an empty verificationId, and surface a misleading 'rate limit exceeded, try again' error -- masking the real problem and giving the user no path to actually fix it. Now fails fast with a diagnostic pointing at reconnecting the installation. --- src/Appwrite/Vcs/InstallationTokens.php | 4 ++++ tests/unit/Vcs/InstallationTokensTest.php | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php index 44f9d10c27..d3dd202e12 100644 --- a/src/Appwrite/Vcs/InstallationTokens.php +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -37,6 +37,10 @@ class InstallationTokens return $installation; } + if (empty($refreshToken)) { + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, 'This installation has no refresh token on file. Please reconnect it.'); + } + $oauth2->refreshTokens($refreshToken); $accessToken = $oauth2->getAccessToken(''); diff --git a/tests/unit/Vcs/InstallationTokensTest.php b/tests/unit/Vcs/InstallationTokensTest.php index f661ca771b..e6f9f39231 100644 --- a/tests/unit/Vcs/InstallationTokensTest.php +++ b/tests/unit/Vcs/InstallationTokensTest.php @@ -97,6 +97,27 @@ final class InstallationTokensTest extends TestCase $this->assertSame('identity-token', $result->getAttribute('personalAccessToken')); } + public function testMissingRefreshTokenThrowsClearError(): void + { + $installation = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'stale-token', + 'personalRefreshToken' => null, + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), -3600), + ]); + + $oauth2 = $this->fakeOAuth2(); + + try { + (new InstallationTokens())->refresh($installation, $this->db(), $oauth2); + $this->fail('Expected an Exception'); + } catch (Exception $e) { + $this->assertSame(Exception::GENERAL_PROVIDER_FAILURE, $e->getType()); + } + + $this->assertSame(0, $oauth2->refreshCalls); + } + public function testFailedRefreshThrows(): void { $installation = new Document([ From de6d55d8ac24219978d6d736827777bc4597b9fc Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 18:45:29 +0530 Subject: [PATCH 23/52] style: use createStub for unasserted BuildPublisher mock Rector's CreateStubOverCreateMockArgRector flagged this -- the fake is never asserted on, so it should be a stub not a mock. --- tests/unit/Vcs/GiteaClosedPullRequestEventTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php b/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php index 2cb0417c76..85877b4005 100644 --- a/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php +++ b/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php @@ -63,7 +63,7 @@ final class GiteaClosedPullRequestEventTest extends TestCase return true; })); - $publisherForBuilds = $this->createMock(BuildPublisher::class); + $publisherForBuilds = $this->createStub(BuildPublisher::class); $this->callHandler('handlePullRequestEvent', [ 'action' => 'closed', From c3d8684b8ec22f67f94811434f1ea89b214eb9a4 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 18:54:04 +0530 Subject: [PATCH 24/52] fix: resolve the correct VCS adapter for non-GitHub redeploys The Builds worker unconditionally called $vcs->initializeVariables() with GitHub App credentials for every VCS-connected deployment, regardless of the installation's actual provider. Any Gitea (or other non-GitHub) redeploy would fail deep inside the adapter with 'Signing key cannot be empty', since there's no GitHub App key to sign a JWT with. Now resolves the adapter via vcsForInstallation using the installation's own provider once it's loaded, matching what every other VCS entry point in the codebase already does. The pre-existing github-based $vcs is still used for template cloning, which is unaffected -- template and VCS-repo cloning are mutually exclusive branches (isVcsEnabled). --- .../Platform/Modules/Functions/Workers/Builds.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index f7c9503000..f6412ece50 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -90,6 +90,7 @@ class Builds extends Action ->inject('usage') ->inject('publisherForUsage') ->inject('vcsForProvider') + ->inject('vcsForInstallation') ->inject('dbForProject') ->inject('deviceForFunctions') ->inject('deviceForSites') @@ -116,6 +117,7 @@ class Builds extends Action Context $usage, UsagePublisher $publisherForUsage, callable $vcsForProvider, + callable $vcsForInstallation, Database $dbForProject, Device $deviceForFunctions, Device $deviceForSites, @@ -160,6 +162,7 @@ class Builds extends Action $dbForPlatform, $dbForProject, $vcs, + $vcsForInstallation, $project, $resource, $deployment, @@ -196,6 +199,7 @@ class Builds extends Action Database $dbForPlatform, Database $dbForProject, Git $vcs, + callable $vcsForInstallation, Document $project, Document $resource, Document $deployment, @@ -312,11 +316,9 @@ class Builds extends Action if ($isVcsEnabled) { $installation = $dbForPlatform->getDocument('installations', $installationId); $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); try { - $vcs->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs = $vcsForInstallation($installation); } catch (\Exception $e) { if ($e->getCode() === 404 && $resource->getAttribute('installationId', '') === $installationId) { From 189e7047de016629e46b16feaf89639b3f8a9455 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 19:14:47 +0530 Subject: [PATCH 25/52] refactor: route remaining GitHub adapter init through the factory mock.php, GitHub/Callback/Get.php, and GitHub/Events/Create.php all called $vcs->initializeVariables() directly with env vars read outside the Factory, duplicating what fromInstallation() already does. None of these had a persisted installation Document to pass to vcsForInstallation, so they now build a lightweight synthetic one from the raw providerInstallationId, keeping every adapter initialization behind the same closure the rest of the codebase uses. --- app/controllers/mock.php | 10 ++++---- .../Modules/VCS/Http/GitHub/Callback/Get.php | 10 ++++---- .../Modules/VCS/Http/GitHub/Events/Create.php | 24 +++++++++++-------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index 316ed9b148..e1f0d138ee 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -238,11 +238,11 @@ Http::get('/v1/mock/github/callback') ->label('docs', false) ->param('providerInstallationId', '', new UID(), 'GitHub installation ID') ->param('projectId', '', new UID(), 'Project ID of the project where app is to be installed') - ->inject('vcsForProvider') + ->inject('vcsForInstallation') ->inject('project') ->inject('response') ->inject('dbForPlatform') - ->action(function (string $providerInstallationId, string $projectId, callable $vcsForProvider, Document $project, Response $response, Database $dbForPlatform) { + ->action(function (string $providerInstallationId, string $projectId, callable $vcsForInstallation, Document $project, Response $response, Database $dbForPlatform) { $isDevelopment = System::getEnv('_APP_ENV', 'development') === 'development'; if (!$isDevelopment) { @@ -260,8 +260,10 @@ Http::get('/v1/mock/github/callback') throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing provider installation ID'); } - $vcs = $vcsForProvider('github'); - $vcs->initializeVariables($providerInstallationId, System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'), System::getEnv('_APP_VCS_GITHUB_APP_ID')); + $vcs = $vcsForInstallation(new Document([ + 'provider' => 'github', + 'providerInstallationId' => $providerInstallationId, + ])); $owner = $vcs->getOwnerName($providerInstallationId); $projectInternalId = $project->getSequence(); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 29c2723c98..bcb64ac990 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -39,7 +39,7 @@ class Get extends Action ->param('setup_action', '', new Text(256, 0), 'GitHub setup action type', true) ->param('state', '', new Text(2048), 'GitHub state. Contains info sent when starting authorization flow.', true) ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) - ->inject('vcsForProvider') + ->inject('vcsForInstallation') ->inject('project') ->inject('response') ->inject('dbForPlatform') @@ -52,7 +52,7 @@ class Get extends Action string $setupAction, string $state, string $code, - callable $vcsForProvider, + callable $vcsForInstallation, Document $project, Response $response, Database $dbForPlatform, @@ -100,8 +100,10 @@ class Get extends Action // Create / Update installation if (!empty($providerInstallationId)) { - $vcs = $vcsForProvider('github'); - $vcs->initializeVariables($providerInstallationId, System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'), System::getEnv('_APP_VCS_GITHUB_APP_ID')); + $vcs = $vcsForInstallation(new Document([ + 'provider' => 'github', + 'providerInstallationId' => $providerInstallationId, + ])); $owner = $vcs->getOwnerName($providerInstallationId); $projectInternalId = $project->getSequence(); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 7edccfd1eb..0dfe5ec8b7 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -38,6 +38,7 @@ class Create extends Action ->groups(['api', 'vcs']) ->label('scope', 'public') ->inject('vcsForProvider') + ->inject('vcsForInstallation') ->inject('request') ->inject('response') ->inject('dbForPlatform') @@ -50,6 +51,7 @@ class Create extends Action public function action( callable $vcsForProvider, + callable $vcsForInstallation, Request $request, Response $response, Database $dbForPlatform, @@ -76,14 +78,12 @@ class Create extends Action throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, "Invalid webhook payload signature. Please make sure the webhook secret has same value in your GitHub app and in the _APP_VCS_GITHUB_WEBHOOK_SECRET environment variable"); } - $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); - $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); $parsedPayload = $vcs->getEvent($event, $payload); match ($event) { GitHub::EVENT_INSTALLATION => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), - GitHub::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $githubAppId, $privateKey, $vcs, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), - GitHub::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $privateKey, $githubAppId, $vcs, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + GitHub::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $vcsForInstallation, $vcs, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + GitHub::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $vcsForInstallation, $vcs, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), default => null, }; @@ -182,8 +182,7 @@ class Create extends Action private function handlePushEvent( array $parsedPayload, - string $githubAppId, - string $privateKey, + callable $vcsForInstallation, Git $vcs, Database $dbForPlatform, Authorization $authorization, @@ -211,7 +210,10 @@ class Create extends Action Span::add('vcs.github.event.branch', $providerBranch); Span::add('vcs.github.event.installation.id', $providerInstallationId); - $vcs->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs = $vcsForInstallation(new Document([ + 'provider' => 'github', + 'providerInstallationId' => $providerInstallationId, + ])); // Find associated repositories $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ @@ -228,8 +230,7 @@ class Create extends Action private function handlePullRequestEvent( array $parsedPayload, - string $privateKey, - string $githubAppId, + callable $vcsForInstallation, Git $vcs, Database $dbForPlatform, Authorization $authorization, @@ -263,7 +264,10 @@ class Create extends Action return; } - $vcs->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs = $vcsForInstallation(new Document([ + 'provider' => 'github', + 'providerInstallationId' => $providerInstallationId, + ])); try { $commitDetails = $vcs->getCommit($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash); From ef119784e0af4e76b252ee5488505eb2178d9854 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 19:25:49 +0530 Subject: [PATCH 26/52] fix: separate template adapter from provider adapter in Builds worker $vcs previously played two roles in the same function: the GitHub template adapter passed in from action(), then silently reassigned to the resolved installation adapter inside the isVcsEnabled block. That reassignment happens in the same branch that also clones a template repo for the VCS+Template combined flow (a VCS-connected function that also imports a template) -- which unconditionally needs the GitHub adapter, since templates are always hosted on github.com, regardless of what provider the connected repository uses. The prior fix for the Gitea build failure would have broken this combined flow for any non-GitHub provider, since the template clone would have run against the wrong host through the reassigned adapter. Split into two distinct variables ($templateVcs, $providerAdapter) so each call site is unambiguous about which adapter it needs. --- .../Modules/Functions/Workers/Builds.php | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index f6412ece50..c2c28882eb 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -147,7 +147,7 @@ class Builds extends Action switch ($type) { case BUILD_TYPE_DEPLOYMENT: case BUILD_TYPE_RETRY: - $vcs = $vcsForProvider('github'); + $templateVcs = $vcsForProvider('github'); $this->buildDeployment( $deviceForFunctions, $deviceForSites, @@ -161,7 +161,7 @@ class Builds extends Action $publisherForUsage, $dbForPlatform, $dbForProject, - $vcs, + $templateVcs, $vcsForInstallation, $project, $resource, @@ -198,7 +198,7 @@ class Builds extends Action UsagePublisher $publisherForUsage, Database $dbForPlatform, Database $dbForProject, - Git $vcs, + Git $templateVcs, callable $vcsForInstallation, Document $project, Document $resource, @@ -312,13 +312,14 @@ class Builds extends Action $isVcsEnabled = ! empty($providerRepositoryId); $owner = ''; $repositoryName = ''; + $providerAdapter = null; if ($isVcsEnabled) { $installation = $dbForPlatform->getDocument('installations', $installationId); $providerInstallationId = $installation->getAttribute('providerInstallationId'); try { - $vcs = $vcsForInstallation($installation); + $providerAdapter = $vcsForInstallation($installation); } catch (\Exception $e) { if ($e->getCode() === 404 && $resource->getAttribute('installationId', '') === $installationId) { @@ -351,7 +352,7 @@ class Builds extends Action // Clone template repo $tmpTemplateDirectory = '/tmp/builds/' . $deploymentId . '-template'; - $gitCloneCommandForTemplate = $vcs->generateCloneCommand($templateOwnerName, $templateRepositoryName, $templateReferenceValue, $templateReferenceType, $tmpTemplateDirectory, $templateRootDirectory); + $gitCloneCommandForTemplate = $templateVcs->generateCloneCommand($templateOwnerName, $templateRepositoryName, $templateReferenceValue, $templateReferenceType, $tmpTemplateDirectory, $templateRootDirectory); $exit = Console::execute($gitCloneCommandForTemplate, '', $stdout, $stderr); @@ -409,8 +410,8 @@ class Builds extends Action $rootDirectory = \ltrim($rootDirectory, '.'); $rootDirectory = \ltrim($rootDirectory, '/'); - $owner = $vcs->getOwnerName($providerInstallationId); - $repositoryName = $vcs->getRepositoryName($providerRepositoryId); + $owner = $providerAdapter->getOwnerName($providerInstallationId); + $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); $cloneOwner = $deployment->getAttribute('providerRepositoryOwner', $owner); $cloneRepository = $deployment->getAttribute('providerRepositoryName', $repositoryName); @@ -425,7 +426,7 @@ class Builds extends Action $cloneType = Git::CLONE_TYPE_COMMIT; } - $gitCloneCommand = $vcs->generateCloneCommand($cloneOwner, $cloneRepository, $cloneVersion, $cloneType, $tmpDirectory, $rootDirectory); + $gitCloneCommand = $providerAdapter->generateCloneCommand($cloneOwner, $cloneRepository, $cloneVersion, $cloneType, $tmpDirectory, $rootDirectory); $stdout = ''; $stderr = ''; @@ -471,7 +472,7 @@ class Builds extends Action // Clone template repo $tmpTemplateDirectory = '/tmp/builds/' . $deploymentId . '/template'; - $gitCloneCommandForTemplate = $vcs->generateCloneCommand($templateOwnerName, $templateRepositoryName, $templateReferenceValue, $templateReferenceType, $tmpTemplateDirectory, $templateRootDirectory); + $gitCloneCommandForTemplate = $templateVcs->generateCloneCommand($templateOwnerName, $templateRepositoryName, $templateReferenceValue, $templateReferenceType, $tmpTemplateDirectory, $templateRootDirectory); $exit = Console::execute($gitCloneCommandForTemplate, '', $stdout, $stderr); if ($exit !== 0) { @@ -505,7 +506,7 @@ class Builds extends Action $deployment->setAttribute('providerCommitAuthorUrl', APP_VCS_GITHUB_URL); $deployment->setAttribute('providerCommitAuthor', APP_VCS_GITHUB_USERNAME); $deployment->setAttribute('providerCommitMessage', "Create '" . $resource->getAttribute('name', '') . "' function"); - $deployment->setAttribute('providerCommitUrl', $vcs->getCommitUrl($cloneOwner, $cloneRepository, $providerCommitHash)); + $deployment->setAttribute('providerCommitUrl', $providerAdapter->getCommitUrl($cloneOwner, $cloneRepository, $providerCommitHash)); $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ 'providerCommitHash' => $deployment->getAttribute('providerCommitHash'), 'providerCommitAuthorUrl' => $deployment->getAttribute('providerCommitAuthorUrl'), @@ -570,7 +571,7 @@ class Builds extends Action Span::add('build.source_size', $deployment->getAttribute('sourceSize')); - $this->runGitAction('processing', $vcs, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); + $this->runGitAction('processing', $providerAdapter, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); } Span::add('timings.source', \round(\microtime(true) - $phaseStart, 3)); @@ -599,7 +600,7 @@ class Builds extends Action ->trigger(); if ($isVcsEnabled) { - $this->runGitAction('building', $vcs, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); + $this->runGitAction('building', $providerAdapter, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); } $deploymentModel = new Deployment(); @@ -987,7 +988,7 @@ class Builds extends Action $resource = $this->updateLatestDeployment($dbForProject, $resource); if ($isVcsEnabled) { - $this->runGitAction('ready', $vcs, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); + $this->runGitAction('ready', $providerAdapter, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); } /** Set auto deploy */ @@ -1236,7 +1237,7 @@ class Builds extends Action ->trigger(); if ($isVcsEnabled) { - $this->runGitAction('failed', $vcs, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform, true); + $this->runGitAction('failed', $providerAdapter, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform, true); } // Let the worker harness record internal errors via the span and logger. From 2596f8b6e949425f8b4834fae9cdc3d690779a4d Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 19:33:19 +0530 Subject: [PATCH 27/52] refactor: drop dead Git $vcs parameter from webhook handlers handlePushEvent/handlePullRequestEvent immediately reassign $vcs via vcsForInstallation on entry, so the incoming Git $vcs parameter's value was discarded before ever being read -- a leftover from the earlier fix that resolved the correct per-installation adapter inside these methods instead of at the call site. --- .../Platform/Modules/VCS/Http/GitHub/Events/Create.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 0dfe5ec8b7..66363fd869 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -16,7 +16,6 @@ use Utopia\Database\Validator\Authorization; use Utopia\Platform\Scope\HTTP; use Utopia\Span\Span; use Utopia\System\System; -use Utopia\VCS\Adapter\Git; use Utopia\VCS\Adapter\Git\GitHub; class Create extends Action @@ -82,8 +81,8 @@ class Create extends Action match ($event) { GitHub::EVENT_INSTALLATION => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), - GitHub::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $vcsForInstallation, $vcs, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), - GitHub::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $vcsForInstallation, $vcs, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + GitHub::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $vcsForInstallation, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + GitHub::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $vcsForInstallation, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), default => null, }; @@ -183,7 +182,6 @@ class Create extends Action private function handlePushEvent( array $parsedPayload, callable $vcsForInstallation, - Git $vcs, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, @@ -231,7 +229,6 @@ class Create extends Action private function handlePullRequestEvent( array $parsedPayload, callable $vcsForInstallation, - Git $vcs, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, From df24bcaa3a174eab9e902f151c33e879261828e9 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 19:41:43 +0530 Subject: [PATCH 28/52] fix: cover vcsForInstallation() itself with the orphan-cleanup try/catch vcsForInstallation($installation) was called before the try block in all four repo-connect handlers. If adapter construction throws (e.g. token refresh failing at construction time), the repositories document created just before it was left orphaned uncaught -- the next retry's idempotency check in RepositoryWebhooks::ensure() would then see it as an existing connection and silently skip webhook creation, permanently breaking that connection. Moved adapter resolution inside the try. Also removes a redundant duplicate installation fetch in the Gitea events handler: resolveAdapterForRepository() re-queried the same installation document that isGiteaRepository() (now resolveGiteaInstallation(), returning the Document instead of a bool) had already loaded. --- .../Functions/Http/Functions/Create.php | 12 ++++++------ .../Functions/Http/Functions/Update.php | 12 ++++++------ .../Modules/Sites/Http/Sites/Create.php | 18 +++++++++--------- .../Modules/Sites/Http/Sites/Update.php | 12 ++++++------ .../Modules/VCS/Http/Gitea/Events/Create.php | 16 ++++++++-------- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php index c765759c94..c965d5a17e 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -306,16 +306,16 @@ class Create extends Base 'providerPullRequestIds' => [] ])); - $providerAdapter = $vcsForInstallation($installation); - if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { - try { + try { + $providerAdapter = $vcsForInstallation($installation); + if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); - } catch (\Throwable $error) { - $dbForPlatform->deleteDocument('repositories', $repository->getId()); - throw $error; } + } catch (\Throwable $error) { + $dbForPlatform->deleteDocument('repositories', $repository->getId()); + throw $error; } $function->setAttribute('repositoryId', $repository->getId()); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php index 4b9c922a7f..a2fecf5ce3 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -247,16 +247,16 @@ class Update extends Base $repositoryId = $repository->getId(); $repositoryInternalId = $repository->getSequence(); - $providerAdapter = $vcsForInstallation($installation); - if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { - try { + try { + $providerAdapter = $vcsForInstallation($installation); + if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); - } catch (\Throwable $error) { - $dbForPlatform->deleteDocument('repositories', $repository->getId()); - throw $error; } + } catch (\Throwable $error) { + $dbForPlatform->deleteDocument('repositories', $repository->getId()); + throw $error; } } diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php index 76dd2ba083..e8513fe5f7 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php @@ -222,19 +222,19 @@ class Create extends Base ]); $repository = $dbForPlatform->createDocument('repositories', $repository); - $providerAdapter = $vcsForInstallation($installation); - if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { - try { + try { + $providerAdapter = $vcsForInstallation($installation); + if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); - } catch (\Throwable $error) { - // Don't leave an orphaned repositories document behind -- it - // would make the next retry's connection count look like a - // repeat, silently skipping webhook creation. - $dbForPlatform->deleteDocument('repositories', $repository->getId()); - throw $error; } + } catch (\Throwable $error) { + // Don't leave an orphaned repositories document behind -- it + // would make the next retry's connection count look like a + // repeat, silently skipping webhook creation. + $dbForPlatform->deleteDocument('repositories', $repository->getId()); + throw $error; } $site->setAttribute('repositoryId', $repository->getId()); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php index a6a0605524..303d21779f 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php @@ -245,16 +245,16 @@ class Update extends Base $repositoryId = $repository->getId(); $repositoryInternalId = $repository->getSequence(); - $providerAdapter = $vcsForInstallation($installation); - if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { - try { + try { + $providerAdapter = $vcsForInstallation($installation); + if (!\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $providerAdapter->getSupportedWebhookScopes(), true)) { $owner = $providerAdapter->getOwnerName($installation->getAttribute('providerInstallationId', ''), (int)$providerRepositoryId); $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); $repositoryWebhooks->ensure($providerAdapter, $installation, $dbForPlatform, $providerRepositoryId, $owner, $repositoryName); - } catch (\Throwable $error) { - $dbForPlatform->deleteDocument('repositories', $repository->getId()); - throw $error; } + } catch (\Throwable $error) { + $dbForPlatform->deleteDocument('repositories', $repository->getId()); + throw $error; } } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index cb1c8199e1..e115ac03e3 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -103,25 +103,25 @@ class Create extends Action * and a Gitea repository can collide on the same ID. Filter by the * matched installation's own provider instead of the query. */ - private function isGiteaRepository(Document $repository, Database $dbForPlatform, Authorization $authorization): bool + private function resolveGiteaInstallation(Document $repository, Database $dbForPlatform, Authorization $authorization): ?Document { $installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', ''))); - if ($installation->isEmpty()) { - return false; + if ($installation->isEmpty() || $installation->getAttribute('provider', 'github') !== 'gitea') { + return null; } - return $installation->getAttribute('provider', 'github') === 'gitea'; + return $installation; } private function resolveAdapterForRepository(Document $repository, callable $vcsForInstallation, Database $dbForPlatform, Authorization $authorization): ?Git { - if (!$this->isGiteaRepository($repository, $dbForPlatform, $authorization)) { + $installation = $this->resolveGiteaInstallation($repository, $dbForPlatform, $authorization); + + if ($installation === null) { return null; } - $installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', ''))); - try { return $vcsForInstallation($installation); } catch (\Throwable $error) { @@ -262,7 +262,7 @@ class Create extends Action ])); foreach ($repositories as $repository) { - if (!$this->isGiteaRepository($repository, $dbForPlatform, $authorization)) { + if ($this->resolveGiteaInstallation($repository, $dbForPlatform, $authorization) === null) { continue; } From 426b32eb28e5269a834044f5db118a79f5c40ac2 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 10 Jul 2026 19:54:11 +0530 Subject: [PATCH 29/52] fix: sign OAuth2 state and add issue scopes for Gitea The callback endpoint is public and previously trusted the raw JSON state -- anyone with their own valid authorization code could submit an arbitrary projectId and attach their VCS account as an installation on another project (and control the unvalidated redirect URLs). Authorize now signs projectId/success/failure with HMAC-SHA256 over _APP_OPENSSL_KEY_V1 and the callback fails closed on mismatch. Also adds read:issue/write:issue to the requested Gitea scopes -- the adapter's PR comment methods go through /repos/.../issues/... which Gitea's scoped tokens reject without them, breaking non-silent mode. --- src/Appwrite/Auth/OAuth2/Gitea.php | 2 ++ .../Platform/Modules/VCS/Http/Authorize/Base.php | 3 +++ src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php | 9 +++++++++ 3 files changed, 14 insertions(+) diff --git a/src/Appwrite/Auth/OAuth2/Gitea.php b/src/Appwrite/Auth/OAuth2/Gitea.php index dd649047d1..01914ce8e7 100644 --- a/src/Appwrite/Auth/OAuth2/Gitea.php +++ b/src/Appwrite/Auth/OAuth2/Gitea.php @@ -35,6 +35,8 @@ class Gitea extends OAuth2 'read:repository', 'write:repository', 'read:organization', + 'read:issue', + 'write:issue', ]; /** diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php index eeea2c19cd..6784fe385d 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php @@ -97,10 +97,13 @@ abstract class Base extends Action $hostname = $platform['consoleHostname'] ?? ''; $callback = $protocol . '://' . $hostname . '/v1/vcs/' . $key . '/callback'; + // The callback endpoint is public, so it verifies this signature + // before trusting the projectId and redirect URLs in state. $oauth2 = $this->createOAuth2($callback, [ 'projectId' => $project->getId(), 'success' => $success, 'failure' => $failure, + 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), ]); $response diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php index 1c8ab3e3ad..9e76682b13 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php @@ -79,6 +79,15 @@ abstract class Base extends Action $redirectFailure = $state['failure'] ?? ''; $projectId = $state['projectId'] ?? ''; + // This endpoint is public -- without verifying the signature the + // Authorize action put in state, anyone with their own valid + // authorization code could pass an arbitrary projectId here and + // attach their VCS account as an installation on another project. + $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()) { From 28048bfbfc3e2ecf4c448b55dc42fdcba0ce3b2b Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 13 Jul 2026 16:34:54 +0530 Subject: [PATCH 30/52] Fail fast on missing VCS provider --- src/Appwrite/Vcs/Factory.php | 6 +++++- tests/unit/Vcs/FactoryTest.php | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Vcs/Factory.php b/src/Appwrite/Vcs/Factory.php index 3a6bb687a8..ece8a48b1d 100644 --- a/src/Appwrite/Vcs/Factory.php +++ b/src/Appwrite/Vcs/Factory.php @@ -103,7 +103,11 @@ class Factory throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - $provider = $installation->getAttribute('provider', 'github'); + $provider = $installation->getAttribute('provider', ''); + if (empty($provider)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing VCS provider for installation: ' . $installation->getId()); + } + $adapter = $this->fromProvider($provider); $adapter->initializeVariables( diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index c1a6989335..d015b2b11c 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -72,6 +72,18 @@ final class FactoryTest extends TestCase $factory->fromInstallation(new Document()); } + public function testFromInstallationMissingProviderThrows(): void + { + $factory = new Factory($this->cache(), ['github' => $this->githubEntry()]); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Missing VCS provider'); + + $factory->fromInstallation(new Document([ + '$id' => 'installation', + ])); + } + public function testIsConfigured(): void { $entry = [ From 30da7c1eebccb59088c68831979e6957d7a7ecf7 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 13 Jul 2026 16:40:46 +0530 Subject: [PATCH 31/52] Clean up VCS factory refactor --- .../Modules/Functions/Workers/Jobs.php | 29 +++++++++---------- .../Installations/Repositories/Create.php | 4 +-- src/Appwrite/Vcs/Factory.php | 4 +-- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php index 9086ffa550..87a7c27097 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php @@ -22,7 +22,6 @@ use Utopia\Platform\Action; use Utopia\Queue\Message; use Utopia\Storage\Device; use Utopia\System\System; -use Utopia\VCS\Adapter\Git\GitHub; /** * Applies open-runtimes jobs-service callbacks to a deployment. The API @@ -64,7 +63,7 @@ class Jobs extends Action ->inject('publisherForUsage') ->inject('usage') ->inject('deviceForBuilds') - ->inject('gitHub') + ->inject('vcsForInstallation') ->inject('cache') ->inject('locks') ->callback($this->action(...)); @@ -82,7 +81,7 @@ class Jobs extends Action UsagePublisher $publisherForUsage, UsageContext $usage, Device $deviceForBuilds, - GitHub $github, + callable $vcsForInstallation, Cache $cache, callable $locks, ): void { @@ -93,7 +92,7 @@ class Jobs extends Action return; } - $locks('jobs-deployment:' . $deploymentId, self::LOCK_TTL, function () use ($event, $project, $dbForProject, $dbForPlatform, $queueForRealtime, $queueForEvents, $queueForWebhooks, $publisherForFunctions, $publisherForUsage, $usage, $deviceForBuilds, $github, $cache, $deploymentId): void { + $locks('jobs-deployment:' . $deploymentId, self::LOCK_TTL, function () use ($event, $project, $dbForProject, $dbForPlatform, $queueForRealtime, $queueForEvents, $queueForWebhooks, $publisherForFunctions, $publisherForUsage, $usage, $deviceForBuilds, $vcsForInstallation, $cache, $deploymentId): void { if ($event->id !== '') { $key = 'jobs-event-' . $event->id; if ($cache->load($key, self::DEDUPE_TTL) !== false) { @@ -114,7 +113,7 @@ class Jobs extends Action $deployment = match ($event->event) { 'orchestrator.job.log' => $this->onLog($dbForProject, $deployment, $event->data), 'orchestrator.job.artifact' => $this->onArtifact($dbForProject, $deployment, $event->data), - 'orchestrator.job.exit' => $this->onExit($dbForProject, $dbForPlatform, $project, $deployment, (int) ($event->data['exitCode'] ?? 0), $usage, $publisherForUsage, $deviceForBuilds, $github), + 'orchestrator.job.exit' => $this->onExit($dbForProject, $dbForPlatform, $project, $deployment, (int) ($event->data['exitCode'] ?? 0), $usage, $publisherForUsage, $deviceForBuilds, $vcsForInstallation), default => $deployment, }; @@ -203,10 +202,10 @@ class Jobs extends Action UsageContext $usage, UsagePublisher $publisherForUsage, Device $deviceForBuilds, - GitHub $github, + callable $vcsForInstallation, ): Document { if ($exitCode !== 0) { - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, "Build failed with exit code {$exitCode}.", $usage, $publisherForUsage, $github); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, "Build failed with exit code {$exitCode}.", $usage, $publisherForUsage, $vcsForInstallation); } $path = Job::buildPath($project->getId(), $deployment->getId()); @@ -215,10 +214,10 @@ class Jobs extends Action $limit = (int) System::getEnv('_APP_COMPUTE_BUILD_SIZE_LIMIT', '2000000000'); if ($limit !== 0 && $size > $limit) { $deviceForBuilds->delete($path); - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build size should be less than ' . \number_format($limit / (1000 * 1000), 2) . ' MBs.', $usage, $publisherForUsage, $github); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build size should be less than ' . \number_format($limit / (1000 * 1000), 2) . ' MBs.', $usage, $publisherForUsage, $vcsForInstallation); } - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $github, $size); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $vcsForInstallation, $size); } /** @@ -235,7 +234,7 @@ class Jobs extends Action string $message, UsageContext $usage, UsagePublisher $publisherForUsage, - GitHub $github, + callable $vcsForInstallation, int $buildSize = 0, ): Document { $function = $dbForProject->getDocument('functions', $deployment->getAttribute('resourceId')); @@ -288,17 +287,17 @@ class Jobs extends Action // deployments only; best-effort). "pending" at build start is a follow-up. $status = $deployment->getAttribute('status'); if (\in_array($status, ['ready', 'failed'], true) && ! $function->isEmpty()) { - $this->gitStatus($github, $dbForPlatform, $project, $function, $deployment, $status === 'ready' ? 'success' : 'failure'); + $this->gitStatus($vcsForInstallation, $dbForPlatform, $project, $function, $deployment, $status === 'ready' ? 'success' : 'failure'); } return $deployment; } /** - * Post a GitHub commit status for a VCS deployment (no-op for non-VCS builds + * Post a VCS commit status for a VCS deployment (no-op for non-VCS builds * or silent mode). Best-effort — a failed status update never fails the build. */ - private function gitStatus(GitHub $github, Database $dbForPlatform, Document $project, Document $function, Document $deployment, string $state): void + private function gitStatus(callable $vcsForInstallation, Database $dbForPlatform, Document $project, Document $function, Document $deployment, string $state): void { $commitHash = $deployment->getAttribute('providerCommitHash', ''); if ($commitHash === '' || $function->getAttribute('providerSilentMode', false) === true) { @@ -311,7 +310,7 @@ class Jobs extends Action if ($providerInstallationId === '') { return; } - $github->initializeVariables($providerInstallationId, System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY', ''), System::getEnv('_APP_VCS_GITHUB_APP_ID', '')); + $vcs = $vcsForInstallation($installation); $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; $hostname = System::getEnv('_APP_CONSOLE_DOMAIN', System::getEnv('_APP_DOMAIN', '')); @@ -320,7 +319,7 @@ class Jobs extends Action $message = $state === 'success' ? 'Build succeeded.' : 'Build failed.'; $name = $function->getAttribute('name', '') . ' (' . $project->getAttribute('name', '') . ')'; - $github->updateCommitStatus( + $vcs->updateCommitStatus( $deployment->getAttribute('providerRepositoryName', ''), $commitHash, $deployment->getAttribute('providerRepositoryOwner', ''), diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php index eda58745c5..210c9f38e4 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -123,7 +123,7 @@ class Create extends Action try { $repository = $oauth2->createRepository($accessToken, $name, $private); } catch (Exception $exception) { - throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, "GitHub failed to process the request: " . $exception->getMessage()); + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, "VCS provider failed to process the request: " . $exception->getMessage()); } } else { $providerInstallationId = $installation->getAttribute('providerInstallationId'); @@ -133,7 +133,7 @@ class Create extends Action try { $repository = $vcs->createRepository($owner, $name, $private); } catch (Exception $exception) { - throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, "GitHub failed to process the request: " . $exception->getMessage()); + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, "VCS provider failed to process the request: " . $exception->getMessage()); } } diff --git a/src/Appwrite/Vcs/Factory.php b/src/Appwrite/Vcs/Factory.php index ece8a48b1d..4dc55e11d7 100644 --- a/src/Appwrite/Vcs/Factory.php +++ b/src/Appwrite/Vcs/Factory.php @@ -12,8 +12,8 @@ use Utopia\VCS\Adapter\Git; /** * Builds VCS adapters from the `vcs` config registry. * - * Endpoints inject this service (DI name `vcsFactory`) and resolve the - * adapter from an installation document instead of hardcoding a provider. + * DI resources use this service to resolve adapters from installation + * documents or provider keys instead of hardcoding one provider. * The factory only constructs adapters — it never touches the database. */ class Factory From 45aac0aab1bd54d7e360a80f65ca4884cf713f19 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 13 Jul 2026 16:49:13 +0530 Subject: [PATCH 32/52] Harden VCS installation services --- src/Appwrite/Vcs/InstallationTokens.php | 39 ++++++++++++++---- src/Appwrite/Vcs/RepositoryWebhooks.php | 6 ++- tests/unit/Vcs/InstallationTokensTest.php | 50 ++++++++++++++++++++++- tests/unit/Vcs/RepositoryWebhooksTest.php | 25 ++++++++++-- 4 files changed, 108 insertions(+), 12 deletions(-) diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php index d3dd202e12..5c00835cc7 100644 --- a/src/Appwrite/Vcs/InstallationTokens.php +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -28,12 +28,7 @@ class InstallationTokens $installation = $installation->setAttribute('personalAccessToken', $accessToken); - // A missing expiry means the provider issued a non-expiring token - // (or none was recorded yet) -- treat it as not expired rather than - // forcing a refresh on every call. - $isExpired = !empty($accessTokenExpiry) && new \DateTime($accessTokenExpiry) < new \DateTime('now'); - - if (!$isExpired) { + if (!$this->isExpired($accessTokenExpiry)) { return $installation; } @@ -48,7 +43,12 @@ class InstallationTokens $verificationId = $oauth2->getUserID($accessToken); if (empty($verificationId)) { - throw new Exception(Exception::GENERAL_RATE_LIMIT_EXCEEDED, 'Another request is currently refreshing OAuth token. Please try again.'); + $current = $this->getCurrentInstallation($dbForPlatform, $installation); + if (!$current->isEmpty() && !empty($current->getAttribute('personalAccessToken')) && !$this->isExpired($current->getAttribute('personalAccessTokenExpiry'))) { + return $current; + } + + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, 'Failed to refresh OAuth2 access token. Please reconnect the installation.'); } $installation = $installation @@ -64,4 +64,29 @@ class InstallationTokens return $installation; } + + protected function isExpired(?string $expiry): bool + { + // A missing expiry means the provider issued a non-expiring token + // (or none was recorded yet) -- treat it as not expired rather than + // forcing a refresh on every call. + if (empty($expiry)) { + return false; + } + + try { + return new \DateTime($expiry) < new \DateTime('now'); + } catch (\Throwable) { + return false; + } + } + + protected function getCurrentInstallation(Database $dbForPlatform, Document $installation): Document + { + try { + return $dbForPlatform->getDocument('installations', $installation->getId()); + } catch (\Throwable) { + return new Document(); + } + } } diff --git a/src/Appwrite/Vcs/RepositoryWebhooks.php b/src/Appwrite/Vcs/RepositoryWebhooks.php index 388ce4196b..a25f2de991 100644 --- a/src/Appwrite/Vcs/RepositoryWebhooks.php +++ b/src/Appwrite/Vcs/RepositoryWebhooks.php @@ -68,7 +68,11 @@ class RepositoryWebhooks return; } - $provider = $installation->getAttribute('provider', 'github'); + $provider = $installation->getAttribute('provider', ''); + if (empty($provider)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing VCS provider for installation: ' . $installation->getId()); + } + $url = $this->buildWebhookUrl($provider); $secret = $this->vcsFactory->getWebhookSecret($provider); diff --git a/tests/unit/Vcs/InstallationTokensTest.php b/tests/unit/Vcs/InstallationTokensTest.php index e6f9f39231..cecc11113b 100644 --- a/tests/unit/Vcs/InstallationTokensTest.php +++ b/tests/unit/Vcs/InstallationTokensTest.php @@ -16,7 +16,7 @@ final class InstallationTokensTest extends TestCase { protected function db(): Database { - return $this->createMock(Database::class); + return $this->createStub(Database::class); } public function testUnexpiredTokenIsNotRefreshed(): void @@ -53,6 +53,23 @@ final class InstallationTokensTest extends TestCase $this->assertSame(0, $oauth2->refreshCalls); } + public function testInvalidExpiryIsNotRefreshed(): void + { + $installation = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'valid-token', + 'personalRefreshToken' => 'valid-refresh', + 'personalAccessTokenExpiry' => 'not-a-date', + ]); + + $oauth2 = $this->fakeOAuth2(); + + $result = (new InstallationTokens())->refresh($installation, $this->db(), $oauth2); + + $this->assertSame('valid-token', $result->getAttribute('personalAccessToken')); + $this->assertSame(0, $oauth2->refreshCalls); + } + public function testExpiredTokenIsRefreshedAndPersisted(): void { $db = $this->createMock(Database::class); @@ -130,9 +147,40 @@ final class InstallationTokensTest extends TestCase $oauth2 = $this->fakeOAuth2(emptyUserId: true); $this->expectException(Exception::class); + $this->expectExceptionMessage('Failed to refresh OAuth2 access token'); (new InstallationTokens())->refresh($installation, $this->db(), $oauth2); } + public function testFailedRefreshReturnsCurrentInstallationWhenAnotherRequestRefreshed(): void + { + $db = $this->createMock(Database::class); + $current = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'already-refreshed-token', + 'personalRefreshToken' => 'already-refreshed-refresh', + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), 3600), + ]); + + $db->expects($this->once()) + ->method('getDocument') + ->with('installations', 'installation1') + ->willReturn($current); + $db->expects($this->never())->method('updateDocument'); + + $installation = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'stale-token', + 'personalRefreshToken' => 'stale-refresh', + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), -3600), + ]); + + $oauth2 = $this->fakeOAuth2(emptyUserId: true); + + $result = (new InstallationTokens())->refresh($installation, $db, $oauth2); + + $this->assertSame('already-refreshed-token', $result->getAttribute('personalAccessToken')); + } + protected function fakeOAuth2(bool $emptyUserId = false) { return new class ($emptyUserId) extends OAuth2 { diff --git a/tests/unit/Vcs/RepositoryWebhooksTest.php b/tests/unit/Vcs/RepositoryWebhooksTest.php index e1775d82c5..f29e53e1b0 100644 --- a/tests/unit/Vcs/RepositoryWebhooksTest.php +++ b/tests/unit/Vcs/RepositoryWebhooksTest.php @@ -77,10 +77,12 @@ final class RepositoryWebhooksTest extends TestCase { $adapter = $this->createMock(Git::class); $adapter->method('getSupportedWebhookScopes')->willReturn([Git::WEBHOOK_SCOPE_REPOSITORY]); - $adapter->method('createWebhook')->willThrowException(new \RuntimeException('provider unreachable')); + $adapter->expects($this->once()) + ->method('createWebhook') + ->willThrowException(new \RuntimeException('provider unreachable')); $db = $this->createMock(Database::class); - $db->method('count')->willReturn(1); + $db->expects($this->once())->method('count')->willReturn(1); $installation = new Document(['$sequence' => 1, 'provider' => 'gitea']); @@ -88,6 +90,23 @@ final class RepositoryWebhooksTest extends TestCase $this->ensure($adapter, $installation, $db, $this->factory('secret')); } + public function testMissingProviderThrows(): void + { + $adapter = $this->createMock(Git::class); + $adapter->method('getSupportedWebhookScopes')->willReturn([Git::WEBHOOK_SCOPE_REPOSITORY]); + $adapter->expects($this->never())->method('createWebhook'); + + $db = $this->createMock(Database::class); + $db->expects($this->once())->method('count')->willReturn(1); + + $installation = new Document(['$id' => 'installation1', '$sequence' => 1]); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Missing VCS provider'); + + $this->ensure($adapter, $installation, $db, $this->factory('secret')); + } + protected function ensure(Git $adapter, Document $installation, Database $db, Factory $vcsFactory): void { \putenv('_APP_VCS_WEBHOOK_URL=https://example.com'); @@ -99,7 +118,7 @@ final class RepositoryWebhooksTest extends TestCase protected function factory(string $secret): Factory { - $factory = $this->createMock(Factory::class); + $factory = $this->createStub(Factory::class); $factory->method('getWebhookSecret')->willReturn($secret); return $factory; From 570121694801f6f1cb5231461ef85b9e887a6dd3 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 13 Jul 2026 16:52:22 +0530 Subject: [PATCH 33/52] Trim VCS service comments --- src/Appwrite/Vcs/InstallationTokens.php | 7 ------- src/Appwrite/Vcs/RepositoryWebhooks.php | 27 +------------------------ 2 files changed, 1 insertion(+), 33 deletions(-) diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php index 5c00835cc7..17790e91e7 100644 --- a/src/Appwrite/Vcs/InstallationTokens.php +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -8,12 +8,8 @@ use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; -// Owns the one database write OAuth2 token refresh needs, so Factory stays -// free of persistence. class InstallationTokens { - // Falls back to $identity's tokens when the installation has none yet -- - // the first repository action after connecting a personal installation. public function refresh(Document $installation, Database $dbForPlatform, OAuth2 $oauth2, ?Document $identity = null): Document { $accessToken = $installation->getAttribute('personalAccessToken'); @@ -67,9 +63,6 @@ class InstallationTokens protected function isExpired(?string $expiry): bool { - // A missing expiry means the provider issued a non-expiring token - // (or none was recorded yet) -- treat it as not expired rather than - // forcing a refresh on every call. if (empty($expiry)) { return false; } diff --git a/src/Appwrite/Vcs/RepositoryWebhooks.php b/src/Appwrite/Vcs/RepositoryWebhooks.php index a25f2de991..0cae3ab26b 100644 --- a/src/Appwrite/Vcs/RepositoryWebhooks.php +++ b/src/Appwrite/Vcs/RepositoryWebhooks.php @@ -9,15 +9,6 @@ use Utopia\Database\Query; use Utopia\System\System; use Utopia\VCS\Adapter\Git; -/** - * Creates per-repository webhooks for self-hosted providers. - * - * Callers invoke this unconditionally on every repository connection. - * Prefers installation-scope delivery when the adapter supports it (e.g. - * GitHub App, where events already arrive platform-wide) and only falls - * back to creating a per-repository webhook otherwise, so no endpoint - * needs its own per-provider branching. - */ class RepositoryWebhooks { public function __construct( @@ -25,19 +16,6 @@ class RepositoryWebhooks ) { } - /** - * Creates a webhook for $owner/$repositoryName unless the adapter - * already delivers events at the installation level, or a - * `repositories` document already exists for this installation + - * provider repository (an earlier connection already went through this - * path). - * - * Callers must persist the current repository connection's document - * (via createDocument) before calling this — the idempotency check - * counts existing `repositories` rows for this installation + - * providerRepositoryId and expects to see itself already included - * (count 1 on first connection, 2+ on any repeat). - */ public function ensure( Git $adapter, Document $installation, @@ -48,9 +26,6 @@ class RepositoryWebhooks ): void { $scopes = $adapter->getSupportedWebhookScopes(); - // Installation-scope delivery is preferred when available -- it - // already covers every repository the integration has access to, - // so a separate per-repository webhook would be redundant. if (\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $scopes, true)) { return; } @@ -59,6 +34,7 @@ class RepositoryWebhooks return; } + // The caller persists the current repository connection before this check. $connections = $dbForPlatform->count('repositories', [ Query::equal('installationInternalId', [$installation->getSequence()]), Query::equal('providerRepositoryId', [$providerRepositoryId]), @@ -91,7 +67,6 @@ class RepositoryWebhooks $endpoint = $protocol . '://' . System::getEnv('_APP_DOMAIN', 'localhost'); } - // Accept the base with or without a trailing /v1 (both are used in practice). $endpoint = \rtrim($endpoint, '/'); if (\str_ends_with($endpoint, '/v1')) { $endpoint = \substr($endpoint, 0, -3); From 57dd4922421f7d455b3bb1dd6e7781871bdaa690 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 13 Jul 2026 17:01:59 +0530 Subject: [PATCH 34/52] Use VCS factory helpers in GitHub routes --- .../Platform/Modules/VCS/Http/GitHub/Events/Create.php | 6 ++++-- .../VCS/Http/Installations/Repositories/Contents/Get.php | 3 +-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 42be69bd1f..ac7e90b8a7 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -8,6 +8,7 @@ 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 OpenRuntimes\Orchestrator\Jobs; use Utopia\Console; use Utopia\Database\Database; @@ -16,7 +17,6 @@ use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Platform\Scope\HTTP; use Utopia\Span\Span; -use Utopia\System\System; use Utopia\VCS\Adapter\Git\GitHub; class Create extends Action @@ -37,6 +37,7 @@ class Create extends Action ->desc('Create event') ->groups(['api', 'vcs']) ->label('scope', 'public') + ->inject('vcsFactory') ->inject('vcsForProvider') ->inject('vcsForInstallation') ->inject('request') @@ -51,6 +52,7 @@ class Create extends Action } public function action( + VcsFactory $vcsFactory, callable $vcsForProvider, callable $vcsForInstallation, Request $request, @@ -71,7 +73,7 @@ class Create extends Action $payload = $request->getRawPayload(); $signature = $request->getHeaderLine('x-hub-signature-256', ''); - $secretKey = System::getEnv('_APP_VCS_GITHUB_WEBHOOK_SECRET', ''); + $secretKey = $vcsFactory->getWebhookSecret('github'); $valid = empty($secretKey) ? true : $vcs->validateWebhookEvent($payload, $signature, $secretKey); Span::add('vcs.github.event.signature.valid', $valid); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php index 0d95e9e3e9..be71500a1a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php @@ -12,7 +12,6 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Text; -use Utopia\VCS\Adapter\Git\GitHub; use Utopia\VCS\Exception\RepositoryNotFound; class Get extends Action @@ -89,7 +88,7 @@ class Get extends Action $vcsContents = []; foreach ($contents as $content) { $isDirectory = false; - if ($content['type'] === GitHub::CONTENTS_DIRECTORY) { + if ($content['type'] === 'dir') { $isDirectory = true; } From b3b43ab4e9ee8f337caaa9416ddf1026a8747d6c Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 13 Jul 2026 17:04:27 +0530 Subject: [PATCH 35/52] Use bootstrapped VCS config in test --- tests/unit/Vcs/FactoryTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index d015b2b11c..9d2d367922 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -9,7 +9,6 @@ use Appwrite\Vcs\Factory; use PHPUnit\Framework\TestCase; use Utopia\Cache\Adapter\None; use Utopia\Cache\Cache; -use Utopia\Config\Adapters\PHP as ConfigPHP; use Utopia\Config\Config; use Utopia\Database\Document; use Utopia\VCS\Adapter\Git; @@ -24,8 +23,6 @@ final class FactoryTest extends TestCase public function testRegistryEntries(): void { - Config::load('vcs', __DIR__ . '/../../../app/config/vcs.php', new ConfigPHP()); - $registry = Config::getParam('vcs', []); $this->assertNotEmpty($registry); From 0fd8c449ffbe259c638a2781ed272b1a1b6100c7 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 13 Jul 2026 17:05:55 +0530 Subject: [PATCH 36/52] Trim VCS factory comments --- src/Appwrite/Vcs/Factory.php | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/src/Appwrite/Vcs/Factory.php b/src/Appwrite/Vcs/Factory.php index 4dc55e11d7..4d8037e035 100644 --- a/src/Appwrite/Vcs/Factory.php +++ b/src/Appwrite/Vcs/Factory.php @@ -9,13 +9,6 @@ use Utopia\Database\Document; use Utopia\System\System; use Utopia\VCS\Adapter\Git; -/** - * Builds VCS adapters from the `vcs` config registry. - * - * DI resources use this service to resolve adapters from installation - * documents or provider keys instead of hardcoding one provider. - * The factory only constructs adapters — it never touches the database. - */ class Factory { /** @@ -41,11 +34,6 @@ class Factory } } - /** - * Keys of providers that are enabled and fully configured. - * - * @return array - */ public function getProviders(): array { return \array_values(\array_filter( @@ -54,9 +42,6 @@ class Factory )); } - /** - * Whether all required environment variables are set for a provider. - */ public function isConfigured(string $key): bool { if (!isset($this->registry[$key])) { @@ -72,10 +57,6 @@ class Factory return true; } - /** - * Uninitialized adapter for provider-level operations that need no - * installation credentials (webhook signature validation, payload parsing). - */ public function fromProvider(string $key): Git { if (!isset($this->registry[$key])) { @@ -92,11 +73,6 @@ class Factory return $adapter; } - /** - * Initialized adapter for an installation. App credentials and personal - * tokens are both always passed; each adapter uses the set its - * authentication scheme needs and ignores the other. - */ public function fromInstallation(Document $installation): Git { if ($installation->isEmpty()) { @@ -121,9 +97,6 @@ class Factory return $adapter; } - /** - * Webhook secret configured for a provider, empty if none is set. - */ public function getWebhookSecret(string $key): string { return $this->getEnv($key, 'WEBHOOK_SECRET'); From 8a12d98705f5017e14bf3930fafc020fb4168bf5 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 13 Jul 2026 17:11:52 +0530 Subject: [PATCH 37/52] Hide VCS factory behind DI closures --- app/init/resources.php | 4 ++++ .../Platform/Modules/Console/Http/Variables/Get.php | 7 +++---- .../Platform/Modules/VCS/Http/GitHub/Events/Create.php | 7 +++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index d0c7bc07be..37705c48fe 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -378,10 +378,14 @@ $container->set('promiseAdapter', fn ($register) => $register->get('promiseAdapt $container->set('vcsFactory', fn (Cache $cache) => new VcsFactory($cache), ['cache']); +$container->set('vcsProviders', fn (VcsFactory $vcsFactory) => fn () => $vcsFactory->getProviders(), ['vcsFactory']); + $container->set('vcsForInstallation', fn (VcsFactory $vcsFactory) => fn (Document $installation) => $vcsFactory->fromInstallation($installation), ['vcsFactory']); $container->set('vcsForProvider', fn (VcsFactory $vcsFactory) => fn (string $provider) => $vcsFactory->fromProvider($provider), ['vcsFactory']); +$container->set('vcsWebhookSecret', fn (VcsFactory $vcsFactory) => fn (string $provider) => $vcsFactory->getWebhookSecret($provider), ['vcsFactory']); + $container->set('plan', fn () => []); $container->set('smsRates', fn () => []); diff --git a/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php index f1beaeb276..0ab83d27d2 100644 --- a/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php +++ b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php @@ -7,7 +7,6 @@ use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Domains\Domain; @@ -47,7 +46,7 @@ class Get extends Action ], contentType: ContentType::JSON )) - ->inject('vcsFactory') + ->inject('vcsProviders') ->inject('response') ->inject('platform') ->inject('dbForProject') @@ -55,7 +54,7 @@ class Get extends Action } public function action( - Factory $vcsFactory, + callable $vcsProviders, Response $response, array $platform, Database $dbForProject @@ -72,7 +71,7 @@ class Get extends Action $isDomainEnabled = $isAAAAValid || $isAValid || $isCNAMEValid; - $isVcsEnabled = !empty($vcsFactory->getProviders()); + $isVcsEnabled = !empty($vcsProviders()); $isAssistantEnabled = !empty(System::getEnv('_APP_ASSISTANT_OPENAI_API_KEY', '')); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index ac7e90b8a7..687214bd93 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -8,7 +8,6 @@ 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 OpenRuntimes\Orchestrator\Jobs; use Utopia\Console; use Utopia\Database\Database; @@ -37,7 +36,7 @@ class Create extends Action ->desc('Create event') ->groups(['api', 'vcs']) ->label('scope', 'public') - ->inject('vcsFactory') + ->inject('vcsWebhookSecret') ->inject('vcsForProvider') ->inject('vcsForInstallation') ->inject('request') @@ -52,7 +51,7 @@ class Create extends Action } public function action( - VcsFactory $vcsFactory, + callable $vcsWebhookSecret, callable $vcsForProvider, callable $vcsForInstallation, Request $request, @@ -73,7 +72,7 @@ class Create extends Action $payload = $request->getRawPayload(); $signature = $request->getHeaderLine('x-hub-signature-256', ''); - $secretKey = $vcsFactory->getWebhookSecret('github'); + $secretKey = $vcsWebhookSecret('github'); $valid = empty($secretKey) ? true : $vcs->validateWebhookEvent($payload, $signature, $secretKey); Span::add('vcs.github.event.signature.valid', $valid); From f914a4dd91c63593c5fc3fe387832afbfc0437d2 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 13 Jul 2026 17:18:18 +0530 Subject: [PATCH 38/52] Harden VCS token refresh service --- src/Appwrite/Vcs/InstallationTokens.php | 16 +++++++-- tests/unit/Vcs/InstallationTokensTest.php | 42 +++++++++++++++++++++-- tests/unit/Vcs/RepositoryWebhooksTest.php | 8 +++-- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php index 17790e91e7..43fcb3088c 100644 --- a/src/Appwrite/Vcs/InstallationTokens.php +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -22,7 +22,10 @@ class InstallationTokens $accessTokenExpiry = $accessTokenExpiry ?? $identity->getAttribute('providerAccessTokenExpiry'); } - $installation = $installation->setAttribute('personalAccessToken', $accessToken); + $installation = $installation + ->setAttribute('personalAccessToken', $accessToken) + ->setAttribute('personalRefreshToken', $refreshToken) + ->setAttribute('personalAccessTokenExpiry', $accessTokenExpiry); if (!$this->isExpired($accessTokenExpiry)) { return $installation; @@ -32,7 +35,16 @@ class InstallationTokens throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, 'This installation has no refresh token on file. Please reconnect it.'); } - $oauth2->refreshTokens($refreshToken); + try { + $oauth2->refreshTokens($refreshToken); + } catch (\Throwable) { + $current = $this->getCurrentInstallation($dbForPlatform, $installation); + if (!$current->isEmpty() && !empty($current->getAttribute('personalAccessToken')) && !$this->isExpired($current->getAttribute('personalAccessTokenExpiry'))) { + return $current; + } + + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, 'Failed to refresh OAuth2 access token. Please reconnect the installation.'); + } $accessToken = $oauth2->getAccessToken(''); $refreshToken = $oauth2->getRefreshToken(''); diff --git a/tests/unit/Vcs/InstallationTokensTest.php b/tests/unit/Vcs/InstallationTokensTest.php index cecc11113b..96f1381eee 100644 --- a/tests/unit/Vcs/InstallationTokensTest.php +++ b/tests/unit/Vcs/InstallationTokensTest.php @@ -112,6 +112,8 @@ final class InstallationTokensTest extends TestCase $result = (new InstallationTokens())->refresh($installation, $this->db(), $oauth2, $identity); $this->assertSame('identity-token', $result->getAttribute('personalAccessToken')); + $this->assertSame('identity-refresh', $result->getAttribute('personalRefreshToken')); + $this->assertSame($identity->getAttribute('providerAccessTokenExpiry'), $result->getAttribute('personalAccessTokenExpiry')); } public function testMissingRefreshTokenThrowsClearError(): void @@ -181,13 +183,43 @@ final class InstallationTokensTest extends TestCase $this->assertSame('already-refreshed-token', $result->getAttribute('personalAccessToken')); } - protected function fakeOAuth2(bool $emptyUserId = false) + public function testRefreshExceptionReturnsCurrentInstallationWhenAnotherRequestRefreshed(): void { - return new class ($emptyUserId) extends OAuth2 { + $db = $this->createMock(Database::class); + $current = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'already-refreshed-token', + 'personalRefreshToken' => 'already-refreshed-refresh', + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), 3600), + ]); + + $db->expects($this->once()) + ->method('getDocument') + ->with('installations', 'installation1') + ->willReturn($current); + $db->expects($this->never())->method('updateDocument'); + + $installation = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'stale-token', + 'personalRefreshToken' => 'stale-refresh', + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), -3600), + ]); + + $oauth2 = $this->fakeOAuth2(throwOnRefresh: true); + + $result = (new InstallationTokens())->refresh($installation, $db, $oauth2); + + $this->assertSame('already-refreshed-token', $result->getAttribute('personalAccessToken')); + } + + protected function fakeOAuth2(bool $emptyUserId = false, bool $throwOnRefresh = false) + { + return new class ($emptyUserId, $throwOnRefresh) extends OAuth2 { public int $refreshCalls = 0; protected array $tokens = []; - public function __construct(protected bool $emptyUserId) + public function __construct(protected bool $emptyUserId, protected bool $throwOnRefresh) { parent::__construct('id', 'secret', ''); } @@ -210,6 +242,10 @@ final class InstallationTokensTest extends TestCase public function refreshTokens(string $refreshToken): array { $this->refreshCalls++; + if ($this->throwOnRefresh) { + throw new \RuntimeException('refresh token already used'); + } + $this->tokens = [ 'access_token' => 'fresh-token', 'refresh_token' => 'fresh-refresh', diff --git a/tests/unit/Vcs/RepositoryWebhooksTest.php b/tests/unit/Vcs/RepositoryWebhooksTest.php index f29e53e1b0..cb2856d807 100644 --- a/tests/unit/Vcs/RepositoryWebhooksTest.php +++ b/tests/unit/Vcs/RepositoryWebhooksTest.php @@ -111,9 +111,11 @@ final class RepositoryWebhooksTest extends TestCase { \putenv('_APP_VCS_WEBHOOK_URL=https://example.com'); - (new RepositoryWebhooks($vcsFactory))->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo'); - - \putenv('_APP_VCS_WEBHOOK_URL'); + try { + (new RepositoryWebhooks($vcsFactory))->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo'); + } finally { + \putenv('_APP_VCS_WEBHOOK_URL'); + } } protected function factory(string $secret): Factory From 11969eb89c262430cf916cd6d2a6c771199399dd Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 13 Jul 2026 17:32:23 +0530 Subject: [PATCH 39/52] Clean up Gitea VCS provider wiring --- app/config/vcs.php | 5 +- app/init/constants.php | 3 + app/init/resources.php | 2 + src/Appwrite/Auth/OAuth2/Gitea.php | 67 ------------------- .../Modules/Console/Http/Variables/Get.php | 2 +- .../Modules/VCS/Http/Authorize/Base.php | 7 +- .../Modules/VCS/Http/Gitea/Events/Create.php | 24 ++----- .../Installations/Repositories/Create.php | 25 ++++++- .../Vcs/GiteaClosedPullRequestEventTest.php | 2 +- 9 files changed, 38 insertions(+), 99 deletions(-) diff --git a/app/config/vcs.php b/app/config/vcs.php index 787fb07ecc..4264d6c0d9 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -30,9 +30,8 @@ return [ 'CLIENT_SECRET' => '_APP_VCS_GITEA_CLIENT_SECRET', 'WEBHOOK_SECRET' => '_APP_VCS_GITEA_WEBHOOK_SECRET', ], - // WEBHOOK_SECRET is required (unlike GitHub's) because a Gitea webhook - // has no other verification layer -- GitHub App deliveries also carry - // a signed JWT Appwrite always validates independently of the secret. + // Unlike GitHub's legacy optional secret, Gitea webhooks must always + // have a shared secret because Appwrite creates them directly. 'requiredEnvVariables' => ['ENDPOINT', 'CLIENT_ID', 'CLIENT_SECRET', 'WEBHOOK_SECRET'], ], ]; diff --git a/app/init/constants.php b/app/init/constants.php index df85fc0925..c90db28043 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -108,6 +108,9 @@ const APP_SDK_PLATFORM_STATIC = 'static'; const APP_VCS_GITHUB_USERNAME = 'Appwrite'; const APP_VCS_GITHUB_EMAIL = 'team@appwrite.io'; const APP_VCS_GITHUB_URL = 'https://github.com/TeamAppwrite'; +const APP_VCS_COMMIT_USERNAME = APP_VCS_GITHUB_USERNAME; +const APP_VCS_COMMIT_EMAIL = APP_VCS_GITHUB_EMAIL; +const APP_VCS_COMMIT_URL = APP_VCS_GITHUB_URL; const APP_BRANDED_EMAIL_BASE_TEMPLATE = 'email-base-styled'; // Embeddings diff --git a/app/init/resources.php b/app/init/resources.php index 0633c78a95..d0b22475fb 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -384,6 +384,8 @@ $container->set('repositoryWebhooks', fn (VcsFactory $vcsFactory) => new Reposit $container->set('vcsProviders', fn (VcsFactory $vcsFactory) => fn () => $vcsFactory->getProviders(), ['vcsFactory']); +$container->set('vcsConfigured', fn (VcsFactory $vcsFactory) => fn (string $provider) => $vcsFactory->isConfigured($provider), ['vcsFactory']); + $container->set('vcsForInstallation', fn (VcsFactory $vcsFactory) => fn (Document $installation) => $vcsFactory->fromInstallation($installation), ['vcsFactory']); $container->set('vcsForProvider', fn (VcsFactory $vcsFactory) => fn (string $provider) => $vcsFactory->fromProvider($provider), ['vcsFactory']); diff --git a/src/Appwrite/Auth/OAuth2/Gitea.php b/src/Appwrite/Auth/OAuth2/Gitea.php index 01914ce8e7..1cc891c167 100644 --- a/src/Appwrite/Auth/OAuth2/Gitea.php +++ b/src/Appwrite/Auth/OAuth2/Gitea.php @@ -4,32 +4,14 @@ namespace Appwrite\Auth\OAuth2; use Appwrite\Auth\OAuth2; -// Reference Material -// https://docs.gitea.com/development/oauth2-provider - class Gitea extends OAuth2 { - /** - * @var string - */ protected string $endpoint = ''; - /** - * @var array - */ protected array $user = []; - /** - * @var array - */ protected array $tokens = []; - /** - * @var array - */ - // The resulting personal token is reused for every VCS operation on the - // installation (list/create repos, read contents, create webhooks, - // comment on PRs) via Factory::fromInstallation(), not just login. protected array $scopes = [ 'read:user', 'read:repository', @@ -39,25 +21,16 @@ class Gitea extends OAuth2 'write:issue', ]; - /** - * Set the URL of the Gitea instance (self-hosted). - */ public function setEndpoint(string $endpoint): void { $this->endpoint = \rtrim($endpoint, '/'); } - /** - * @return string - */ public function getName(): string { return 'gitea'; } - /** - * @return string - */ public function getLoginURL(): string { return $this->endpoint . '/login/oauth/authorize?' . \http_build_query([ @@ -69,11 +42,6 @@ class Gitea extends OAuth2 ]); } - /** - * @param string $code - * - * @return array - */ protected function getTokens(string $code): array { if (empty($this->tokens)) { @@ -96,11 +64,6 @@ class Gitea extends OAuth2 return $this->tokens; } - /** - * @param string $refreshToken - * - * @return array - */ public function refreshTokens(string $refreshToken): array { $response = $this->request( @@ -124,11 +87,6 @@ class Gitea extends OAuth2 return $this->tokens; } - /** - * @param string $accessToken - * - * @return string - */ public function getUserID(string $accessToken): string { $user = $this->getUser($accessToken); @@ -136,11 +94,6 @@ class Gitea extends OAuth2 return \strval($user['id'] ?? ''); } - /** - * @param string $accessToken - * - * @return string - */ public function getUserEmail(string $accessToken): string { $user = $this->getUser($accessToken); @@ -148,11 +101,6 @@ class Gitea extends OAuth2 return $user['email'] ?? ''; } - /** - * @param string $accessToken - * - * @return bool - */ public function isEmailVerified(string $accessToken): bool { $user = $this->getUser($accessToken); @@ -160,11 +108,6 @@ class Gitea extends OAuth2 return $user['verified'] ?? false; } - /** - * @param string $accessToken - * - * @return string - */ public function getUserName(string $accessToken): string { $user = $this->getUser($accessToken); @@ -172,11 +115,6 @@ class Gitea extends OAuth2 return $user['full_name'] ?? ''; } - /** - * @param string $accessToken - * - * @return string - */ public function getUserSlug(string $accessToken): string { $user = $this->getUser($accessToken); @@ -184,11 +122,6 @@ class Gitea extends OAuth2 return $user['login'] ?? ''; } - /** - * @param string $accessToken - * - * @return array - */ protected function getUser(string $accessToken) { if (empty($this->user)) { diff --git a/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php index 609e3083aa..9adf5ebf9a 100644 --- a/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php +++ b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php @@ -87,7 +87,7 @@ class Get extends Action '_APP_COMPUTE_SIZE_LIMIT' => +System::getEnv('_APP_COMPUTE_SIZE_LIMIT'), '_APP_USAGE_STATS' => System::getEnv('_APP_USAGE_STATS'), '_APP_VCS_ENABLED' => $isVcsEnabled, - '_APP_VCS_PROVIDERS' => $vcsFactory->getProviders(), + '_APP_VCS_PROVIDERS' => $vcsProviders(), '_APP_DOMAIN_ENABLED' => $isDomainEnabled, '_APP_ASSISTANT_ENABLED' => $isAssistantEnabled, '_APP_DOMAIN_SITES' => $platform['sitesDomain'], diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php index 6784fe385d..06b0fcea1a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php @@ -11,7 +11,6 @@ use Appwrite\SDK\Method; use Appwrite\SDK\MethodType; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Appwrite\Vcs\Factory; use Utopia\Database\Document; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; @@ -72,7 +71,7 @@ abstract class Base extends Action )) ->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('vcsFactory') + ->inject('vcsConfigured') ->inject('response') ->inject('project') ->inject('platform') @@ -82,14 +81,14 @@ abstract class Base extends Action public function action( string $success, string $failure, - Factory $vcsFactory, + callable $vcsConfigured, Response $response, Document $project, array $platform ) { $key = static::getProvider(); - if (!$vcsFactory->isConfigured($key)) { + if (!$vcsConfigured($key)) { throw new Exception(Exception::GENERAL_SERVER_ERROR, static::getProviderName() . ' provider is not configured. Please configure VCS (Version Control System) variables in .env file.'); } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index e115ac03e3..a04ef72eda 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -15,7 +15,6 @@ use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Platform\Scope\HTTP; use Utopia\Span\Span; -use Utopia\System\System; use Utopia\VCS\Adapter\Git; class Create extends Action @@ -38,6 +37,7 @@ class Create extends Action ->label('scope', 'public') ->inject('vcsForProvider') ->inject('vcsForInstallation') + ->inject('vcsWebhookSecret') ->inject('request') ->inject('response') ->inject('dbForPlatform') @@ -51,6 +51,7 @@ class Create extends Action public function action( callable $vcsForProvider, callable $vcsForInstallation, + callable $vcsWebhookSecret, Request $request, Response $response, Database $dbForPlatform, @@ -66,12 +67,8 @@ class Create extends Action $payload = $request->getRawPayload(); $signature = $request->getHeaderLine($vcs->getSignatureHeaderName(), ''); - $secretKey = System::getEnv('_APP_VCS_GITEA_WEBHOOK_SECRET', ''); + $secretKey = $vcsWebhookSecret('gitea'); - // Fail closed, unlike GitHub App deliveries: those also carry a - // signed JWT Appwrite always validates, so an empty webhook secret - // there still leaves a real check in place. Gitea has no such - // fallback -- an empty secret here would mean no verification at all. $valid = !empty($secretKey) && $vcs->validateWebhookEvent($payload, $signature, $secretKey); Span::add('vcs.gitea.event.signature.valid', $valid); @@ -90,19 +87,6 @@ class Create extends Action $response->json($parsedPayload); } - /** - * Resolves an authenticated adapter for a repository connection's own - * installation. Unlike GitHub App, where one adapter serves every - * repository under an installation, Gitea's OAuth2 installations each - * carry their own personal token — repositories matching the same - * providerRepositoryId can belong to different Appwrite installations. - */ - /** - * `repositories` has no provider attribute -- providerRepositoryId is - * provider-scoped numeric IDs from independent servers, so a GitHub - * and a Gitea repository can collide on the same ID. Filter by the - * matched installation's own provider instead of the query. - */ private function resolveGiteaInstallation(Document $repository, Database $dbForPlatform, Authorization $authorization): ?Document { $installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', ''))); @@ -157,7 +141,7 @@ class Create extends Action Span::add('vcs.gitea.event.repo.name', $providerRepositoryName); Span::add('vcs.gitea.event.branch', $providerBranch); - if ($providerCommitAuthorEmail === APP_VCS_GITHUB_EMAIL || $providerBranchDeleted) { + if ($providerCommitAuthorEmail === APP_VCS_COMMIT_EMAIL || $providerBranchDeleted) { return; } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php index 18f38cdb38..4962f5f739 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\VCS\Http\Installations\Repositories; +use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea; use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; @@ -76,11 +77,13 @@ class Create extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + $provider = $installation->getAttribute('provider', 'github'); + if ($installation->getAttribute('personal', false) === true) { - $oauth2 = new OAuth2Github(System::getEnv('_APP_VCS_GITHUB_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITHUB_CLIENT_SECRET', ''), ""); + $oauth2 = $this->createOAuth2($provider); $identity = null; - if (empty($installation->getAttribute('personalAccessToken')) || empty($installation->getAttribute('personalRefreshToken')) || empty($installation->getAttribute('personalAccessTokenExpiry'))) { + if ($provider === 'github' && (empty($installation->getAttribute('personalAccessToken')) || empty($installation->getAttribute('personalRefreshToken')) || empty($installation->getAttribute('personalAccessTokenExpiry')))) { $identity = $dbForPlatform->findOne('identities', [ Query::equal('provider', ['github']), Query::equal('userInternalId', [$user->getSequence()]), @@ -95,7 +98,7 @@ class Create extends Action try { $repository = $oauth2->createRepository($accessToken, $name, $private); - } catch (Exception $exception) { + } catch (\Throwable $exception) { throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, "VCS provider failed to process the request: " . $exception->getMessage()); } } else { @@ -131,4 +134,20 @@ class Create extends Action $response->dynamic(new Document($repository), Response::MODEL_PROVIDER_REPOSITORY); } + + protected function createOAuth2(string $provider): OAuth2Github|OAuth2Gitea + { + if ($provider === 'github') { + return new OAuth2Github(System::getEnv('_APP_VCS_GITHUB_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITHUB_CLIENT_SECRET', ''), ""); + } + + if ($provider === 'gitea') { + $oauth2 = new OAuth2Gitea(System::getEnv('_APP_VCS_GITEA_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITEA_CLIENT_SECRET', ''), ""); + $oauth2->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', '')); + + return $oauth2; + } + + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unsupported VCS provider: ' . $provider); + } } diff --git a/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php b/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php index 85877b4005..3a844625e5 100644 --- a/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php +++ b/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php @@ -43,7 +43,7 @@ final class GiteaClosedPullRequestEventTest extends TestCase $githubInstallation = new Document(['$id' => 'install-github', 'provider' => 'github']); $dbForPlatform = $this->createMock(Database::class); - $authorization = $this->createMock(Authorization::class); + $authorization = $this->createStub(Authorization::class); $dbForPlatform->method('find')->willReturn([$giteaRepository, $githubRepository]); From a4af55815d8ac2c2a56a21ccb51eb8bb0115b686 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 11:21:26 +0530 Subject: [PATCH 40/52] Call VCS factory directly instead of via closure wrapper vcsForInstallation/vcsForProvider were 1:1 passthrough closures around VcsFactory::fromInstallation()/fromProvider() with no resolution logic of their own, unlike getProjectDB which does real per-request work. Inject vcsFactory and call it directly at each site instead. --- app/controllers/mock.php | 7 +++--- app/init/resources.php | 4 --- .../Http/Deployments/Template/Create.php | 11 ++++---- .../Functions/Http/Deployments/Vcs/Create.php | 7 +++--- .../Functions/Http/Functions/Create.php | 7 +++--- .../Functions/Http/Functions/Update.php | 7 +++--- .../Modules/Functions/Workers/Builds.php | 15 ++++++----- .../Modules/Functions/Workers/Jobs.php | 25 ++++++++++--------- .../Http/Deployments/Template/Create.php | 7 +++--- .../Sites/Http/Deployments/Vcs/Create.php | 7 +++--- .../Modules/Sites/Http/Sites/Update.php | 7 +++--- .../Http/GitHub/Authorize/External/Update.php | 7 +++--- .../Modules/VCS/Http/GitHub/Callback/Get.php | 7 +++--- .../Modules/VCS/Http/GitHub/Events/Create.php | 21 ++++++++-------- .../Repositories/Branches/XList.php | 7 +++--- .../Repositories/Contents/Get.php | 7 +++--- .../Installations/Repositories/Create.php | 7 +++--- .../Repositories/Detections/Create.php | 7 +++--- .../Http/Installations/Repositories/Get.php | 7 +++--- .../Http/Installations/Repositories/XList.php | 7 +++--- 20 files changed, 95 insertions(+), 86 deletions(-) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index e1f0d138ee..fa691828a4 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -5,6 +5,7 @@ global $utopia, $request, $response; use Appwrite\Extend\Exception; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; @@ -238,11 +239,11 @@ Http::get('/v1/mock/github/callback') ->label('docs', false) ->param('providerInstallationId', '', new UID(), 'GitHub installation ID') ->param('projectId', '', new UID(), 'Project ID of the project where app is to be installed') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('project') ->inject('response') ->inject('dbForPlatform') - ->action(function (string $providerInstallationId, string $projectId, callable $vcsForInstallation, Document $project, Response $response, Database $dbForPlatform) { + ->action(function (string $providerInstallationId, string $projectId, VcsFactory $vcsFactory, Document $project, Response $response, Database $dbForPlatform) { $isDevelopment = System::getEnv('_APP_ENV', 'development') === 'development'; if (!$isDevelopment) { @@ -260,7 +261,7 @@ Http::get('/v1/mock/github/callback') throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing provider installation ID'); } - $vcs = $vcsForInstallation(new Document([ + $vcs = $vcsFactory->fromInstallation(new Document([ 'provider' => 'github', 'providerInstallationId' => $providerInstallationId, ])); diff --git a/app/init/resources.php b/app/init/resources.php index 37705c48fe..20b4390c36 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -380,10 +380,6 @@ $container->set('vcsFactory', fn (Cache $cache) => new VcsFactory($cache), ['cac $container->set('vcsProviders', fn (VcsFactory $vcsFactory) => fn () => $vcsFactory->getProviders(), ['vcsFactory']); -$container->set('vcsForInstallation', fn (VcsFactory $vcsFactory) => fn (Document $installation) => $vcsFactory->fromInstallation($installation), ['vcsFactory']); - -$container->set('vcsForProvider', fn (VcsFactory $vcsFactory) => fn (string $provider) => $vcsFactory->fromProvider($provider), ['vcsFactory']); - $container->set('vcsWebhookSecret', fn (VcsFactory $vcsFactory) => fn (string $provider) => $vcsFactory->getWebhookSecret($provider), ['vcsFactory']); $container->set('plan', fn () => []); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php index c82407cfe6..146c75c11f 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php @@ -13,6 +13,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use OpenRuntimes\Orchestrator\Jobs; use Utopia\Database\Database; use Utopia\Database\Document; @@ -83,8 +84,7 @@ class Create extends Base ->inject('queueForEvents') ->inject('project') ->inject('publisherForBuilds') - ->inject('vcsForInstallation') - ->inject('vcsForProvider') + ->inject('vcsFactory') ->inject('jobs') ->inject('authorization') ->inject('platform') @@ -106,8 +106,7 @@ class Create extends Base Event $queueForEvents, Document $project, BuildPublisher $publisherForBuilds, - callable $vcsForInstallation, - callable $vcsForProvider, + VcsFactory $vcsFactory, Jobs $jobs, Authorization $authorization, array $platform @@ -145,7 +144,7 @@ class Create extends Base dbForProject: $dbForProject, publisherForBuilds: $publisherForBuilds, template: $template, - vcs: $vcsForInstallation($installation), + vcs: $vcsFactory->fromInstallation($installation), activate: $activate, platform: $platform, referenceType: $type, @@ -204,7 +203,7 @@ class Create extends Base try { // Templates are public github.com repositories regardless of // the function's own provider. - $tags = $vcsForProvider('github')->listTags($owner, $repository, $reference); + $tags = $vcsFactory->fromProvider('github')->listTags($owner, $repository, $reference); $ref = \end($tags) ?: $reference; } catch (\Throwable) { // Fall back to the raw reference; the build surfaces a bad ref. diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php index 2919502328..75ade638e9 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php @@ -11,6 +11,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use OpenRuntimes\Orchestrator\Jobs; use Utopia\Database\Database; use Utopia\Database\Document; @@ -73,7 +74,7 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('publisherForBuilds') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('jobs') ->inject('platform') ->callback($this->action(...)); @@ -91,7 +92,7 @@ class Create extends Base Document $project, Event $queueForEvents, BuildPublisher $publisherForBuilds, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Jobs $jobs, array $platform, ) { @@ -117,7 +118,7 @@ class Create extends Base dbForProject: $dbForProject, publisherForBuilds: $publisherForBuilds, template: $template, - vcs: $vcsForInstallation($installation), + vcs: $vcsFactory->fromInstallation($installation), activate: $activate, platform: $platform, reference: $reference, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php index a251a1f8ee..943a5e3af5 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -20,6 +20,7 @@ use Appwrite\Task\Validator\Cron; use Appwrite\Utopia\Database\Validator\CustomId; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model\Rule; +use Appwrite\Vcs\Factory as VcsFactory; use OpenRuntimes\Orchestrator\Jobs; use Utopia\Abuse\Abuse; use Utopia\Config\Config; @@ -129,7 +130,7 @@ class Create extends Base ->inject('publisherForFunctions') ->inject('dbForPlatform') ->inject('request') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -174,7 +175,7 @@ class Create extends Base FunctionPublisher $publisherForFunctions, Database $dbForPlatform, Request $request, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Authorization $authorization, array $platform ) { @@ -347,7 +348,7 @@ class Create extends Base publisherForBuilds: $publisherForBuilds, jobs: $jobs, template: $template, - vcs: $vcsForInstallation($installation), + vcs: $vcsFactory->fromInstallation($installation), activate: true, platform: $platform, reference: $providerBranch, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php index 0f8d685cce..11d1cc8fd7 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -13,6 +13,7 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Task\Validator\Cron; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Executor\Executor; use OpenRuntimes\Orchestrator\Jobs; use Utopia\Config\Config; @@ -113,7 +114,7 @@ class Update extends Base ->inject('publisherForBuilds') ->inject('jobs') ->inject('dbForPlatform') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('executor') ->inject('authorization') ->inject('platform') @@ -151,7 +152,7 @@ class Update extends Base BuildPublisher $publisherForBuilds, Jobs $jobs, Database $dbForPlatform, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Executor $executor, Authorization $authorization, array $platform @@ -311,7 +312,7 @@ class Update extends Base // Redeploy logic if (!$isConnected && !empty($providerRepositoryId)) { - $this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsForInstallation($installation), true, $platform, jobs: $jobs); + $this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsFactory->fromInstallation($installation), true, $platform, jobs: $jobs); } // Inform scheduler if function is still active diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index a10f6bacd0..17e0e03234 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -17,6 +17,7 @@ use Appwrite\Usage\Build as BuildUsage; use Appwrite\Usage\Context; use Appwrite\Utopia\Response\Model\Deployment; use Appwrite\Vcs\Comment; +use Appwrite\Vcs\Factory as VcsFactory; use Exception; use Executor\Exception as ExecutorException; use Executor\Exception\Timeout as ExecutorTimeout; @@ -91,8 +92,7 @@ class Builds extends Action ->inject('queueForRealtime') ->inject('usage') ->inject('publisherForUsage') - ->inject('vcsForProvider') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('dbForProject') ->inject('deviceForFunctions') ->inject('deviceForSites') @@ -119,8 +119,7 @@ class Builds extends Action Realtime $queueForRealtime, Context $usage, UsagePublisher $publisherForUsage, - callable $vcsForProvider, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Database $dbForProject, Device $deviceForFunctions, Device $deviceForSites, @@ -151,7 +150,7 @@ class Builds extends Action switch ($type) { case BUILD_TYPE_DEPLOYMENT: case BUILD_TYPE_RETRY: - $templateVcs = $vcsForProvider('github'); + $templateVcs = $vcsFactory->fromProvider('github'); $this->buildDeployment( $deviceForFunctions, $deviceForSites, @@ -166,7 +165,7 @@ class Builds extends Action $dbForPlatform, $dbForProject, $templateVcs, - $vcsForInstallation, + $vcsFactory, $project, $resource, $deployment, @@ -204,7 +203,7 @@ class Builds extends Action Database $dbForPlatform, Database $dbForProject, Git $templateVcs, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Document $project, Document $resource, Document $deployment, @@ -325,7 +324,7 @@ class Builds extends Action $providerInstallationId = $installation->getAttribute('providerInstallationId'); try { - $providerAdapter = $vcsForInstallation($installation); + $providerAdapter = $vcsFactory->fromInstallation($installation); } catch (\Exception $e) { if ($e->getCode() === 404 && $resource->getAttribute('installationId', '') === $installationId) { diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php index 87a7c27097..b4ced5bce2 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php @@ -13,6 +13,7 @@ use Appwrite\Event\Webhook; use Appwrite\Usage\Build as BuildUsage; use Appwrite\Usage\Context as UsageContext; use Appwrite\Utopia\Response\Model\Deployment; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Cache\Cache; use Utopia\Database\Database; use Utopia\Database\DateTime; @@ -63,7 +64,7 @@ class Jobs extends Action ->inject('publisherForUsage') ->inject('usage') ->inject('deviceForBuilds') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('cache') ->inject('locks') ->callback($this->action(...)); @@ -81,7 +82,7 @@ class Jobs extends Action UsagePublisher $publisherForUsage, UsageContext $usage, Device $deviceForBuilds, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Cache $cache, callable $locks, ): void { @@ -92,7 +93,7 @@ class Jobs extends Action return; } - $locks('jobs-deployment:' . $deploymentId, self::LOCK_TTL, function () use ($event, $project, $dbForProject, $dbForPlatform, $queueForRealtime, $queueForEvents, $queueForWebhooks, $publisherForFunctions, $publisherForUsage, $usage, $deviceForBuilds, $vcsForInstallation, $cache, $deploymentId): void { + $locks('jobs-deployment:' . $deploymentId, self::LOCK_TTL, function () use ($event, $project, $dbForProject, $dbForPlatform, $queueForRealtime, $queueForEvents, $queueForWebhooks, $publisherForFunctions, $publisherForUsage, $usage, $deviceForBuilds, $vcsFactory, $cache, $deploymentId): void { if ($event->id !== '') { $key = 'jobs-event-' . $event->id; if ($cache->load($key, self::DEDUPE_TTL) !== false) { @@ -113,7 +114,7 @@ class Jobs extends Action $deployment = match ($event->event) { 'orchestrator.job.log' => $this->onLog($dbForProject, $deployment, $event->data), 'orchestrator.job.artifact' => $this->onArtifact($dbForProject, $deployment, $event->data), - 'orchestrator.job.exit' => $this->onExit($dbForProject, $dbForPlatform, $project, $deployment, (int) ($event->data['exitCode'] ?? 0), $usage, $publisherForUsage, $deviceForBuilds, $vcsForInstallation), + 'orchestrator.job.exit' => $this->onExit($dbForProject, $dbForPlatform, $project, $deployment, (int) ($event->data['exitCode'] ?? 0), $usage, $publisherForUsage, $deviceForBuilds, $vcsFactory), default => $deployment, }; @@ -202,10 +203,10 @@ class Jobs extends Action UsageContext $usage, UsagePublisher $publisherForUsage, Device $deviceForBuilds, - callable $vcsForInstallation, + VcsFactory $vcsFactory, ): Document { if ($exitCode !== 0) { - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, "Build failed with exit code {$exitCode}.", $usage, $publisherForUsage, $vcsForInstallation); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, "Build failed with exit code {$exitCode}.", $usage, $publisherForUsage, $vcsFactory); } $path = Job::buildPath($project->getId(), $deployment->getId()); @@ -214,10 +215,10 @@ class Jobs extends Action $limit = (int) System::getEnv('_APP_COMPUTE_BUILD_SIZE_LIMIT', '2000000000'); if ($limit !== 0 && $size > $limit) { $deviceForBuilds->delete($path); - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build size should be less than ' . \number_format($limit / (1000 * 1000), 2) . ' MBs.', $usage, $publisherForUsage, $vcsForInstallation); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, false, 'Build size should be less than ' . \number_format($limit / (1000 * 1000), 2) . ' MBs.', $usage, $publisherForUsage, $vcsFactory); } - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $vcsForInstallation, $size); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $vcsFactory, $size); } /** @@ -234,7 +235,7 @@ class Jobs extends Action string $message, UsageContext $usage, UsagePublisher $publisherForUsage, - callable $vcsForInstallation, + VcsFactory $vcsFactory, int $buildSize = 0, ): Document { $function = $dbForProject->getDocument('functions', $deployment->getAttribute('resourceId')); @@ -287,7 +288,7 @@ class Jobs extends Action // deployments only; best-effort). "pending" at build start is a follow-up. $status = $deployment->getAttribute('status'); if (\in_array($status, ['ready', 'failed'], true) && ! $function->isEmpty()) { - $this->gitStatus($vcsForInstallation, $dbForPlatform, $project, $function, $deployment, $status === 'ready' ? 'success' : 'failure'); + $this->gitStatus($vcsFactory, $dbForPlatform, $project, $function, $deployment, $status === 'ready' ? 'success' : 'failure'); } return $deployment; @@ -297,7 +298,7 @@ class Jobs extends Action * Post a VCS commit status for a VCS deployment (no-op for non-VCS builds * or silent mode). Best-effort — a failed status update never fails the build. */ - private function gitStatus(callable $vcsForInstallation, Database $dbForPlatform, Document $project, Document $function, Document $deployment, string $state): void + private function gitStatus(VcsFactory $vcsFactory, Database $dbForPlatform, Document $project, Document $function, Document $deployment, string $state): void { $commitHash = $deployment->getAttribute('providerCommitHash', ''); if ($commitHash === '' || $function->getAttribute('providerSilentMode', false) === true) { @@ -310,7 +311,7 @@ class Jobs extends Action if ($providerInstallationId === '') { return; } - $vcs = $vcsForInstallation($installation); + $vcs = $vcsFactory->fromInstallation($installation); $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; $hostname = System::getEnv('_APP_CONSOLE_DOMAIN', System::getEnv('_APP_DOMAIN', '')); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index 49fcd497c3..772fd88720 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php @@ -12,6 +12,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -81,7 +82,7 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('publisherForBuilds') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -102,7 +103,7 @@ class Create extends Base Document $project, Event $queueForEvents, BuildPublisher $publisherForBuilds, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Authorization $authorization, array $platform ) { @@ -135,7 +136,7 @@ class Create extends Base dbForPlatform: $dbForPlatform, publisherForBuilds: $publisherForBuilds, template: $template, - vcs: $vcsForInstallation($installation), + vcs: $vcsFactory->fromInstallation($installation), activate: $activate, authorization: $authorization, platform: $platform diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php index 98d544cd37..6e79b8fa81 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Vcs/Create.php @@ -11,6 +11,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; @@ -73,7 +74,7 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('publisherForBuilds') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -91,7 +92,7 @@ class Create extends Base Document $project, Event $queueForEvents, BuildPublisher $publisherForBuilds, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Authorization $authorization, array $platform ) { @@ -118,7 +119,7 @@ class Create extends Base dbForPlatform: $dbForPlatform, publisherForBuilds: $publisherForBuilds, template: $template, - vcs: $vcsForInstallation($installation), + vcs: $vcsFactory->fromInstallation($installation), activate: $activate, authorization: $authorization, reference: $reference, diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php index a512c27b97..7c5bea3a16 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php @@ -11,6 +11,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Executor\Executor; use Utopia\Config\Config; use Utopia\Database\Database; @@ -107,7 +108,7 @@ class Update extends Base ->inject('queueForEvents') ->inject('publisherForBuilds') ->inject('dbForPlatform') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('executor') ->inject('platform') ->callback($this->action(...)); @@ -144,7 +145,7 @@ class Update extends Base Event $queueForEvents, BuildPublisher $publisherForBuilds, Database $dbForPlatform, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Executor $executor, array $platform ) { @@ -314,7 +315,7 @@ class Update extends Base // Redeploy logic if (!$isConnected && !empty($providerRepositoryId)) { - $this->redeployVcsFunction($request, $site, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsForInstallation($installation), true, $platform); + $this->redeployVcsFunction($request, $site, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsFactory->fromInstallation($installation), true, $platform); } $queueForEvents->setParam('siteId', $site->getId()); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php index 3edc1478df..e06912452a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php @@ -10,6 +10,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use OpenRuntimes\Orchestrator\Jobs; use Utopia\Database\Database; use Utopia\Database\Document; @@ -53,7 +54,7 @@ class Update extends Action ->param('installationId', '', new Text(256), 'Installation Id') ->param('repositoryId', '', new Text(256), 'VCS Repository Id') ->param('providerPullRequestId', '', new Text(256), 'GitHub Pull Request Id') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('response') ->inject('project') ->inject('dbForPlatform') @@ -69,7 +70,7 @@ class Update extends Action string $installationId, string $repositoryId, string $providerPullRequestId, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Response $response, Document $project, Database $dbForPlatform, @@ -103,7 +104,7 @@ class Update extends Action $repository = $authorization->skip(fn () => $dbForPlatform->updateDocument('repositories', $repository->getId(), new Document(['providerPullRequestIds' => $providerPullRequestIds]))); $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsForInstallation($installation); + $vcs = $vcsFactory->fromInstallation($installation); $repositories = [$repository]; $providerRepositoryId = $repository->getAttribute('providerRepositoryId'); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index bcb64ac990..4bd10c3885 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -6,6 +6,7 @@ use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Appwrite\Extend\Exception; use Appwrite\Platform\Permission as AppwritePermission; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; @@ -39,7 +40,7 @@ class Get extends Action ->param('setup_action', '', new Text(256, 0), 'GitHub setup action type', true) ->param('state', '', new Text(2048), 'GitHub state. Contains info sent when starting authorization flow.', true) ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('project') ->inject('response') ->inject('dbForPlatform') @@ -52,7 +53,7 @@ class Get extends Action string $setupAction, string $state, string $code, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Document $project, Response $response, Database $dbForPlatform, @@ -100,7 +101,7 @@ class Get extends Action // Create / Update installation if (!empty($providerInstallationId)) { - $vcs = $vcsForInstallation(new Document([ + $vcs = $vcsFactory->fromInstallation(new Document([ 'provider' => 'github', 'providerInstallationId' => $providerInstallationId, ])); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 687214bd93..dfbc1656f7 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -8,6 +8,7 @@ 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 OpenRuntimes\Orchestrator\Jobs; use Utopia\Console; use Utopia\Database\Database; @@ -37,8 +38,7 @@ class Create extends Action ->groups(['api', 'vcs']) ->label('scope', 'public') ->inject('vcsWebhookSecret') - ->inject('vcsForProvider') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('request') ->inject('response') ->inject('dbForPlatform') @@ -52,8 +52,7 @@ class Create extends Action public function action( callable $vcsWebhookSecret, - callable $vcsForProvider, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Request $request, Response $response, Database $dbForPlatform, @@ -65,7 +64,7 @@ class Create extends Action ) { $this->preprocessEvent($request); - $vcs = $vcsForProvider('github'); + $vcs = $vcsFactory->fromProvider('github'); $event = $request->getHeaderLine('x-github-event', ''); Span::add('vcs.github.event.name', $event); @@ -85,8 +84,8 @@ class Create extends Action match ($event) { GitHub::EVENT_INSTALLATION => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), - GitHub::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $vcsForInstallation, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform, $jobs), - GitHub::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $vcsForInstallation, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform, $jobs), + GitHub::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform, $jobs), + GitHub::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform, $jobs), default => null, }; @@ -185,7 +184,7 @@ class Create extends Action private function handlePushEvent( array $parsedPayload, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, @@ -213,7 +212,7 @@ class Create extends Action Span::add('vcs.github.event.branch', $providerBranch); Span::add('vcs.github.event.installation.id', $providerInstallationId); - $vcs = $vcsForInstallation(new Document([ + $vcs = $vcsFactory->fromInstallation(new Document([ 'provider' => 'github', 'providerInstallationId' => $providerInstallationId, ])); @@ -233,7 +232,7 @@ class Create extends Action private function handlePullRequestEvent( array $parsedPayload, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, @@ -267,7 +266,7 @@ class Create extends Action return; } - $vcs = $vcsForInstallation(new Document([ + $vcs = $vcsFactory->fromInstallation(new Document([ 'provider' => 'github', 'providerInstallationId' => $providerInstallationId, ])); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php index dde4255855..65ea728e3a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php @@ -9,6 +9,7 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Database\Validator\Queries\Branches; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Query as QueryException; @@ -53,7 +54,7 @@ class XList extends Action ->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('queries', [], new Branches(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit, offset, cursorAfter, and cursorBefore', true) - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -64,7 +65,7 @@ class XList extends Action string $providerRepositoryId, string $search, array $queries, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Response $response, Database $dbForPlatform ) { @@ -81,7 +82,7 @@ class XList extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsForInstallation($installation); + $vcs = $vcsFactory->fromInstallation($installation); $owner = $vcs->getOwnerName($providerInstallationId); try { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php index be71500a1a..cc189a1af9 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php @@ -8,6 +8,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Platform\Scope\HTTP; @@ -49,7 +50,7 @@ class Get extends Action ->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('providerRootDirectory', '', new Text(256, 0), 'Path to get contents of nested directory', true) ->param('providerReference', '', new Text(256, 0), 'Git reference (branch, tag, commit) to get contents from', true) - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -60,7 +61,7 @@ class Get extends Action string $providerRepositoryId, string $providerRootDirectory, string $providerReference, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Response $response, Database $dbForPlatform ) { @@ -71,7 +72,7 @@ class Get extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsForInstallation($installation); + $vcs = $vcsFactory->fromInstallation($installation); $owner = $vcs->getOwnerName($providerInstallationId); try { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php index 210c9f38e4..9e3732e967 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -9,6 +9,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; @@ -52,7 +53,7 @@ class Create extends Action ->param('installationId', '', new Text(256), 'Installation Id') ->param('name', '', new Text(256), 'Repository name (slug)') ->param('private', '', new Boolean(false), 'Mark repository public or private') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('user') ->inject('response') ->inject('dbForPlatform') @@ -63,7 +64,7 @@ class Create extends Action string $installationId, string $name, bool $private, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Document $user, Response $response, Database $dbForPlatform @@ -127,7 +128,7 @@ class Create extends Action } } else { $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsForInstallation($installation); + $vcs = $vcsFactory->fromInstallation($installation); $owner = $vcs->getOwnerName($providerInstallationId); try { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php index 16e125c732..a5518d988a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php @@ -8,6 +8,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Swoole\Coroutine\WaitGroup; use Utopia\Config\Adapters\Dotenv as ConfigDotenv; use Utopia\Config\Config; @@ -92,7 +93,7 @@ class Create extends Action ->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->param('type', '', new WhiteList(['runtime', 'framework']), 'Detector type. Must be one of the following: runtime, framework', enum: new Enum(name: 'VCSDetectionType')) ->param('providerRootDirectory', '', new Text(256, 0), 'Path to Root Directory', true) - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -103,7 +104,7 @@ class Create extends Action string $providerRepositoryId, string $type, string $providerRootDirectory, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Response $response, Database $dbForPlatform ) { @@ -114,7 +115,7 @@ class Create extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsForInstallation($installation); + $vcs = $vcsFactory->fromInstallation($installation); $owner = $vcs->getOwnerName($providerInstallationId); try { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php index 086f3a3e51..4c3514207e 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php @@ -8,6 +8,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Platform\Scope\HTTP; @@ -47,7 +48,7 @@ class Get extends Action )) ->param('installationId', '', new Text(256), 'Installation Id') ->param('providerRepositoryId', '', new Text(256), 'Repository Id') - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -56,7 +57,7 @@ class Get extends Action public function action( string $installationId, string $providerRepositoryId, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Response $response, Database $dbForPlatform ) { @@ -67,7 +68,7 @@ class Get extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsForInstallation($installation); + $vcs = $vcsFactory->fromInstallation($installation); $owner = $vcs->getOwnerName($providerInstallationId); try { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php index 54b7fc7941..ab09ca6ce4 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php @@ -8,6 +8,7 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Swoole\Coroutine\WaitGroup; use Utopia\Config\Adapters\Dotenv as ConfigDotenv; use Utopia\Config\Config; @@ -96,7 +97,7 @@ class XList extends Action ->param('type', '', new WhiteList(['runtime', 'framework']), 'Detector type. Must be one of the following: runtime, framework', enum: new Enum(name: 'VCSDetectionType')) ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) - ->inject('vcsForInstallation') + ->inject('vcsFactory') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -107,7 +108,7 @@ class XList extends Action string $type, string $search, array $queries, - callable $vcsForInstallation, + VcsFactory $vcsFactory, Response $response, Database $dbForPlatform ) { @@ -122,7 +123,7 @@ class XList extends Action } $providerInstallationId = $installation->getAttribute('providerInstallationId'); - $vcs = $vcsForInstallation($installation); + $vcs = $vcsFactory->fromInstallation($installation); $queries = Query::parseQueries($queries); $limitQuery = current(array_filter($queries, fn ($query) => $query->getMethod() === Query::TYPE_LIMIT)); From e9838f1cda34d3871c39962050d4c555953e83ef Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 12:04:17 +0530 Subject: [PATCH 41/52] Pin utopia-php/vcs to the released 4.4.2 version The provider-parity/Gitea work has been tagged and released; switch off the dev-branch pin now that a stable version exists. --- composer.json | 2 +- composer.lock | 26 +++++++++----------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index 3bb281b496..92ae6de740 100644 --- a/composer.json +++ b/composer.json @@ -94,7 +94,7 @@ "utopia-php/storage": "2.*", "utopia-php/system": "0.10.*", "utopia-php/telemetry": "^0.4", - "utopia-php/vcs": "dev-feat/appwrite-provider-parity as 4.99.99", + "utopia-php/vcs": "4.4.2", "utopia-php/websocket": "1.0.*", "matomo/device-detector": "6.4.*", "dragonmantank/cron-expression": "3.4.*", diff --git a/composer.lock b/composer.lock index 105f5e2a11..b80d98d684 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a6047efffed6ab429844825f452e1706", + "content-hash": "4dbdae63ffc96eb2d9c3fcd7b2ecdc5b", "packages": [ { "name": "adhocore/jwt", @@ -5279,16 +5279,16 @@ }, { "name": "utopia-php/vcs", - "version": "dev-feat/appwrite-provider-parity", + "version": "4.4.2", "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "f785974cb61238675a1269f9f262f77245c6d634" + "reference": "ea623171caeaad2ef600b6c6bd52fd5f76b38486" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/f785974cb61238675a1269f9f262f77245c6d634", - "reference": "f785974cb61238675a1269f9f262f77245c6d634", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/ea623171caeaad2ef600b6c6bd52fd5f76b38486", + "reference": "ea623171caeaad2ef600b6c6bd52fd5f76b38486", "shasum": "" }, "require": { @@ -5322,9 +5322,9 @@ ], "support": { "issues": "https://github.com/utopia-php/vcs/issues", - "source": "https://github.com/utopia-php/vcs/tree/feat/appwrite-provider-parity" + "source": "https://github.com/utopia-php/vcs/tree/4.4.2" }, - "time": "2026-07-10T12:16:02+00:00" + "time": "2026-07-14T06:22:41+00:00" }, { "name": "utopia-php/websocket", @@ -8509,19 +8509,11 @@ "time": "2026-05-30T17:09:26+00:00" } ], - "aliases": [ - { - "package": "utopia-php/vcs", - "version": "dev-feat/appwrite-provider-parity", - "alias": "4.99.99", - "alias_normalized": "4.99.99.0" - } - ], + "aliases": [], "minimum-stability": "dev", "stability-flags": { "utopia-php/http": 5, - "utopia-php/platform": 5, - "utopia-php/vcs": 20 + "utopia-php/platform": 5 }, "prefer-stable": true, "prefer-lowest": false, From 9dedf391b12c24b4e16ab53ac8d79bf6914418c9 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 15:53:02 +0530 Subject: [PATCH 42/52] Pass repositoryId to getOwnerName() for org-owned Gitea repos Without it, Gitea's adapter can't identify the owner via installationId (unused for Gitea) and falls back to the authenticated token's own username -- correct for a personal repo, wrong for an organization-owned one the user has push access to but doesn't personally own. GitHub's adapter explicitly ignores repositoryId (resolves owner via the App installation itself), so this is a no-op there. --- .../Platform/Modules/VCS/Http/GitHub/Deployment.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php index 2a6f21bb4c..fb11603285 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php @@ -147,7 +147,7 @@ trait Deployment $activate = true; } - $owner = $vcs->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId, (int) $providerRepositoryId); try { $repositoryName = $vcs->getRepositoryName($providerRepositoryId); } catch (RepositoryNotFound $e) { @@ -334,7 +334,7 @@ trait Deployment } catch (RepositoryNotFound $e) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } - $owner = $vcs->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId, (int) $providerRepositoryId); $vcs->updateCommitStatus($repositoryName, $providerCommitHash, $owner, 'pending', $message, $authorizeUrl, $name); continue; } @@ -566,7 +566,7 @@ trait Deployment } catch (RepositoryNotFound $e) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } - $owner = $vcs->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId, (int) $providerRepositoryId); $providerTargetUrl = $protocol . '://' . $hostname . "/console/project-$region-$projectId/$resourceCollection/$resourceType-$resourceId"; $vcs->updateCommitStatus($repositoryName, $providerCommitHash, $owner, 'pending', $message, $providerTargetUrl, $name); From 7b08377a90f09b8d78795cf9049581854ad7abb5 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 16:09:57 +0530 Subject: [PATCH 43/52] Regenerate composer.lock hash after merging main composer validate failed with a stale lock-file hash after the main-sync merge -- composer.json's content didn't actually change, just the lock's content-hash needed regenerating. --- composer.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.lock b/composer.lock index ca73fc33d4..bac36b17a5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "188ecad278dfe1db8beef12790f11843", + "content-hash": "96021b6135d3574df979ad674890cabc", "packages": [ { "name": "adhocore/jwt", From 01064bace5734486f5542372c36e30afb97bd68e Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 18:45:01 +0530 Subject: [PATCH 44/52] Remove redundant enabled flag from VCS provider registry Presence in the registry already means "supported" -- a separate enabled flag that every real entry hardcodes to true adds nothing. To disable a provider, remove its entry instead. --- app/config/vcs.php | 2 -- src/Appwrite/Vcs/Factory.php | 10 +--------- tests/unit/Vcs/FactoryTest.php | 13 ------------- 3 files changed, 1 insertion(+), 24 deletions(-) diff --git a/app/config/vcs.php b/app/config/vcs.php index 4264d6c0d9..f456f80d96 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -9,7 +9,6 @@ use Utopia\VCS\Adapter\Git\GitHub; return [ 'github' => [ - 'enabled' => true, 'adapter' => GitHub::class, 'envVariables' => [ 'APP_NAME' => '_APP_VCS_GITHUB_APP_NAME', @@ -22,7 +21,6 @@ return [ 'requiredEnvVariables' => ['APP_NAME', 'PRIVATE_KEY', 'APP_ID', 'CLIENT_ID', 'CLIENT_SECRET'], ], 'gitea' => [ - 'enabled' => true, 'adapter' => Gitea::class, 'envVariables' => [ 'ENDPOINT' => '_APP_VCS_GITEA_ENDPOINT', diff --git a/src/Appwrite/Vcs/Factory.php b/src/Appwrite/Vcs/Factory.php index 4d8037e035..58a5e06c13 100644 --- a/src/Appwrite/Vcs/Factory.php +++ b/src/Appwrite/Vcs/Factory.php @@ -23,15 +23,7 @@ class Factory protected Cache $cache, ?array $registry = null, ) { - $registry ??= Config::getParam('vcs', []); - - foreach ($registry as $key => $entry) { - if (!($entry['enabled'] ?? false)) { - continue; - } - - $this->registry[$key] = $entry; - } + $this->registry = $registry ?? Config::getParam('vcs', []); } public function getProviders(): array diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index 9d2d367922..ec38c4149e 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -35,17 +35,6 @@ final class FactoryTest extends TestCase } } - public function testDisabledProvidersAreNotRegistered(): void - { - $factory = new Factory($this->cache(), [ - 'github' => $this->githubEntry(), - 'legacy' => ['enabled' => false] + $this->githubEntry(), - ]); - - $this->expectException(Exception::class); - $factory->fromProvider('legacy'); - } - public function testFromProviderUnknownThrows(): void { $factory = new Factory($this->cache(), ['github' => $this->githubEntry()]); @@ -84,7 +73,6 @@ final class FactoryTest extends TestCase public function testIsConfigured(): void { $entry = [ - 'enabled' => true, 'adapter' => GitHub::class, 'envVariables' => ['TOKEN' => '_APP_VCS_TEST_TOKEN'], 'requiredEnvVariables' => ['TOKEN'], @@ -122,7 +110,6 @@ final class FactoryTest extends TestCase protected function githubEntry(): array { return [ - 'enabled' => true, 'adapter' => GitHub::class, 'envVariables' => [ 'APP_NAME' => '_APP_VCS_GITHUB_APP_NAME', From 534b695dad6ed7a714f1ea8319170199cd85cd8e Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 18:49:31 +0530 Subject: [PATCH 45/52] Collapse envVariables/requiredEnvVariables into per-field variables The two arrays were both keyed by the same semantic names and had to be kept manually in sync -- adding a field to one but forgetting the other (or a typo) would silently make getEnv() return empty and isConfigured() silently treat the whole provider as unconfigured, with no error anywhere. Each field is now self-contained: name => {required, envVariable}. --- app/config/vcs.php | 30 ++++++++++++++---------------- src/Appwrite/Vcs/Factory.php | 14 +++++++------- tests/unit/Vcs/FactoryTest.php | 26 +++++++++++++------------- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/app/config/vcs.php b/app/config/vcs.php index f456f80d96..6df0cc924d 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -10,26 +10,24 @@ use Utopia\VCS\Adapter\Git\GitHub; return [ 'github' => [ 'adapter' => GitHub::class, - 'envVariables' => [ - 'APP_NAME' => '_APP_VCS_GITHUB_APP_NAME', - 'PRIVATE_KEY' => '_APP_VCS_GITHUB_PRIVATE_KEY', - 'APP_ID' => '_APP_VCS_GITHUB_APP_ID', - 'CLIENT_ID' => '_APP_VCS_GITHUB_CLIENT_ID', - 'CLIENT_SECRET' => '_APP_VCS_GITHUB_CLIENT_SECRET', - 'WEBHOOK_SECRET' => '_APP_VCS_GITHUB_WEBHOOK_SECRET', + 'variables' => [ + 'appName' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_NAME'], + 'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_PRIVATE_KEY'], + 'appId' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_ID'], + 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_CLIENT_ID'], + 'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_CLIENT_SECRET'], + 'webhookSecret' => ['required' => false, 'envVariable' => '_APP_VCS_GITHUB_WEBHOOK_SECRET'], ], - 'requiredEnvVariables' => ['APP_NAME', 'PRIVATE_KEY', 'APP_ID', 'CLIENT_ID', 'CLIENT_SECRET'], ], 'gitea' => [ 'adapter' => Gitea::class, - 'envVariables' => [ - 'ENDPOINT' => '_APP_VCS_GITEA_ENDPOINT', - 'CLIENT_ID' => '_APP_VCS_GITEA_CLIENT_ID', - 'CLIENT_SECRET' => '_APP_VCS_GITEA_CLIENT_SECRET', - 'WEBHOOK_SECRET' => '_APP_VCS_GITEA_WEBHOOK_SECRET', + 'variables' => [ + 'endpoint' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_ENDPOINT'], + 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_CLIENT_ID'], + 'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_CLIENT_SECRET'], + // Unlike GitHub's legacy optional secret, Gitea webhooks must + // always have a shared secret because Appwrite creates them directly. + 'webhookSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_WEBHOOK_SECRET'], ], - // Unlike GitHub's legacy optional secret, Gitea webhooks must always - // have a shared secret because Appwrite creates them directly. - 'requiredEnvVariables' => ['ENDPOINT', 'CLIENT_ID', 'CLIENT_SECRET', 'WEBHOOK_SECRET'], ], ]; diff --git a/src/Appwrite/Vcs/Factory.php b/src/Appwrite/Vcs/Factory.php index 58a5e06c13..d654875144 100644 --- a/src/Appwrite/Vcs/Factory.php +++ b/src/Appwrite/Vcs/Factory.php @@ -40,8 +40,8 @@ class Factory return false; } - foreach ($this->registry[$key]['requiredEnvVariables'] ?? [] as $name) { - if (empty($this->getEnv($key, $name))) { + foreach ($this->registry[$key]['variables'] ?? [] as $name => $variable) { + if (($variable['required'] ?? false) && empty($this->getEnv($key, $name))) { return false; } } @@ -57,7 +57,7 @@ class Factory $adapter = new ($this->registry[$key]['adapter'])($this->cache); - $endpoint = $this->getEnv($key, 'ENDPOINT'); + $endpoint = $this->getEnv($key, 'endpoint'); if (!empty($endpoint) && \method_exists($adapter, 'setEndpoint')) { $adapter->setEndpoint(\rtrim($endpoint, '/')); } @@ -80,8 +80,8 @@ class Factory $adapter->initializeVariables( $installation->getAttribute('providerInstallationId', ''), - $this->getEnv($provider, 'PRIVATE_KEY'), - $this->getEnv($provider, 'APP_ID'), + $this->getEnv($provider, 'privateKey'), + $this->getEnv($provider, 'appId'), $installation->getAttribute('personalAccessToken', ''), $installation->getAttribute('personalRefreshToken', ''), ); @@ -91,12 +91,12 @@ class Factory public function getWebhookSecret(string $key): string { - return $this->getEnv($key, 'WEBHOOK_SECRET'); + return $this->getEnv($key, 'webhookSecret'); } protected function getEnv(string $key, string $name): string { - $variable = $this->registry[$key]['envVariables'][$name] ?? ''; + $variable = $this->registry[$key]['variables'][$name]['envVariable'] ?? ''; return empty($variable) ? '' : System::getEnv($variable, ''); } diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index ec38c4149e..db89369252 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -28,9 +28,9 @@ 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->assertNotEmpty($entry['requiredEnvVariables'], "Required env variables missing for '{$key}'"); - foreach ($entry['requiredEnvVariables'] as $required) { - $this->assertStringStartsWith('_APP_VCS_', $entry['envVariables'][$required] ?? '', "Env variable for required key '{$required}' missing or invalid for '{$key}'"); + $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}'"); } } } @@ -74,8 +74,9 @@ final class FactoryTest extends TestCase { $entry = [ 'adapter' => GitHub::class, - 'envVariables' => ['TOKEN' => '_APP_VCS_TEST_TOKEN'], - 'requiredEnvVariables' => ['TOKEN'], + 'variables' => [ + 'token' => ['required' => true, 'envVariable' => '_APP_VCS_TEST_TOKEN'], + ], ]; $factory = new Factory($this->cache(), ['test' => $entry]); @@ -111,15 +112,14 @@ final class FactoryTest extends TestCase { return [ 'adapter' => GitHub::class, - 'envVariables' => [ - 'APP_NAME' => '_APP_VCS_GITHUB_APP_NAME', - 'PRIVATE_KEY' => '_APP_VCS_GITHUB_PRIVATE_KEY', - 'APP_ID' => '_APP_VCS_GITHUB_APP_ID', - 'CLIENT_ID' => '_APP_VCS_GITHUB_CLIENT_ID', - 'CLIENT_SECRET' => '_APP_VCS_GITHUB_CLIENT_SECRET', - 'WEBHOOK_SECRET' => '_APP_VCS_GITHUB_WEBHOOK_SECRET', + 'variables' => [ + 'appName' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_NAME'], + 'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_PRIVATE_KEY'], + 'appId' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_ID'], + 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_CLIENT_ID'], + 'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_CLIENT_SECRET'], + 'webhookSecret' => ['required' => false, 'envVariable' => '_APP_VCS_GITHUB_WEBHOOK_SECRET'], ], - 'requiredEnvVariables' => ['APP_NAME', 'PRIVATE_KEY', 'APP_ID', 'CLIENT_ID', 'CLIENT_SECRET'], ]; } } From e71bec65f2f4cc38d0e78b60d8810d086e9717e2 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 18:59:11 +0530 Subject: [PATCH 46/52] Replace generic APP_VCS_COMMIT_* aliases with a real Gitea constant APP_VCS_COMMIT_USERNAME/URL were dead code (zero usages), and APP_VCS_COMMIT_EMAIL was just an alias for APP_VCS_GITHUB_EMAIL -- meaning Gitea's self-commit check was silently comparing a Gitea commit's author against GitHub's bot identity, which a real Gitea commit could never match. Add a real APP_VCS_GITEA_EMAIL instead of a fake-generic alias; Gitea has no bot-commit-push flow yet, so username/url aren't needed until it does. --- app/init/constants.php | 7 ++++--- .../Platform/Modules/VCS/Http/Gitea/Events/Create.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/init/constants.php b/app/init/constants.php index c90db28043..8268088ffe 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -108,9 +108,10 @@ const APP_SDK_PLATFORM_STATIC = 'static'; const APP_VCS_GITHUB_USERNAME = 'Appwrite'; const APP_VCS_GITHUB_EMAIL = 'team@appwrite.io'; const APP_VCS_GITHUB_URL = 'https://github.com/TeamAppwrite'; -const APP_VCS_COMMIT_USERNAME = APP_VCS_GITHUB_USERNAME; -const APP_VCS_COMMIT_EMAIL = APP_VCS_GITHUB_EMAIL; -const APP_VCS_COMMIT_URL = APP_VCS_GITHUB_URL; +// Gitea has no bot-commit-push flow yet (unlike GitHub's classic template +// merge), so only the email is needed, to detect Appwrite's own commits and +// skip re-triggering a build on them. +const APP_VCS_GITEA_EMAIL = 'team@appwrite.io'; const APP_BRANDED_EMAIL_BASE_TEMPLATE = 'email-base-styled'; // Embeddings diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index 62e86483aa..82bdc51b62 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -140,7 +140,7 @@ class Create extends Action Span::add('vcs.gitea.event.repo.name', $providerRepositoryName); Span::add('vcs.gitea.event.branch', $providerBranch); - if ($providerCommitAuthorEmail === APP_VCS_COMMIT_EMAIL || $providerBranchDeleted) { + if ($providerCommitAuthorEmail === APP_VCS_GITEA_EMAIL || $providerBranchDeleted) { return; } From 315325fdefa71b9804826c11f0b55fa3a73d6b7e Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 18:59:58 +0530 Subject: [PATCH 47/52] Shorten APP_VCS_GITEA_EMAIL comment --- app/init/constants.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/init/constants.php b/app/init/constants.php index 8268088ffe..f5eaa3a4ab 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -108,10 +108,7 @@ const APP_SDK_PLATFORM_STATIC = 'static'; const APP_VCS_GITHUB_USERNAME = 'Appwrite'; const APP_VCS_GITHUB_EMAIL = 'team@appwrite.io'; const APP_VCS_GITHUB_URL = 'https://github.com/TeamAppwrite'; -// Gitea has no bot-commit-push flow yet (unlike GitHub's classic template -// merge), so only the email is needed, to detect Appwrite's own commits and -// skip re-triggering a build on them. -const APP_VCS_GITEA_EMAIL = 'team@appwrite.io'; +const APP_VCS_GITEA_EMAIL = 'team@appwrite.io'; // Used to detect Appwrite's own commits const APP_BRANDED_EMAIL_BASE_TEMPLATE = 'email-base-styled'; // Embeddings From c9273ac19cd6997c61acd699d363d76176ec0baa Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 19:05:24 +0530 Subject: [PATCH 48/52] Move VCS-specific Gitea OAuth2 scopes to the call site The adapter class defaulted to the full repo/org/issue scope set, so any future "sign in with Gitea" feature reusing this OAuth2 class would force that same scope set on a plain login, which it doesn't need. Mirrors OAuth2\Github, whose adapter only defaults to 'user:email' -- the VCS authorize flow now passes its own required scopes via the constructor's existing $scopes parameter instead of the adapter hardcoding them. --- src/Appwrite/Auth/OAuth2/Gitea.php | 5 ----- .../Platform/Modules/VCS/Http/Gitea/Authorize/Get.php | 11 ++++++++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Auth/OAuth2/Gitea.php b/src/Appwrite/Auth/OAuth2/Gitea.php index 1cc891c167..390189fc72 100644 --- a/src/Appwrite/Auth/OAuth2/Gitea.php +++ b/src/Appwrite/Auth/OAuth2/Gitea.php @@ -14,11 +14,6 @@ class Gitea extends OAuth2 protected array $scopes = [ 'read:user', - 'read:repository', - 'write:repository', - 'read:organization', - 'read:issue', - 'write:issue', ]; public function setEndpoint(string $endpoint): void diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Authorize/Get.php index e77d2267ec..efeef8fc3a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Authorize/Get.php @@ -30,7 +30,16 @@ class Get extends Base System::getEnv('_APP_VCS_GITEA_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITEA_CLIENT_SECRET', ''), $callback, - $state + $state, + // VCS-specific scopes -- the adapter's own default is just + // read:user, enough for a plain "sign in with Gitea". + [ + 'read:repository', + 'write:repository', + 'read:organization', + 'read:issue', + 'write:issue', + ] ); // The login page is opened by the browser, which may reach Gitea on a From 54df8be890f0a7d4662ec679ed7043f717b71f33 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 19:11:23 +0530 Subject: [PATCH 49/52] Use a caret range for utopia-php/vcs instead of an exact pin Allows compatible patch/minor updates instead of requiring a manual bump every release. --- composer.json | 2 +- composer.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index b39c68617c..94370df7d6 100644 --- a/composer.json +++ b/composer.json @@ -94,7 +94,7 @@ "utopia-php/storage": "2.*", "utopia-php/system": "0.10.*", "utopia-php/telemetry": "^0.4", - "utopia-php/vcs": "4.4.2", + "utopia-php/vcs": "^4.4.2", "utopia-php/websocket": "1.0.*", "matomo/device-detector": "6.4.*", "dragonmantank/cron-expression": "3.4.*", diff --git a/composer.lock b/composer.lock index bac36b17a5..2d19366603 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "96021b6135d3574df979ad674890cabc", + "content-hash": "7c47a07b7c261e80a7e12374619f5237", "packages": [ { "name": "adhocore/jwt", From 304248db442df15d6290e58d37f78de23e088007 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 14 Jul 2026 19:21:40 +0530 Subject: [PATCH 50/52] Refresh Gitea installation tokens before building the webhook adapter resolveAdapterForRepository() called vcsFactory->fromInstallation() directly with whatever access token happened to be stored, with no refresh -- unlike the HTTP-triggered paths (e.g. Repositories/Create), which call InstallationTokens::refresh() first. Once a Gitea OAuth2 token expired, every subsequent API call in the webhook handler (getCommit, getPullRequestFiles, updateCommitStatus, createGitDeployments) would fail with 401, silently dropping push/PR-triggered builds for that installation with no user-facing indication. --- .../Modules/VCS/Http/Gitea/Events/Create.php | 26 +++++++++++++++---- .../Vcs/GiteaClosedPullRequestEventTest.php | 4 ++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index 82bdc51b62..40820d93dc 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\VCS\Http\Gitea\Events; +use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea; use Appwrite\Event\Publisher\Build as BuildPublisher; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; @@ -9,6 +10,7 @@ use Appwrite\Platform\Modules\VCS\Http\GitHub\Deployment; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory as VcsFactory; +use Appwrite\Vcs\InstallationTokens; use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; @@ -16,6 +18,7 @@ use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Platform\Scope\HTTP; use Utopia\Span\Span; +use Utopia\System\System; use Utopia\VCS\Adapter\Git; class Create extends Action @@ -38,6 +41,7 @@ class Create extends Action ->label('scope', 'public') ->inject('vcsFactory') ->inject('vcsWebhookSecret') + ->inject('installationTokens') ->inject('request') ->inject('response') ->inject('dbForPlatform') @@ -51,6 +55,7 @@ class Create extends Action public function action( VcsFactory $vcsFactory, callable $vcsWebhookSecret, + InstallationTokens $installationTokens, Request $request, Response $response, Database $dbForPlatform, @@ -78,8 +83,8 @@ class Create extends Action $parsedPayload = $vcs->getEvent($event, $payload); match ($event) { - 'push' => $this->handlePushEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), - 'pull_request' => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + 'push' => $this->handlePushEvent($parsedPayload, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + 'pull_request' => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), default => null, }; @@ -97,7 +102,7 @@ class Create extends Action return $installation; } - private function resolveAdapterForRepository(Document $repository, VcsFactory $vcsFactory, Database $dbForPlatform, Authorization $authorization): ?Git + private function resolveAdapterForRepository(Document $repository, VcsFactory $vcsFactory, InstallationTokens $installationTokens, Database $dbForPlatform, Authorization $authorization): ?Git { $installation = $this->resolveGiteaInstallation($repository, $dbForPlatform, $authorization); @@ -106,6 +111,15 @@ class Create extends Action } try { + $oauth2 = new OAuth2Gitea( + System::getEnv('_APP_VCS_GITEA_CLIENT_ID', ''), + System::getEnv('_APP_VCS_GITEA_CLIENT_SECRET', ''), + '' + ); + $oauth2->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', '')); + + $installation = $installationTokens->refresh($installation, $dbForPlatform, $oauth2); + return $vcsFactory->fromInstallation($installation); } catch (\Throwable $error) { Console::warning("Failed to resolve Gitea adapter for installation '{$installation->getId()}': " . $error->getMessage()); @@ -116,6 +130,7 @@ class Create extends Action private function handlePushEvent( array $parsedPayload, VcsFactory $vcsFactory, + InstallationTokens $installationTokens, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, @@ -152,7 +167,7 @@ class Create extends Action $providerAffectedFiles = $parsedPayload['affectedFiles'] ?? []; foreach ($repositories as $repository) { - $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $dbForPlatform, $authorization); + $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization); if ($adapter === null) { continue; @@ -167,6 +182,7 @@ class Create extends Action private function handlePullRequestEvent( array $parsedPayload, VcsFactory $vcsFactory, + InstallationTokens $installationTokens, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, @@ -204,7 +220,7 @@ class Create extends Action ])); foreach ($repositories as $repository) { - $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $dbForPlatform, $authorization); + $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization); if ($adapter === null) { continue; diff --git a/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php b/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php index d0544e5a8b..93000c3339 100644 --- a/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php +++ b/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php @@ -7,6 +7,7 @@ namespace Tests\Unit\Vcs; use Appwrite\Event\Publisher\Build as BuildPublisher; use Appwrite\Platform\Modules\VCS\Http\Gitea\Events\Create; use Appwrite\Vcs\Factory as VcsFactory; +use Appwrite\Vcs\InstallationTokens; use PHPUnit\Framework\TestCase; use ReflectionMethod; use Utopia\Database\Database; @@ -66,13 +67,14 @@ final class GiteaClosedPullRequestEventTest extends TestCase $publisherForBuilds = $this->createStub(BuildPublisher::class); $vcsFactory = $this->createStub(VcsFactory::class); + $installationTokens = $this->createStub(InstallationTokens::class); $this->callHandler('handlePullRequestEvent', [ 'action' => 'closed', 'repositoryId' => '5', 'pullRequestNumber' => 5, 'external' => true, - ], $vcsFactory, $dbForPlatform, $authorization, $publisherForBuilds, fn () => null, []); + ], $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $publisherForBuilds, fn () => null, []); } private function callHandler(string $method, mixed ...$arguments): mixed From 8dd7549eeb9bc7e4425f9583e5f7c0426989e85c Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 15 Jul 2026 17:17:44 +0530 Subject: [PATCH 51/52] Restore gitHub resource, namespace Gitea build spans, surface token-refresh failures - app/init/resources.php: re-register the gitHub DI resource (still needed by Template/Duplicate Create.php for the always-GitHub template path), lost during the adapter-driven Factory refactor. - GitHub/Deployment.php: derive the build-lifecycle span namespace from the adapter instead of hardcoding vcs.github.*, so Gitea deployments are no longer mislabeled as GitHub in monitoring. - Gitea/Events/Create.php: a failed OAuth2 token refresh was silently swallowed (console warning only), returning Gitea a 200 with no retry and no signal to the project owner. Now aggregated into a thrown exception, mirroring GitHub's error-aggregation pattern, so the webhook delivery fails visibly and shows up in Gitea's redelivery log. --- app/init/resources.php | 1 + .../Modules/VCS/Http/GitHub/Deployment.php | 5 ++-- .../Modules/VCS/Http/Gitea/Events/Create.php | 29 ++++++++++++++++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index 8957b4f8ff..c08f823f9e 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -379,6 +379,7 @@ $container->set('servers', function () { $container->set('promiseAdapter', fn ($register) => $register->get('promiseAdapter'), ['register']); $container->set('vcsFactory', fn (Cache $cache) => new VcsFactory($cache), ['cache']); +$container->set('gitHub', fn (VcsFactory $vcsFactory) => $vcsFactory->fromProvider('github'), ['vcsFactory']); $container->set('installationTokens', fn () => new InstallationTokens(), []); $container->set('repositoryWebhooks', fn (VcsFactory $vcsFactory) => new RepositoryWebhooks($vcsFactory), ['vcsFactory']); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php index 5c20f10eb4..c1e3da5adf 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php @@ -53,8 +53,9 @@ trait Deployment ?Backend $deployments = null, ) { $errors = []; + $provider = $vcs->getName(); foreach ($repositories as $repository) { - $logBase = 'vcs.github.event.repo.unknown'; + $logBase = "vcs.{$provider}.event.repo.unknown"; try { $repositoryId = $repository->getId(); @@ -62,7 +63,7 @@ trait Deployment $resourceId = $repository->getAttribute('resourceId'); $resourceType = $repository->getAttribute('resourceType'); - $logBase = "vcs.github.event.repo.{$repositoryId}"; + $logBase = "vcs.{$provider}.event.repo.{$repositoryId}"; Span::add('project.id', $projectId); Span::add("{$logBase}.resource.id", $resourceId); Span::add("{$logBase}.resource.type", $resourceType); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index 40820d93dc..10ec1bbcb6 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -102,7 +102,15 @@ class Create extends Action return $installation; } - private function resolveAdapterForRepository(Document $repository, VcsFactory $vcsFactory, InstallationTokens $installationTokens, Database $dbForPlatform, Authorization $authorization): ?Git + /** + * Resolves the adapter for a repository's installation, refreshing its + * OAuth2 token first. A null installation (not a Gitea installation) is + * not an error -- the caller silently skips it. A token-refresh/adapter + * failure, on the other hand, is pushed onto $errors so the caller can + * surface it instead of dropping it silently -- Gitea's webhook delivery + * gets a non-2xx response and the failure shows up in its redelivery log. + */ + private function resolveAdapterForRepository(Document $repository, VcsFactory $vcsFactory, InstallationTokens $installationTokens, Database $dbForPlatform, Authorization $authorization, array &$errors): ?Git { $installation = $this->resolveGiteaInstallation($repository, $dbForPlatform, $authorization); @@ -122,7 +130,10 @@ class Create extends Action return $vcsFactory->fromInstallation($installation); } catch (\Throwable $error) { - Console::warning("Failed to resolve Gitea adapter for installation '{$installation->getId()}': " . $error->getMessage()); + $message = "Failed to resolve Gitea adapter for installation '{$installation->getId()}': " . $error->getMessage(); + Console::warning($message); + Span::add("vcs.gitea.event.installation.{$installation->getId()}.error", $message); + $errors[] = $message; return null; } } @@ -166,8 +177,9 @@ class Create extends Action $providerAffectedFiles = $parsedPayload['affectedFiles'] ?? []; + $errors = []; foreach ($repositories as $repository) { - $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization); + $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $errors); if ($adapter === null) { continue; @@ -177,6 +189,10 @@ class Create extends Action $this->createGitDeployments($adapter, $providerInstallationId, [$repository], $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); } + + if (!empty($errors)) { + throw new Exception(Exception::GENERAL_UNKNOWN, \implode("\n", $errors)); + } } private function handlePullRequestEvent( @@ -219,8 +235,9 @@ class Create extends Action Query::limit(100), ])); + $errors = []; foreach ($repositories as $repository) { - $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization); + $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $errors); if ($adapter === null) { continue; @@ -245,6 +262,10 @@ class Create extends Action $this->createGitDeployments($adapter, $providerInstallationId, [$repository], $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, $providerPullRequestId, $providerAffectedFiles, $external, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform); } + + if (!empty($errors)) { + throw new Exception(Exception::GENERAL_UNKNOWN, \implode("\n", $errors)); + } } elseif ($action === 'closed') { $providerRepositoryId = $parsedPayload['repositoryId'] ?? ''; $providerPullRequestId = $parsedPayload['pullRequestNumber'] ?? ''; From 2813d65fcd5440d6d59eaa4bb59edecee22bb2be Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 15 Jul 2026 17:28:51 +0530 Subject: [PATCH 52/52] Drop the gitHub DI resource, call fromProvider('github') inline No special-cased resource for a single provider; both remaining call sites now resolve the adapter directly, matching the existing pattern in Builds.php and GitHub/Events/Create.php. Duplicate/Create.php's installation-based branch now resolves via fromInstallation() instead of hardcoding GitHub, since it duplicates deployments from any connected provider, not just GitHub. --- app/init/resources.php | 1 - .../Functions/Http/Deployments/Duplicate/Create.php | 12 ++++-------- .../Functions/Http/Deployments/Template/Create.php | 4 +--- 3 files changed, 5 insertions(+), 12 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index c08f823f9e..8957b4f8ff 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -379,7 +379,6 @@ $container->set('servers', function () { $container->set('promiseAdapter', fn ($register) => $register->get('promiseAdapter'), ['register']); $container->set('vcsFactory', fn (Cache $cache) => new VcsFactory($cache), ['cache']); -$container->set('gitHub', fn (VcsFactory $vcsFactory) => $vcsFactory->fromProvider('github'), ['vcsFactory']); $container->set('installationTokens', fn () => new InstallationTokens(), []); $container->set('repositoryWebhooks', fn (VcsFactory $vcsFactory) => new RepositoryWebhooks($vcsFactory), ['vcsFactory']); diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php index 44d6cdd68b..c80249d8a9 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php @@ -9,13 +9,13 @@ use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; use Utopia\Database\Helpers\ID; use Utopia\Database\Validator\UID; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\Storage\Device; -use Utopia\System\System; use Utopia\VCS\Adapter\Git\GitHub; class Create extends Action @@ -66,7 +66,7 @@ class Create extends Action ->inject('queueForEvents') ->inject('deployments') ->inject('deviceForFunctions') - ->inject('gitHub') + ->inject('vcsFactory') ->callback($this->action(...)); } @@ -80,7 +80,7 @@ class Create extends Action Event $queueForEvents, Backend $deployments, Device $deviceForFunctions, - GitHub $github, + VcsFactory $vcsFactory, ) { $function = $dbForProject->getDocument('functions', $functionId); @@ -144,11 +144,7 @@ class Create extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - $github->initializeVariables( - $installation->getAttribute('providerInstallationId', ''), - System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'), - System::getEnv('_APP_VCS_GITHUB_APP_ID'), - ); + $github = $vcsFactory->fromInstallation($installation); $ref = $deployment->getAttribute('providerCommitHash') ?: $deployment->getAttribute('providerBranch'); $deployment = $deployments->createFromUrl( diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php index 699425ce39..8ce0644678 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Template/Create.php @@ -81,7 +81,6 @@ class Create extends Base ->inject('publisherForBuilds') ->inject('vcsFactory') ->inject('deployments') - ->inject('gitHub') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -104,7 +103,6 @@ class Create extends Base BuildPublisher $publisherForBuilds, VcsFactory $vcsFactory, Backend $deployments, - GitHub $github, Authorization $authorization, array $platform ) { @@ -170,7 +168,7 @@ class Create extends Base $ref = $reference; if ($type === GitHub::CLONE_TYPE_TAG && \str_contains($reference, '*')) { try { - $tags = $github->listTags($owner, $repository, $reference); + $tags = $vcsFactory->fromProvider('github')->listTags($owner, $repository, $reference); $ref = \end($tags) ?: $reference; } catch (\Throwable) { // Fall back to the raw reference; the build surfaces a bad ref.