diff --git a/composer.lock b/composer.lock index b1fa6bf2d1..f719710677 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", diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index c2b8837c13..d0806ccadf 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -295,7 +295,15 @@ 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 ($e->getCode() === 404 + && $resource->getAttribute('installationId', '') === $installationId) { + $this->disconnectVcs($resource, $dbForProject, $dbForPlatform); + } + throw $e; + } } Span::add('timings.setup', \round(\microtime(true) - $phaseStart, 3)); @@ -1555,6 +1563,24 @@ 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(), new Document([ + 'installationId' => '', + 'installationInternalId' => '', + 'providerRepositoryId' => '', + 'providerBranch' => '', + 'providerSilentMode' => false, + 'providerRootDirectory' => '', + 'repositoryId' => '', + 'repositoryInternalId' => '', + ])); + } + private function updateLatestDeployment(Database $dbForProject, Document $resource): Document { $latestDeployment = $dbForProject->findOne('deployments', [ 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..53f67f34b0 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, @@ -92,35 +92,89 @@ class Create extends Action return; } - private function handleInstallationEvent( + protected function handleInstallationEvent( 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', [ - 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 = $authorization->skip(fn () => $dbForPlatform->find('installations', $installationQueries)); - foreach ($installations as $installation) { - $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ - Query::equal('installationInternalId', [$installation->getSequence()]), - Query::limit(1000) - ])); + foreach ($installations as $installation) { + $projectId = $installation->getAttribute('projectId', ''); + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId)); - foreach ($repositories as $repository) { - $authorization->skip(fn () => $dbForPlatform->deleteDocument('repositories', $repository->getId())); + 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 ($resources as $resource) { + $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; + } 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())); } - $authorization->skip(fn () => $dbForPlatform->deleteDocument('installations', $installation->getId())); - } + $installationCursor = count($installations) === 1000 ? $installations[array_key_last($installations)] : null; + } while ($installationCursor !== null); } private function handlePushEvent( diff --git a/tests/unit/Functions/BuildsTest.php b/tests/unit/Functions/BuildsTest.php index b757856c83..36515d2f5b 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,62 @@ 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) { + $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); + } + + 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); diff --git a/tests/unit/Vcs/GitHubInstallationEventTest.php b/tests/unit/Vcs/GitHubInstallationEventTest.php new file mode 100644 index 0000000000..045ac5d9d6 --- /dev/null +++ b/tests/unit/Vcs/GitHubInstallationEventTest.php @@ -0,0 +1,160 @@ +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 (string $collection): array => 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 (string $collection): array => $collection === 'functions' ? [$function] : []); + + $dbForProject->expects($this->once()) + ->method('updateDocument') + ->with('functions', 'func1', $this->callback(function (Document $doc) { + $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; + })); + + // 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()); + + $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 (string $collection): array => match ($collection) { + 'installations' => [$installation], + 'repositories' => [], + default => [], + }); + + $dbForPlatform->expects($this->once()) + ->method('getDocument') + ->with('projects', 'project1') + ->willReturn(new Document()); + + // 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()); + + $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); + } +}