diff --git a/.env b/.env index 6f7f62a12f..eed895eb1e 100644 --- a/.env +++ b/.env @@ -155,4 +155,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 0eb43f783b..ac39a49444 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -1257,6 +1257,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 new file mode 100644 index 0000000000..6df0cc924d --- /dev/null +++ b/app/config/vcs.php @@ -0,0 +1,33 @@ + [ + 'adapter' => GitHub::class, + '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'], + ], + ], + 'gitea' => [ + 'adapter' => Gitea::class, + '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'], + ], + ], +]; diff --git a/app/controllers/mock.php b/app/controllers/mock.php index 40bd55c5fa..ca80f08e38 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; @@ -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') @@ -239,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('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, VcsFactory $vcsFactory, Document $project, Response $response, Database $dbForPlatform) { $isDevelopment = System::getEnv('_APP_ENV', 'development') === 'development'; if (!$isDevelopment) { @@ -261,10 +261,11 @@ 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->fromInstallation(new Document([ + 'provider' => 'github', + 'providerInstallationId' => $providerInstallationId, + ])); + $owner = $vcs->getOwnerName($providerInstallationId); $projectInternalId = $project->getSequence(); @@ -294,6 +295,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/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/constants.php b/app/init/constants.php index df85fc0925..f5eaa3a4ab 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -108,6 +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'; +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 diff --git a/app/init/resources.php b/app/init/resources.php index 9dfa88c6d5..8957b4f8ff 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -18,6 +18,9 @@ use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl; use Appwrite\Screenshots\Client as ScreenshotsClient; +use Appwrite\Vcs\Factory as VcsFactory; +use Appwrite\Vcs\InstallationTokens; +use Appwrite\Vcs\RepositoryWebhooks; use Executor\Executor; use OpenRuntimes\Orchestrator\Jobs; use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimitRedis; @@ -49,7 +52,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; @@ -376,7 +378,15 @@ $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('installationTokens', fn () => new InstallationTokens(), []); +$container->set('repositoryWebhooks', fn (VcsFactory $vcsFactory) => new RepositoryWebhooks($vcsFactory), ['vcsFactory']); + +$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('vcsWebhookSecret', fn (VcsFactory $vcsFactory) => fn (string $provider) => $vcsFactory->getWebhookSecret($provider), ['vcsFactory']); $container->set('plan', fn () => []); diff --git a/composer.json b/composer.json index 557825b1d1..daa1ab4483 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.1", + "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 ba07c6bc8a..5436edf6c3 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": "bed2fe8ed058ba0dd6e4740893c8d7e8", + "content-hash": "aaa69508ad474daafe2261e01a65f27c", "packages": [ { "name": "adhocore/jwt", @@ -5196,16 +5196,16 @@ }, { "name": "utopia-php/vcs", - "version": "4.4.1", + "version": "4.4.2", "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "75f36b9db5c957829a3625a91293b164f74faa0d" + "reference": "ea623171caeaad2ef600b6c6bd52fd5f76b38486" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/75f36b9db5c957829a3625a91293b164f74faa0d", - "reference": "75f36b9db5c957829a3625a91293b164f74faa0d", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/ea623171caeaad2ef600b6c6bd52fd5f76b38486", + "reference": "ea623171caeaad2ef600b6c6bd52fd5f76b38486", "shasum": "" }, "require": { @@ -5239,9 +5239,9 @@ ], "support": { "issues": "https://github.com/utopia-php/vcs/issues", - "source": "https://github.com/utopia-php/vcs/tree/4.4.1" + "source": "https://github.com/utopia-php/vcs/tree/4.4.2" }, - "time": "2026-07-08T18:15:35+00:00" + "time": "2026-07-14T06:22:41+00:00" }, { "name": "utopia-php/websocket", diff --git a/docker-compose.yml b/docker-compose.yml index 1f5cc628d3..37677abd2d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -197,6 +197,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 @@ -477,6 +483,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..390189fc72 --- /dev/null +++ b/src/Appwrite/Auth/OAuth2/Gitea.php @@ -0,0 +1,149 @@ +endpoint = \rtrim($endpoint, '/'); + } + + public function getName(): string + { + return 'gitea'; + } + + 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) + ]); + } + + 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; + } + + 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; + } + + public function getUserID(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return \strval($user['id'] ?? ''); + } + + public function getUserEmail(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['email'] ?? ''; + } + + public function isEmailVerified(string $accessToken): bool + { + $user = $this->getUser($accessToken); + + return $user['verified'] ?? false; + } + + public function getUserName(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['full_name'] ?? ''; + } + + public function getUserSlug(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['login'] ?? ''; + } + + 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 3a997c865d..4aa05673c1 100644 --- a/src/Appwrite/Migration/Version/V25.php +++ b/src/Appwrite/Migration/Version/V25.php @@ -66,6 +66,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; + case 'databases': if ($collectionType === 'projects') { try { diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index b863c2b612..60591096ce 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -21,7 +21,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 @@ -68,21 +68,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 = '', ?Backend $deployments = null): 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 = '', ?Backend $deployments = null): 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); } @@ -97,15 +94,15 @@ 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 = $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 } @@ -114,10 +111,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 = new Document([ '$id' => $deploymentId, @@ -158,7 +155,7 @@ class Base extends Action $deployment = $deployments->createFromUrl( $function, $deployment, - $github->getRepositoryPresignedUrl($owner, $repositoryName, $ref), + $vcs->getRepositoryPresignedUrl($owner, $repositoryName, $ref), $function->getAttribute('providerRootDirectory', ''), ); } else { @@ -185,20 +182,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); } @@ -213,15 +207,15 @@ 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 = $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 } @@ -230,10 +224,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/Console/Http/Variables/Get.php b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php index d39049a409..9adf5ebf9a 100644 --- a/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php +++ b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php @@ -46,14 +46,19 @@ class Get extends Action ], contentType: ContentType::JSON )) + ->inject('vcsProviders') ->inject('response') ->inject('platform') ->inject('dbForProject') ->callback($this->action(...)); } - public function action(Response $response, array $platform, Database $dbForProject) - { + public function action( + callable $vcsProviders, + 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 +71,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($vcsProviders()); $isAssistantEnabled = !empty(System::getEnv('_APP_ASSISTANT_OPENAI_API_KEY', '')); @@ -86,6 +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' => $vcsProviders(), '_APP_DOMAIN_ENABLED' => $isDomainEnabled, '_APP_ASSISTANT_ENABLED' => $isAssistantEnabled, '_APP_DOMAIN_SITES' => $platform['sitesDomain'], 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 707ba8afde..8ce0644678 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,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; @@ -53,7 +54,7 @@ class Create extends Base name: 'createTemplateDeployment', description: <<inject('queueForEvents') ->inject('project') ->inject('publisherForBuilds') + ->inject('vcsFactory') ->inject('deployments') - ->inject('gitHub') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -100,8 +101,8 @@ class Create extends Base Event $queueForEvents, Document $project, BuildPublisher $publisherForBuilds, + VcsFactory $vcsFactory, Backend $deployments, - GitHub $github, Authorization $authorization, array $platform ) { @@ -138,7 +139,7 @@ class Create extends Base dbForProject: $dbForProject, publisherForBuilds: $publisherForBuilds, template: $template, - github: $github, + vcs: $vcsFactory->fromInstallation($installation), activate: $activate, deployments: $deployments, platform: $platform, @@ -162,10 +163,12 @@ class Create extends Base // Templates can pin a version range (e.g. "0.3.*"); codeload only // takes a concrete ref, so resolve the range to the highest matching // tag (mirrors the executor's `git ls-remote --tags | tail -1`). + // Templates are public github.com repositories regardless of the + // function's own provider, so this always uses the GitHub adapter. $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. 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 af334dee3f..f539373048 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Vcs/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\Validator\UID; @@ -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,8 +74,8 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('publisherForBuilds') + ->inject('vcsFactory') ->inject('deployments') - ->inject('gitHub') ->inject('platform') ->callback($this->action(...)); } @@ -92,8 +92,8 @@ class Create extends Base Document $project, Event $queueForEvents, BuildPublisher $publisherForBuilds, + VcsFactory $vcsFactory, Backend $deployments, - GitHub $github, array $platform, ) { $function = $dbForProject->getDocument('functions', $functionId); @@ -118,7 +118,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 be284292ce..d486ce2e8b 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Create.php @@ -21,6 +21,8 @@ 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 Appwrite\Vcs\RepositoryWebhooks; use Utopia\Abuse\Abuse; use Utopia\Config\Config; use Utopia\Database\Database; @@ -42,7 +44,7 @@ use Utopia\Validator\Boolean; use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; +use Utopia\VCS\Adapter\Git; class Create extends Base { @@ -130,7 +132,8 @@ class Create extends Base ->inject('publisherForFunctions') ->inject('dbForPlatform') ->inject('request') - ->inject('gitHub') + ->inject('vcsFactory') + ->inject('repositoryWebhooks') ->inject('authorization') ->inject('platform') ->callback($this->action(...)); @@ -175,7 +178,8 @@ class Create extends Base FunctionPublisher $publisherForFunctions, Database $dbForPlatform, Request $request, - GitHub $github, + VcsFactory $vcsFactory, + RepositoryWebhooks $repositoryWebhooks, Authorization $authorization, array $platform ) { @@ -306,6 +310,18 @@ class Create extends Base 'providerPullRequestIds' => [] ])); + try { + $providerAdapter = $vcsFactory->fromInstallation($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; + } + $function->setAttribute('repositoryId', $repository->getId()); $function->setAttribute('repositoryInternalId', $repository->getSequence()); } @@ -348,7 +364,7 @@ class Create extends Base publisherForBuilds: $publisherForBuilds, deployments: $deployments, 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 528a811cda..b44c8a7ef8 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Update.php @@ -14,6 +14,8 @@ 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 Appwrite\Vcs\RepositoryWebhooks; use Executor\Executor; use Utopia\Config\Config; use Utopia\Database\Database; @@ -35,7 +37,7 @@ use Utopia\Validator\Nullable; use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; +use Utopia\VCS\Adapter\Git; class Update extends Base { @@ -114,7 +116,8 @@ class Update extends Base ->inject('publisherForBuilds') ->inject('deployments') ->inject('dbForPlatform') - ->inject('gitHub') + ->inject('vcsFactory') + ->inject('repositoryWebhooks') ->inject('executor') ->inject('authorization') ->inject('platform') @@ -152,7 +155,8 @@ class Update extends Base BuildPublisher $publisherForBuilds, Backend $deployments, Database $dbForPlatform, - GitHub $github, + VcsFactory $vcsFactory, + RepositoryWebhooks $repositoryWebhooks, Executor $executor, Authorization $authorization, array $platform @@ -247,6 +251,18 @@ class Update extends Base $repositoryId = $repository->getId(); $repositoryInternalId = $repository->getSequence(); + + try { + $providerAdapter = $vcsFactory->fromInstallation($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; + } } $live = true; @@ -313,7 +329,7 @@ class Update extends Base // Redeploy logic if (!$isConnected && !empty($providerRepositoryId)) { - $this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $github, true, $platform, deployments: $deployments); + $this->redeployVcsFunction($request, $function, $project, $installation, $dbForProject, $publisherForBuilds, new Document(), $vcsFactory->fromInstallation($installation), true, $platform, deployments: $deployments); } // 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 1c32d11e9c..3b2daf976b 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -17,12 +17,12 @@ 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; use Executor\Executor; use Swoole\Coroutine as Co; -use Utopia\Cache\Cache; use Utopia\Config\Config; use Utopia\Console; use Utopia\Database\Database; @@ -44,7 +44,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 { @@ -91,7 +91,7 @@ class Builds extends Action ->inject('queueForRealtime') ->inject('usage') ->inject('publisherForUsage') - ->inject('cache') + ->inject('vcsFactory') ->inject('dbForProject') ->inject('deviceForFunctions') ->inject('deviceForSites') @@ -118,7 +118,7 @@ class Builds extends Action Realtime $queueForRealtime, Context $usage, UsagePublisher $publisherForUsage, - Cache $cache, + VcsFactory $vcsFactory, Database $dbForProject, Device $deviceForFunctions, Device $deviceForSites, @@ -149,7 +149,7 @@ class Builds extends Action switch ($type) { case BUILD_TYPE_DEPLOYMENT: case BUILD_TYPE_RETRY: - $github = new GitHub($cache); + $templateVcs = $vcsFactory->fromProvider('github'); $this->buildDeployment( $deviceForFunctions, $deviceForSites, @@ -163,7 +163,8 @@ class Builds extends Action $publisherForUsage, $dbForPlatform, $dbForProject, - $github, + $templateVcs, + $vcsFactory, $project, $resource, $deployment, @@ -200,7 +201,8 @@ class Builds extends Action UsagePublisher $publisherForUsage, Database $dbForPlatform, Database $dbForProject, - GitHub $github, + Git $templateVcs, + VcsFactory $vcsFactory, Document $project, Document $resource, Document $deployment, @@ -314,15 +316,14 @@ class Builds extends Action $isVcsEnabled = ! empty($providerRepositoryId); $owner = ''; $repositoryName = ''; + $providerAdapter = null; 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 { - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $providerAdapter = $vcsFactory->fromInstallation($installation); } catch (\Exception $e) { if ($e->getCode() === 404 && $resource->getAttribute('installationId', '') === $installationId) { @@ -355,7 +356,7 @@ class Builds extends Action // Clone template repo $tmpTemplateDirectory = '/tmp/builds/' . $deploymentId . '-template'; - $gitCloneCommandForTemplate = $github->generateCloneCommand($templateOwnerName, $templateRepositoryName, $templateReferenceValue, $templateReferenceType, $tmpTemplateDirectory, $templateRootDirectory); + $gitCloneCommandForTemplate = $templateVcs->generateCloneCommand($templateOwnerName, $templateRepositoryName, $templateReferenceValue, $templateReferenceType, $tmpTemplateDirectory, $templateRootDirectory); $exit = Console::execute($gitCloneCommandForTemplate, '', $stdout, $stderr); @@ -413,8 +414,8 @@ class Builds extends Action $rootDirectory = \ltrim($rootDirectory, '.'); $rootDirectory = \ltrim($rootDirectory, '/'); - $owner = $github->getOwnerName($providerInstallationId); - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $owner = $providerAdapter->getOwnerName($providerInstallationId); + $repositoryName = $providerAdapter->getRepositoryName($providerRepositoryId); $cloneOwner = $deployment->getAttribute('providerRepositoryOwner', $owner); $cloneRepository = $deployment->getAttribute('providerRepositoryName', $repositoryName); @@ -423,13 +424,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 = $providerAdapter->generateCloneCommand($cloneOwner, $cloneRepository, $cloneVersion, $cloneType, $tmpDirectory, $rootDirectory); $stdout = ''; $stderr = ''; @@ -475,7 +476,7 @@ class Builds extends Action // Clone template repo $tmpTemplateDirectory = '/tmp/builds/' . $deploymentId . '/template'; - $gitCloneCommandForTemplate = $github->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) { @@ -509,7 +510,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', $providerAdapter->getCommitUrl($cloneOwner, $cloneRepository, $providerCommitHash)); $deployment = $dbForProject->updateDocument('deployments', $deployment->getId(), new Document([ 'providerCommitHash' => $deployment->getAttribute('providerCommitHash'), 'providerCommitAuthorUrl' => $deployment->getAttribute('providerCommitAuthorUrl'), @@ -534,7 +535,7 @@ class Builds extends Action $deployments->createFromUrl( $resource, $deployment, - $github->getRepositoryPresignedUrl($cloneOwner, $cloneRepository, $ref), + $providerAdapter->getRepositoryPresignedUrl($cloneOwner, $cloneRepository, $ref), $resource->getAttribute('providerRootDirectory', ''), ); @@ -594,7 +595,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', $providerAdapter, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); } Span::add('timings.source', \round(\microtime(true) - $phaseStart, 3)); @@ -623,7 +624,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', $providerAdapter, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); } $deploymentModel = new Deployment(); @@ -1011,7 +1012,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', $providerAdapter, $providerCommitHash, $owner, $repositoryName, $project, $resource, $deployment->getId(), $dbForProject, $dbForPlatform, $queueForRealtime, $platform); } /** Set auto deploy */ @@ -1260,7 +1261,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', $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. @@ -1447,7 +1448,7 @@ class Builds extends Action */ protected function runGitAction( string $status, - GitHub $github, + Git $vcs, string $providerCommitHash, string $owner, string $repositoryName, @@ -1502,7 +1503,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)) { @@ -1546,9 +1547,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/Functions/Workers/Jobs.php b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php index 8a2ed11dfe..95703988cf 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; @@ -22,7 +23,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 +64,7 @@ class Jobs extends Action ->inject('publisherForUsage') ->inject('usage') ->inject('deviceForBuilds') - ->inject('gitHub') + ->inject('vcsFactory') ->inject('cache') ->inject('locks') ->callback($this->action(...)); @@ -82,7 +82,7 @@ class Jobs extends Action UsagePublisher $publisherForUsage, UsageContext $usage, Device $deviceForBuilds, - GitHub $github, + VcsFactory $vcsFactory, Cache $cache, callable $locks, ): void { @@ -93,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, $github, $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) { @@ -112,8 +112,8 @@ 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, $cache), - 'orchestrator.job.complete' => $this->onComplete($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $deviceForBuilds, $github, $cache), + 'orchestrator.job.exit' => $this->onExit($dbForProject, $dbForPlatform, $project, $deployment, (int) ($event->data['exitCode'] ?? 0), $usage, $publisherForUsage, $deviceForBuilds, $vcsFactory, $cache), + 'orchestrator.job.complete' => $this->onComplete($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $deviceForBuilds, $vcsFactory, $cache), default => $deployment, }; @@ -208,16 +208,16 @@ class Jobs extends Action UsageContext $usage, UsagePublisher $publisherForUsage, Device $deviceForBuilds, - GitHub $github, + VcsFactory $vcsFactory, Cache $cache, ): 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, $vcsFactory); } $cache->save('jobs-exit-' . $deployment->getId(), true); if ($cache->load('jobs-complete-' . $deployment->getId(), self::DEDUPE_TTL) !== false) { - return $this->ready($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $deviceForBuilds, $github); + return $this->ready($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $deviceForBuilds, $vcsFactory); } return $deployment; @@ -236,7 +236,7 @@ class Jobs extends Action UsageContext $usage, UsagePublisher $publisherForUsage, Device $deviceForBuilds, - GitHub $github, + VcsFactory $vcsFactory, Cache $cache, ): Document { if (\in_array($deployment->getAttribute('status'), ['ready', 'failed'], true)) { @@ -244,7 +244,7 @@ class Jobs extends Action } if ($cache->load('jobs-exit-' . $deployment->getId(), self::DEDUPE_TTL) !== false) { - return $this->ready($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $deviceForBuilds, $github); + return $this->ready($dbForProject, $dbForPlatform, $project, $deployment, $usage, $publisherForUsage, $deviceForBuilds, $vcsFactory); } $cache->save('jobs-complete-' . $deployment->getId(), true); @@ -264,7 +264,7 @@ class Jobs extends Action UsageContext $usage, UsagePublisher $publisherForUsage, Device $deviceForBuilds, - GitHub $github, + VcsFactory $vcsFactory, ): Document { $path = Orchestrator::buildPath($project->getId(), $deployment->getId()); $size = $deviceForBuilds->exists($path) ? $deviceForBuilds->getFileSize($path) : 0; @@ -273,10 +273,10 @@ class Jobs extends Action 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, $vcsFactory); } - return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $github, $size); + return $this->finalize($dbForProject, $dbForPlatform, $project, $deployment, true, '', $usage, $publisherForUsage, $vcsFactory, $size); } /** @@ -293,7 +293,7 @@ class Jobs extends Action string $message, UsageContext $usage, UsagePublisher $publisherForUsage, - GitHub $github, + VcsFactory $vcsFactory, int $buildSize = 0, ): Document { $function = $dbForProject->getDocument('functions', $deployment->getAttribute('resourceId')); @@ -346,17 +346,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($vcsFactory, $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(VcsFactory $vcsFactory, Database $dbForPlatform, Document $project, Document $function, Document $deployment, string $state): void { $commitHash = $deployment->getAttribute('providerCommitHash', ''); if ($commitHash === '' || $function->getAttribute('providerSilentMode', false) === true) { @@ -369,7 +369,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 = $vcsFactory->fromInstallation($installation); $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; $hostname = System::getEnv('_APP_CONSOLE_DOMAIN', System::getEnv('_APP_DOMAIN', '')); @@ -378,7 +378,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/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index aebac55154..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; @@ -26,7 +27,7 @@ use Utopia\System\System; use Utopia\Validator\Boolean; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; +use Utopia\VCS\Adapter\Git; 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, + VcsFactory $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..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; @@ -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, + VcsFactory $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/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Create.php index 9472a82174..289142bdd4 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 as VcsFactory; +use Appwrite\Vcs\RepositoryWebhooks; use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; @@ -25,6 +27,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 { @@ -102,6 +105,8 @@ class Create extends Base ->inject('project') ->inject('queueForEvents') ->inject('dbForPlatform') + ->inject('vcsFactory') + ->inject('repositoryWebhooks') ->callback($this->action(...)); } @@ -133,7 +138,9 @@ class Create extends Base Database $dbForProject, Document $project, Event $queueForEvents, - Database $dbForPlatform + Database $dbForPlatform, + VcsFactory $vcsFactory, + RepositoryWebhooks $repositoryWebhooks ) { if (!empty($adapter)) { $configFramework = Config::getParam('frameworks')[$framework] ?? []; @@ -221,6 +228,22 @@ class Create extends Base 'providerPullRequestIds' => [] ]); $repository = $dbForPlatform->createDocument('repositories', $repository); + + try { + $providerAdapter = $vcsFactory->fromInstallation($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; + } + $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 b97f30ff58..6ae47e5a6e 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Update.php @@ -11,6 +11,8 @@ 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 Appwrite\Vcs\RepositoryWebhooks; use Executor\Executor; use Utopia\Config\Config; use Utopia\Database\Database; @@ -29,7 +31,7 @@ use Utopia\Validator\Nullable; use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -use Utopia\VCS\Adapter\Git\GitHub; +use Utopia\VCS\Adapter\Git; class Update extends Base { @@ -108,7 +110,8 @@ class Update extends Base ->inject('queueForEvents') ->inject('publisherForBuilds') ->inject('dbForPlatform') - ->inject('gitHub') + ->inject('vcsFactory') + ->inject('repositoryWebhooks') ->inject('executor') ->inject('platform') ->callback($this->action(...)); @@ -145,7 +148,8 @@ class Update extends Base Event $queueForEvents, BuildPublisher $publisherForBuilds, Database $dbForPlatform, - GitHub $github, + VcsFactory $vcsFactory, + RepositoryWebhooks $repositoryWebhooks, Executor $executor, array $platform ) { @@ -247,6 +251,18 @@ class Update extends Base $repository = $dbForPlatform->createDocument('repositories', $repository); $repositoryId = $repository->getId(); $repositoryInternalId = $repository->getSequence(); + + try { + $providerAdapter = $vcsFactory->fromInstallation($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; + } } $live = true; @@ -315,7 +331,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/Authorize/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php new file mode 100644 index 0000000000..06b0fcea1a --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php @@ -0,0 +1,113 @@ +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('vcsConfigured') + ->inject('response') + ->inject('project') + ->inject('platform') + ->callback($this->action(...)); + } + + public function action( + string $success, + string $failure, + callable $vcsConfigured, + Response $response, + Document $project, + array $platform + ) { + $key = static::getProvider(); + + 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.'); + } + + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; + $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 + ->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..9e76682b13 --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php @@ -0,0 +1,191 @@ +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'] ?? ''; + + // 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()) { + $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/GitHub/Authorize/External/Update.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/External/Update.php index 79c08f3103..df629138e4 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 @@ -11,14 +11,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\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 @@ -55,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('gitHub') + ->inject('vcsFactory') ->inject('response') ->inject('project') ->inject('dbForPlatform') @@ -71,7 +70,7 @@ class Update extends Action string $installationId, string $repositoryId, string $providerPullRequestId, - GitHub $github, + VcsFactory $vcsFactory, Response $response, Document $project, Database $dbForPlatform, @@ -104,22 +103,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, (int) $providerRepositoryId); + $pullRequestResponse = $vcs->getPullRequest($owner, $providerRepositoryName, $providerPullRequestId); $providerRepositoryUrl = $pullRequestResponse['head']['repo']['html_url'] ?? ''; $providerRepositoryOwner = $pullRequestResponse['head']['repo']['owner']['login'] ?? ''; @@ -127,20 +124,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, $deployments); + $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, $providerPullRequestId, $providerAffectedFiles, true, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform, $deployments); $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..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; @@ -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, + VcsFactory $vcsFactory, Document $project, Response $response, Database $dbForPlatform, @@ -101,10 +101,11 @@ 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->fromInstallation(new Document([ + 'provider' => 'github', + 'providerInstallationId' => $providerInstallationId, + ])); + $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 3c1b6190b6..c1e3da5adf 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, @@ -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); @@ -145,9 +146,9 @@ trait Deployment $activate = true; } - $owner = $github->getOwnerName($providerInstallationId); + $owner = $vcs->getOwnerName($providerInstallationId, (int) $providerRepositoryId); try { - $repositoryName = $github->getRepositoryName($providerRepositoryId); + $repositoryName = $vcs->getRepositoryName($providerRepositoryId); } catch (RepositoryNotFound $e) { throw new Exception(Exception::PROVIDER_REPOSITORY_NOT_FOUND); } @@ -234,10 +235,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 { @@ -247,7 +248,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', ''); @@ -307,10 +308,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 { @@ -328,12 +329,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, (int) $providerRepositoryId); + $vcs->updateCommitStatus($repositoryName, $providerCommitHash, $owner, 'pending', $message, $authorizeUrl, $name); continue; } @@ -399,7 +400,7 @@ trait Deployment $deployment = $authorization->skip(fn () => $deployments->createFromUrl( $resource, $deployment, - $github->getRepositoryPresignedUrl($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash), + $vcs->getRepositoryPresignedUrl($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash), $resource->getAttribute('providerRootDirectory', ''), )); } else { @@ -552,9 +553,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()); @@ -573,14 +574,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, (int) $providerRepositoryId); $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); } Span::add("{$logBase}.build.triggered", 'true'); 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 0a09d3966a..9e48159505 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -9,6 +9,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 Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; @@ -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,7 +37,8 @@ class Create extends Action ->desc('Create event') ->groups(['api', 'vcs']) ->label('scope', 'public') - ->inject('gitHub') + ->inject('vcsWebhookSecret') + ->inject('vcsFactory') ->inject('request') ->inject('response') ->inject('dbForPlatform') @@ -50,7 +51,8 @@ class Create extends Action } public function action( - GitHub $github, + callable $vcsWebhookSecret, + VcsFactory $vcsFactory, Request $request, Response $response, Database $dbForPlatform, @@ -62,28 +64,28 @@ class Create extends Action ) { $this->preprocessEvent($request); + $vcs = $vcsFactory->fromProvider('github'); + $event = $request->getHeaderLine('x-github-event', ''); Span::add('vcs.github.event.name', $event); $payload = $request->getRawPayload(); $signature = $request->getHeaderLine('x-hub-signature-256', ''); - $secretKey = System::getEnv('_APP_VCS_GITHUB_WEBHOOK_SECRET', ''); + $secretKey = $vcsWebhookSecret('github'); - $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) { 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 = $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, $deployments), - $github::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $privateKey, $githubAppId, $github, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform, $deployments), + GitHub::EVENT_INSTALLATION => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), + GitHub::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform, $deployments), + GitHub::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform, $deployments), default => null, }; @@ -182,9 +184,7 @@ class Create extends Action private function handlePushEvent( array $parsedPayload, - string $githubAppId, - string $privateKey, - GitHub $github, + VcsFactory $vcsFactory, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, @@ -212,7 +212,10 @@ 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 = $vcsFactory->fromInstallation(new Document([ + 'provider' => 'github', + 'providerInstallationId' => $providerInstallationId, + ])); // Find associated repositories $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ @@ -223,15 +226,13 @@ 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, $deployments); + $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform, $deployments); } } private function handlePullRequestEvent( array $parsedPayload, - string $privateKey, - string $githubAppId, - GitHub $github, + VcsFactory $vcsFactory, Database $dbForPlatform, Authorization $authorization, BuildPublisher $publisherForBuilds, @@ -265,10 +266,13 @@ class Create extends Action return; } - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + $vcs = $vcsFactory->fromInstallation(new Document([ + 'provider' => 'github', + 'providerInstallationId' => $providerInstallationId, + ])); 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 = []; @@ -276,7 +280,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. @@ -288,7 +292,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, $deployments); + $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, $providerPullRequestId, $providerAffectedFiles, $external, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform, $deployments); } elseif ($action == "closed") { // Allowed external contributions cleanup 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..efeef8fc3a --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Authorize/Get.php @@ -0,0 +1,52 @@ +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..10ec1bbcb6 --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -0,0 +1,298 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_POST) + ->setHttpPath('/v1/vcs/gitea/events') + ->desc('Create event') + ->groups(['api', 'vcs']) + ->label('scope', 'public') + ->inject('vcsFactory') + ->inject('vcsWebhookSecret') + ->inject('installationTokens') + ->inject('request') + ->inject('response') + ->inject('dbForPlatform') + ->inject('authorization') + ->inject('getProjectDB') + ->inject('publisherForBuilds') + ->inject('platform') + ->callback($this->action(...)); + } + + public function action( + VcsFactory $vcsFactory, + callable $vcsWebhookSecret, + InstallationTokens $installationTokens, + 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 = $vcsWebhookSecret('gitea'); + + $valid = !empty($secretKey) && $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, $installationTokens, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + 'pull_request' => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + default => null, + }; + + $response->json($parsedPayload); + } + + private function resolveGiteaInstallation(Document $repository, Database $dbForPlatform, Authorization $authorization): ?Document + { + $installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', ''))); + + if ($installation->isEmpty() || $installation->getAttribute('provider', 'github') !== 'gitea') { + return null; + } + + return $installation; + } + + /** + * 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); + + if ($installation === null) { + return null; + } + + 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) { + $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; + } + } + + private function handlePushEvent( + array $parsedPayload, + VcsFactory $vcsFactory, + InstallationTokens $installationTokens, + 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_GITEA_EMAIL || $providerBranchDeleted) { + return; + } + + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ + Query::equal('providerRepositoryId', [$providerRepositoryId]), + Query::limit(100), + ])); + + $providerAffectedFiles = $parsedPayload['affectedFiles'] ?? []; + + $errors = []; + foreach ($repositories as $repository) { + $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $errors); + + 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); + } + + if (!empty($errors)) { + throw new Exception(Exception::GENERAL_UNKNOWN, \implode("\n", $errors)); + } + } + + private function handlePullRequestEvent( + array $parsedPayload, + VcsFactory $vcsFactory, + InstallationTokens $installationTokens, + 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), + ])); + + $errors = []; + foreach ($repositories as $repository) { + $adapter = $this->resolveAdapterForRepository($repository, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $errors); + + 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); + } + + if (!empty($errors)) { + throw new Exception(Exception::GENERAL_UNKNOWN, \implode("\n", $errors)); + } + } 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) { + if ($this->resolveGiteaInstallation($repository, $dbForPlatform, $authorization) === null) { + continue; + } + + $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/Http/Installations/Repositories/Branches/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php index c1a86c5597..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,12 +9,12 @@ 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; 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; @@ -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, + VcsFactory $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..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,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 as VcsFactory; 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 @@ -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('gitHub') + ->inject('vcsFactory') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -62,7 +61,7 @@ class Get extends Action string $providerRepositoryId, string $providerRootDirectory, string $providerReference, - GitHub $github, + VcsFactory $vcsFactory, Response $response, Database $dbForPlatform ) { @@ -73,13 +72,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,12 +84,12 @@ 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) { $isDirectory = false; - if ($content['type'] === GitHub::CONTENTS_DIRECTORY) { + if ($content['type'] === 'dir') { $isDirectory = true; } 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..56454b6457 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; @@ -9,15 +10,15 @@ 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 Appwrite\Vcs\InstallationTokens; use Utopia\Database\Database; -use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Query; 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 +54,8 @@ 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('installationTokens') ->inject('user') ->inject('response') ->inject('dbForPlatform') @@ -64,7 +66,8 @@ class Create extends Action string $installationId, string $name, bool $private, - GitHub $github, + VcsFactory $vcsFactory, + InstallationTokens $installationTokens, Document $user, Response $response, Database $dbForPlatform @@ -75,14 +78,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); - $accessToken = $installation->getAttribute('personalAccessToken'); - $refreshToken = $installation->getAttribute('personalRefreshToken'); - $accessTokenExpiry = $installation->getAttribute('personalAccessTokenExpiry'); - - if (empty($accessToken) || empty($refreshToken) || empty($accessTokenExpiry)) { + $identity = null; + 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()]), @@ -90,53 +92,25 @@ 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); - } catch (Exception $exception) { - throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, "GitHub failed to process the request: " . $exception->getMessage()); + } catch (\Throwable $exception) { + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, "VCS provider failed to process the request: " . $exception->getMessage()); } } 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()); + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, "VCS provider failed to process the request: " . $exception->getMessage()); } } @@ -161,4 +135,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/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..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; @@ -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, + VcsFactory $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..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,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 as VcsFactory; 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, + VcsFactory $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..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; @@ -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, + VcsFactory $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/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 new file mode 100644 index 0000000000..d654875144 --- /dev/null +++ b/src/Appwrite/Vcs/Factory.php @@ -0,0 +1,103 @@ +> + */ + protected array $registry = []; + + /** + * @param array>|null $registry Override, defaults to the `vcs` config. + */ + public function __construct( + protected Cache $cache, + ?array $registry = null, + ) { + $this->registry = $registry ?? Config::getParam('vcs', []); + } + + public function getProviders(): array + { + return \array_values(\array_filter( + \array_keys($this->registry), + fn (string $key) => $this->isConfigured($key), + )); + } + + public function isConfigured(string $key): bool + { + if (!isset($this->registry[$key])) { + return false; + } + + foreach ($this->registry[$key]['variables'] ?? [] as $name => $variable) { + if (($variable['required'] ?? false) && empty($this->getEnv($key, $name))) { + return false; + } + } + + return true; + } + + 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; + } + + public function fromInstallation(Document $installation): Git + { + if ($installation->isEmpty()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + + $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( + $installation->getAttribute('providerInstallationId', ''), + $this->getEnv($provider, 'privateKey'), + $this->getEnv($provider, 'appId'), + $installation->getAttribute('personalAccessToken', ''), + $installation->getAttribute('personalRefreshToken', ''), + ); + + return $adapter; + } + + public function getWebhookSecret(string $key): string + { + return $this->getEnv($key, 'webhookSecret'); + } + + protected function getEnv(string $key, string $name): string + { + $variable = $this->registry[$key]['variables'][$name]['envVariable'] ?? ''; + + return empty($variable) ? '' : System::getEnv($variable, ''); + } +} diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php new file mode 100644 index 0000000000..43fcb3088c --- /dev/null +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -0,0 +1,97 @@ +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) + ->setAttribute('personalRefreshToken', $refreshToken) + ->setAttribute('personalAccessTokenExpiry', $accessTokenExpiry); + + if (!$this->isExpired($accessTokenExpiry)) { + return $installation; + } + + if (empty($refreshToken)) { + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, 'This installation has no refresh token on file. Please reconnect it.'); + } + + 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(''); + $verificationId = $oauth2->getUserID($accessToken); + + if (empty($verificationId)) { + $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 + ->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; + } + + protected function isExpired(?string $expiry): bool + { + 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 new file mode 100644 index 0000000000..0cae3ab26b --- /dev/null +++ b/src/Appwrite/Vcs/RepositoryWebhooks.php @@ -0,0 +1,77 @@ +getSupportedWebhookScopes(); + + if (\in_array(Git::WEBHOOK_SCOPE_INSTALLATION, $scopes, true)) { + return; + } + + if (!\in_array(Git::WEBHOOK_SCOPE_REPOSITORY, $scopes, true)) { + return; + } + + // The caller persists the current repository connection before this check. + $connections = $dbForPlatform->count('repositories', [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::equal('providerRepositoryId', [$providerRepositoryId]), + ], 2); + + if ($connections > 1) { + return; + } + + $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); + + 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'); + } + + $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/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php new file mode 100644 index 0000000000..db89369252 --- /dev/null +++ b/tests/unit/Vcs/FactoryTest.php @@ -0,0 +1,125 @@ +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['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}'"); + } + } + } + + 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 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 = [ + 'adapter' => GitHub::class, + 'variables' => [ + 'token' => ['required' => true, 'envVariable' => '_APP_VCS_TEST_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()); + } + + 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()); + } + + /** + * @return array + */ + protected function githubEntry(): array + { + return [ + 'adapter' => GitHub::class, + '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'], + ], + ]; + } +} diff --git a/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php b/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php new file mode 100644 index 0000000000..93000c3339 --- /dev/null +++ b/tests/unit/Vcs/GiteaClosedPullRequestEventTest.php @@ -0,0 +1,84 @@ +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->createStub(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->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, $installationTokens, $dbForPlatform, $authorization, $publisherForBuilds, fn () => null, []); + } + + private function callHandler(string $method, mixed ...$arguments): mixed + { + return (new ReflectionMethod($this->handler, $method))->invoke($this->handler, ...$arguments); + } +} diff --git a/tests/unit/Vcs/InstallationTokensTest.php b/tests/unit/Vcs/InstallationTokensTest.php new file mode 100644 index 0000000000..96f1381eee --- /dev/null +++ b/tests/unit/Vcs/InstallationTokensTest.php @@ -0,0 +1,279 @@ +createStub(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(); + + $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); + + $this->assertSame('valid-token', $result->getAttribute('personalAccessToken')); + $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); + + $db->expects($this->once()) + ->method('updateDocument') + ->with('installations', 'installation1', $this->callback(function (Document $update) { + $this->assertSame('fresh-token', $update->getAttribute('personalAccessToken')); + $this->assertSame('fresh-refresh', $update->getAttribute('personalRefreshToken')); + return true; + })) + ->willReturnArgument(2); + + $installation = new Document([ + '$id' => 'installation1', + 'personalAccessToken' => 'stale-token', + 'personalRefreshToken' => 'stale-refresh', + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), -3600), + ]); + + $oauth2 = $this->fakeOAuth2(); + + $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(); + + $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 + { + $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([ + '$id' => 'installation1', + 'personalAccessToken' => 'stale-token', + 'personalRefreshToken' => 'stale-refresh', + 'personalAccessTokenExpiry' => DateTime::addSeconds(new \DateTime(), -3600), + ]); + + $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')); + } + + public function testRefreshExceptionReturnsCurrentInstallationWhenAnotherRequestRefreshed(): 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(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, protected bool $throwOnRefresh) + { + 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++; + if ($this->throwOnRefresh) { + throw new \RuntimeException('refresh token already used'); + } + + $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..cb2856d807 --- /dev/null +++ b/tests/unit/Vcs/RepositoryWebhooksTest.php @@ -0,0 +1,128 @@ +createMock(Git::class); + $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); + $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 + { + $adapter = $this->createMock(Git::class); + $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') + ->willReturn(1); + + $db = $this->createMock(Database::class); + $db->expects($this->once())->method('count')->willReturn(1); + + $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('getSupportedWebhookScopes')->willReturn([Git::WEBHOOK_SCOPE_REPOSITORY]); + $adapter->expects($this->never())->method('createWebhook'); + + $db = $this->createMock(Database::class); + $db->expects($this->once())->method('count')->willReturn(2); + + $installation = new Document(['$sequence' => 1, 'provider' => 'gitea']); + + $this->ensure($adapter, $installation, $db, $this->factory('secret')); + } + + public function testAdapterFailureIsWrapped(): void + { + $adapter = $this->createMock(Git::class); + $adapter->method('getSupportedWebhookScopes')->willReturn([Git::WEBHOOK_SCOPE_REPOSITORY]); + $adapter->expects($this->once()) + ->method('createWebhook') + ->willThrowException(new \RuntimeException('provider unreachable')); + + $db = $this->createMock(Database::class); + $db->expects($this->once())->method('count')->willReturn(1); + + $installation = new Document(['$sequence' => 1, 'provider' => 'gitea']); + + $this->expectException(Exception::class); + $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'); + + try { + (new RepositoryWebhooks($vcsFactory))->ensure($adapter, $installation, $db, 'repo-1', 'owner', 'repo'); + } finally { + \putenv('_APP_VCS_WEBHOOK_URL'); + } + } + + protected function factory(string $secret): Factory + { + $factory = $this->createStub(Factory::class); + $factory->method('getWebhookSecret')->willReturn($secret); + + return $factory; + } +}