From c4a217ae5d22f346a314f6a273de0fbd6d69f54b Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Sun, 19 Jul 2026 17:34:44 +0530 Subject: [PATCH] fix: use tenant database for VCS deployments --- src/Appwrite/Deployment/Backend.php | 5 ++ src/Appwrite/Deployment/Backend/Executor.php | 7 +- .../Deployment/Backend/Orchestrator.php | 7 +- .../Modules/VCS/Http/GitHub/Deployment.php | 14 ++-- .../e2e/Services/VCS/VCSConsoleClientTest.php | 76 +++++++++++++++++++ 5 files changed, 101 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Deployment/Backend.php b/src/Appwrite/Deployment/Backend.php index c2f03faa2e..b68d8f0276 100644 --- a/src/Appwrite/Deployment/Backend.php +++ b/src/Appwrite/Deployment/Backend.php @@ -23,6 +23,11 @@ abstract readonly class Backend ) { } + /** + * Return a backend bound to a project resolved after request initialization. + */ + abstract public function forProject(Database $dbForProject, Document $project): static; + /** * Saves chunked-upload progress onto the deployment — source path/size, * chunk counters, metadata. Never triggers a build; call createFromUpload() diff --git a/src/Appwrite/Deployment/Backend/Executor.php b/src/Appwrite/Deployment/Backend/Executor.php index 1039c24272..5b9d077ca2 100644 --- a/src/Appwrite/Deployment/Backend/Executor.php +++ b/src/Appwrite/Deployment/Backend/Executor.php @@ -11,7 +11,7 @@ use Utopia\Database\Document; readonly class Executor extends Backend { - public function __construct( + final public function __construct( private BuildPublisher $publisherForBuilds, Database $dbForProject, Document $project, @@ -21,6 +21,11 @@ readonly class Executor extends Backend parent::__construct($dbForProject, $project); } + public function forProject(Database $dbForProject, Document $project): static + { + return new static($this->publisherForBuilds, $dbForProject, $project, $this->executor, $this->platform); + } + public function createFromUpload(Document $resource, Document $deployment): Document { return $this->dispatch($resource, $deployment, null); diff --git a/src/Appwrite/Deployment/Backend/Orchestrator.php b/src/Appwrite/Deployment/Backend/Orchestrator.php index a003b036a0..9d029d3fab 100644 --- a/src/Appwrite/Deployment/Backend/Orchestrator.php +++ b/src/Appwrite/Deployment/Backend/Orchestrator.php @@ -37,7 +37,7 @@ use Utopia\System\System; */ readonly class Orchestrator extends Backend { - public function __construct( + final public function __construct( private Jobs $jobs, Database $dbForProject, Document $project, @@ -46,6 +46,11 @@ readonly class Orchestrator extends Backend parent::__construct($dbForProject, $project); } + public function forProject(Database $dbForProject, Document $project): static + { + return new static($this->jobs, $dbForProject, $project, $this->platform); + } + public function createFromUpload(Document $resource, Document $deployment): Document { return $this->submit($resource, $deployment, null); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php index c1e3da5adf..1f38eee712 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php @@ -397,12 +397,14 @@ trait Deployment // or jobs-service, decided by _APP_BUILDS_BACKEND) when the // caller opts in. Sites always stay on the executor. if ($deployments !== null && $resourceCollection === 'functions') { - $deployment = $authorization->skip(fn () => $deployments->createFromUrl( - $resource, - $deployment, - $vcs->getRepositoryPresignedUrl($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash), - $resource->getAttribute('providerRootDirectory', ''), - )); + $deployment = $authorization->skip(fn () => $deployments + ->forProject($dbForProject, $project) + ->createFromUrl( + $resource, + $deployment, + $vcs->getRepositoryPresignedUrl($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash), + $resource->getAttribute('providerRootDirectory', ''), + )); } else { $deployment = $authorization->skip(fn () => $dbForProject->createDocument('deployments', new Document([ '$permissions' => [ diff --git a/tests/e2e/Services/VCS/VCSConsoleClientTest.php b/tests/e2e/Services/VCS/VCSConsoleClientTest.php index e936013a90..134336769f 100644 --- a/tests/e2e/Services/VCS/VCSConsoleClientTest.php +++ b/tests/e2e/Services/VCS/VCSConsoleClientTest.php @@ -621,6 +621,82 @@ final class VCSConsoleClientTest extends Scope $this->assertEquals('main', $function['body']['providerBranch']); } + public function testGitHubPushCreatesFunctionDeploymentWithoutProjectHeader(): void + { + $data = $this->setupFunctionUsingVCS(); + $github = new GitHub(new Cache(new None())); + $github->initializeVariables( + $this->providerInstallationId, + System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'), + System::getEnv('_APP_VCS_GITHUB_APP_ID'), + ); + $commit = $github->getLatestCommit('appwrite-test', 'ruby-starter', 'main'); + $payload = [ + 'created' => false, + 'deleted' => false, + 'ref' => 'refs/heads/main', + 'before' => $commit['commitHash'], + 'after' => $commit['commitHash'], + 'repository' => [ + 'id' => (int) $this->providerRepositoryId, + 'name' => 'ruby-starter', + 'full_name' => 'appwrite-test/ruby-starter', + 'private' => false, + 'html_url' => 'https://github.com/appwrite-test/ruby-starter', + 'owner' => ['name' => 'appwrite-test'], + ], + 'installation' => ['id' => (int) $this->providerInstallationId], + 'head_commit' => [ + 'author' => [ + 'name' => $commit['commitAuthor'], + 'email' => 'vcs-e2e@appwrite.io', + ], + 'message' => $commit['commitMessage'], + 'url' => $commit['commitUrl'], + ], + 'commits' => [[ + 'id' => $commit['commitHash'], + 'added' => [], + 'removed' => [], + 'modified' => ['main.rb'], + ]], + 'sender' => [ + 'html_url' => $commit['commitAuthorUrl'], + 'avatar_url' => $commit['commitAuthorAvatar'], + ], + ]; + $headers = [ + 'content-type' => 'application/json', + 'x-github-event' => 'push', + ]; + $secret = System::getEnv('_APP_VCS_GITHUB_WEBHOOK_SECRET', ''); + if (!empty($secret)) { + $headers['x-hub-signature-256'] = 'sha256=' . \hash_hmac( + 'sha256', + \json_encode($payload, JSON_THROW_ON_ERROR), + $secret, + ); + } + + // GitHub webhooks are public and intentionally have no x-appwrite-project header. + $event = $this->client->call(Client::METHOD_POST, '/vcs/github/events', $headers, $payload); + + $this->assertEquals(200, $event['headers']['status-code']); + + $deployments = $this->client->call(Client::METHOD_GET, '/functions/' . $data['functionId'] . '/deployments', array_merge([ + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'queries' => [ + Query::equal('providerCommitHash', [$commit['commitHash']])->toString(), + Query::equal('type', ['vcs'])->toString(), + ], + ]); + + $this->assertEquals(200, $deployments['headers']['status-code']); + $this->assertGreaterThanOrEqual(1, $deployments['body']['total']); + $this->assertSame($data['functionId'], $deployments['body']['deployments'][0]['resourceId']); + } + public function testUpdateFunctionUsingVCS(): void { $data = $this->setupFunctionUsingVCS();