Merge pull request #12667 from appwrite/fix/vcs-disconnect-on-revoked-installation

fix: disconnect VCS when GitHub installation is revoked (404)
This commit is contained in:
Harsh Mahajan authored and GitHub committed 2026-06-25 19:19:37 +05:30
commit 7f270316ea
5 files changed
+320 -23

No files matched your search

Generated
+6 -6
View File
@@ -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",
@@ -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', [
@@ -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(
+57
View File
@@ -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);
@@ -0,0 +1,160 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Vcs;
use Appwrite\Platform\Modules\VCS\Http\GitHub\Events\Create;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Validator\Authorization;
final class GitHubInstallationEventTest extends TestCase
{
private Create $handler;
public function setUp(): void
{
$this->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);
}
}