diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index abb9ba69a0..ded35b685a 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -57,10 +57,11 @@ App::get('/v1/vcs/github/incominginstallation') ->param('installation_id', '', new Text(256), 'GitHub installation ID') ->param('setup_action', '', new Text(256), 'GitHub setup actuon type') ->param('state', '', new Text(2048), 'GitHub state. Contains info sent when starting authorization flow.') + ->inject('gitHub') ->inject('request') ->inject('response') ->inject('dbForConsole') - ->action(function (string $installationId, string $setupAction, string $state, Request $request, Response $response, Database $dbForConsole) { + ->action(function (string $installationId, string $setupAction, string $state, GitHub $github, Request $request, Response $response, Database $dbForConsole) { $state = \json_decode($state, true); $projectId = $state['projectId'] ?? ''; @@ -79,7 +80,6 @@ App::get('/v1/vcs/github/incominginstallation') $installationId = $installationId; $privateKey = App::getEnv('VCS_GITHUB_PRIVATE_KEY'); $githubAppId = App::getEnv('VCS_GITHUB_APP_ID'); - $github = new GitHub(); $github->initialiseVariables($installationId, $privateKey, $githubAppId); $owner = $github->getOwnerName($installationId); @@ -130,10 +130,11 @@ App::get('v1/vcs/github/installations/:installationId/repositories') ->label('sdk.response.model', Response::MODEL_REPOSITORY_LIST) ->param('installationId', '', new Text(256), 'Installation Id') ->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true) + ->inject('gitHub') ->inject('response') ->inject('project') ->inject('dbForConsole') - ->action(function (string $vcsInstallationId, string $search, Response $response, Document $project, Database $dbForConsole) { + ->action(function (string $vcsInstallationId, string $search, GitHub $github, Response $response, Document $project, Database $dbForConsole) { if (empty($search)) { $search = ""; } @@ -149,7 +150,6 @@ App::get('v1/vcs/github/installations/:installationId/repositories') $installationId = $installation->getAttribute('installationId'); $privateKey = App::getEnv('VCS_GITHUB_PRIVATE_KEY'); $githubAppId = App::getEnv('VCS_GITHUB_APP_ID'); - $github = new GitHub(); $github->initialiseVariables($installationId, $privateKey, $githubAppId); $page = 1; @@ -200,10 +200,11 @@ App::get('v1/vcs/github/installations/:installationId/repositories/:repositoryId ->label('sdk.response.model', Response::MODEL_BRANCH_LIST) ->param('installationId', '', new Text(256), 'Installation Id') ->param('repositoryId', '', new Text(256), 'Repository Id') + ->inject('gitHub') ->inject('response') ->inject('project') ->inject('dbForConsole') - ->action(function (string $vcsInstallationId, string $repositoryId, Response $response, Document $project, Database $dbForConsole) { + ->action(function (string $vcsInstallationId, string $repositoryId, GitHub $github, Response $response, Document $project, Database $dbForConsole) { $installation = $dbForConsole->getDocument('vcs_installations', $vcsInstallationId, [ Query::equal('projectInternalId', [$project->getInternalId()]) ]); @@ -215,7 +216,6 @@ App::get('v1/vcs/github/installations/:installationId/repositories/:repositoryId $installationId = $installation->getAttribute('installationId'); $privateKey = App::getEnv('VCS_GITHUB_PRIVATE_KEY'); $githubAppId = App::getEnv('VCS_GITHUB_APP_ID'); - $github = new GitHub(); $github->initialiseVariables($installationId, $privateKey, $githubAppId); $owner = $github->getOwnerName($installationId); @@ -299,15 +299,15 @@ App::post('/v1/vcs/github/incomingwebhook') ->desc('Captures GitHub Webhook Events') ->groups(['api', 'vcs']) ->label('scope', 'public') + ->inject('gitHub') ->inject('request') ->inject('response') ->inject('dbForConsole') ->inject('getProjectDB') ->action( - function (Request $request, Response $response, Database $dbForConsole, callable $getProjectDB) use ($createGitDeployments) { + function (GitHub $github, Request $request, Response $response, Database $dbForConsole, callable $getProjectDB) use ($createGitDeployments) { $event = $request->getHeader('x-github-event', ''); $payload = $request->getRawPayload(); - $github = new GitHub(); $privateKey = App::getEnv('VCS_GITHUB_PRIVATE_KEY'); $githubAppId = App::getEnv('VCS_GITHUB_APP_ID'); $parsedPayload = $github->parseWebhookEventPayload($event, $payload); diff --git a/app/init.php b/app/init.php index 3030364e80..507ca95c88 100644 --- a/app/init.php +++ b/app/init.php @@ -69,6 +69,7 @@ use Utopia\Pools\Group; use Utopia\Pools\Pool; use Ahc\Jwt\JWT; use Ahc\Jwt\JWTException; +use Appwrite\Auth\OAuth2\Github; use Appwrite\Event\Func; use MaxMind\Db\Reader; use PHPMailer\PHPMailer\PHPMailer; @@ -77,6 +78,7 @@ use Utopia\CLI\Console; use Utopia\Queue; use Utopia\Queue\Connection; use Utopia\Storage\Storage; +use Utopia\VCS\Adapter\Git\GitHub as VcsGitHub; const APP_NAME = 'Appwrite'; const APP_DOMAIN = 'appwrite.io'; @@ -1231,3 +1233,7 @@ App::setResource('heroes', function () { $list = (file_exists($path)) ? json_decode(file_get_contents($path), true) : []; return $list; }, []); + +App::setResource('gitHub', function (Cache $cache) { + return new VcsGitHub($cache); +}, ['cache']); diff --git a/app/workers/builds.php b/app/workers/builds.php index 3ca42c22ff..7cb4bbcbbb 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -51,7 +51,8 @@ class BuildsV1 extends Worker case BUILD_TYPE_DEPLOYMENT: case BUILD_TYPE_RETRY: Console::info('Creating build for deployment: ' . $deployment->getId()); - $this->buildDeployment($project, $resource, $deployment, $SHA, $targetUrl); + $github = new GitHub($this->getCache()); + $this->buildDeployment($github, $project, $resource, $deployment, $SHA, $targetUrl); break; default: @@ -60,7 +61,7 @@ class BuildsV1 extends Worker } } - protected function buildDeployment(Document $project, Document $function, Document $deployment, string $SHA = '', string $targetUrl = '') + protected function buildDeployment(GitHub $github, Document $project, Document $function, Document $deployment, string $SHA = '', string $targetUrl = '') { global $register; @@ -115,7 +116,6 @@ class BuildsV1 extends Worker $privateKey = App::getEnv('VCS_GITHUB_PRIVATE_KEY'); $githubAppId = App::getEnv('VCS_GITHUB_APP_ID'); - $github = new GitHub(); $github->initialiseVariables($installationId, $privateKey, $githubAppId); $owner = $github->getOwnerName($installationId); $repositoryName = $github->getRepositoryName($repositoryId); diff --git a/composer.lock b/composer.lock index a047ff1ec0..d949f65d8f 100644 --- a/composer.lock +++ b/composer.lock @@ -3090,11 +3090,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "de473962979c1ebd9c98952a0c22ce3e29242002" + "reference": "1331193f6ba0443a87f4fd665a8f91dc07817370" }, "require": { "adhocore/jwt": "^1.1", "php": ">=8.0", + "utopia-php/cache": "^0.8.0", "utopia-php/framework": "0.26.*", "vlucas/phpdotenv": "^5.5" }, @@ -3134,7 +3135,7 @@ "utopia", "vcs" ], - "time": "2023-05-25T15:25:45+00:00" + "time": "2023-05-26T08:41:54+00:00" }, { "name": "utopia-php/websocket",