From 452ff7ff75c4f0f45e2269d64f83256eee87add5 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 12:17:27 +0530 Subject: [PATCH 01/11] fix: disconnect VCS when GitHub installation is revoked (404) --- .../Modules/Functions/Workers/Builds.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index c2b8837c13..ead686b86f 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -295,7 +295,23 @@ class Builds extends Action $privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); $githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID'); - $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + try { + $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); + } catch (\Exception $e) { + if (\str_contains($e->getMessage(), 'Status: 404')) { + $collection = $resource->getCollection(); + $dbForProject->updateDocument($collection, $resource->getId(), new Document([ + 'providerRepositoryId' => '', + 'installationId' => '', + 'repositoryId' => '', + 'repositoryInternalId' => '', + 'providerBranch' => '', + 'providerRootDirectory' => '', + 'providerSilentMode' => true, + ])); + } + throw $e; + } } Span::add('timings.setup', \round(\microtime(true) - $phaseStart, 3)); From b6bc787554dc65553b5393a075e32daab0ae6f0e Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 12:23:34 +0530 Subject: [PATCH 02/11] fix: use correct disconnect pattern matching Deletes worker --- .../Modules/Functions/Workers/Builds.php | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index ead686b86f..f129a94172 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -299,16 +299,19 @@ class Builds extends Action $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); } catch (\Exception $e) { if (\str_contains($e->getMessage(), 'Status: 404')) { - $collection = $resource->getCollection(); - $dbForProject->updateDocument($collection, $resource->getId(), new Document([ - 'providerRepositoryId' => '', - 'installationId' => '', - 'repositoryId' => '', - 'repositoryInternalId' => '', - 'providerBranch' => '', - 'providerRootDirectory' => '', - 'providerSilentMode' => true, - ])); + $repositoryId = $resource->getAttribute('repositoryId', ''); + if (!empty($repositoryId)) { + $dbForPlatform->deleteDocument('repositories', $repositoryId); + } + $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), $resource + ->setAttribute('installationId', '') + ->setAttribute('installationInternalId', '') + ->setAttribute('providerRepositoryId', '') + ->setAttribute('providerBranch', '') + ->setAttribute('providerSilentMode', false) + ->setAttribute('providerRootDirectory', '') + ->setAttribute('repositoryId', '') + ->setAttribute('repositoryInternalId', '')); } throw $e; } From 4e793764583f0ec2ef32fda886aaebc1dbe4794d Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 12:27:33 +0530 Subject: [PATCH 03/11] test: add unit tests for disconnectVcs method --- .../Modules/Functions/Workers/Builds.php | 31 +++++----- tests/unit/Functions/BuildsTest.php | 56 +++++++++++++++++++ 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index f129a94172..748128ca8b 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -299,19 +299,7 @@ class Builds extends Action $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); } catch (\Exception $e) { if (\str_contains($e->getMessage(), 'Status: 404')) { - $repositoryId = $resource->getAttribute('repositoryId', ''); - if (!empty($repositoryId)) { - $dbForPlatform->deleteDocument('repositories', $repositoryId); - } - $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), $resource - ->setAttribute('installationId', '') - ->setAttribute('installationInternalId', '') - ->setAttribute('providerRepositoryId', '') - ->setAttribute('providerBranch', '') - ->setAttribute('providerSilentMode', false) - ->setAttribute('providerRootDirectory', '') - ->setAttribute('repositoryId', '') - ->setAttribute('repositoryInternalId', '')); + $this->disconnectVcs($resource, $dbForProject, $dbForPlatform); } throw $e; } @@ -1574,6 +1562,23 @@ class Builds extends Action } } + protected function disconnectVcs(Document $resource, Database $dbForProject, Database $dbForPlatform): void + { + $repositoryId = $resource->getAttribute('repositoryId', ''); + if (!empty($repositoryId)) { + $dbForPlatform->deleteDocument('repositories', $repositoryId); + } + $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), $resource + ->setAttribute('installationId', '') + ->setAttribute('installationInternalId', '') + ->setAttribute('providerRepositoryId', '') + ->setAttribute('providerBranch', '') + ->setAttribute('providerSilentMode', false) + ->setAttribute('providerRootDirectory', '') + ->setAttribute('repositoryId', '') + ->setAttribute('repositoryInternalId', '')); + } + private function updateLatestDeployment(Database $dbForProject, Document $resource): Document { $latestDeployment = $dbForProject->findOne('deployments', [ diff --git a/tests/unit/Functions/BuildsTest.php b/tests/unit/Functions/BuildsTest.php index b757856c83..d4021b5d7e 100644 --- a/tests/unit/Functions/BuildsTest.php +++ b/tests/unit/Functions/BuildsTest.php @@ -8,6 +8,7 @@ use Appwrite\Platform\Modules\Functions\Workers\Builds; use PHPUnit\Framework\TestCase; use ReflectionMethod; use Utopia\Config\Config; +use Utopia\Database\Database; use Utopia\Database\Document; final class BuildsTest extends TestCase @@ -87,6 +88,61 @@ final class BuildsTest extends TestCase $this->assertNull($detection->getFallbackFile()); } + public function testDisconnectVcsClearsAllFieldsAndDeletesRepository(): void + { + $resource = new Document([ + '$id' => 'func123', + '$collection' => 'functions', + 'repositoryId' => 'repo456', + 'installationId' => 'install789', + 'installationInternalId' => 'install789internal', + 'providerRepositoryId' => 'gh123', + 'providerBranch' => 'main', + 'providerRootDirectory' => 'functions/my-func', + 'providerSilentMode' => true, + 'repositoryInternalId' => 'repo456internal', + ]); + + $dbForProject = $this->createMock(Database::class); + $dbForPlatform = $this->createMock(Database::class); + + $dbForPlatform->expects($this->once()) + ->method('deleteDocument') + ->with('repositories', 'repo456'); + + $dbForProject->expects($this->once()) + ->method('updateDocument') + ->with('functions', 'func123', $this->callback(function (Document $doc) { + return $doc->getAttribute('installationId') === '' + && $doc->getAttribute('installationInternalId') === '' + && $doc->getAttribute('providerRepositoryId') === '' + && $doc->getAttribute('providerBranch') === '' + && $doc->getAttribute('providerRootDirectory') === '' + && $doc->getAttribute('providerSilentMode') === false + && $doc->getAttribute('repositoryId') === '' + && $doc->getAttribute('repositoryInternalId') === ''; + })); + + $this->callBuilds('disconnectVcs', $resource, $dbForProject, $dbForPlatform); + } + + public function testDisconnectVcsSkipsRepositoryDeletionWhenRepositoryIdEmpty(): void + { + $resource = new Document([ + '$id' => 'func123', + '$collection' => 'functions', + 'repositoryId' => '', + ]); + + $dbForProject = $this->createMock(Database::class); + $dbForPlatform = $this->createMock(Database::class); + + $dbForPlatform->expects($this->never())->method('deleteDocument'); + $dbForProject->expects($this->once())->method('updateDocument'); + + $this->callBuilds('disconnectVcs', $resource, $dbForProject, $dbForPlatform); + } + private function callBuilds(string $method, mixed ...$arguments): mixed { return (new ReflectionMethod($this->builds, $method))->invoke($this->builds, ...$arguments); From 48c78ee1cc897c97d9134caf03822865f73462a0 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 12:34:24 +0530 Subject: [PATCH 04/11] fix: disconnect functions and sites on GitHub installation deleted webhook --- .../Modules/VCS/Http/GitHub/Events/Create.php | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index e40e447ed5..e75099b3f3 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -78,7 +78,7 @@ class Create extends Action $parsedPayload = $github->getEvent($event, $payload); match ($event) { - $github::EVENT_INSTALLATION => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization), + $github::EVENT_INSTALLATION => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), $github::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $githubAppId, $privateKey, $github, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), $github::EVENT_PULL_REQUEST => $this->handlePullRequestEvent($parsedPayload, $privateKey, $githubAppId, $github, $dbForPlatform, $authorization, $publisherForBuilds, $getProjectDB, $platform), default => null, @@ -96,12 +96,12 @@ class Create extends Action array $parsedPayload, Database $dbForPlatform, Authorization $authorization, + callable $getProjectDB, ) { if ($parsedPayload["action"] !== "deleted") { return; } - // TODO: Use worker for this job instead (update function/site as well) $providerInstallationId = $parsedPayload["installationId"]; $installations = $dbForPlatform->find('installations', [ @@ -110,6 +110,32 @@ class Create extends Action ]); foreach ($installations as $installation) { + $projectId = $installation->getAttribute('projectId', ''); + $project = $dbForPlatform->getDocument('projects', $projectId); + + if (!$project->isEmpty()) { + $dbForProject = $getProjectDB($project); + + foreach (['functions', 'sites'] as $collection) { + $resources = $dbForProject->find($collection, [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]); + + foreach ($resources as $resource) { + $dbForProject->updateDocument($collection, $resource->getId(), $resource + ->setAttribute('installationId', '') + ->setAttribute('installationInternalId', '') + ->setAttribute('providerRepositoryId', '') + ->setAttribute('providerBranch', '') + ->setAttribute('providerSilentMode', false) + ->setAttribute('providerRootDirectory', '') + ->setAttribute('repositoryId', '') + ->setAttribute('repositoryInternalId', '')); + } + } + } + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ Query::equal('installationInternalId', [$installation->getSequence()]), Query::limit(1000) From d6c4ce4fecf510c6142d932d6d7162485d4bcb15 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 12:47:39 +0530 Subject: [PATCH 05/11] test: add unit tests for GitHub installation deleted webhook handler --- .../unit/Vcs/GitHubInstallationEventTest.php | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 tests/unit/Vcs/GitHubInstallationEventTest.php diff --git a/tests/unit/Vcs/GitHubInstallationEventTest.php b/tests/unit/Vcs/GitHubInstallationEventTest.php new file mode 100644 index 0000000000..ac313df596 --- /dev/null +++ b/tests/unit/Vcs/GitHubInstallationEventTest.php @@ -0,0 +1,156 @@ +handler = new Create(); + } + + public function testHandleInstallationEventIgnoresNonDeletedActions(): void + { + $dbForPlatform = $this->createMock(Database::class); + $authorization = $this->createStub(Authorization::class); + + $dbForPlatform->expects($this->never())->method('find'); + $dbForPlatform->expects($this->never())->method('deleteDocument'); + + $result = $this->callHandler('handleInstallationEvent', [ + 'action' => 'created', + 'installationId' => '123', + ], $dbForPlatform, $authorization, fn () => null); + + $this->assertNull($result); + } + + public function testHandleInstallationEventDisconnectsFunctionsAndSites(): void + { + $installation = new Document([ + '$id' => 'install1', + '$sequence' => 'install1seq', + 'projectId' => 'project1', + 'providerInstallationId' => 'gh-install-123', + ]); + + $function = new Document([ + '$id' => 'func1', + '$collection' => 'functions', + 'installationId' => 'install1', + 'installationInternalId' => 'install1seq', + 'providerRepositoryId' => 'repo123', + 'providerBranch' => 'main', + 'providerRootDirectory' => 'functions/my-func', + 'providerSilentMode' => true, + 'repositoryId' => 'repo1', + 'repositoryInternalId' => 'repo1seq', + ]); + + $project = new Document(['$id' => 'project1']); + + $dbForProject = $this->createMock(Database::class); + $dbForPlatform = $this->createMock(Database::class); + $authorization = $this->createMock(Authorization::class); + + $dbForPlatform->expects($this->exactly(2)) + ->method('find') + ->willReturnCallback(fn ($collection) => match ($collection) { + 'installations' => [$installation], + 'repositories' => [], + default => [], + }); + + $dbForPlatform->expects($this->once()) + ->method('getDocument') + ->with('projects', 'project1') + ->willReturn($project); + + $dbForProject->expects($this->exactly(2)) + ->method('find') + ->willReturnCallback(fn ($collection) => $collection === 'functions' ? [$function] : []); + + $dbForProject->expects($this->once()) + ->method('updateDocument') + ->with('functions', 'func1', $this->callback(function (Document $doc) { + return $doc->getAttribute('installationId') === '' + && $doc->getAttribute('installationInternalId') === '' + && $doc->getAttribute('providerRepositoryId') === '' + && $doc->getAttribute('providerBranch') === '' + && $doc->getAttribute('providerRootDirectory') === '' + && $doc->getAttribute('providerSilentMode') === false + && $doc->getAttribute('repositoryId') === '' + && $doc->getAttribute('repositoryInternalId') === ''; + })); + + $authorization->expects($this->exactly(2)) + ->method('skip') + ->willReturnCallback(fn ($fn) => $fn()); + + $dbForPlatform->expects($this->once()) + ->method('deleteDocument') + ->with('installations', 'install1'); + + $getProjectDB = fn () => $dbForProject; + + $this->callHandler('handleInstallationEvent', [ + 'action' => 'deleted', + 'installationId' => 'gh-install-123', + ], $dbForPlatform, $authorization, $getProjectDB); + } + + public function testHandleInstallationEventSkipsDisconnectWhenProjectNotFound(): void + { + $installation = new Document([ + '$id' => 'install1', + '$sequence' => 'install1seq', + 'projectId' => 'project1', + 'providerInstallationId' => 'gh-install-123', + ]); + + $dbForPlatform = $this->createMock(Database::class); + $authorization = $this->createMock(Authorization::class); + + $dbForPlatform->expects($this->exactly(2)) + ->method('find') + ->willReturnCallback(fn ($collection) => match ($collection) { + 'installations' => [$installation], + 'repositories' => [], + default => [], + }); + + $dbForPlatform->expects($this->once()) + ->method('getDocument') + ->with('projects', 'project1') + ->willReturn(new Document()); + + $authorization->expects($this->exactly(2)) + ->method('skip') + ->willReturnCallback(fn ($fn) => $fn()); + + $dbForPlatform->expects($this->once()) + ->method('deleteDocument') + ->with('installations', 'install1'); + + $this->callHandler('handleInstallationEvent', [ + 'action' => 'deleted', + 'installationId' => 'gh-install-123', + ], $dbForPlatform, $authorization, fn () => null); + } + + private function callHandler(string $method, mixed ...$arguments): mixed + { + return (new ReflectionMethod($this->handler, $method))->invoke($this->handler, ...$arguments); + } +} From 9c0c7b51ca278413c67bf72a6192357617ffa3e1 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 13:00:50 +0530 Subject: [PATCH 06/11] fix: address Greptile P1s - auth skip and pagination in installation webhook --- .../Modules/VCS/Http/GitHub/Events/Create.php | 66 ++++++++++++------- tests/unit/Functions/BuildsTest.php | 17 ++--- .../unit/Vcs/GitHubInstallationEventTest.php | 34 +++++----- 3 files changed, 70 insertions(+), 47 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index e75099b3f3..15b2a4f7b7 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -92,7 +92,7 @@ class Create extends Action return; } - private function handleInstallationEvent( + protected function handleInstallationEvent( array $parsedPayload, Database $dbForPlatform, Authorization $authorization, @@ -111,39 +111,57 @@ class Create extends Action foreach ($installations as $installation) { $projectId = $installation->getAttribute('projectId', ''); - $project = $dbForPlatform->getDocument('projects', $projectId); + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId)); if (!$project->isEmpty()) { $dbForProject = $getProjectDB($project); foreach (['functions', 'sites'] as $collection) { - $resources = $dbForProject->find($collection, [ - Query::equal('installationInternalId', [$installation->getSequence()]), - Query::limit(1000), - ]); + $cursor = null; + do { + $queries = [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]; + if ($cursor !== null) { + $queries[] = Query::cursorAfter($cursor); + } + $resources = $authorization->skip(fn () => $dbForProject->find($collection, $queries)); - foreach ($resources as $resource) { - $dbForProject->updateDocument($collection, $resource->getId(), $resource - ->setAttribute('installationId', '') - ->setAttribute('installationInternalId', '') - ->setAttribute('providerRepositoryId', '') - ->setAttribute('providerBranch', '') - ->setAttribute('providerSilentMode', false) - ->setAttribute('providerRootDirectory', '') - ->setAttribute('repositoryId', '') - ->setAttribute('repositoryInternalId', '')); - } + foreach ($resources as $resource) { + $authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), $resource + ->setAttribute('installationId', '') + ->setAttribute('installationInternalId', '') + ->setAttribute('providerRepositoryId', '') + ->setAttribute('providerBranch', '') + ->setAttribute('providerSilentMode', false) + ->setAttribute('providerRootDirectory', '') + ->setAttribute('repositoryId', '') + ->setAttribute('repositoryInternalId', ''))); + } + + $cursor = count($resources) === 1000 ? $resources[array_key_last($resources)] : null; + } while ($cursor !== null); } } - $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ - Query::equal('installationInternalId', [$installation->getSequence()]), - Query::limit(1000) - ])); + $cursor = null; + do { + $queries = [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]; + if ($cursor !== null) { + $queries[] = Query::cursorAfter($cursor); + } + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', $queries)); - foreach ($repositories as $repository) { - $authorization->skip(fn () => $dbForPlatform->deleteDocument('repositories', $repository->getId())); - } + foreach ($repositories as $repository) { + $authorization->skip(fn () => $dbForPlatform->deleteDocument('repositories', $repository->getId())); + } + + $cursor = count($repositories) === 1000 ? $repositories[array_key_last($repositories)] : null; + } while ($cursor !== null); $authorization->skip(fn () => $dbForPlatform->deleteDocument('installations', $installation->getId())); } diff --git a/tests/unit/Functions/BuildsTest.php b/tests/unit/Functions/BuildsTest.php index d4021b5d7e..36515d2f5b 100644 --- a/tests/unit/Functions/BuildsTest.php +++ b/tests/unit/Functions/BuildsTest.php @@ -113,14 +113,15 @@ final class BuildsTest extends TestCase $dbForProject->expects($this->once()) ->method('updateDocument') ->with('functions', 'func123', $this->callback(function (Document $doc) { - return $doc->getAttribute('installationId') === '' - && $doc->getAttribute('installationInternalId') === '' - && $doc->getAttribute('providerRepositoryId') === '' - && $doc->getAttribute('providerBranch') === '' - && $doc->getAttribute('providerRootDirectory') === '' - && $doc->getAttribute('providerSilentMode') === false - && $doc->getAttribute('repositoryId') === '' - && $doc->getAttribute('repositoryInternalId') === ''; + $this->assertSame('', $doc->getAttribute('installationId')); + $this->assertSame('', $doc->getAttribute('installationInternalId')); + $this->assertSame('', $doc->getAttribute('providerRepositoryId')); + $this->assertSame('', $doc->getAttribute('providerBranch')); + $this->assertSame('', $doc->getAttribute('providerRootDirectory')); + $this->assertFalse($doc->getAttribute('providerSilentMode')); + $this->assertSame('', $doc->getAttribute('repositoryId')); + $this->assertSame('', $doc->getAttribute('repositoryInternalId')); + return true; })); $this->callBuilds('disconnectVcs', $resource, $dbForProject, $dbForPlatform); diff --git a/tests/unit/Vcs/GitHubInstallationEventTest.php b/tests/unit/Vcs/GitHubInstallationEventTest.php index ac313df596..46582ffd61 100644 --- a/tests/unit/Vcs/GitHubInstallationEventTest.php +++ b/tests/unit/Vcs/GitHubInstallationEventTest.php @@ -66,7 +66,7 @@ final class GitHubInstallationEventTest extends TestCase $dbForPlatform->expects($this->exactly(2)) ->method('find') - ->willReturnCallback(fn ($collection) => match ($collection) { + ->willReturnCallback(fn (string $collection): array => match ($collection) { 'installations' => [$installation], 'repositories' => [], default => [], @@ -79,24 +79,27 @@ final class GitHubInstallationEventTest extends TestCase $dbForProject->expects($this->exactly(2)) ->method('find') - ->willReturnCallback(fn ($collection) => $collection === 'functions' ? [$function] : []); + ->willReturnCallback(fn (string $collection): array => $collection === 'functions' ? [$function] : []); $dbForProject->expects($this->once()) ->method('updateDocument') ->with('functions', 'func1', $this->callback(function (Document $doc) { - return $doc->getAttribute('installationId') === '' - && $doc->getAttribute('installationInternalId') === '' - && $doc->getAttribute('providerRepositoryId') === '' - && $doc->getAttribute('providerBranch') === '' - && $doc->getAttribute('providerRootDirectory') === '' - && $doc->getAttribute('providerSilentMode') === false - && $doc->getAttribute('repositoryId') === '' - && $doc->getAttribute('repositoryInternalId') === ''; + $this->assertSame('', $doc->getAttribute('installationId')); + $this->assertSame('', $doc->getAttribute('installationInternalId')); + $this->assertSame('', $doc->getAttribute('providerRepositoryId')); + $this->assertSame('', $doc->getAttribute('providerBranch')); + $this->assertSame('', $doc->getAttribute('providerRootDirectory')); + $this->assertFalse($doc->getAttribute('providerSilentMode')); + $this->assertSame('', $doc->getAttribute('repositoryId')); + $this->assertSame('', $doc->getAttribute('repositoryInternalId')); + return true; })); - $authorization->expects($this->exactly(2)) + // 1: getDocument(projects), 2: find(functions), 3: updateDocument(func1), + // 4: find(sites), 5: find(repositories), 6: deleteDocument(installations) + $authorization->expects($this->exactly(6)) ->method('skip') - ->willReturnCallback(fn ($fn) => $fn()); + ->willReturnCallback(fn (callable $fn): mixed => $fn()); $dbForPlatform->expects($this->once()) ->method('deleteDocument') @@ -124,7 +127,7 @@ final class GitHubInstallationEventTest extends TestCase $dbForPlatform->expects($this->exactly(2)) ->method('find') - ->willReturnCallback(fn ($collection) => match ($collection) { + ->willReturnCallback(fn (string $collection): array => match ($collection) { 'installations' => [$installation], 'repositories' => [], default => [], @@ -135,9 +138,10 @@ final class GitHubInstallationEventTest extends TestCase ->with('projects', 'project1') ->willReturn(new Document()); - $authorization->expects($this->exactly(2)) + // 1: getDocument(projects), 2: find(repositories), 3: deleteDocument(installations) + $authorization->expects($this->exactly(3)) ->method('skip') - ->willReturnCallback(fn ($fn) => $fn()); + ->willReturnCallback(fn (callable $fn): mixed => $fn()); $dbForPlatform->expects($this->once()) ->method('deleteDocument') From cd8266af3bbcf78cc6c976b4409a26c877e528f8 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 13:18:54 +0530 Subject: [PATCH 07/11] fix: paginate outer installation lookup in installation deleted webhook --- .../Modules/VCS/Http/GitHub/Events/Create.php | 115 ++++++++++-------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 15b2a4f7b7..789c01ef47 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -104,67 +104,76 @@ class Create extends Action $providerInstallationId = $parsedPayload["installationId"]; - $installations = $dbForPlatform->find('installations', [ - Query::equal('providerInstallationId', [$providerInstallationId]), - Query::limit(1000) - ]); + $installationCursor = null; + do { + $installationQueries = [ + Query::equal('providerInstallationId', [$providerInstallationId]), + Query::limit(1000), + ]; + if ($installationCursor !== null) { + $installationQueries[] = Query::cursorAfter($installationCursor); + } + $installations = $dbForPlatform->find('installations', $installationQueries); - foreach ($installations as $installation) { - $projectId = $installation->getAttribute('projectId', ''); - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId)); + foreach ($installations as $installation) { + $projectId = $installation->getAttribute('projectId', ''); + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId)); - if (!$project->isEmpty()) { - $dbForProject = $getProjectDB($project); + if (!$project->isEmpty()) { + $dbForProject = $getProjectDB($project); - foreach (['functions', 'sites'] as $collection) { - $cursor = null; - do { - $queries = [ - Query::equal('installationInternalId', [$installation->getSequence()]), - Query::limit(1000), - ]; - if ($cursor !== null) { - $queries[] = Query::cursorAfter($cursor); - } - $resources = $authorization->skip(fn () => $dbForProject->find($collection, $queries)); + foreach (['functions', 'sites'] as $collection) { + $cursor = null; + do { + $queries = [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]; + if ($cursor !== null) { + $queries[] = Query::cursorAfter($cursor); + } + $resources = $authorization->skip(fn () => $dbForProject->find($collection, $queries)); - foreach ($resources as $resource) { - $authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), $resource - ->setAttribute('installationId', '') - ->setAttribute('installationInternalId', '') - ->setAttribute('providerRepositoryId', '') - ->setAttribute('providerBranch', '') - ->setAttribute('providerSilentMode', false) - ->setAttribute('providerRootDirectory', '') - ->setAttribute('repositoryId', '') - ->setAttribute('repositoryInternalId', ''))); - } + foreach ($resources as $resource) { + $authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), $resource + ->setAttribute('installationId', '') + ->setAttribute('installationInternalId', '') + ->setAttribute('providerRepositoryId', '') + ->setAttribute('providerBranch', '') + ->setAttribute('providerSilentMode', false) + ->setAttribute('providerRootDirectory', '') + ->setAttribute('repositoryId', '') + ->setAttribute('repositoryInternalId', ''))); + } - $cursor = count($resources) === 1000 ? $resources[array_key_last($resources)] : null; - } while ($cursor !== null); + $cursor = count($resources) === 1000 ? $resources[array_key_last($resources)] : null; + } while ($cursor !== null); + } } + + $cursor = null; + do { + $queries = [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]; + if ($cursor !== null) { + $queries[] = Query::cursorAfter($cursor); + } + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', $queries)); + + foreach ($repositories as $repository) { + $authorization->skip(fn () => $dbForPlatform->deleteDocument('repositories', $repository->getId())); + } + + $cursor = count($repositories) === 1000 ? $repositories[array_key_last($repositories)] : null; + } while ($cursor !== null); + + $authorization->skip(fn () => $dbForPlatform->deleteDocument('installations', $installation->getId())); } - $cursor = null; - do { - $queries = [ - Query::equal('installationInternalId', [$installation->getSequence()]), - Query::limit(1000), - ]; - if ($cursor !== null) { - $queries[] = Query::cursorAfter($cursor); - } - $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', $queries)); - - foreach ($repositories as $repository) { - $authorization->skip(fn () => $dbForPlatform->deleteDocument('repositories', $repository->getId())); - } - - $cursor = count($repositories) === 1000 ? $repositories[array_key_last($repositories)] : null; - } while ($cursor !== null); - - $authorization->skip(fn () => $dbForPlatform->deleteDocument('installations', $installation->getId())); - } + $installationCursor = count($installations) === 1000 ? $installations[array_key_last($installations)] : null; + } while ($installationCursor !== null); } private function handlePushEvent( From 317f7515bb971bfd8dd4b3b3aab54de07774f760 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 17:39:51 +0530 Subject: [PATCH 08/11] fix: wrap outer installations lookup in authorization skip --- .../Platform/Modules/VCS/Http/GitHub/Events/Create.php | 2 +- tests/unit/Vcs/GitHubInstallationEventTest.php | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 789c01ef47..725779a969 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -113,7 +113,7 @@ class Create extends Action if ($installationCursor !== null) { $installationQueries[] = Query::cursorAfter($installationCursor); } - $installations = $dbForPlatform->find('installations', $installationQueries); + $installations = $authorization->skip(fn () => $dbForPlatform->find('installations', $installationQueries)); foreach ($installations as $installation) { $projectId = $installation->getAttribute('projectId', ''); diff --git a/tests/unit/Vcs/GitHubInstallationEventTest.php b/tests/unit/Vcs/GitHubInstallationEventTest.php index 46582ffd61..045ac5d9d6 100644 --- a/tests/unit/Vcs/GitHubInstallationEventTest.php +++ b/tests/unit/Vcs/GitHubInstallationEventTest.php @@ -95,9 +95,9 @@ final class GitHubInstallationEventTest extends TestCase return true; })); - // 1: getDocument(projects), 2: find(functions), 3: updateDocument(func1), - // 4: find(sites), 5: find(repositories), 6: deleteDocument(installations) - $authorization->expects($this->exactly(6)) + // 1: find(installations), 2: getDocument(projects), 3: find(functions), 4: updateDocument(func1), + // 5: find(sites), 6: find(repositories), 7: deleteDocument(installations) + $authorization->expects($this->exactly(7)) ->method('skip') ->willReturnCallback(fn (callable $fn): mixed => $fn()); @@ -138,8 +138,8 @@ final class GitHubInstallationEventTest extends TestCase ->with('projects', 'project1') ->willReturn(new Document()); - // 1: getDocument(projects), 2: find(repositories), 3: deleteDocument(installations) - $authorization->expects($this->exactly(3)) + // 1: find(installations), 2: getDocument(projects), 3: find(repositories), 4: deleteDocument(installations) + $authorization->expects($this->exactly(4)) ->method('skip') ->willReturnCallback(fn (callable $fn): mixed => $fn()); From f5bb65f50ecd9394551f2010e8fdece0de7830fb Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 17:46:58 +0530 Subject: [PATCH 09/11] fix: guard disconnectVcs against stale deployment overwriting reconnected installation --- src/Appwrite/Platform/Modules/Functions/Workers/Builds.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 748128ca8b..cceb2630b1 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -298,7 +298,8 @@ class Builds extends Action try { $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); } catch (\Exception $e) { - if (\str_contains($e->getMessage(), 'Status: 404')) { + if (\str_contains($e->getMessage(), 'Status: 404') + && $resource->getAttribute('installationId', '') === $installationId) { $this->disconnectVcs($resource, $dbForProject, $dbForPlatform); } throw $e; From 0d3038ffe93f41d4af3ea47da411a89369432339 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 24 Jun 2026 19:05:13 +0530 Subject: [PATCH 10/11] fix: use partial document for VCS disconnect updates --- .../Modules/Functions/Workers/Builds.php | 21 ++++++++++--------- .../Modules/VCS/Http/GitHub/Events/Create.php | 19 +++++++++-------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index cceb2630b1..d0806ccadf 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -298,7 +298,7 @@ class Builds extends Action try { $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); } catch (\Exception $e) { - if (\str_contains($e->getMessage(), 'Status: 404') + if ($e->getCode() === 404 && $resource->getAttribute('installationId', '') === $installationId) { $this->disconnectVcs($resource, $dbForProject, $dbForPlatform); } @@ -1569,15 +1569,16 @@ class Builds extends Action if (!empty($repositoryId)) { $dbForPlatform->deleteDocument('repositories', $repositoryId); } - $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), $resource - ->setAttribute('installationId', '') - ->setAttribute('installationInternalId', '') - ->setAttribute('providerRepositoryId', '') - ->setAttribute('providerBranch', '') - ->setAttribute('providerSilentMode', false) - ->setAttribute('providerRootDirectory', '') - ->setAttribute('repositoryId', '') - ->setAttribute('repositoryInternalId', '')); + $dbForProject->updateDocument($resource->getCollection(), $resource->getId(), new Document([ + 'installationId' => '', + 'installationInternalId' => '', + 'providerRepositoryId' => '', + 'providerBranch' => '', + 'providerSilentMode' => false, + 'providerRootDirectory' => '', + 'repositoryId' => '', + 'repositoryInternalId' => '', + ])); } private function updateLatestDeployment(Database $dbForProject, Document $resource): Document diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 725779a969..53f67f34b0 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -135,15 +135,16 @@ class Create extends Action $resources = $authorization->skip(fn () => $dbForProject->find($collection, $queries)); foreach ($resources as $resource) { - $authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), $resource - ->setAttribute('installationId', '') - ->setAttribute('installationInternalId', '') - ->setAttribute('providerRepositoryId', '') - ->setAttribute('providerBranch', '') - ->setAttribute('providerSilentMode', false) - ->setAttribute('providerRootDirectory', '') - ->setAttribute('repositoryId', '') - ->setAttribute('repositoryInternalId', ''))); + $authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), new Document([ + 'installationId' => '', + 'installationInternalId' => '', + 'providerRepositoryId' => '', + 'providerBranch' => '', + 'providerSilentMode' => false, + 'providerRootDirectory' => '', + 'repositoryId' => '', + 'repositoryInternalId' => '', + ]))); } $cursor = count($resources) === 1000 ? $resources[array_key_last($resources)] : null; From 420c0b6d49ca58d921881d3a300f6ea5ef919612 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 25 Jun 2026 19:00:14 +0530 Subject: [PATCH 11/11] fix: bump utopia-php/vcs to 4.2.4 --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index 4e0de16a28..3072718613 100644 --- a/composer.lock +++ b/composer.lock @@ -5124,16 +5124,16 @@ }, { "name": "utopia-php/vcs", - "version": "4.2.3", + "version": "4.2.4", "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "598cbd2793a5a6394493f1046743e75ec6544a57" + "reference": "f19c61daa1e9594734d85ae25b3e90ba2b350d33" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/598cbd2793a5a6394493f1046743e75ec6544a57", - "reference": "598cbd2793a5a6394493f1046743e75ec6544a57", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/f19c61daa1e9594734d85ae25b3e90ba2b350d33", + "reference": "f19c61daa1e9594734d85ae25b3e90ba2b350d33", "shasum": "" }, "require": { @@ -5167,9 +5167,9 @@ ], "support": { "issues": "https://github.com/utopia-php/vcs/issues", - "source": "https://github.com/utopia-php/vcs/tree/4.2.3" + "source": "https://github.com/utopia-php/vcs/tree/4.2.4" }, - "time": "2026-06-16T12:14:08+00:00" + "time": "2026-06-25T13:27:19+00:00" }, { "name": "utopia-php/websocket",