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\Role;
use Utopia\Database\Validator\Authorization;
use Utopia\Database\Validator\UID;
use Utopia\Detector\Adapter\CPP;
use Utopia\Detector\Adapter\Dart;
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.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('projectId', '', new UID(), 'Project ID')
->inject('response')
->inject('project')
->action(function (string $redirect, Response $response, Document $project) {
$projectId = $project->getId();
->inject('user')
->inject('dbForConsole')
->action(function (string $redirect, string $projectId, Response $response, Document $user, Database $dbForConsole) {
$state = \json_encode([
'projectId' => $projectId,
'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');
$response
->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
->addHeader('Pragma', 'no-cache')
->redirect("https://github.com/apps/$appName/installations/new?" . \http_build_query([
'state' => $state
]));
->redirect("https://github.com/apps/$appName/installations/new");
});
App::get('/v1/vcs/github/redirect')
@ -75,7 +80,7 @@ App::get('/v1/vcs/github/redirect')
->label('error', __DIR__ . '/../../views/general/error.phtml')
->param('installation_id', '', new Text(256), 'GitHub installation ID', 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)
->inject('gitHub')
->inject('user')
@ -83,7 +88,14 @@ App::get('/v1/vcs/github/redirect')
->inject('request')
->inject('response')
->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)) {
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;
$loadPage = function ($page) use ($github, $perPage) {
$repos = $github->listRepositoriesForGitHubApp($page, $perPage);
$repos = $github->listRepositoriesForVCSApp($page, $perPage);
return $repos;
};
@ -636,7 +648,7 @@ App::post('/v1/vcs/github/incomingwebhook')
$signatureKey = App::getEnv('VCS_GITHUB_WEBHOOK_SECRET', '');
$valid = $github->validateWebhook($payload, $signature, $signatureKey);
$valid = $github->validateWebhookEvent($payload, $signature, $signatureKey);
if (!$valid) {
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', '');
$privateKey = App::getEnv('VCS_GITHUB_PRIVATE_KEY');
$githubAppId = App::getEnv('VCS_GITHUB_APP_ID');
$parsedPayload = $github->parseWebhookEventPayload($event, $payload);
$parsedPayload = $github->parseWebhookEvent($event, $payload);
if ($event == $github::EVENT_PUSH) {
$branchName = $parsedPayload["branch"];

View file

@ -174,7 +174,9 @@ class BuildsV1 extends Worker
$cloneRepository = !empty($vcsContribution) ? $vcsContribution->getAttribute('repositoryName', $repositoryName) : $repositoryName;
$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 = '';
$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)) {
// Clone template repo
$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);
if ($exit !== 0) {

26
composer.lock generated
View file

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