From 3e5e20fa906922ccda94082ee7994c5a06375c8f Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Wed, 12 Apr 2023 18:25:02 +0530 Subject: [PATCH] Delete vcs documents from collections on Uninstallation event --- app/config/collections.php | 11 +++++++++++ app/controllers/api/functions.php | 8 ++++++++ app/controllers/api/vcs.php | 19 ++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/app/config/collections.php b/app/config/collections.php index f92e53ab21..200a972f64 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -555,6 +555,17 @@ $collections = [ '$id' => ID::custom('vcs_repos'), 'name' => 'vcs_repos', 'attributes' => [ + [ + '$id' => ID::custom('vcsInstallationId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => true, + 'default' => null, + 'array' => false, + 'filters' => [] + ], [ '$id' => ID::custom('projectId'), 'type' => Database::VAR_STRING, diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index cc09d8491a..19623a7c54 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -527,6 +527,13 @@ App::put('/v1/functions/:functionId') $branchName = "main"; $code = $github->generateGitCloneCommand($repositoryId, $branchName); + $vcsInstallations = $dbForConsole->findOne('vcs_installations', [ + Query::equal('projectId', [$project->getId()]), + Query::equal('provider', ["GitHub"]), + Query::limit(100), + ]); + $vcsInstallationsId = $vcsInstallations->getId(); + //Add document in VCS map collection $vcs_repos = new Document([ '$id' => ID::unique(), @@ -535,6 +542,7 @@ App::put('/v1/functions/:functionId') Permission::update(Role::any()), Permission::delete(Role::any()), ], + 'vcsInstallationId' => $vcsInstallationsId, 'projectId' => $project->getId(), 'projectInternalId' => $projectInternalId, 'repositoryId' => $repositoryId, diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index e67b560a62..08ebd2f44e 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -191,7 +191,24 @@ App::post('/v1/vcs/github/incomingwebhook') } } else if ($event == $github::EVENT_INSTALLATION) { $installationId = $parsedPayload["installationId"]; - // delete documents from all respective collections + + $vcsInstallations = $dbForConsole->find('vcs_installations', [ + Query::equal('installationId', [$installationId]), + Query::limit(1000) + ]); + + foreach ($vcsInstallations as $installation) { + $vcsRepos = $dbForConsole->find('vcs_repos', [ + Query::equal('vcsInstallationId', [$installation->getId()]), + Query::limit(1000) + ]); + + foreach ($vcsRepos as $repo) { + $dbForConsole->deleteDocument('vcs_repos', $repo->getId()); + } + + $dbForConsole->deleteDocument('vcs_installations', $installation->getId()); + } } else if ($event == $github::EVENT_PULL_REQUEST) { // find last push to this branch }