From 4285d99f08c885ef62f0941593eda02bba974f18 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Thu, 17 Aug 2023 19:24:57 +0530 Subject: [PATCH] Added exception for repo creation + small fixes --- app/config/errors.php | 5 +++++ app/console | 2 +- app/controllers/api/vcs.php | 13 +++++++++++-- app/workers/builds.php | 15 +++++++++------ src/Appwrite/Extend/Exception.php | 1 + 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index ed2b897d9..cba8ed336 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -385,6 +385,11 @@ return [ 'description' => 'External contribution is already authorized.', 'code' => 409, ], + Exception::GENERAL_PROVIDER_FAILURE => [ + 'name' => Exception::GENERAL_PROVIDER_FAILURE, + 'description' => 'VCS (Version Control System) provider failed to proccess the request.', + 'code' => 400, + ], /** Functions */ Exception::FUNCTION_NOT_FOUND => [ diff --git a/app/console b/app/console index a1ec99e86..c4331ea10 160000 --- a/app/console +++ b/app/console @@ -1 +1 @@ -Subproject commit a1ec99e86331e5eeb56614c69f50e83f334deafb +Subproject commit c4331ea10dce0cbf403cae9aec8273f2d003ba2e diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index e6797cc43..0f8ee9f5f 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -36,6 +36,7 @@ use Utopia\Detector\Adapter\Swift; use Utopia\Detector\Detector; use Utopia\Validator\Boolean; +use function PHPUnit\Framework\throwException; use function Swoole\Coroutine\batch; $createGitDeployments = function (GitHub $github, string $providerInstallationId, array $repositories, string $providerBranch, string $providerBranchUrl, string $providerRepositoryName, string $providerRepositoryUrl, string $providerRepositoryOwner, string $providerCommitHash, string $providerCommitAuthor, string $providerCommitAuthorUrl, string $providerCommitMessage, string $providerCommitUrl, string $providerPullRequestId, bool $external, Database $dbForConsole, callable $getProjectDB, Request $request) { @@ -702,7 +703,11 @@ App::post('/v1/vcs/github/installations/:installationId/providerRepositories') $dbForConsole->updateDocument('identities', $identity->getId(), $identity); } - $repository = $oauth2->createRepository($accessToken, $name, $private); + try { + $repository = $oauth2->createRepository($accessToken, $name, $private); + } catch (Exception $exception) { + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, "GitHub failed to process the request: " . $exception->getMessage()); + } } else { $providerInstallationId = $installation->getAttribute('providerInstallationId'); $privateKey = App::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY'); @@ -710,7 +715,11 @@ App::post('/v1/vcs/github/installations/:installationId/providerRepositories') $github->initializeVariables($providerInstallationId, $privateKey, $githubAppId); $owner = $github->getOwnerName($providerInstallationId); - $repository = $github->createRepository($owner, $name, $private); + try { + $repository = $github->createRepository($owner, $name, $private); + } catch (Exception $exception) { + throw new Exception(Exception::GENERAL_PROVIDER_FAILURE, "GitHub failed to process the request: " . $exception->getMessage()); + } } if (isset($repository['message'])) { diff --git a/app/workers/builds.php b/app/workers/builds.php index 038650acd..11ec44f12 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -454,12 +454,15 @@ class BuildsV1 extends Worker /** Update function schedule */ $dbForConsole = $this->getConsoleDB(); // Inform scheduler if function is still active - $schedule = $dbForConsole->getDocument('schedules', $function->getAttribute('scheduleId')); - $schedule - ->setAttribute('resourceUpdatedAt', DateTime::now()) - ->setAttribute('schedule', $function->getAttribute('schedule')) - ->setAttribute('active', !empty($function->getAttribute('schedule')) && !empty($function->getAttribute('deployment'))); - Authorization::skip(fn () => $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule)); + $scheduleId = $function->getAttribute('scheduleId', ''); + if (!empty($scheduleId)) { + $schedule = $dbForConsole->getDocument('schedules', $scheduleId); + $schedule + ->setAttribute('resourceUpdatedAt', DateTime::now()) + ->setAttribute('schedule', $function->getAttribute('schedule')) + ->setAttribute('active', !empty($function->getAttribute('schedule')) && !empty($function->getAttribute('deployment'))); + Authorization::skip(fn () => $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule)); + } } catch (\Throwable $th) { $endTime = DateTime::now(); $durationEnd = \microtime(true); diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 4f467f0b1..79e3059f0 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -121,6 +121,7 @@ class Exception extends \Exception public const PROVIDER_REPOSITORY_NOT_FOUND = 'provider_repository_not_found'; public const REPOSITORY_NOT_FOUND = 'repository_not_found'; public const PROVIDER_CONTRIBUTION_CONFLICT = 'provider_contribution_conflict'; + public const GENERAL_PROVIDER_FAILURE = 'general_provider_failure'; /** Functions */ public const FUNCTION_NOT_FOUND = 'function_not_found';