From 0bb35b544150e7658edf2c9c491f54f0a7fbf00b Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 16 Jul 2026 12:51:08 +0530 Subject: [PATCH 01/19] Add GitLab as a VCS provider via adapter-driven Vcs\Factory Mirrors the Gitea/GitHub pattern: per-repo webhooks, personal OAuth2 install flow, token refresh on webhook events, error aggregation (non-2xx response on adapter-resolution failure so GitLab surfaces it in its redelivery log), span namespacing under vcs.gitlab.*. Unlike Gitea (self-hosted only, no sensible default), GitLab has a public SaaS at gitlab.com, so its endpoint defaults there when unset -- works out of the box with just client credentials, same as GitHub. That needed a small, fully generic addition to Vcs\Factory: any provider's config can declare a 'default' for a variable; only GitLab opts in today, GitHub/Gitea are unaffected (verified via the full VCS unit suite). Reuses the existing Auth\OAuth2\Gitlab class (the "sign in with GitLab" account-login provider) for the VCS flow too, adding only the missing getUserSlug() method -- its JSON-encoded appSecret already carries an endpoint, which is exactly what the VCS flow needs for self-hosted GitLab instances. --- .env | 5 + app/config/variables.php | 45 +++ app/config/vcs.php | 15 + app/controllers/mock.php | 56 ++++ app/init/constants.php | 1 + docker-compose.yml | 10 + src/Appwrite/Auth/OAuth2/Gitlab.php | 12 + .../Modules/VCS/Http/Gitlab/Authorize/Get.php | 56 ++++ .../Modules/VCS/Http/Gitlab/Callback/Get.php | 41 +++ .../Modules/VCS/Http/Gitlab/Events/Create.php | 300 ++++++++++++++++++ .../Platform/Modules/VCS/Services/Http.php | 8 + src/Appwrite/Vcs/Factory.php | 2 +- tests/unit/Vcs/FactoryTest.php | 20 ++ .../Vcs/GitlabClosedPullRequestEventTest.php | 84 +++++ 14 files changed, 654 insertions(+), 1 deletion(-) create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php create mode 100644 tests/unit/Vcs/GitlabClosedPullRequestEventTest.php diff --git a/.env b/.env index eed895eb1e..503a74dfd7 100644 --- a/.env +++ b/.env @@ -160,5 +160,10 @@ _APP_VCS_GITEA_BROWSER_ENDPOINT= _APP_VCS_GITEA_CLIENT_ID= _APP_VCS_GITEA_CLIENT_SECRET= _APP_VCS_GITEA_WEBHOOK_SECRET= +_APP_VCS_GITLAB_ENDPOINT= +_APP_VCS_GITLAB_BROWSER_ENDPOINT= +_APP_VCS_GITLAB_CLIENT_ID= +_APP_VCS_GITLAB_CLIENT_SECRET= +_APP_VCS_GITLAB_WEBHOOK_SECRET= _APP_VCS_WEBHOOK_URL= _APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 diff --git a/app/config/variables.php b/app/config/variables.php index 9675e483ef..1adb2bd8b3 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -1311,6 +1311,51 @@ return [ 'question' => '', 'filter' => '' ], + [ + 'name' => '_APP_VCS_GITLAB_ENDPOINT', + 'description' => 'URL of your self-hosted GitLab instance, reachable from the Appwrite server (e.g. for API calls and webhook token exchange). Defaults to https://gitlab.com when unset.', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], + [ + 'name' => '_APP_VCS_GITLAB_BROWSER_ENDPOINT', + 'description' => 'URL of your GitLab instance reachable from the browser, if different from _APP_VCS_GITLAB_ENDPOINT (e.g. when Appwrite and GitLab are on the same Docker network but GitLab is exposed to the browser on a different host). Falls back to _APP_VCS_GITLAB_ENDPOINT when unset.', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], + [ + 'name' => '_APP_VCS_GITLAB_CLIENT_ID', + 'description' => 'GitLab OAuth2 application client ID. You can generate one in your GitLab instance under Settings > Applications.', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], + [ + 'name' => '_APP_VCS_GITLAB_CLIENT_SECRET', + 'description' => 'GitLab OAuth2 application client secret. You can generate one in your GitLab instance under Settings > Applications.', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], + [ + 'name' => '_APP_VCS_GITLAB_WEBHOOK_SECRET', + 'description' => 'Secret used to validate incoming GitLab 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.', diff --git a/app/config/vcs.php b/app/config/vcs.php index 6df0cc924d..bc451a265e 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -6,6 +6,7 @@ use Utopia\VCS\Adapter\Git\Gitea; use Utopia\VCS\Adapter\Git\GitHub; +use Utopia\VCS\Adapter\Git\GitLab; return [ 'github' => [ @@ -30,4 +31,18 @@ return [ 'webhookSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_WEBHOOK_SECRET'], ], ], + 'gitlab' => [ + 'adapter' => GitLab::class, + 'variables' => [ + // Unset endpoint defaults to gitlab.com's public SaaS -- like + // GitHub, GitLab is usable out of the box with just client + // credentials; self-hosted GitLab instances override this. + 'endpoint' => ['required' => false, 'envVariable' => '_APP_VCS_GITLAB_ENDPOINT', 'default' => 'https://gitlab.com'], + 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_CLIENT_ID'], + 'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_CLIENT_SECRET'], + // Appwrite creates repository webhooks directly via the API, so + // (like Gitea) a shared secret is always required. + 'webhookSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_WEBHOOK_SECRET'], + ], + ], ]; diff --git a/app/controllers/mock.php b/app/controllers/mock.php index ca80f08e38..c74b6582da 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -351,6 +351,62 @@ Http::get('/v1/mock/gitea/callback') ]); }); +Http::get('/v1/mock/gitlab/callback') + ->desc('Create installation document using GitLab OAuth2 tokens') + ->groups(['mock', 'api', 'vcs']) + ->label('scope', 'public') + ->label('docs', false) + ->param('projectId', '', new UID(), 'Project ID of the project where GitLab is to be installed') + ->param('gitlabUserId', '', new Text(256), 'GitLab user ID') + ->param('organization', '', new Text(256), 'GitLab username or namespace') + ->param('accessToken', '', new Text(2048), 'GitLab access token') + ->inject('response') + ->inject('dbForPlatform') + ->action(function (string $projectId, string $gitlabUserId, 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($gitlabUserId) || empty($organization) || empty($accessToken)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing GitLab 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' => $gitlabUserId, + 'projectId' => $projectId, + 'projectInternalId' => $projectInternalId, + 'provider' => 'gitlab', + '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/constants.php b/app/init/constants.php index f5eaa3a4ab..d55bb01e79 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -109,6 +109,7 @@ 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_VCS_GITLAB_EMAIL = 'team@appwrite.io'; // Used to detect Appwrite's own commits const APP_BRANDED_EMAIL_BASE_TEMPLATE = 'email-base-styled'; // Embeddings diff --git a/docker-compose.yml b/docker-compose.yml index b520f14d88..90f511f949 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -203,6 +203,11 @@ services: - _APP_VCS_GITEA_CLIENT_ID - _APP_VCS_GITEA_CLIENT_SECRET - _APP_VCS_GITEA_WEBHOOK_SECRET + - _APP_VCS_GITLAB_ENDPOINT + - _APP_VCS_GITLAB_BROWSER_ENDPOINT + - _APP_VCS_GITLAB_CLIENT_ID + - _APP_VCS_GITLAB_CLIENT_SECRET + - _APP_VCS_GITLAB_WEBHOOK_SECRET - _APP_VCS_WEBHOOK_URL - _APP_MIGRATIONS_FIREBASE_CLIENT_ID - _APP_MIGRATIONS_FIREBASE_CLIENT_SECRET @@ -493,6 +498,11 @@ services: - _APP_VCS_GITEA_CLIENT_ID - _APP_VCS_GITEA_CLIENT_SECRET - _APP_VCS_GITEA_WEBHOOK_SECRET + - _APP_VCS_GITLAB_ENDPOINT + - _APP_VCS_GITLAB_BROWSER_ENDPOINT + - _APP_VCS_GITLAB_CLIENT_ID + - _APP_VCS_GITLAB_CLIENT_SECRET + - _APP_VCS_GITLAB_WEBHOOK_SECRET - _APP_VCS_WEBHOOK_URL - _APP_FUNCTIONS_TIMEOUT - _APP_SITES_TIMEOUT diff --git a/src/Appwrite/Auth/OAuth2/Gitlab.php b/src/Appwrite/Auth/OAuth2/Gitlab.php index 7d98bf1921..9ae4a9c214 100644 --- a/src/Appwrite/Auth/OAuth2/Gitlab.php +++ b/src/Appwrite/Auth/OAuth2/Gitlab.php @@ -155,6 +155,18 @@ class Gitlab extends OAuth2 return $user['name'] ?? ''; } + /** + * @param string $accessToken + * + * @return string + */ + public function getUserSlug(string $accessToken): string + { + $user = $this->getUser($accessToken); + + return $user['username'] ?? ''; + } + /** * @param string $accessToken * diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php new file mode 100644 index 0000000000..7501374d65 --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php @@ -0,0 +1,56 @@ + System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), + 'endpoint' => $browserEndpoint, + ]), + $callback, + $state, + // VCS-specific scopes -- the adapter's own default is just + // read_user, enough for a plain "sign in with GitLab". `api` + // is required for creating/updating repository webhooks and + // merge request notes; GitLab has no finer-grained scope that + // covers both. + [ + 'read_user', + 'api', + ] + ); + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php new file mode 100644 index 0000000000..1c6153c765 --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php @@ -0,0 +1,41 @@ + System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), + 'endpoint' => System::getEnv('_APP_VCS_GITLAB_ENDPOINT', 'https://gitlab.com'), + ]), + $callback + ); + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php new file mode 100644 index 0000000000..11fe86bb66 --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php @@ -0,0 +1,300 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_POST) + ->setHttpPath('/v1/vcs/gitlab/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('gitlab'); + + $event = $request->getHeaderLine($vcs->getEventHeaderName(), ''); + Span::add('vcs.gitlab.event.name', $event); + + $payload = $request->getRawPayload(); + $signature = $request->getHeaderLine($vcs->getSignatureHeaderName(), ''); + $secretKey = $vcsWebhookSecret('gitlab'); + + $valid = !empty($secretKey) && $vcs->validateWebhookEvent($payload, $signature, $secretKey); + Span::add('vcs.gitlab.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 GitLab repository settings and in the _APP_VCS_GITLAB_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 resolveGitlabInstallation(Document $repository, Database $dbForPlatform, Authorization $authorization): ?Document + { + $installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', ''))); + + if ($installation->isEmpty() || $installation->getAttribute('provider', 'github') !== 'gitlab') { + return null; + } + + return $installation; + } + + /** + * Resolves the adapter for a repository's installation, refreshing its + * OAuth2 token first. A null installation (not a GitLab 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 -- GitLab'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->resolveGitlabInstallation($repository, $dbForPlatform, $authorization); + + if ($installation === null) { + return null; + } + + try { + $oauth2 = new OAuth2Gitlab( + System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), + \json_encode([ + 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), + 'endpoint' => System::getEnv('_APP_VCS_GITLAB_ENDPOINT', 'https://gitlab.com'), + ]), + '' + ); + + $installation = $installationTokens->refresh($installation, $dbForPlatform, $oauth2); + + return $vcsFactory->fromInstallation($installation); + } catch (\Throwable $error) { + $message = "Failed to resolve GitLab adapter for installation '{$installation->getId()}': " . $error->getMessage(); + Console::warning($message); + Span::add("vcs.gitlab.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.gitlab.event.repo.id', $providerRepositoryId); + Span::add('vcs.gitlab.event.repo.name', $providerRepositoryName); + Span::add('vcs.gitlab.event.branch', $providerBranch); + + if ($providerCommitAuthorEmail === APP_VCS_GITLAB_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.gitlab.event.repo.id', $providerRepositoryId); + Span::add('vcs.gitlab.event.repo.name', $providerRepositoryName); + Span::add('vcs.gitlab.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->resolveGitlabInstallation($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/Services/Http.php b/src/Appwrite/Platform/Modules/VCS/Services/Http.php index dfdd6c8a5f..a9cedf40f6 100644 --- a/src/Appwrite/Platform/Modules/VCS/Services/Http.php +++ b/src/Appwrite/Platform/Modules/VCS/Services/Http.php @@ -9,6 +9,9 @@ use Appwrite\Platform\Modules\VCS\Http\GitHub\Authorize\External\Update as Updat use Appwrite\Platform\Modules\VCS\Http\GitHub\Authorize\Get as GetGitHubAuthorize; use Appwrite\Platform\Modules\VCS\Http\GitHub\Callback\Get as GetGitHubCallback; use Appwrite\Platform\Modules\VCS\Http\GitHub\Events\Create as CreateGitHubEvent; +use Appwrite\Platform\Modules\VCS\Http\Gitlab\Authorize\Get as GetGitlabAuthorize; +use Appwrite\Platform\Modules\VCS\Http\Gitlab\Callback\Get as GetGitlabCallback; +use Appwrite\Platform\Modules\VCS\Http\Gitlab\Events\Create as CreateGitlabEvent; use Appwrite\Platform\Modules\VCS\Http\Installations\Delete as DeleteInstallation; use Appwrite\Platform\Modules\VCS\Http\Installations\Get as GetInstallation; use Appwrite\Platform\Modules\VCS\Http\Installations\Repositories\Branches\XList as ListRepositoryBranches; @@ -35,6 +38,10 @@ class Http extends Service $this->addAction(GetGiteaAuthorize::getName(), new GetGiteaAuthorize()); $this->addAction(GetGiteaCallback::getName(), new GetGiteaCallback()); + // GitLab Authorization & Callback + $this->addAction(GetGitlabAuthorize::getName(), new GetGitlabAuthorize()); + $this->addAction(GetGitlabCallback::getName(), new GetGitlabCallback()); + // Installations $this->addAction(GetInstallation::getName(), new GetInstallation()); $this->addAction(ListInstallations::getName(), new ListInstallations()); @@ -51,5 +58,6 @@ class Http extends Service // Events $this->addAction(CreateGitHubEvent::getName(), new CreateGitHubEvent()); $this->addAction(CreateGiteaEvent::getName(), new CreateGiteaEvent()); + $this->addAction(CreateGitlabEvent::getName(), new CreateGitlabEvent()); } } diff --git a/src/Appwrite/Vcs/Factory.php b/src/Appwrite/Vcs/Factory.php index d654875144..6f69dd3a6e 100644 --- a/src/Appwrite/Vcs/Factory.php +++ b/src/Appwrite/Vcs/Factory.php @@ -57,7 +57,7 @@ class Factory $adapter = new ($this->registry[$key]['adapter'])($this->cache); - $endpoint = $this->getEnv($key, 'endpoint'); + $endpoint = $this->getEnv($key, 'endpoint') ?: ($this->registry[$key]['variables']['endpoint']['default'] ?? ''); if (!empty($endpoint) && \method_exists($adapter, 'setEndpoint')) { $adapter->setEndpoint(\rtrim($endpoint, '/')); } diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index db89369252..01de5ef332 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -89,6 +89,26 @@ final class FactoryTest extends TestCase $this->assertSame(['test'], $factory->getProviders()); } + public function testFromProviderAppliesEndpointDefault(): void + { + $entry = [ + 'adapter' => \Utopia\VCS\Adapter\Git\GitLab::class, + 'variables' => [ + 'endpoint' => ['required' => false, 'envVariable' => '_APP_VCS_TEST_ENDPOINT', 'default' => 'https://gitlab.com'], + ], + ]; + $factory = new Factory($this->cache(), ['gitlab' => $entry]); + + $adapter = $factory->fromProvider('gitlab'); + $property = new \ReflectionProperty($adapter, 'endpoint'); + $this->assertSame('https://gitlab.com/api/v4', $property->getValue($adapter)); + + \putenv('_APP_VCS_TEST_ENDPOINT=https://gitlab.example.com'); + $adapter = $factory->fromProvider('gitlab'); + $this->assertSame('https://gitlab.example.com/api/v4', $property->getValue($adapter)); + \putenv('_APP_VCS_TEST_ENDPOINT'); + } + public function testGetWebhookSecret(): void { $factory = new Factory($this->cache(), ['github' => $this->githubEntry()]); diff --git a/tests/unit/Vcs/GitlabClosedPullRequestEventTest.php b/tests/unit/Vcs/GitlabClosedPullRequestEventTest.php new file mode 100644 index 0000000000..2d181e1744 --- /dev/null +++ b/tests/unit/Vcs/GitlabClosedPullRequestEventTest.php @@ -0,0 +1,84 @@ +handler = new Create(); + } + + public function testClosedEventOnlyUpdatesGitlabRepositories(): void + { + // A GitHub repo and a GitLab repo can share the same numeric + // providerRepositoryId since each is scoped to its own server. + $gitlabRepository = new Document([ + '$id' => 'repo-gitlab', + 'installationId' => 'install-gitlab', + 'providerRepositoryId' => '5', + 'providerPullRequestIds' => [5], + ]); + + $githubRepository = new Document([ + '$id' => 'repo-github', + 'installationId' => 'install-github', + 'providerRepositoryId' => '5', + 'providerPullRequestIds' => [5], + ]); + + $gitlabInstallation = new Document(['$id' => 'install-gitlab', 'provider' => 'gitlab']); + $githubInstallation = new Document(['$id' => 'install-github', 'provider' => 'github']); + + $dbForPlatform = $this->createMock(Database::class); + $authorization = $this->createStub(Authorization::class); + + $dbForPlatform->method('find')->willReturn([$gitlabRepository, $githubRepository]); + + $dbForPlatform->method('getDocument') + ->willReturnCallback(fn (string $collection, string $id): Document => match ($id) { + 'install-gitlab' => $gitlabInstallation, + 'install-github' => $githubInstallation, + default => new Document(), + }); + + $authorization->method('skip')->willReturnCallback(fn (callable $fn): mixed => $fn()); + + $dbForPlatform->expects($this->once()) + ->method('updateDocument') + ->with('repositories', 'repo-gitlab', $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); + } +} From 011abb82bb855cde1ff6126a3d69ed4e3ff5d717 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 16 Jul 2026 12:56:12 +0530 Subject: [PATCH 02/19] Trim verbose comments in GitLab config/OAuth2 files --- app/config/vcs.php | 6 +----- .../Modules/VCS/Http/Gitlab/Authorize/Get.php | 13 ++----------- .../Modules/VCS/Http/Gitlab/Callback/Get.php | 4 +--- 3 files changed, 4 insertions(+), 19 deletions(-) diff --git a/app/config/vcs.php b/app/config/vcs.php index bc451a265e..6df3f92c74 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -34,14 +34,10 @@ return [ 'gitlab' => [ 'adapter' => GitLab::class, 'variables' => [ - // Unset endpoint defaults to gitlab.com's public SaaS -- like - // GitHub, GitLab is usable out of the box with just client - // credentials; self-hosted GitLab instances override this. + // Unset defaults to gitlab.com; self-hosted instances override this. 'endpoint' => ['required' => false, 'envVariable' => '_APP_VCS_GITLAB_ENDPOINT', 'default' => 'https://gitlab.com'], 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_CLIENT_ID'], 'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_CLIENT_SECRET'], - // Appwrite creates repository webhooks directly via the API, so - // (like Gitea) a shared secret is always required. 'webhookSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_WEBHOOK_SECRET'], ], ], diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php index 7501374d65..e6a4daeb8c 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php @@ -26,12 +26,7 @@ class Get extends Base protected function createOAuth2(string $callback, array $state): OAuth2 { - // Auth\OAuth2\Gitlab (shared with the "sign in with GitLab" account - // flow) reads both the client secret and the endpoint out of a - // JSON-encoded appSecret -- there's no separate setEndpoint(). The - // login page is opened by the browser, which may reach a self-hosted - // GitLab on a different host than the server-side API endpoint (e.g. - // Docker); public gitlab.com defaults apply to both. + // Auth\OAuth2\Gitlab reads the endpoint out of a JSON-encoded appSecret; no setEndpoint(). $browserEndpoint = System::getEnv('_APP_VCS_GITLAB_BROWSER_ENDPOINT', System::getEnv('_APP_VCS_GITLAB_ENDPOINT', 'https://gitlab.com')); return new OAuth2Gitlab( @@ -42,11 +37,7 @@ class Get extends Base ]), $callback, $state, - // VCS-specific scopes -- the adapter's own default is just - // read_user, enough for a plain "sign in with GitLab". `api` - // is required for creating/updating repository webhooks and - // merge request notes; GitLab has no finer-grained scope that - // covers both. + // api is required for webhook/merge-request-note writes; no finer-grained scope covers both. [ 'read_user', 'api', diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php index 1c6153c765..ed161880ec 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php @@ -26,9 +26,7 @@ class Get extends Base protected function createOAuth2(string $callback): OAuth2 { - // See Authorize/Get.php -- Auth\OAuth2\Gitlab reads the endpoint out - // of a JSON-encoded appSecret. Token exchange is a server-to-server - // call, so this uses the API endpoint, not the browser one. + // Server-to-server token exchange, so the API endpoint, not the browser one. return new OAuth2Gitlab( System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), \json_encode([ From 7cb58cd612767dbded7b00f256068e8f873c5b2e Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 16 Jul 2026 12:58:11 +0530 Subject: [PATCH 03/19] Fix FactoryTest style: import classes, move env cleanup to tearDown The inline putenv() cleanup would leak _APP_VCS_TEST_ENDPOINT into later tests if an assertion above it failed. --- tests/unit/Vcs/FactoryTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index 01de5ef332..415df30a38 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -7,18 +7,21 @@ namespace Tests\Unit\Vcs; use Appwrite\Extend\Exception; use Appwrite\Vcs\Factory; use PHPUnit\Framework\TestCase; +use ReflectionProperty; use Utopia\Cache\Adapter\None; use Utopia\Cache\Cache; use Utopia\Config\Config; use Utopia\Database\Document; use Utopia\VCS\Adapter\Git; use Utopia\VCS\Adapter\Git\GitHub; +use Utopia\VCS\Adapter\Git\GitLab; final class FactoryTest extends TestCase { protected function tearDown(): void { \putenv('_APP_VCS_TEST_TOKEN'); + \putenv('_APP_VCS_TEST_ENDPOINT'); } public function testRegistryEntries(): void @@ -92,7 +95,7 @@ final class FactoryTest extends TestCase public function testFromProviderAppliesEndpointDefault(): void { $entry = [ - 'adapter' => \Utopia\VCS\Adapter\Git\GitLab::class, + 'adapter' => GitLab::class, 'variables' => [ 'endpoint' => ['required' => false, 'envVariable' => '_APP_VCS_TEST_ENDPOINT', 'default' => 'https://gitlab.com'], ], @@ -100,13 +103,12 @@ final class FactoryTest extends TestCase $factory = new Factory($this->cache(), ['gitlab' => $entry]); $adapter = $factory->fromProvider('gitlab'); - $property = new \ReflectionProperty($adapter, 'endpoint'); + $property = new ReflectionProperty($adapter, 'endpoint'); $this->assertSame('https://gitlab.com/api/v4', $property->getValue($adapter)); \putenv('_APP_VCS_TEST_ENDPOINT=https://gitlab.example.com'); $adapter = $factory->fromProvider('gitlab'); $this->assertSame('https://gitlab.example.com/api/v4', $property->getValue($adapter)); - \putenv('_APP_VCS_TEST_ENDPOINT'); } public function testGetWebhookSecret(): void From 3d99200d4a9c7c256e2978130200c3c8e49dfae3 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 16 Jul 2026 13:33:51 +0530 Subject: [PATCH 04/19] Fix GitLab webhook event routing: match on the real header values GitLab sends x-gitlab-event as 'Push Hook'/'Merge Request Hook', not 'push'/'pull_request' -- every real webhook was silently falling through to the default no-op. Caught by Greptile review. Blocked on utopia-php/vcs#119 (the field/action-name parity fix for GitLab::getEvent()) merging and releasing before this integration is fully correct end-to-end -- verified against the patched library locally, but composer.json still points at the published 4.4.2 since a personal-fork dependency can't ship in this PR. --- .../Platform/Modules/VCS/Http/Gitlab/Events/Create.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php index 11fe86bb66..21ac836d13 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php @@ -83,8 +83,8 @@ class Create extends Action $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), + 'Push Hook' => $this->handlePushEvent($parsedPayload, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), + 'Merge Request Hook' => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $installationTokens, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), default => null, }; From d98d92b63c196cf97e928c981d8965a5cd14e173 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 17 Jul 2026 13:52:16 +0530 Subject: [PATCH 05/19] Fix missing token refresh and GitLab OAuth2 support across VCS repository endpoints Repository list/get/branches/contents/detections endpoints never refreshed expired installation tokens before calling the provider, causing stale-token failures once personal access tokens expired. Centralize the refresh via InstallationTokens::refreshForInstallation(). Also add the missing GitLab branch in Repositories/Create.php's createOAuth2(), which only handled github/gitea and threw on gitlab installations. --- .../Repositories/Branches/XList.php | 4 +++ .../Repositories/Contents/Get.php | 4 +++ .../Installations/Repositories/Create.php | 10 +++++- .../Repositories/Detections/Create.php | 4 +++ .../Http/Installations/Repositories/Get.php | 4 +++ .../Http/Installations/Repositories/XList.php | 4 +++ src/Appwrite/Vcs/InstallationTokens.php | 32 +++++++++++++++++++ 7 files changed, 61 insertions(+), 1 deletion(-) 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 65ea728e3a..7216a0143e 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php @@ -10,6 +10,7 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Database\Validator\Queries\Branches; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory as VcsFactory; +use Appwrite\Vcs\InstallationTokens; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception\Query as QueryException; @@ -55,6 +56,7 @@ class XList extends Action ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('queries', [], new Branches(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit, offset, cursorAfter, and cursorBefore', true) ->inject('vcsFactory') + ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -66,6 +68,7 @@ class XList extends Action string $search, array $queries, VcsFactory $vcsFactory, + InstallationTokens $installationTokens, Response $response, Database $dbForPlatform ) { @@ -81,6 +84,7 @@ class XList extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); 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 cc189a1af9..b26e2069af 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 @@ -9,6 +9,7 @@ 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\Document; use Utopia\Platform\Scope\HTTP; @@ -51,6 +52,7 @@ class Get extends Action ->param('providerRootDirectory', '', new Text(256, 0), 'Path to get contents of nested directory', true) ->param('providerReference', '', new Text(256, 0), 'Git reference (branch, tag, commit) to get contents from', true) ->inject('vcsFactory') + ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -62,6 +64,7 @@ class Get extends Action string $providerRootDirectory, string $providerReference, VcsFactory $vcsFactory, + InstallationTokens $installationTokens, Response $response, Database $dbForPlatform ) { @@ -71,6 +74,7 @@ class Get extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); 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 56454b6457..e2e69711b2 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -4,6 +4,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\Auth\OAuth2\Gitlab as OAuth2Gitlab; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; use Appwrite\SDK\AuthType; @@ -136,7 +137,7 @@ class Create extends Action $response->dynamic(new Document($repository), Response::MODEL_PROVIDER_REPOSITORY); } - protected function createOAuth2(string $provider): OAuth2Github|OAuth2Gitea + protected function createOAuth2(string $provider): OAuth2Github|OAuth2Gitea|OAuth2Gitlab { if ($provider === 'github') { return new OAuth2Github(System::getEnv('_APP_VCS_GITHUB_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITHUB_CLIENT_SECRET', ''), ""); @@ -149,6 +150,13 @@ class Create extends Action return $oauth2; } + if ($provider === 'gitlab') { + return new OAuth2Gitlab(System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), \json_encode([ + 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), + 'endpoint' => System::getEnv('_APP_VCS_GITLAB_ENDPOINT', 'https://gitlab.com'), + ]), ""); + } + 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 a5518d988a..4d4a33cf8d 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 @@ -9,6 +9,7 @@ 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 Swoole\Coroutine\WaitGroup; use Utopia\Config\Adapters\Dotenv as ConfigDotenv; use Utopia\Config\Config; @@ -94,6 +95,7 @@ class Create extends Action ->param('type', '', new WhiteList(['runtime', 'framework']), 'Detector type. Must be one of the following: runtime, framework', enum: new Enum(name: 'VCSDetectionType')) ->param('providerRootDirectory', '', new Text(256, 0), 'Path to Root Directory', true) ->inject('vcsFactory') + ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -105,6 +107,7 @@ class Create extends Action string $type, string $providerRootDirectory, VcsFactory $vcsFactory, + InstallationTokens $installationTokens, Response $response, Database $dbForPlatform ) { @@ -114,6 +117,7 @@ class Create extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); 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 4c3514207e..bdbd86c927 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php @@ -9,6 +9,7 @@ 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\Document; use Utopia\Platform\Scope\HTTP; @@ -49,6 +50,7 @@ class Get extends Action ->param('installationId', '', new Text(256), 'Installation Id') ->param('providerRepositoryId', '', new Text(256), 'Repository Id') ->inject('vcsFactory') + ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -58,6 +60,7 @@ class Get extends Action string $installationId, string $providerRepositoryId, VcsFactory $vcsFactory, + InstallationTokens $installationTokens, Response $response, Database $dbForPlatform ) { @@ -67,6 +70,7 @@ class Get extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); 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 ab09ca6ce4..881aa70663 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php @@ -9,6 +9,7 @@ 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 Swoole\Coroutine\WaitGroup; use Utopia\Config\Adapters\Dotenv as ConfigDotenv; use Utopia\Config\Config; @@ -98,6 +99,7 @@ class XList extends Action ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) ->param('queries', [], new Queries([new Limit(), new Offset()]), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/queries). Only supported methods are limit and offset', true) ->inject('vcsFactory') + ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') ->callback($this->action(...)); @@ -109,6 +111,7 @@ class XList extends Action string $search, array $queries, VcsFactory $vcsFactory, + InstallationTokens $installationTokens, Response $response, Database $dbForPlatform ) { @@ -122,6 +125,7 @@ class XList extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php index 43fcb3088c..75547935ea 100644 --- a/src/Appwrite/Vcs/InstallationTokens.php +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -3,13 +3,45 @@ namespace Appwrite\Vcs; use Appwrite\Auth\OAuth2; +use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea; +use Appwrite\Auth\OAuth2\Github as OAuth2Github; +use Appwrite\Auth\OAuth2\Gitlab as OAuth2Gitlab; use Appwrite\Extend\Exception; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; +use Utopia\System\System; class InstallationTokens { + /** + * Refreshes an installation's token, resolving the right OAuth2 client + * for its provider. Centralizes the per-provider construction that used + * to be duplicated at every call site -- callers just pass the + * installation. A no-op for App-based installations (e.g. GitHub App), + * since those have no personalAccessTokenExpiry to begin with. + */ + public function refreshForInstallation(Document $installation, Database $dbForPlatform): Document + { + $provider = $installation->getAttribute('provider', 'github'); + + $oauth2 = match ($provider) { + 'github' => new OAuth2Github(System::getEnv('_APP_VCS_GITHUB_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITHUB_CLIENT_SECRET', ''), ''), + 'gitea' => (function () { + $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; + })(), + 'gitlab' => new OAuth2Gitlab(System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), \json_encode([ + 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), + 'endpoint' => System::getEnv('_APP_VCS_GITLAB_ENDPOINT', 'https://gitlab.com'), + ]), ''), + default => throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unsupported VCS provider: ' . $provider), + }; + + return $this->refresh($installation, $dbForPlatform, $oauth2); + } + public function refresh(Document $installation, Database $dbForPlatform, OAuth2 $oauth2, ?Document $identity = null): Document { $accessToken = $installation->getAttribute('personalAccessToken'); From b7418bf666b9f1d974ce762c63c310a02742b9a3 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 17 Jul 2026 16:15:15 +0530 Subject: [PATCH 06/19] Bump utopia-php/vcs to 4.4.3 Now that the GitLab getEvent()/auth-header/searchRepositories fixes (utopia-php/vcs#120) are released, unpin from the temporary fork reference and lock to the real published version. --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index 6e5bd2445d..0e5e89203f 100644 --- a/composer.lock +++ b/composer.lock @@ -5191,16 +5191,16 @@ }, { "name": "utopia-php/vcs", - "version": "4.4.2", + "version": "4.4.3", "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "ea623171caeaad2ef600b6c6bd52fd5f76b38486" + "reference": "15f6cd4781d15f3f165fded324788efb037260b9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/ea623171caeaad2ef600b6c6bd52fd5f76b38486", - "reference": "ea623171caeaad2ef600b6c6bd52fd5f76b38486", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/15f6cd4781d15f3f165fded324788efb037260b9", + "reference": "15f6cd4781d15f3f165fded324788efb037260b9", "shasum": "" }, "require": { @@ -5234,9 +5234,9 @@ ], "support": { "issues": "https://github.com/utopia-php/vcs/issues", - "source": "https://github.com/utopia-php/vcs/tree/4.4.2" + "source": "https://github.com/utopia-php/vcs/tree/4.4.3" }, - "time": "2026-07-14T06:22:41+00:00" + "time": "2026-07-17T10:39:05+00:00" }, { "name": "utopia-php/websocket", From 561f079b985b28ba45545c6a59f0c52724e37639 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 17 Jul 2026 16:23:46 +0530 Subject: [PATCH 07/19] refactor: reuse InstallationTokens::refreshForInstallation for GitLab events resolveAdapterForRepository() was manually rebuilding the GitLab OAuth2 client instead of using the refreshForInstallation() helper this same PR introduces to centralize that construction. Removes the now-unused OAuth2Gitlab/System imports. --- .../Modules/VCS/Http/Gitlab/Events/Create.php | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php index 21ac836d13..c2b6222749 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php @@ -2,7 +2,6 @@ namespace Appwrite\Platform\Modules\VCS\Http\Gitlab\Events; -use Appwrite\Auth\OAuth2\Gitlab as OAuth2Gitlab; use Appwrite\Event\Publisher\Build as BuildPublisher; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; @@ -18,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; class Create extends Action @@ -119,16 +117,7 @@ class Create extends Action } try { - $oauth2 = new OAuth2Gitlab( - System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), - \json_encode([ - 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), - 'endpoint' => System::getEnv('_APP_VCS_GITLAB_ENDPOINT', 'https://gitlab.com'), - ]), - '' - ); - - $installation = $installationTokens->refresh($installation, $dbForPlatform, $oauth2); + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); return $vcsFactory->fromInstallation($installation); } catch (\Throwable $error) { From 8a5a5dd0890a744921a044b4bec72a6b4ca9d24d Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 17 Jul 2026 16:31:59 +0530 Subject: [PATCH 08/19] fix: implement createRepository() on OAuth2\Gitlab Repositories/Create.php calls $oauth2->createRepository() on the personal-installation path -- exactly how GitLab installations work -- but OAuth2\Gitlab never defined it, causing a fatal error for any GitLab user creating a repository from the Console. Mirrors the Github/Gitea implementations, using GitLab's projects API with OAuth2 Bearer auth. --- src/Appwrite/Auth/OAuth2/Gitlab.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Appwrite/Auth/OAuth2/Gitlab.php b/src/Appwrite/Auth/OAuth2/Gitlab.php index 9ae4a9c214..370106073b 100644 --- a/src/Appwrite/Auth/OAuth2/Gitlab.php +++ b/src/Appwrite/Auth/OAuth2/Gitlab.php @@ -167,6 +167,25 @@ class Gitlab extends OAuth2 return $user['username'] ?? ''; } + /** + * @link https://docs.gitlab.com/ee/api/projects.html#create-project + * + * @param string $accessToken + * @param string $repositoryName + * @param bool $private + * + * @return array + */ + public function createRepository(string $accessToken, string $repositoryName, bool $private): array + { + $repository = $this->request('POST', $this->getEndpoint() . '/api/v4/projects', ['Authorization: Bearer ' . $accessToken, 'Content-Type: application/json'], \json_encode([ + 'name' => $repositoryName, + 'visibility' => $private ? 'private' : 'public', + ])); + + return \json_decode($repository, true) ?? []; + } + /** * @param string $accessToken * From a9ce64be2355875c402ab35e846d2af33fadb601 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 17 Jul 2026 16:48:30 +0530 Subject: [PATCH 09/19] style: trim over-long docblocks in InstallationTokens/Gitlab events --- .../Platform/Modules/VCS/Http/Gitlab/Events/Create.php | 8 ++------ src/Appwrite/Vcs/InstallationTokens.php | 6 +----- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php index c2b6222749..f85b4a6921 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php @@ -101,12 +101,8 @@ class Create extends Action } /** - * Resolves the adapter for a repository's installation, refreshing its - * OAuth2 token first. A null installation (not a GitLab 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 -- GitLab's webhook delivery - * gets a non-2xx response and the failure shows up in its redelivery log. + * A refresh/adapter failure is pushed onto $errors instead of swallowed, so the + * caller can surface a non-2xx response and GitLab logs it as a failed delivery. */ private function resolveAdapterForRepository(Document $repository, VcsFactory $vcsFactory, InstallationTokens $installationTokens, Database $dbForPlatform, Authorization $authorization, array &$errors): ?Git { diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php index 75547935ea..f0253a711e 100644 --- a/src/Appwrite/Vcs/InstallationTokens.php +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -15,11 +15,7 @@ use Utopia\System\System; class InstallationTokens { /** - * Refreshes an installation's token, resolving the right OAuth2 client - * for its provider. Centralizes the per-provider construction that used - * to be duplicated at every call site -- callers just pass the - * installation. A no-op for App-based installations (e.g. GitHub App), - * since those have no personalAccessTokenExpiry to begin with. + * Refreshes an installation's token, resolving the OAuth2 client for its provider. */ public function refreshForInstallation(Document $installation, Database $dbForPlatform): Document { From 05350b183c62e034eeddae2f0adaf30ffb6d90c0 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 17 Jul 2026 17:11:28 +0530 Subject: [PATCH 10/19] fix: normalize GitLab's createRepository response field names GitLab's projects API returns visibility ('private'/'public') and last_activity_at instead of GitHub/Gitea's private (bool) and pushed_at, so every private repo created via the Console reported private: false and an empty pushedAt in the ProviderRepository response. Map both fields to match the shape Create.php already expects from GitHub/Gitea. --- src/Appwrite/Auth/OAuth2/Gitlab.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Auth/OAuth2/Gitlab.php b/src/Appwrite/Auth/OAuth2/Gitlab.php index 370106073b..604857a07c 100644 --- a/src/Appwrite/Auth/OAuth2/Gitlab.php +++ b/src/Appwrite/Auth/OAuth2/Gitlab.php @@ -183,7 +183,18 @@ class Gitlab extends OAuth2 'visibility' => $private ? 'private' : 'public', ])); - return \json_decode($repository, true) ?? []; + $repository = \json_decode($repository, true) ?? []; + + // Normalize to the GitHub/Gitea field shape ProviderRepository expects. + if (isset($repository['visibility'])) { + $repository['private'] = $repository['visibility'] !== 'public'; + } + + if (isset($repository['last_activity_at'])) { + $repository['pushed_at'] = $repository['last_activity_at']; + } + + return $repository; } /** From b9c1174d24cc20800111861322e0db17de91923b Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 17 Jul 2026 17:27:09 +0530 Subject: [PATCH 11/19] fix: flatten GitLab's nested validation error messages GitLab's error response nests per-field messages (e.g. {"name": ["has already been taken"]}) instead of GitHub/Gitea's plain string, which Create.php string-concatenates directly -- producing "Provider Error: Array" instead of a useful message. --- src/Appwrite/Auth/OAuth2/Gitlab.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Appwrite/Auth/OAuth2/Gitlab.php b/src/Appwrite/Auth/OAuth2/Gitlab.php index 604857a07c..e18288abb5 100644 --- a/src/Appwrite/Auth/OAuth2/Gitlab.php +++ b/src/Appwrite/Auth/OAuth2/Gitlab.php @@ -194,9 +194,30 @@ class Gitlab extends OAuth2 $repository['pushed_at'] = $repository['last_activity_at']; } + // Validation errors nest per-field messages (e.g. {"name": ["has already been taken"]}) + // instead of GitHub/Gitea's plain string, which Create.php string-concatenates as-is. + if (isset($repository['message']) && !\is_string($repository['message'])) { + $repository['message'] = $this->flattenErrorMessage($repository['message']); + } + return $repository; } + private function flattenErrorMessage(mixed $message): string + { + if (!\is_array($message)) { + return \strval($message); + } + + $parts = []; + foreach ($message as $field => $errors) { + $errors = \is_array($errors) ? \implode(', ', $errors) : $errors; + $parts[] = \is_string($field) ? "{$field} {$errors}" : $errors; + } + + return \implode('; ', $parts); + } + /** * @param string $accessToken * From c72d2771219e82c7a59afa427879b0f94ec92089 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 17 Jul 2026 17:29:36 +0530 Subject: [PATCH 12/19] simplify: replace flattenErrorMessage() with a plain json_encode The 12-line field-flattening helper was over-engineering a display-only edge case -- the actual bug was just the literal string "Array" showing up in error messages. json_encode fixes that in one line without needing pretty prose. --- src/Appwrite/Auth/OAuth2/Gitlab.php | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/src/Appwrite/Auth/OAuth2/Gitlab.php b/src/Appwrite/Auth/OAuth2/Gitlab.php index e18288abb5..41101fce35 100644 --- a/src/Appwrite/Auth/OAuth2/Gitlab.php +++ b/src/Appwrite/Auth/OAuth2/Gitlab.php @@ -194,30 +194,15 @@ class Gitlab extends OAuth2 $repository['pushed_at'] = $repository['last_activity_at']; } - // Validation errors nest per-field messages (e.g. {"name": ["has already been taken"]}) - // instead of GitHub/Gitea's plain string, which Create.php string-concatenates as-is. + // Validation errors nest per-field messages instead of GitHub/Gitea's plain + // string, which Create.php string-concatenates as-is. if (isset($repository['message']) && !\is_string($repository['message'])) { - $repository['message'] = $this->flattenErrorMessage($repository['message']); + $repository['message'] = \json_encode($repository['message']); } return $repository; } - private function flattenErrorMessage(mixed $message): string - { - if (!\is_array($message)) { - return \strval($message); - } - - $parts = []; - foreach ($message as $field => $errors) { - $errors = \is_array($errors) ? \implode(', ', $errors) : $errors; - $parts[] = \is_string($field) ? "{$field} {$errors}" : $errors; - } - - return \implode('; ', $parts); - } - /** * @param string $accessToken * From 5434b2a7413c45b8a57f0285623a8dfe42fb7d46 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Fri, 17 Jul 2026 17:30:43 +0530 Subject: [PATCH 13/19] style: remove redundant comment in Gitlab::createRepository --- src/Appwrite/Auth/OAuth2/Gitlab.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Appwrite/Auth/OAuth2/Gitlab.php b/src/Appwrite/Auth/OAuth2/Gitlab.php index 41101fce35..2c643d4f88 100644 --- a/src/Appwrite/Auth/OAuth2/Gitlab.php +++ b/src/Appwrite/Auth/OAuth2/Gitlab.php @@ -194,8 +194,6 @@ class Gitlab extends OAuth2 $repository['pushed_at'] = $repository['last_activity_at']; } - // Validation errors nest per-field messages instead of GitHub/Gitea's plain - // string, which Create.php string-concatenates as-is. if (isset($repository['message']) && !\is_string($repository['message'])) { $repository['message'] = \json_encode($repository['message']); } From 3a0421df58f276fbff34d583fcd5607db220eeb1 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 20 Jul 2026 18:06:55 +0530 Subject: [PATCH 14/19] Remove redundant Gitea/GitLab OAuth mock callback endpoints Unlike GitHub App installs (which need a mock since github.com can't be run locally), Gitea and GitLab are self-hostable and already run as real containers in CI, so E2E tests can exercise the actual /v1/vcs/{gitea,gitlab}/callback endpoints directly instead of a hand-written substitute that could drift from the real logic. --- app/controllers/mock.php | 112 --------------------------------------- 1 file changed, 112 deletions(-) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index c74b6582da..1c71406926 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -295,118 +295,6 @@ 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::get('/v1/mock/gitlab/callback') - ->desc('Create installation document using GitLab OAuth2 tokens') - ->groups(['mock', 'api', 'vcs']) - ->label('scope', 'public') - ->label('docs', false) - ->param('projectId', '', new UID(), 'Project ID of the project where GitLab is to be installed') - ->param('gitlabUserId', '', new Text(256), 'GitLab user ID') - ->param('organization', '', new Text(256), 'GitLab username or namespace') - ->param('accessToken', '', new Text(2048), 'GitLab access token') - ->inject('response') - ->inject('dbForPlatform') - ->action(function (string $projectId, string $gitlabUserId, 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($gitlabUserId) || empty($organization) || empty($accessToken)) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing GitLab 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' => $gitlabUserId, - 'projectId' => $projectId, - 'projectInternalId' => $projectInternalId, - 'provider' => 'gitlab', - 'organization' => $organization, - 'personal' => true, - 'personalAccessToken' => $accessToken, - 'personalRefreshToken' => '', - 'personalAccessTokenExpiry' => null, - ])); - - $response->json([ - 'installationId' => $installation->getId(), - ]); - }); - Http::shutdown() ->groups(['mock']) ->inject('response') From 7139d88ad383fe5c0422aeac5edd27404bce2d53 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 21 Jul 2026 11:13:48 +0530 Subject: [PATCH 15/19] Replace per-provider OAuth2 conditionals with registry-driven polymorphism Create.php's createOAuth2() and InstallationTokens::refreshForInstallation() each had a match/if chain constructing the right OAuth2 client per provider -- easy to forget to update when adding a provider, and the exact "replace conditional with polymorphism" smell. Each OAuth2 client now implements a new EnvOAuth2 interface (fromEnv()) and is registered in the vcs.php registry alongside its adapter; Factory::oauth2FromProvider() resolves it generically, so adding a provider is a registry entry + an interface implementation, not a new branch in two call sites. Also drop self-hosted GitLab configurability (_APP_VCS_GITLAB_ENDPOINT, _APP_VCS_GITLAB_BROWSER_ENDPOINT): only official gitlab.com is supported, so the endpoint is now a fixed value in the registry and in OAuth2\Gitlab, instead of an env-configurable value with a fallback default. --- .env | 2 - app/config/variables.php | 18 -------- app/config/vcs.php | 10 +++- docker-compose.yml | 4 -- src/Appwrite/Auth/OAuth2/Gitea.php | 12 ++++- src/Appwrite/Auth/OAuth2/Github.php | 9 +++- src/Appwrite/Auth/OAuth2/Gitlab.php | 44 +++++++----------- .../Modules/VCS/Http/Gitea/Events/Create.php | 19 ++------ .../Modules/VCS/Http/Gitlab/Authorize/Get.php | 8 +--- .../Modules/VCS/Http/Gitlab/Callback/Get.php | 6 +-- .../Modules/VCS/Http/Gitlab/Events/Create.php | 2 +- .../Repositories/Branches/XList.php | 2 +- .../Repositories/Contents/Get.php | 2 +- .../Installations/Repositories/Create.php | 29 +----------- .../Repositories/Detections/Create.php | 2 +- .../Http/Installations/Repositories/Get.php | 2 +- .../Http/Installations/Repositories/XList.php | 2 +- src/Appwrite/Vcs/EnvOAuth2.php | 18 ++++++++ src/Appwrite/Vcs/Factory.php | 14 +++++- src/Appwrite/Vcs/InstallationTokens.php | 22 +-------- tests/unit/Vcs/FactoryTest.php | 46 +++++++++++++++++-- 21 files changed, 129 insertions(+), 144 deletions(-) create mode 100644 src/Appwrite/Vcs/EnvOAuth2.php diff --git a/.env b/.env index 503a74dfd7..b48a023df1 100644 --- a/.env +++ b/.env @@ -160,8 +160,6 @@ _APP_VCS_GITEA_BROWSER_ENDPOINT= _APP_VCS_GITEA_CLIENT_ID= _APP_VCS_GITEA_CLIENT_SECRET= _APP_VCS_GITEA_WEBHOOK_SECRET= -_APP_VCS_GITLAB_ENDPOINT= -_APP_VCS_GITLAB_BROWSER_ENDPOINT= _APP_VCS_GITLAB_CLIENT_ID= _APP_VCS_GITLAB_CLIENT_SECRET= _APP_VCS_GITLAB_WEBHOOK_SECRET= diff --git a/app/config/variables.php b/app/config/variables.php index 1adb2bd8b3..e25a0f8fd2 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -1311,24 +1311,6 @@ return [ 'question' => '', 'filter' => '' ], - [ - 'name' => '_APP_VCS_GITLAB_ENDPOINT', - 'description' => 'URL of your self-hosted GitLab instance, reachable from the Appwrite server (e.g. for API calls and webhook token exchange). Defaults to https://gitlab.com when unset.', - 'introduction' => '2.0.0', - 'default' => '', - 'required' => false, - 'question' => '', - 'filter' => '' - ], - [ - 'name' => '_APP_VCS_GITLAB_BROWSER_ENDPOINT', - 'description' => 'URL of your GitLab instance reachable from the browser, if different from _APP_VCS_GITLAB_ENDPOINT (e.g. when Appwrite and GitLab are on the same Docker network but GitLab is exposed to the browser on a different host). Falls back to _APP_VCS_GITLAB_ENDPOINT when unset.', - 'introduction' => '2.0.0', - 'default' => '', - 'required' => false, - 'question' => '', - 'filter' => '' - ], [ 'name' => '_APP_VCS_GITLAB_CLIENT_ID', 'description' => 'GitLab OAuth2 application client ID. You can generate one in your GitLab instance under Settings > Applications.', diff --git a/app/config/vcs.php b/app/config/vcs.php index 6df3f92c74..3f8a266ea4 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -4,6 +4,9 @@ * VCS provider registry, read by Appwrite\Vcs\Factory. */ +use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea; +use Appwrite\Auth\OAuth2\Github as OAuth2Github; +use Appwrite\Auth\OAuth2\Gitlab as OAuth2Gitlab; use Utopia\VCS\Adapter\Git\Gitea; use Utopia\VCS\Adapter\Git\GitHub; use Utopia\VCS\Adapter\Git\GitLab; @@ -11,6 +14,7 @@ use Utopia\VCS\Adapter\Git\GitLab; return [ 'github' => [ 'adapter' => GitHub::class, + 'oauth2' => OAuth2Github::class, 'variables' => [ 'appName' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_NAME'], 'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_PRIVATE_KEY'], @@ -22,6 +26,7 @@ return [ ], 'gitea' => [ 'adapter' => Gitea::class, + 'oauth2' => OAuth2Gitea::class, 'variables' => [ 'endpoint' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_ENDPOINT'], 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_CLIENT_ID'], @@ -33,9 +38,10 @@ return [ ], 'gitlab' => [ 'adapter' => GitLab::class, + 'oauth2' => OAuth2Gitlab::class, + // Only official gitlab.com is supported -- fixed, not configurable. + 'endpoint' => 'https://gitlab.com', 'variables' => [ - // Unset defaults to gitlab.com; self-hosted instances override this. - 'endpoint' => ['required' => false, 'envVariable' => '_APP_VCS_GITLAB_ENDPOINT', 'default' => 'https://gitlab.com'], 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_CLIENT_ID'], 'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_CLIENT_SECRET'], 'webhookSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_WEBHOOK_SECRET'], diff --git a/docker-compose.yml b/docker-compose.yml index 90f511f949..a5ffd34ac6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -203,8 +203,6 @@ services: - _APP_VCS_GITEA_CLIENT_ID - _APP_VCS_GITEA_CLIENT_SECRET - _APP_VCS_GITEA_WEBHOOK_SECRET - - _APP_VCS_GITLAB_ENDPOINT - - _APP_VCS_GITLAB_BROWSER_ENDPOINT - _APP_VCS_GITLAB_CLIENT_ID - _APP_VCS_GITLAB_CLIENT_SECRET - _APP_VCS_GITLAB_WEBHOOK_SECRET @@ -498,8 +496,6 @@ services: - _APP_VCS_GITEA_CLIENT_ID - _APP_VCS_GITEA_CLIENT_SECRET - _APP_VCS_GITEA_WEBHOOK_SECRET - - _APP_VCS_GITLAB_ENDPOINT - - _APP_VCS_GITLAB_BROWSER_ENDPOINT - _APP_VCS_GITLAB_CLIENT_ID - _APP_VCS_GITLAB_CLIENT_SECRET - _APP_VCS_GITLAB_WEBHOOK_SECRET diff --git a/src/Appwrite/Auth/OAuth2/Gitea.php b/src/Appwrite/Auth/OAuth2/Gitea.php index 390189fc72..1b73817bbd 100644 --- a/src/Appwrite/Auth/OAuth2/Gitea.php +++ b/src/Appwrite/Auth/OAuth2/Gitea.php @@ -3,8 +3,10 @@ namespace Appwrite\Auth\OAuth2; use Appwrite\Auth\OAuth2; +use Appwrite\Vcs\EnvOAuth2; +use Utopia\System\System; -class Gitea extends OAuth2 +class Gitea extends OAuth2 implements EnvOAuth2 { protected string $endpoint = ''; @@ -16,6 +18,14 @@ class Gitea extends OAuth2 'read:user', ]; + public static function fromEnv(): OAuth2&EnvOAuth2 + { + $oauth2 = new self(System::getEnv('_APP_VCS_GITEA_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITEA_CLIENT_SECRET', ''), ''); + $oauth2->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', '')); + + return $oauth2; + } + public function setEndpoint(string $endpoint): void { $this->endpoint = \rtrim($endpoint, '/'); diff --git a/src/Appwrite/Auth/OAuth2/Github.php b/src/Appwrite/Auth/OAuth2/Github.php index f80c067daa..b3be5243a7 100644 --- a/src/Appwrite/Auth/OAuth2/Github.php +++ b/src/Appwrite/Auth/OAuth2/Github.php @@ -3,10 +3,17 @@ namespace Appwrite\Auth\OAuth2; use Appwrite\Auth\OAuth2; +use Appwrite\Vcs\EnvOAuth2; use Utopia\Fetch\Client as FetchClient; +use Utopia\System\System; -class Github extends OAuth2 +class Github extends OAuth2 implements EnvOAuth2 { + public static function fromEnv(): OAuth2&EnvOAuth2 + { + return new self(System::getEnv('_APP_VCS_GITHUB_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITHUB_CLIENT_SECRET', ''), ''); + } + /** * @var array */ diff --git a/src/Appwrite/Auth/OAuth2/Gitlab.php b/src/Appwrite/Auth/OAuth2/Gitlab.php index 2c643d4f88..0c3ca85b0c 100644 --- a/src/Appwrite/Auth/OAuth2/Gitlab.php +++ b/src/Appwrite/Auth/OAuth2/Gitlab.php @@ -3,12 +3,24 @@ namespace Appwrite\Auth\OAuth2; use Appwrite\Auth\OAuth2; +use Appwrite\Vcs\EnvOAuth2; +use Utopia\System\System; // Reference Material // https://docs.gitlab.com/ee/api/oauth2.html -class Gitlab extends OAuth2 +class Gitlab extends OAuth2 implements EnvOAuth2 { + /** + * Only official gitlab.com is supported -- fixed, not configurable. + */ + protected const ENDPOINT = 'https://gitlab.com'; + + public static function fromEnv(): OAuth2&EnvOAuth2 + { + return new self(System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), ''); + } + /** * @var array */ @@ -61,7 +73,7 @@ class Gitlab extends OAuth2 $this->getEndpoint() . '/oauth/token?' . \http_build_query([ 'code' => $code, 'client_id' => $this->appID, - 'client_secret' => $this->getAppSecret()['clientSecret'], + 'client_secret' => $this->appSecret, 'redirect_uri' => $this->callback, 'grant_type' => 'authorization_code' ]) @@ -83,7 +95,7 @@ class Gitlab extends OAuth2 $this->getEndpoint() . '/oauth/token?' . \http_build_query([ 'refresh_token' => $refreshToken, 'client_id' => $this->appID, - 'client_secret' => $this->getAppSecret()['clientSecret'], + 'client_secret' => $this->appSecret, 'grant_type' => 'refresh_token' ]) ), true); @@ -216,32 +228,8 @@ class Gitlab extends OAuth2 return $this->user; } - /** - * Decode the JSON stored in appSecret - * - * @return array - */ - protected function getAppSecret(): array - { - try { - $secret = \json_decode($this->appSecret, true, 512, JSON_THROW_ON_ERROR); - } catch (\Throwable $th) { - throw new \Exception('Invalid secret'); - } - return $secret; - } - - - /** - * Extracts the Tenant Id from the JSON stored in appSecret. Defaults to 'common' as a fallback - * - * @return string - */ protected function getEndpoint(): string { - $defaultEndpoint = 'https://gitlab.com'; - $secret = $this->getAppSecret(); - $endpoint = $secret['endpoint'] ?? $defaultEndpoint; - return empty($endpoint) ? $defaultEndpoint : $endpoint; + return self::ENDPOINT; } } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php index 10ec1bbcb6..f7be6cd521 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitea/Events/Create.php @@ -2,7 +2,6 @@ namespace Appwrite\Platform\Modules\VCS\Http\Gitea\Events; -use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea; use Appwrite\Event\Publisher\Build as BuildPublisher; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; @@ -18,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; class Create extends Action @@ -103,12 +101,8 @@ class Create extends Action } /** - * 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. + * A refresh/adapter failure is pushed onto $errors instead of swallowed, so the + * caller can surface a non-2xx response and Gitea logs it as a failed delivery. */ private function resolveAdapterForRepository(Document $repository, VcsFactory $vcsFactory, InstallationTokens $installationTokens, Database $dbForPlatform, Authorization $authorization, array &$errors): ?Git { @@ -119,14 +113,7 @@ class Create extends Action } try { - $oauth2 = new OAuth2Gitea( - System::getEnv('_APP_VCS_GITEA_CLIENT_ID', ''), - System::getEnv('_APP_VCS_GITEA_CLIENT_SECRET', ''), - '' - ); - $oauth2->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', '')); - - $installation = $installationTokens->refresh($installation, $dbForPlatform, $oauth2); + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); return $vcsFactory->fromInstallation($installation); } catch (\Throwable $error) { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php index e6a4daeb8c..e8557a07d6 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php @@ -26,15 +26,9 @@ class Get extends Base protected function createOAuth2(string $callback, array $state): OAuth2 { - // Auth\OAuth2\Gitlab reads the endpoint out of a JSON-encoded appSecret; no setEndpoint(). - $browserEndpoint = System::getEnv('_APP_VCS_GITLAB_BROWSER_ENDPOINT', System::getEnv('_APP_VCS_GITLAB_ENDPOINT', 'https://gitlab.com')); - return new OAuth2Gitlab( System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), - \json_encode([ - 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), - 'endpoint' => $browserEndpoint, - ]), + System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), $callback, $state, // api is required for webhook/merge-request-note writes; no finer-grained scope covers both. diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php index ed161880ec..2df933a0ac 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php @@ -26,13 +26,9 @@ class Get extends Base protected function createOAuth2(string $callback): OAuth2 { - // Server-to-server token exchange, so the API endpoint, not the browser one. return new OAuth2Gitlab( System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), - \json_encode([ - 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), - 'endpoint' => System::getEnv('_APP_VCS_GITLAB_ENDPOINT', 'https://gitlab.com'), - ]), + System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), $callback ); } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php index f85b4a6921..4722b61698 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php @@ -113,7 +113,7 @@ class Create extends Action } try { - $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); return $vcsFactory->fromInstallation($installation); } catch (\Throwable $error) { 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 7216a0143e..58261a78e5 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 @@ -84,7 +84,7 @@ class XList extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); 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 b26e2069af..89e50559c3 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 @@ -74,7 +74,7 @@ class Get extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); 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 e2e69711b2..10d46bc80a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -2,9 +2,6 @@ namespace Appwrite\Platform\Modules\VCS\Http\Installations\Repositories; -use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea; -use Appwrite\Auth\OAuth2\Github as OAuth2Github; -use Appwrite\Auth\OAuth2\Gitlab as OAuth2Gitlab; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; use Appwrite\SDK\AuthType; @@ -17,7 +14,6 @@ use Utopia\Database\Database; 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; @@ -82,7 +78,7 @@ class Create extends Action $provider = $installation->getAttribute('provider', 'github'); if ($installation->getAttribute('personal', false) === true) { - $oauth2 = $this->createOAuth2($provider); + $oauth2 = $vcsFactory->oauth2FromProvider($provider); $identity = null; if ($provider === 'github' && (empty($installation->getAttribute('personalAccessToken')) || empty($installation->getAttribute('personalRefreshToken')) || empty($installation->getAttribute('personalAccessTokenExpiry')))) { @@ -136,27 +132,4 @@ class Create extends Action $response->dynamic(new Document($repository), Response::MODEL_PROVIDER_REPOSITORY); } - - protected function createOAuth2(string $provider): OAuth2Github|OAuth2Gitea|OAuth2Gitlab - { - 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; - } - - if ($provider === 'gitlab') { - return new OAuth2Gitlab(System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), \json_encode([ - 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), - 'endpoint' => System::getEnv('_APP_VCS_GITLAB_ENDPOINT', 'https://gitlab.com'), - ]), ""); - } - - 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 4d4a33cf8d..6006e96aae 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 @@ -117,7 +117,7 @@ class Create extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); 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 bdbd86c927..e7f340bb19 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php @@ -70,7 +70,7 @@ class Get extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); 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 881aa70663..75e7dfab7a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php @@ -125,7 +125,7 @@ class XList extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform); + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Vcs/EnvOAuth2.php b/src/Appwrite/Vcs/EnvOAuth2.php new file mode 100644 index 0000000000..6f11df33bc --- /dev/null +++ b/src/Appwrite/Vcs/EnvOAuth2.php @@ -0,0 +1,18 @@ +registry[$key]['adapter'])($this->cache); - $endpoint = $this->getEnv($key, 'endpoint') ?: ($this->registry[$key]['variables']['endpoint']['default'] ?? ''); + $endpoint = $this->registry[$key]['endpoint'] ?? $this->getEnv($key, 'endpoint'); if (!empty($endpoint) && \method_exists($adapter, 'setEndpoint')) { $adapter->setEndpoint(\rtrim($endpoint, '/')); } @@ -94,6 +95,17 @@ class Factory return $this->getEnv($key, 'webhookSecret'); } + public function oauth2FromProvider(string $key): OAuth2&EnvOAuth2 + { + $class = $this->registry[$key]['oauth2'] ?? null; + + if ($class === null || !\is_a($class, EnvOAuth2::class, true)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unsupported VCS provider: ' . $key); + } + + return $class::fromEnv(); + } + protected function getEnv(string $key, string $name): string { $variable = $this->registry[$key]['variables'][$name]['envVariable'] ?? ''; diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php index f0253a711e..974ee2260f 100644 --- a/src/Appwrite/Vcs/InstallationTokens.php +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -3,39 +3,21 @@ namespace Appwrite\Vcs; use Appwrite\Auth\OAuth2; -use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea; -use Appwrite\Auth\OAuth2\Github as OAuth2Github; -use Appwrite\Auth\OAuth2\Gitlab as OAuth2Gitlab; use Appwrite\Extend\Exception; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; -use Utopia\System\System; class InstallationTokens { /** * Refreshes an installation's token, resolving the OAuth2 client for its provider. */ - public function refreshForInstallation(Document $installation, Database $dbForPlatform): Document + public function refreshForInstallation(Document $installation, Database $dbForPlatform, Factory $vcsFactory): Document { $provider = $installation->getAttribute('provider', 'github'); - $oauth2 = match ($provider) { - 'github' => new OAuth2Github(System::getEnv('_APP_VCS_GITHUB_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITHUB_CLIENT_SECRET', ''), ''), - 'gitea' => (function () { - $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; - })(), - 'gitlab' => new OAuth2Gitlab(System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), \json_encode([ - 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), - 'endpoint' => System::getEnv('_APP_VCS_GITLAB_ENDPOINT', 'https://gitlab.com'), - ]), ''), - default => throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unsupported VCS provider: ' . $provider), - }; - - return $this->refresh($installation, $dbForPlatform, $oauth2); + return $this->refresh($installation, $dbForPlatform, $vcsFactory->oauth2FromProvider($provider)); } public function refresh(Document $installation, Database $dbForPlatform, OAuth2 $oauth2, ?Document $identity = null): Document diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index 415df30a38..578cd9d95c 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -4,7 +4,9 @@ declare(strict_types=1); namespace Tests\Unit\Vcs; +use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Appwrite\Extend\Exception; +use Appwrite\Vcs\EnvOAuth2; use Appwrite\Vcs\Factory; use PHPUnit\Framework\TestCase; use ReflectionProperty; @@ -31,6 +33,7 @@ final class FactoryTest extends TestCase foreach ($registry as $key => $entry) { $this->assertTrue(\is_subclass_of($entry['adapter'], Git::class), "Adapter for '{$key}' must extend Git"); + $this->assertTrue(\is_a($entry['oauth2'], EnvOAuth2::class, true), "OAuth2 client for '{$key}' must implement EnvOAuth2"); $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}'"); @@ -38,6 +41,28 @@ final class FactoryTest extends TestCase } } + public function testOauth2FromProviderUnknownThrows(): void + { + $factory = new Factory($this->cache(), ['github' => $this->githubEntry()]); + + $this->expectException(Exception::class); + $factory->oauth2FromProvider('bitbucket'); + } + + public function testOauth2FromProviderBuildsClient(): void + { + \putenv('_APP_VCS_GITHUB_CLIENT_ID=client-id'); + \putenv('_APP_VCS_GITHUB_CLIENT_SECRET=client-secret'); + + $factory = new Factory($this->cache(), ['github' => $this->githubEntry()]); + $oauth2 = $factory->oauth2FromProvider('github'); + + $this->assertInstanceOf(OAuth2Github::class, $oauth2); + + \putenv('_APP_VCS_GITHUB_CLIENT_ID'); + \putenv('_APP_VCS_GITHUB_CLIENT_SECRET'); + } + public function testFromProviderUnknownThrows(): void { $factory = new Factory($this->cache(), ['github' => $this->githubEntry()]); @@ -92,23 +117,33 @@ final class FactoryTest extends TestCase $this->assertSame(['test'], $factory->getProviders()); } - public function testFromProviderAppliesEndpointDefault(): void + public function testFromProviderAppliesFixedEndpoint(): void { $entry = [ 'adapter' => GitLab::class, - 'variables' => [ - 'endpoint' => ['required' => false, 'envVariable' => '_APP_VCS_TEST_ENDPOINT', 'default' => 'https://gitlab.com'], - ], + 'endpoint' => 'https://gitlab.com', + 'variables' => [], ]; $factory = new Factory($this->cache(), ['gitlab' => $entry]); $adapter = $factory->fromProvider('gitlab'); $property = new ReflectionProperty($adapter, 'endpoint'); $this->assertSame('https://gitlab.com/api/v4', $property->getValue($adapter)); + } + + public function testFromProviderFixedEndpointIgnoresEnv(): void + { + $entry = [ + 'adapter' => GitLab::class, + 'endpoint' => 'https://gitlab.com', + 'variables' => [], + ]; + $factory = new Factory($this->cache(), ['gitlab' => $entry]); \putenv('_APP_VCS_TEST_ENDPOINT=https://gitlab.example.com'); $adapter = $factory->fromProvider('gitlab'); - $this->assertSame('https://gitlab.example.com/api/v4', $property->getValue($adapter)); + $property = new ReflectionProperty($adapter, 'endpoint'); + $this->assertSame('https://gitlab.com/api/v4', $property->getValue($adapter)); } public function testGetWebhookSecret(): void @@ -134,6 +169,7 @@ final class FactoryTest extends TestCase { return [ 'adapter' => GitHub::class, + 'oauth2' => OAuth2Github::class, 'variables' => [ 'appName' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_NAME'], 'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_PRIVATE_KEY'], From d4fd89a2a4531ed285beb9d34ef276231a6b6776 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 21 Jul 2026 11:25:33 +0530 Subject: [PATCH 16/19] Fix regression: restore JSON secret format shared with account-login GitLab OAuth2 Appwrite\Auth\OAuth2\Gitlab is shared by the VCS integration and the pre-existing "Sign in with GitLab" account-login provider, which persists its secret as JSON ({"clientSecret": "...", "endpoint": "..."}) to support a per-project custom endpoint. My earlier simplification (hardcoding the endpoint, treating appSecret as a plain string) broke that unrelated feature -- every login/refresh would have sent a raw JSON blob as client_secret to GitLab. Restores getAppSecret()/getEndpoint() to their original JSON-decoding behavior; the VCS flow's fromEnv() and the two Authorize/Callback call sites now encode to the same JSON shape (fixed to https://gitlab.com, since VCS doesn't support self-hosted GitLab) instead of changing the shared class's contract. --- src/Appwrite/Auth/OAuth2/Gitlab.php | 44 ++++++++++++++++--- .../Modules/VCS/Http/Gitlab/Authorize/Get.php | 6 ++- .../Modules/VCS/Http/Gitlab/Callback/Get.php | 5 ++- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/Appwrite/Auth/OAuth2/Gitlab.php b/src/Appwrite/Auth/OAuth2/Gitlab.php index 0c3ca85b0c..9e8c89e51e 100644 --- a/src/Appwrite/Auth/OAuth2/Gitlab.php +++ b/src/Appwrite/Auth/OAuth2/Gitlab.php @@ -12,13 +12,19 @@ use Utopia\System\System; class Gitlab extends OAuth2 implements EnvOAuth2 { /** - * Only official gitlab.com is supported -- fixed, not configurable. + * This class is shared with the "Sign in with GitLab" account-login + * OAuth2 provider, which stores its secret as JSON + * ({"clientSecret": "...", "endpoint": "..."}) to support self-hosted + * GitLab per-project. The VCS flow only supports official gitlab.com, + * but still encodes to that same JSON shape so getAppSecret()/getEndpoint() + * stay correct for both consumers. */ - protected const ENDPOINT = 'https://gitlab.com'; - public static function fromEnv(): OAuth2&EnvOAuth2 { - return new self(System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), ''); + return new self(System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), \json_encode([ + 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), + 'endpoint' => 'https://gitlab.com', + ]), ''); } /** @@ -73,7 +79,7 @@ class Gitlab extends OAuth2 implements EnvOAuth2 $this->getEndpoint() . '/oauth/token?' . \http_build_query([ 'code' => $code, 'client_id' => $this->appID, - 'client_secret' => $this->appSecret, + 'client_secret' => $this->getAppSecret()['clientSecret'], 'redirect_uri' => $this->callback, 'grant_type' => 'authorization_code' ]) @@ -95,7 +101,7 @@ class Gitlab extends OAuth2 implements EnvOAuth2 $this->getEndpoint() . '/oauth/token?' . \http_build_query([ 'refresh_token' => $refreshToken, 'client_id' => $this->appID, - 'client_secret' => $this->appSecret, + 'client_secret' => $this->getAppSecret()['clientSecret'], 'grant_type' => 'refresh_token' ]) ), true); @@ -228,8 +234,32 @@ class Gitlab extends OAuth2 implements EnvOAuth2 return $this->user; } + /** + * Decode the JSON stored in appSecret + * + * @return array + */ + protected function getAppSecret(): array + { + try { + $secret = \json_decode($this->appSecret, true, 512, JSON_THROW_ON_ERROR); + } catch (\Throwable $th) { + throw new \Exception('Invalid secret'); + } + return $secret; + } + + /** + * Extracts the endpoint from the JSON stored in appSecret. Defaults to + * gitlab.com as a fallback. + * + * @return string + */ protected function getEndpoint(): string { - return self::ENDPOINT; + $defaultEndpoint = 'https://gitlab.com'; + $secret = $this->getAppSecret(); + $endpoint = $secret['endpoint'] ?? $defaultEndpoint; + return empty($endpoint) ? $defaultEndpoint : $endpoint; } } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php index e8557a07d6..34e8dd71b0 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Authorize/Get.php @@ -26,9 +26,13 @@ class Get extends Base protected function createOAuth2(string $callback, array $state): OAuth2 { + // Auth\OAuth2\Gitlab reads the endpoint out of a JSON-encoded appSecret; no setEndpoint(). return new OAuth2Gitlab( System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), - System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), + \json_encode([ + 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), + 'endpoint' => 'https://gitlab.com', + ]), $callback, $state, // api is required for webhook/merge-request-note writes; no finer-grained scope covers both. diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php index 2df933a0ac..6d86b25fa1 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Callback/Get.php @@ -28,7 +28,10 @@ class Get extends Base { return new OAuth2Gitlab( System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), - System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), + \json_encode([ + 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), + 'endpoint' => 'https://gitlab.com', + ]), $callback ); } From 7c136f382579ac7be846d391604d06dd709c0c8b Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 21 Jul 2026 11:55:29 +0530 Subject: [PATCH 17/19] Fix Rector refactor:check: avoid false-positive assertTrue(is_a()) -> assertInstanceOf() rewrite Rector's AssertTrueFalseToSpecificMethodRector rewrites assertTrue(is_a($x, Y::class, true)) to assertInstanceOf(Y::class, $x), but $entry['oauth2'] is a class-string, not an object -- assertInstanceOf requires an actual instance. Rewritten to assertContains() over class_implements(), which is both correct and doesn't match the rule's pattern. --- tests/unit/Vcs/FactoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index 578cd9d95c..f66bb26aef 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -33,7 +33,7 @@ final class FactoryTest extends TestCase foreach ($registry as $key => $entry) { $this->assertTrue(\is_subclass_of($entry['adapter'], Git::class), "Adapter for '{$key}' must extend Git"); - $this->assertTrue(\is_a($entry['oauth2'], EnvOAuth2::class, true), "OAuth2 client for '{$key}' must implement EnvOAuth2"); + $this->assertContains(EnvOAuth2::class, \class_implements($entry['oauth2']) ?: [], "OAuth2 client for '{$key}' must implement EnvOAuth2"); $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}'"); From e47a2b54dd90b7de71ac171c1a9081120d77f838 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 21 Jul 2026 13:04:52 +0530 Subject: [PATCH 18/19] Fix VCS OAuth polymorphism: registry owns construction, no reflection in tests Factory::oauth2FromProvider() delegated construction to a static fromEnv() on each OAuth2 class, which re-hardcoded the same env var names the registry's 'variables' map already owns -- two sources of truth for the same mapping. The registry's 'oauth2' key is now a construction closure (clientId, clientSecret, endpoint) -> OAuth2, so Factory just resolves the env values via its own getEnv() and calls it; no per-provider branching in Factory, and no env var name duplicated outside app/config/vcs.php. EnvOAuth2 also conflated two responsibilities (build-from-env, and create-a-repository). Replaced with a narrow RepositoryOAuth2 interface covering only createRepository(), which is the one capability call sites actually need to check for beyond the base OAuth2 contract. Also removes the two reflection-based tests this repo's conventions disallow: FactoryTest asserted on the vendored GitLab adapter's protected $endpoint via ReflectionProperty -- replaced with an assertion on the public getRepositoryUrl() output, which is derived from the same value. GitlabClosedPullRequestEventTest invoked a private handlePullRequestEvent() via ReflectionMethod -- extracted the closed-PR cleanup into RepositoryPullRequestCleanup, a small public collaborator, and tested that directly instead. --- app/config/vcs.php | 19 ++++++-- src/Appwrite/Auth/OAuth2/Gitea.php | 13 +----- src/Appwrite/Auth/OAuth2/Github.php | 10 +--- src/Appwrite/Auth/OAuth2/Gitlab.php | 28 ++++------- .../Modules/VCS/Http/Gitlab/Events/Create.php | 20 +------- .../Installations/Repositories/Create.php | 5 ++ src/Appwrite/Vcs/EnvOAuth2.php | 18 -------- src/Appwrite/Vcs/Factory.php | 18 ++++++-- src/Appwrite/Vcs/RepositoryOAuth2.php | 14 ++++++ .../Vcs/RepositoryPullRequestCleanup.php | 46 +++++++++++++++++++ tests/unit/Vcs/FactoryTest.php | 12 ++--- ...p => RepositoryPullRequestCleanupTest.php} | 32 ++----------- 12 files changed, 118 insertions(+), 117 deletions(-) delete mode 100644 src/Appwrite/Vcs/EnvOAuth2.php create mode 100644 src/Appwrite/Vcs/RepositoryOAuth2.php create mode 100644 src/Appwrite/Vcs/RepositoryPullRequestCleanup.php rename tests/unit/Vcs/{GitlabClosedPullRequestEventTest.php => RepositoryPullRequestCleanupTest.php} (64%) diff --git a/app/config/vcs.php b/app/config/vcs.php index 3f8a266ea4..f32d95acc8 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -14,7 +14,10 @@ use Utopia\VCS\Adapter\Git\GitLab; return [ 'github' => [ 'adapter' => GitHub::class, - 'oauth2' => OAuth2Github::class, + // Each provider owns its own construction quirks (Gitea calls + // setEndpoint(), GitLab JSON-encodes its secret); Factory just + // supplies the resolved env values, so no per-provider branching lives there. + 'oauth2' => fn (string $clientId, string $clientSecret, string $endpoint) => new OAuth2Github($clientId, $clientSecret, ''), 'variables' => [ 'appName' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_NAME'], 'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_PRIVATE_KEY'], @@ -26,7 +29,11 @@ return [ ], 'gitea' => [ 'adapter' => Gitea::class, - 'oauth2' => OAuth2Gitea::class, + 'oauth2' => function (string $clientId, string $clientSecret, string $endpoint) { + $oauth2 = new OAuth2Gitea($clientId, $clientSecret, ''); + $oauth2->setEndpoint($endpoint); + return $oauth2; + }, 'variables' => [ 'endpoint' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_ENDPOINT'], 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_GITEA_CLIENT_ID'], @@ -38,7 +45,13 @@ return [ ], 'gitlab' => [ 'adapter' => GitLab::class, - 'oauth2' => OAuth2Gitlab::class, + // Auth\OAuth2\Gitlab is shared with the "Sign in with GitLab" account-login + // provider, which JSON-encodes its secret as {"clientSecret","endpoint"} + // to support a per-project self-hosted endpoint -- match that shape here too. + 'oauth2' => fn (string $clientId, string $clientSecret, string $endpoint) => new OAuth2Gitlab($clientId, \json_encode([ + 'clientSecret' => $clientSecret, + 'endpoint' => $endpoint, + ]), ''), // Only official gitlab.com is supported -- fixed, not configurable. 'endpoint' => 'https://gitlab.com', 'variables' => [ diff --git a/src/Appwrite/Auth/OAuth2/Gitea.php b/src/Appwrite/Auth/OAuth2/Gitea.php index 1b73817bbd..57467130aa 100644 --- a/src/Appwrite/Auth/OAuth2/Gitea.php +++ b/src/Appwrite/Auth/OAuth2/Gitea.php @@ -3,10 +3,9 @@ namespace Appwrite\Auth\OAuth2; use Appwrite\Auth\OAuth2; -use Appwrite\Vcs\EnvOAuth2; -use Utopia\System\System; +use Appwrite\Vcs\RepositoryOAuth2; -class Gitea extends OAuth2 implements EnvOAuth2 +class Gitea extends OAuth2 implements RepositoryOAuth2 { protected string $endpoint = ''; @@ -18,14 +17,6 @@ class Gitea extends OAuth2 implements EnvOAuth2 'read:user', ]; - public static function fromEnv(): OAuth2&EnvOAuth2 - { - $oauth2 = new self(System::getEnv('_APP_VCS_GITEA_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITEA_CLIENT_SECRET', ''), ''); - $oauth2->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', '')); - - return $oauth2; - } - public function setEndpoint(string $endpoint): void { $this->endpoint = \rtrim($endpoint, '/'); diff --git a/src/Appwrite/Auth/OAuth2/Github.php b/src/Appwrite/Auth/OAuth2/Github.php index b3be5243a7..71a52d2604 100644 --- a/src/Appwrite/Auth/OAuth2/Github.php +++ b/src/Appwrite/Auth/OAuth2/Github.php @@ -3,17 +3,11 @@ namespace Appwrite\Auth\OAuth2; use Appwrite\Auth\OAuth2; -use Appwrite\Vcs\EnvOAuth2; +use Appwrite\Vcs\RepositoryOAuth2; use Utopia\Fetch\Client as FetchClient; -use Utopia\System\System; -class Github extends OAuth2 implements EnvOAuth2 +class Github extends OAuth2 implements RepositoryOAuth2 { - public static function fromEnv(): OAuth2&EnvOAuth2 - { - return new self(System::getEnv('_APP_VCS_GITHUB_CLIENT_ID', ''), System::getEnv('_APP_VCS_GITHUB_CLIENT_SECRET', ''), ''); - } - /** * @var array */ diff --git a/src/Appwrite/Auth/OAuth2/Gitlab.php b/src/Appwrite/Auth/OAuth2/Gitlab.php index 9e8c89e51e..bc34e69a28 100644 --- a/src/Appwrite/Auth/OAuth2/Gitlab.php +++ b/src/Appwrite/Auth/OAuth2/Gitlab.php @@ -3,30 +3,20 @@ namespace Appwrite\Auth\OAuth2; use Appwrite\Auth\OAuth2; -use Appwrite\Vcs\EnvOAuth2; -use Utopia\System\System; +use Appwrite\Vcs\RepositoryOAuth2; // Reference Material // https://docs.gitlab.com/ee/api/oauth2.html -class Gitlab extends OAuth2 implements EnvOAuth2 +/** + * Shared with the "Sign in with GitLab" account-login OAuth2 provider, which + * stores its secret as JSON ({"clientSecret": "...", "endpoint": "..."}) to + * support self-hosted GitLab per-project. The VCS flow (see app/config/vcs.php) + * only supports official gitlab.com, but still encodes to that same JSON + * shape so getAppSecret()/getEndpoint() stay correct for both consumers. + */ +class Gitlab extends OAuth2 implements RepositoryOAuth2 { - /** - * This class is shared with the "Sign in with GitLab" account-login - * OAuth2 provider, which stores its secret as JSON - * ({"clientSecret": "...", "endpoint": "..."}) to support self-hosted - * GitLab per-project. The VCS flow only supports official gitlab.com, - * but still encodes to that same JSON shape so getAppSecret()/getEndpoint() - * stay correct for both consumers. - */ - public static function fromEnv(): OAuth2&EnvOAuth2 - { - return new self(System::getEnv('_APP_VCS_GITLAB_CLIENT_ID', ''), \json_encode([ - 'clientSecret' => System::getEnv('_APP_VCS_GITLAB_CLIENT_SECRET', ''), - 'endpoint' => 'https://gitlab.com', - ]), ''); - } - /** * @var array */ diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php index 24f4c7b844..e770a930a9 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Gitlab/Events/Create.php @@ -9,6 +9,7 @@ use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory as VcsFactory; use Appwrite\Vcs\InstallationTokens; +use Appwrite\Vcs\RepositoryPullRequestCleanup; use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; @@ -261,24 +262,7 @@ class Create extends Action return; } - $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ - Query::equal('providerRepositoryId', [$providerRepositoryId]), - Query::orderDesc('$createdAt'), - Query::limit(100), - ])); - - foreach ($repositories as $repository) { - if ($this->resolveGitlabInstallation($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]))); - } - } + (new RepositoryPullRequestCleanup())->remove($dbForPlatform, $authorization, 'gitlab', $providerRepositoryId, $providerPullRequestId); } } } 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 10d46bc80a..28db0f1b4a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -10,6 +10,7 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory as VcsFactory; use Appwrite\Vcs\InstallationTokens; +use Appwrite\Vcs\RepositoryOAuth2; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; @@ -80,6 +81,10 @@ class Create extends Action if ($installation->getAttribute('personal', false) === true) { $oauth2 = $vcsFactory->oauth2FromProvider($provider); + if (!$oauth2 instanceof RepositoryOAuth2) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'VCS provider does not support repository creation: ' . $provider); + } + $identity = null; if ($provider === 'github' && (empty($installation->getAttribute('personalAccessToken')) || empty($installation->getAttribute('personalRefreshToken')) || empty($installation->getAttribute('personalAccessTokenExpiry')))) { $identity = $dbForPlatform->findOne('identities', [ diff --git a/src/Appwrite/Vcs/EnvOAuth2.php b/src/Appwrite/Vcs/EnvOAuth2.php deleted file mode 100644 index 6f11df33bc..0000000000 --- a/src/Appwrite/Vcs/EnvOAuth2.php +++ /dev/null @@ -1,18 +0,0 @@ -getEnv($key, 'webhookSecret'); } - public function oauth2FromProvider(string $key): OAuth2&EnvOAuth2 + public function oauth2FromProvider(string $key): OAuth2 { - $class = $this->registry[$key]['oauth2'] ?? null; + $builder = $this->registry[$key]['oauth2'] ?? null; - if ($class === null || !\is_a($class, EnvOAuth2::class, true)) { + if (!\is_callable($builder)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Unsupported VCS provider: ' . $key); } - return $class::fromEnv(); + $clientId = $this->getEnv($key, 'clientId'); + $clientSecret = $this->getEnv($key, 'clientSecret'); + $endpoint = $this->registry[$key]['endpoint'] ?? $this->getEnv($key, 'endpoint'); + + $oauth2 = $builder($clientId, $clientSecret, $endpoint); + + if (!$oauth2 instanceof OAuth2) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'VCS provider "' . $key . '" oauth2 builder returned an invalid client'); + } + + return $oauth2; } protected function getEnv(string $key, string $name): string diff --git a/src/Appwrite/Vcs/RepositoryOAuth2.php b/src/Appwrite/Vcs/RepositoryOAuth2.php new file mode 100644 index 0000000000..6923275bb0 --- /dev/null +++ b/src/Appwrite/Vcs/RepositoryOAuth2.php @@ -0,0 +1,14 @@ +skip(fn () => $dbForPlatform->find('repositories', [ + Query::equal('providerRepositoryId', [$providerRepositoryId]), + Query::orderDesc('$createdAt'), + Query::limit(100), + ])); + + foreach ($repositories as $repository) { + $installation = $authorization->skip(fn () => $dbForPlatform->getDocument('installations', $repository->getAttribute('installationId', ''))); + + if ($installation->isEmpty() || $installation->getAttribute('provider', 'github') !== $provider) { + 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/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index f66bb26aef..9c758593bd 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -6,10 +6,8 @@ namespace Tests\Unit\Vcs; use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Appwrite\Extend\Exception; -use Appwrite\Vcs\EnvOAuth2; use Appwrite\Vcs\Factory; use PHPUnit\Framework\TestCase; -use ReflectionProperty; use Utopia\Cache\Adapter\None; use Utopia\Cache\Cache; use Utopia\Config\Config; @@ -33,7 +31,7 @@ final class FactoryTest extends TestCase foreach ($registry as $key => $entry) { $this->assertTrue(\is_subclass_of($entry['adapter'], Git::class), "Adapter for '{$key}' must extend Git"); - $this->assertContains(EnvOAuth2::class, \class_implements($entry['oauth2']) ?: [], "OAuth2 client for '{$key}' must implement EnvOAuth2"); + $this->assertIsCallable($entry['oauth2'], "OAuth2 builder for '{$key}' must be callable"); $this->assertNotEmpty($entry['variables'], "Variables missing for '{$key}'"); foreach ($entry['variables'] as $name => $variable) { $this->assertStringStartsWith('_APP_VCS_', $variable['envVariable'] ?? '', "Env variable for '{$name}' missing or invalid for '{$key}'"); @@ -127,8 +125,7 @@ final class FactoryTest extends TestCase $factory = new Factory($this->cache(), ['gitlab' => $entry]); $adapter = $factory->fromProvider('gitlab'); - $property = new ReflectionProperty($adapter, 'endpoint'); - $this->assertSame('https://gitlab.com/api/v4', $property->getValue($adapter)); + $this->assertSame('https://gitlab.com/owner/repo', $adapter->getRepositoryUrl('owner', 'repo')); } public function testFromProviderFixedEndpointIgnoresEnv(): void @@ -142,8 +139,7 @@ final class FactoryTest extends TestCase \putenv('_APP_VCS_TEST_ENDPOINT=https://gitlab.example.com'); $adapter = $factory->fromProvider('gitlab'); - $property = new ReflectionProperty($adapter, 'endpoint'); - $this->assertSame('https://gitlab.com/api/v4', $property->getValue($adapter)); + $this->assertSame('https://gitlab.com/owner/repo', $adapter->getRepositoryUrl('owner', 'repo')); } public function testGetWebhookSecret(): void @@ -169,7 +165,7 @@ final class FactoryTest extends TestCase { return [ 'adapter' => GitHub::class, - 'oauth2' => OAuth2Github::class, + 'oauth2' => fn (string $clientId, string $clientSecret, string $endpoint) => new OAuth2Github($clientId, $clientSecret, ''), 'variables' => [ 'appName' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_APP_NAME'], 'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_GITHUB_PRIVATE_KEY'], diff --git a/tests/unit/Vcs/GitlabClosedPullRequestEventTest.php b/tests/unit/Vcs/RepositoryPullRequestCleanupTest.php similarity index 64% rename from tests/unit/Vcs/GitlabClosedPullRequestEventTest.php rename to tests/unit/Vcs/RepositoryPullRequestCleanupTest.php index fefbde0c5e..b289ec9263 100644 --- a/tests/unit/Vcs/GitlabClosedPullRequestEventTest.php +++ b/tests/unit/Vcs/RepositoryPullRequestCleanupTest.php @@ -4,25 +4,15 @@ declare(strict_types=1); namespace Tests\Unit\Vcs; -use Appwrite\Platform\Modules\VCS\Http\Gitlab\Events\Create; -use Appwrite\Vcs\Factory as VcsFactory; -use Appwrite\Vcs\InstallationTokens; +use Appwrite\Vcs\RepositoryPullRequestCleanup; use PHPUnit\Framework\TestCase; -use ReflectionMethod; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -final class GitlabClosedPullRequestEventTest extends TestCase +final class RepositoryPullRequestCleanupTest extends TestCase { - private Create $handler; - - public function setUp(): void - { - $this->handler = new Create(); - } - - public function testClosedEventOnlyUpdatesGitlabRepositories(): void + public function testRemoveOnlyUpdatesGitlabRepositories(): void { // A GitHub repo and a GitLab repo can share the same numeric // providerRepositoryId since each is scoped to its own server. @@ -64,20 +54,6 @@ final class GitlabClosedPullRequestEventTest extends TestCase return true; })); - $vcsFactory = $this->createStub(VcsFactory::class); - $installationTokens = $this->createStub(InstallationTokens::class); - $deploymentsFactory = fn () => null; - - $this->callHandler('handlePullRequestEvent', [ - 'action' => 'closed', - 'repositoryId' => '5', - 'pullRequestNumber' => 5, - 'external' => true, - ], $vcsFactory, $installationTokens, $dbForPlatform, $authorization, fn () => null, [], $deploymentsFactory); - } - - private function callHandler(string $method, mixed ...$arguments): mixed - { - return (new ReflectionMethod($this->handler, $method))->invoke($this->handler, ...$arguments); + (new RepositoryPullRequestCleanup())->remove($dbForPlatform, $authorization, 'gitlab', '5', 5); } } From 6a63b7ade27502f68782a1a06e9dae4b56a51302 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 21 Jul 2026 13:07:25 +0530 Subject: [PATCH 19/19] Drop RepositoryOAuth2 interface in favor of a method_exists() guard Matches the existing dynamic style already used in Factory::fromProvider() for setEndpoint() -- less code than a formal interface just to gate one optional capability on one call site. --- src/Appwrite/Auth/OAuth2/Gitea.php | 3 +-- src/Appwrite/Auth/OAuth2/Github.php | 3 +-- src/Appwrite/Auth/OAuth2/Gitlab.php | 3 +-- .../VCS/Http/Installations/Repositories/Create.php | 3 +-- src/Appwrite/Vcs/RepositoryOAuth2.php | 14 -------------- 5 files changed, 4 insertions(+), 22 deletions(-) delete mode 100644 src/Appwrite/Vcs/RepositoryOAuth2.php diff --git a/src/Appwrite/Auth/OAuth2/Gitea.php b/src/Appwrite/Auth/OAuth2/Gitea.php index 57467130aa..390189fc72 100644 --- a/src/Appwrite/Auth/OAuth2/Gitea.php +++ b/src/Appwrite/Auth/OAuth2/Gitea.php @@ -3,9 +3,8 @@ namespace Appwrite\Auth\OAuth2; use Appwrite\Auth\OAuth2; -use Appwrite\Vcs\RepositoryOAuth2; -class Gitea extends OAuth2 implements RepositoryOAuth2 +class Gitea extends OAuth2 { protected string $endpoint = ''; diff --git a/src/Appwrite/Auth/OAuth2/Github.php b/src/Appwrite/Auth/OAuth2/Github.php index 71a52d2604..f80c067daa 100644 --- a/src/Appwrite/Auth/OAuth2/Github.php +++ b/src/Appwrite/Auth/OAuth2/Github.php @@ -3,10 +3,9 @@ namespace Appwrite\Auth\OAuth2; use Appwrite\Auth\OAuth2; -use Appwrite\Vcs\RepositoryOAuth2; use Utopia\Fetch\Client as FetchClient; -class Github extends OAuth2 implements RepositoryOAuth2 +class Github extends OAuth2 { /** * @var array diff --git a/src/Appwrite/Auth/OAuth2/Gitlab.php b/src/Appwrite/Auth/OAuth2/Gitlab.php index bc34e69a28..fb6244bb52 100644 --- a/src/Appwrite/Auth/OAuth2/Gitlab.php +++ b/src/Appwrite/Auth/OAuth2/Gitlab.php @@ -3,7 +3,6 @@ namespace Appwrite\Auth\OAuth2; use Appwrite\Auth\OAuth2; -use Appwrite\Vcs\RepositoryOAuth2; // Reference Material // https://docs.gitlab.com/ee/api/oauth2.html @@ -15,7 +14,7 @@ use Appwrite\Vcs\RepositoryOAuth2; * only supports official gitlab.com, but still encodes to that same JSON * shape so getAppSecret()/getEndpoint() stay correct for both consumers. */ -class Gitlab extends OAuth2 implements RepositoryOAuth2 +class Gitlab extends OAuth2 { /** * @var array 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 28db0f1b4a..1277f44373 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -10,7 +10,6 @@ use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory as VcsFactory; use Appwrite\Vcs\InstallationTokens; -use Appwrite\Vcs\RepositoryOAuth2; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; @@ -81,7 +80,7 @@ class Create extends Action if ($installation->getAttribute('personal', false) === true) { $oauth2 = $vcsFactory->oauth2FromProvider($provider); - if (!$oauth2 instanceof RepositoryOAuth2) { + if (!\method_exists($oauth2, 'createRepository')) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'VCS provider does not support repository creation: ' . $provider); } diff --git a/src/Appwrite/Vcs/RepositoryOAuth2.php b/src/Appwrite/Vcs/RepositoryOAuth2.php deleted file mode 100644 index 6923275bb0..0000000000 --- a/src/Appwrite/Vcs/RepositoryOAuth2.php +++ /dev/null @@ -1,14 +0,0 @@ -