1
0
Fork 0
mirror of synced 2024-10-01 01:37:56 +13:00

Add vcsState to user collection

This commit is contained in:
Khushboo Verma 2023-07-20 23:38:33 +05:30
parent 3fafefb2c2
commit e86382ce55
3 changed files with 43 additions and 25 deletions

View file

@ -21,6 +21,7 @@ use Utopia\Database\ID;
use Utopia\Database\Permission; use Utopia\Database\Permission;
use Utopia\Database\Role; use Utopia\Database\Role;
use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization;
use Utopia\Database\Validator\UID;
use Utopia\Detector\Adapter\CPP; use Utopia\Detector\Adapter\CPP;
use Utopia\Detector\Adapter\Dart; use Utopia\Detector\Adapter\Dart;
use Utopia\Detector\Adapter\Deno; use Utopia\Detector\Adapter\Deno;
@ -49,23 +50,27 @@ App::get('/v1/vcs/github/installations')
->label('sdk.response.type', Response::CONTENT_TYPE_HTML) ->label('sdk.response.type', Response::CONTENT_TYPE_HTML)
->label('sdk.methodType', 'webAuth') ->label('sdk.methodType', 'webAuth')
->param('redirect', '', fn ($clients) => new Host($clients), 'URL to redirect back to your Git authorization. Only console hostnames are allowed.', true, ['clients']) ->param('redirect', '', fn ($clients) => new Host($clients), 'URL to redirect back to your Git authorization. Only console hostnames are allowed.', true, ['clients'])
->param('projectId', '', new UID(), 'Project ID')
->inject('response') ->inject('response')
->inject('project') ->inject('user')
->action(function (string $redirect, Response $response, Document $project) { ->inject('dbForConsole')
$projectId = $project->getId(); ->action(function (string $redirect, string $projectId, Response $response, Document $user, Database $dbForConsole) {
$state = \json_encode([ $state = \json_encode([
'projectId' => $projectId, 'projectId' => $projectId,
'redirect' => $redirect 'redirect' => $redirect
]); ]);
// replace github url state with vcsState in user prefs attribute
$prefs = $user->getAttribute('prefs', []);
$prefs['vcsState'] = $state;
$user->setAttribute('prefs', $prefs);
$dbForConsole->updateDocument('users', $user->getId(), $user);
$appName = App::getEnv('VCS_GITHUB_APP_NAME'); $appName = App::getEnv('VCS_GITHUB_APP_NAME');
$response $response
->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
->addHeader('Pragma', 'no-cache') ->addHeader('Pragma', 'no-cache')
->redirect("https://github.com/apps/$appName/installations/new?" . \http_build_query([ ->redirect("https://github.com/apps/$appName/installations/new");
'state' => $state
]));
}); });
App::get('/v1/vcs/github/redirect') App::get('/v1/vcs/github/redirect')
@ -75,7 +80,7 @@ App::get('/v1/vcs/github/redirect')
->label('error', __DIR__ . '/../../views/general/error.phtml') ->label('error', __DIR__ . '/../../views/general/error.phtml')
->param('installation_id', '', new Text(256), 'GitHub installation ID', true) ->param('installation_id', '', new Text(256), 'GitHub installation ID', true)
->param('setup_action', '', new Text(256), 'GitHub setup actuon type', true) ->param('setup_action', '', new Text(256), 'GitHub setup actuon type', true)
->param('state', '', new Text(2048), 'GitHub state. Contains info sent when starting authorization flow.', true) // ->param('state', '', new Text(2048), 'GitHub state. Contains info sent when starting authorization flow.', true)
->param('code', '', new Text(2048), 'OAuth2 code.', true) ->param('code', '', new Text(2048), 'OAuth2 code.', true)
->inject('gitHub') ->inject('gitHub')
->inject('user') ->inject('user')
@ -83,7 +88,14 @@ App::get('/v1/vcs/github/redirect')
->inject('request') ->inject('request')
->inject('response') ->inject('response')
->inject('dbForConsole') ->inject('dbForConsole')
->action(function (string $installationId, string $setupAction, string $state, string $code, GitHub $github, Document $user, Document $project, Request $request, Response $response, Database $dbForConsole) { ->action(function (string $installationId, string $setupAction, string $code, GitHub $github, Document $user, Document $project, Request $request, Response $response, Database $dbForConsole) {
// replace github url state with vcsState in user prefs attribute
$prefs = $user->getAttribute('prefs', []);
$state = $prefs['vcsState'] ?? '{}';
$prefs['vcsState'] = '';
$user->setAttribute('prefs', $prefs);
$dbForConsole->updateDocument('users', $user->getId(), $user);
if (empty($state)) { if (empty($state)) {
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Installation requests from organisation members for the Appwrite GitHub App are currently unsupported. To proceed with the installation, login to the Appwrite Console and install the GitHub App.'); throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Installation requests from organisation members for the Appwrite GitHub App are currently unsupported. To proceed with the installation, login to the Appwrite Console and install the GitHub App.');
} }
@ -212,7 +224,7 @@ App::get('/v1/vcs/github/installations/:installationId/repositories')
$perPage = 100; $perPage = 100;
$loadPage = function ($page) use ($github, $perPage) { $loadPage = function ($page) use ($github, $perPage) {
$repos = $github->listRepositoriesForGitHubApp($page, $perPage); $repos = $github->listRepositoriesForVCSApp($page, $perPage);
return $repos; return $repos;
}; };
@ -636,7 +648,7 @@ App::post('/v1/vcs/github/incomingwebhook')
$signatureKey = App::getEnv('VCS_GITHUB_WEBHOOK_SECRET', ''); $signatureKey = App::getEnv('VCS_GITHUB_WEBHOOK_SECRET', '');
$valid = $github->validateWebhook($payload, $signature, $signatureKey); $valid = $github->validateWebhookEvent($payload, $signature, $signatureKey);
if (!$valid) { if (!$valid) {
throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, "Invalid webhook signature."); throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, "Invalid webhook signature.");
} }
@ -644,7 +656,7 @@ App::post('/v1/vcs/github/incomingwebhook')
$event = $request->getHeader('x-github-event', ''); $event = $request->getHeader('x-github-event', '');
$privateKey = App::getEnv('VCS_GITHUB_PRIVATE_KEY'); $privateKey = App::getEnv('VCS_GITHUB_PRIVATE_KEY');
$githubAppId = App::getEnv('VCS_GITHUB_APP_ID'); $githubAppId = App::getEnv('VCS_GITHUB_APP_ID');
$parsedPayload = $github->parseWebhookEventPayload($event, $payload); $parsedPayload = $github->parseWebhookEvent($event, $payload);
if ($event == $github::EVENT_PUSH) { if ($event == $github::EVENT_PUSH) {
$branchName = $parsedPayload["branch"]; $branchName = $parsedPayload["branch"];

View file

@ -174,7 +174,9 @@ class BuildsV1 extends Worker
$cloneRepository = !empty($vcsContribution) ? $vcsContribution->getAttribute('repositoryName', $repositoryName) : $repositoryName; $cloneRepository = !empty($vcsContribution) ? $vcsContribution->getAttribute('repositoryName', $repositoryName) : $repositoryName;
$branchName = $deployment->getAttribute('vcsBranch'); $branchName = $deployment->getAttribute('vcsBranch');
$gitCloneCommand = $github->generateGitCloneCommand($cloneOwner, $cloneRepository, $branchName, $tmpDirectory, $rootDirectory); var_dump("root " . $rootDirectory);
$gitCloneCommand = $github->generateCloneCommand($cloneOwner, $cloneRepository, $branchName, $tmpDirectory, $rootDirectory);
var_dump($gitCloneCommand);
$stdout = ''; $stdout = '';
$stderr = ''; $stderr = '';
Console::execute('mkdir -p /tmp/builds/' . $buildId, '', $stdout, $stderr); Console::execute('mkdir -p /tmp/builds/' . $buildId, '', $stdout, $stderr);
@ -197,7 +199,7 @@ class BuildsV1 extends Worker
if (!empty($templateRepositoryName) && !empty($templateOwnerName) && !empty($templateBranch)) { if (!empty($templateRepositoryName) && !empty($templateOwnerName) && !empty($templateBranch)) {
// Clone template repo // Clone template repo
$tmpTemplateDirectory = '/tmp/builds/' . $buildId . '/template'; $tmpTemplateDirectory = '/tmp/builds/' . $buildId . '/template';
$gitCloneCommandForTemplate = $github->generateGitCloneCommand($templateOwnerName, $templateRepositoryName, $templateBranch, $tmpTemplateDirectory, $templateRootDirectory); $gitCloneCommandForTemplate = $github->generateCloneCommand($templateOwnerName, $templateRepositoryName, $templateBranch, $tmpTemplateDirectory, $templateRootDirectory);
$exit = Console::execute($gitCloneCommandForTemplate, '', $stdout, $stderr); $exit = Console::execute($gitCloneCommandForTemplate, '', $stdout, $stderr);
if ($exit !== 0) { if ($exit !== 0) {

26
composer.lock generated
View file

@ -2705,7 +2705,7 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/utopia-php/vcs.git", "url": "https://github.com/utopia-php/vcs.git",
"reference": "580474fcbc5a88a908a1df3e124e47ebad2c014f" "reference": "c836cb55c2d3e0e28506228c55a6c6efcb16ddbd"
}, },
"require": { "require": {
"adhocore/jwt": "^1.1", "adhocore/jwt": "^1.1",
@ -2715,6 +2715,7 @@
}, },
"require-dev": { "require-dev": {
"laravel/pint": "1.2.*", "laravel/pint": "1.2.*",
"phpstan/phpstan": "1.8.*",
"phpunit/phpunit": "^9.4" "phpunit/phpunit": "^9.4"
}, },
"type": "library", "type": "library",
@ -2731,13 +2732,16 @@
}, },
"scripts": { "scripts": {
"lint": [ "lint": [
"./vendor/bin/pint --test" "./vendor/bin/pint --test --config pint.json"
], ],
"format": [ "format": [
"./vendor/bin/pint" "./vendor/bin/pint --config pint.json"
],
"check": [
"./vendor/bin/phpstan analyse --level 8 -c phpstan.neon src tests"
], ],
"test": [ "test": [
"vendor/bin/phpunit --configuration phpunit.xml" "./vendor/bin/phpunit --configuration phpunit.xml --debug"
] ]
}, },
"license": [ "license": [
@ -2750,7 +2754,7 @@
"utopia", "utopia",
"vcs" "vcs"
], ],
"time": "2023-06-28T10:54:07+00:00" "time": "2023-07-20T12:42:56+00:00"
}, },
{ {
"name": "utopia-php/websocket", "name": "utopia-php/websocket",
@ -3626,16 +3630,16 @@
}, },
{ {
"name": "phpstan/phpdoc-parser", "name": "phpstan/phpdoc-parser",
"version": "1.22.0", "version": "1.22.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/phpstan/phpdoc-parser.git", "url": "https://github.com/phpstan/phpdoc-parser.git",
"reference": "ec58baf7b3c7f1c81b3b00617c953249fb8cf30c" "reference": "65c39594fbd8c67abfc68bb323f86447bab79cc0"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/ec58baf7b3c7f1c81b3b00617c953249fb8cf30c", "url": "https://api.github.com/repos/phpstan/phpdoc-parser/zipball/65c39594fbd8c67abfc68bb323f86447bab79cc0",
"reference": "ec58baf7b3c7f1c81b3b00617c953249fb8cf30c", "reference": "65c39594fbd8c67abfc68bb323f86447bab79cc0",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -3667,9 +3671,9 @@
"description": "PHPDoc parser with support for nullable, intersection and generic types", "description": "PHPDoc parser with support for nullable, intersection and generic types",
"support": { "support": {
"issues": "https://github.com/phpstan/phpdoc-parser/issues", "issues": "https://github.com/phpstan/phpdoc-parser/issues",
"source": "https://github.com/phpstan/phpdoc-parser/tree/1.22.0" "source": "https://github.com/phpstan/phpdoc-parser/tree/1.22.1"
}, },
"time": "2023-06-01T12:35:21+00:00" "time": "2023-06-29T20:46:06+00:00"
}, },
{ {
"name": "phpunit/php-code-coverage", "name": "phpunit/php-code-coverage",