Address PR reviews

This commit is contained in:
Matej Bačo
2023-07-28 09:56:07 +02:00
parent 930f9d9526
commit ce98b04348
11 changed files with 126 additions and 71 deletions
+29 -1
View File
@@ -732,6 +732,17 @@ $collections = [
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('variables'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 16384,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => ['subQueryProjectVariables'],
],
],
'indexes' => [
[
@@ -2412,7 +2423,17 @@ $collections = [
'array' => false,
'filters' => [],
],
// TODO: Resource Internal ID?
[
'$id' => ID::custom('resourceInternalId'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => Database::LENGTH_KEY,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('resourceType'),
'type' => Database::VAR_STRING,
@@ -2479,6 +2500,13 @@ $collections = [
'lengths' => [Database::LENGTH_KEY],
'orders' => [Database::ORDER_ASC],
],
[
'$id' => '_key_resourceInternalId',
'type' => Database::INDEX_KEY,
'attributes' => ['resourceInternalId'],
'lengths' => [Database::LENGTH_KEY],
'orders' => [Database::ORDER_ASC],
],
[
'$id' => ID::custom('_key_resourceType'),
'type' => Database::INDEX_KEY,
+2 -2
View File
@@ -566,8 +566,8 @@ return [
'description' => '_APP_DOMAIN_TARGET must be a public domain.',
'code' => 501,
],
Exception::RULE_RESOURCE_ID_NOT_FOUND => [
'name' => Exception::RULE_RESOURCE_ID_NOT_FOUND,
Exception::RULE_RESOURCE_NOT_FOUND => [
'name' => Exception::RULE_RESOURCE_NOT_FOUND,
'description' => 'Resource could not be found. Check resourceId and resourceType.',
'code' => 404,
],
+54 -44
View File
@@ -155,32 +155,6 @@ App::post('/v1/functions')
throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'When connecting to VCS you need to provide all VCS parameters.');
}
$vcsRepositoryDocId = '';
$vcsRepositoryDocInternalId = '';
// Git connect logic
if (!empty($vcsRepositoryId)) {
$vcsRepoDoc = $dbForConsole->createDocument('vcsRepos', new Document([
'$id' => ID::unique(),
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
'vcsInstallationId' => $installation->getId(),
'vcsInstallationInternalId' => $installation->getInternalId(),
'projectId' => $project->getId(),
'projectInternalId' => $project->getInternalId(),
'repositoryId' => $vcsRepositoryId,
'resourceId' => $functionId,
'resourceType' => 'function',
'pullRequests' => []
]));
$vcsRepositoryDocId = $vcsRepoDoc->getId();
$vcsRepositoryDocInternalId = $vcsRepoDoc->getInternalId();
}
$function = $dbForProject->createDocument('functions', new Document([
'$id' => $functionId,
'execute' => $execute,
@@ -199,8 +173,8 @@ App::post('/v1/functions')
'vcsInstallationId' => $installation->getId(),
'vcsInstallationInternalId' => $installation->getInternalId(),
'vcsRepositoryId' => $vcsRepositoryId,
'vcsRepositoryDocId' => $vcsRepositoryDocId,
'vcsRepositoryDocInternalId' => $vcsRepositoryDocInternalId,
'vcsRepositoryDocId' => '',
'vcsRepositoryDocInternalId' => '',
'vcsBranch' => $vcsBranch,
'vcsRootDirectory' => $vcsRootDirectory,
'vcsSilentMode' => $vcsSilentMode,
@@ -208,6 +182,38 @@ App::post('/v1/functions')
'version' => 'v3'
]));
$vcsRepositoryDocId = '';
$vcsRepositoryDocInternalId = '';
// Git connect logic
if (!empty($vcsRepositoryId)) {
$vcsRepoDoc = $dbForConsole->createDocument('vcsRepos', new Document([
'$id' => ID::unique(),
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
'vcsInstallationId' => $installation->getId(),
'vcsInstallationInternalId' => $installation->getInternalId(),
'projectId' => $project->getId(),
'projectInternalId' => $project->getInternalId(),
'repositoryId' => $vcsRepositoryId,
'resourceId' => $function->getId(),
'resourceInternalId' => $function->getInternalId(),
'resourceType' => 'function',
'pullRequests' => []
]));
$vcsRepositoryDocId = $vcsRepoDoc->getId();
$vcsRepositoryDocInternalId = $vcsRepoDoc->getInternalId();
$function = $dbForProject->updateDocument('functions', $function->getId(), $function
->setAttribute('vcsRepositoryDocId', $vcsRepositoryDocId)
->setAttribute('vcsRepositoryDocInternalId', $vcsRepositoryDocInternalId));
}
$schedule = Authorization::skip(
fn () => $dbForConsole->createDocument('schedules', new Document([
'region' => App::getEnv('_APP_REGION', 'default'), // Todo replace with projects region
@@ -680,7 +686,7 @@ App::put('/v1/functions/:functionId')
if ($isConnected && empty($vcsRepositoryId)) {
$repoDocs = $dbForConsole->find('vcsRepos', [
Query::equal('projectInternalId', [$project->getInternalId()]),
Query::equal('resourceId', [$functionId]),
Query::equal('resourceInternalId', [$function->getInternalId()]),
Query::equal('resourceType', ['function']),
Query::limit(100),
]);
@@ -712,7 +718,8 @@ App::put('/v1/functions/:functionId')
'projectId' => $project->getId(),
'projectInternalId' => $project->getInternalId(),
'repositoryId' => $vcsRepositoryId,
'resourceId' => $functionId,
'resourceId' => $function->getId(),
'resourceInternalId' => $function->getInternalId(),
'resourceType' => 'function',
'pullRequests' => []
]));
@@ -758,13 +765,12 @@ App::put('/v1/functions/:functionId')
$redeployVcsLogic($request, $function, $project, $installation, $dbForProject, new Document());
}
// 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));
$eventsInstance->setParam('functionId', $function->getId());
@@ -820,6 +826,7 @@ App::patch('/v1/functions/:functionId/deployments/:deploymentId')
'deployment' => $deployment->getId()
])));
// Inform scheduler if function is still active
$schedule = $dbForConsole->getDocument('schedules', $function->getAttribute('scheduleId'));
$schedule
->setAttribute('resourceUpdatedAt', DateTime::now())
@@ -866,12 +873,11 @@ App::delete('/v1/functions/:functionId')
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove function from DB');
}
// Inform scheduler to no longer run function
$schedule = $dbForConsole->getDocument('schedules', $function->getAttribute('scheduleId'));
$schedule
->setAttribute('resourceUpdatedAt', DateTime::now())
->setAttribute('active', false);
Authorization::skip(fn () => $dbForConsole->updateDocument('schedules', $schedule->getId(), $schedule));
$deletes
@@ -1411,11 +1417,6 @@ App::post('/v1/functions/:functionId/executions')
'agent' => $agent
]);
if ($function->getAttribute('logging')) {
/** @var Document $execution */
$execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution));
}
$jwt = ''; // initialize
if (!$user->isEmpty()) { // If userId exists, generate a JWT for function
$sessions = $user->getAttribute('sessions', []);
@@ -1464,6 +1465,11 @@ App::post('/v1/functions/:functionId/executions')
->setContext('function', $function);
if ($async) {
if ($function->getAttribute('logging')) {
/** @var Document $execution */
$execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution));
}
$queueForFunctions
->setType('http')
->setExecution($execution)
@@ -1487,10 +1493,8 @@ App::post('/v1/functions/:functionId/executions')
$vars = [];
// Shared vars
$vars = \array_merge($vars, \array_reduce($dbForProject->find('variables', [
Query::equal('resourceType', ['project']),
Query::limit(APP_LIMIT_SUBQUERY)
]), function (array $carry, Document $var) {
$varsShared = $project->getAttribute('variables', []);
$vars = \array_merge($vars, \array_reduce($varsShared, function (array $carry, Document $var) {
$carry[$var->getAttribute('key')] = $var->getAttribute('value') ?? '';
return $carry;
}, []));
@@ -1550,7 +1554,8 @@ App::post('/v1/functions/:functionId/executions')
}
if ($function->getAttribute('logging')) {
Authorization::skip(fn () => $dbForProject->updateDocument('executions', $executionId, $execution));
/** @var Document $execution */
$execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', $execution));
}
// TODO revise this later using route label
@@ -1748,9 +1753,11 @@ App::post('/v1/functions/:functionId/variables')
} catch (DuplicateException $th) {
throw new Exception(Exception::VARIABLE_ALREADY_EXISTS);
}
$dbForConsole->deleteCachedDocument('projects', $project->getId());
$dbForProject->updateDocument('functions', $function->getId(), $function->setAttribute('live', false));
// Inform scheduler to pull the latest changes
$schedule = $dbForConsole->getDocument('schedules', $function->getAttribute('scheduleId'));
$schedule
->setAttribute('resourceUpdatedAt', DateTime::now())
@@ -1874,9 +1881,11 @@ App::put('/v1/functions/:functionId/variables/:variableId')
} catch (DuplicateException $th) {
throw new Exception(Exception::VARIABLE_ALREADY_EXISTS);
}
$dbForConsole->deleteCachedDocument('projects', $project->getId());
$dbForProject->updateDocument('functions', $function->getId(), $function->setAttribute('live', false));
// Inform scheduler to pull the latest changes
$schedule = $dbForConsole->getDocument('schedules', $function->getAttribute('scheduleId'));
$schedule
->setAttribute('resourceUpdatedAt', DateTime::now())
@@ -1927,6 +1936,7 @@ App::delete('/v1/functions/:functionId/variables/:variableId')
$dbForProject->updateDocument('functions', $function->getId(), $function->setAttribute('live', false));
// Inform scheduler to pull the latest changes
$schedule = $dbForConsole->getDocument('schedules', $function->getAttribute('scheduleId'));
$schedule
->setAttribute('resourceUpdatedAt', DateTime::now())
+6 -2
View File
@@ -139,7 +139,8 @@ App::post('/v1/project/variables')
->inject('project')
->inject('response')
->inject('dbForProject')
->action(function (string $key, string $value, Document $project, Response $response, Database $dbForProject) {
->inject('dbForConsole')
->action(function (string $key, string $value, Document $project, Response $response, Database $dbForProject, Database $dbForConsole) {
$variableId = ID::unique();
$variable = new Document([
@@ -162,6 +163,7 @@ App::post('/v1/project/variables')
} catch (DuplicateException $th) {
throw new Exception(Exception::VARIABLE_ALREADY_EXISTS);
}
$dbForConsole->deleteCachedDocument('projects', $project->getId());
$functions = $dbForProject->find('functions', [
Query::limit(APP_LIMIT_SUBQUERY)
@@ -244,7 +246,8 @@ App::put('/v1/project/variables/:variableId')
->inject('project')
->inject('response')
->inject('dbForProject')
->action(function (string $variableId, string $key, ?string $value, Document $project, Response $response, Database $dbForProject) {
->inject('dbForConsole')
->action(function (string $variableId, string $key, ?string $value, Document $project, Response $response, Database $dbForProject, Database $dbForConsole) {
$variable = $dbForProject->getDocument('variables', $variableId);
if ($variable === false || $variable->isEmpty() || $variable->getAttribute('resourceType') !== 'project') {
throw new Exception(Exception::VARIABLE_NOT_FOUND);
@@ -260,6 +263,7 @@ App::put('/v1/project/variables/:variableId')
} catch (DuplicateException $th) {
throw new Exception(Exception::VARIABLE_ALREADY_EXISTS);
}
$dbForConsole->deleteCachedDocument('projects', $project->getId());
$functions = $dbForProject->find('functions', [
Query::limit(APP_LIMIT_SUBQUERY)
+1 -1
View File
@@ -73,7 +73,7 @@ App::post('/v1/proxy/rules')
$function = $dbForProject->getDocument('functions', $resourceId);
if ($function->isEmpty()) {
throw new Exception(Exception::RULE_RESOURCE_ID_NOT_FOUND);
throw new Exception(Exception::RULE_RESOURCE_NOT_FOUND);
}
$resourceInternalId = $function->getInternalId();
+7 -6
View File
@@ -43,11 +43,11 @@ Config::setParam('domainVerification', false);
Config::setParam('cookieDomain', 'localhost');
Config::setParam('cookieSamesite', Response::COOKIE_SAMESITE_NONE);
function router(App $utopia, Database $dbForConsole, SwooleRequest $swooleRequest, Response $response)
function router(App $utopia, Database $dbForConsole, SwooleRequest $swooleRequest, Request $request, Response $response)
{
$utopia->getRoute()->label('error', __DIR__ . '/../views/general/error.phtml');
$host = $swooleRequest->header['host'] ?? '';
$host = $request->getHostname() ?? '';
$route = Authorization::skip(
fn() => $dbForConsole->find('rules', [
@@ -169,11 +169,12 @@ App::init()
/*
* Appwrite Router
*/
$host = $swooleRequest->header['host'] ?? '';
$host = $request->getHostname() ?? '';
$mainDomain = App::getEnv('_APP_DOMAIN', '');
// Only run Router when external domain
if ($host !== $mainDomain && $host !== 'localhost') {
if (router($utopia, $dbForConsole, $swooleRequest, $response)) {
if (router($utopia, $dbForConsole, $swooleRequest, $request, $response)) {
return;
}
}
@@ -460,11 +461,11 @@ App::options()
/*
* Appwrite Router
*/
$host = $swooleRequest->header['host'] ?? '';
$host = $request->getHostname() ?? '';
$mainDomain = App::getEnv('_APP_DOMAIN', '');
// Only run Router when external domain
if ($host !== $mainDomain && $host !== 'localhost') {
if (router($utopia, $dbForConsole, $swooleRequest, $response)) {
if (router($utopia, $dbForConsole, $swooleRequest, $request, $response)) {
return;
}
}
+15
View File
@@ -436,6 +436,21 @@ Database::addFilter(
}
);
// READ-ONLY! TO update, write directly to 'variables' collection. After update to vars, make sure to deleteCachedDocument()
Database::addFilter(
'subQueryProjectVariables',
function (mixed $value) {
return null;
},
function (mixed $value, Document $document, Database $database) {
return $database
->find('variables', [
Query::equal('resourceType', ['project']),
Query::limit(APP_LIMIT_SUBQUERY)
]);
}
);
/**
* DB Formats
*/
+2 -3
View File
@@ -427,13 +427,12 @@ 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());
$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();
+3 -5
View File
@@ -129,11 +129,9 @@ Server::setResource('execute', function () {
$vars = [];
// global vars
$vars = \array_merge($vars, \array_reduce($dbForProject->find('variables', [
Query::equal('resourceType', ['project']),
Query::limit(APP_LIMIT_SUBQUERY)
]), function (array $carry, Document $var) {
// Shared vars
$varsShared = $project->getAttribute('variables', []);
$vars = \array_merge($vars, \array_reduce($varsShared, function (array $carry, Document $var) {
$carry[$var->getAttribute('key')] = $var->getAttribute('value') ?? '';
return $carry;
}, []));
+1 -1
View File
@@ -178,7 +178,7 @@ class Exception extends \Exception
/** Proxy */
public const RULE_CONFIGURATION_MISSING = 'rule_configuration_missing';
public const RULE_RESOURCE_ID_MISSING = 'rule_resource_id_missing';
public const RULE_RESOURCE_ID_NOT_FOUND = 'rule_resource_id_not_found';
public const RULE_RESOURCE_NOT_FOUND = 'rule_resource_not_found';
public const RULE_NOT_FOUND = 'rule_not_found';
public const RULE_ALREADY_EXISTS = 'rule_already_exists';
public const RULE_VERIFICATION_FAILED = 'rule_verification_failed';
+6 -6
View File
@@ -97,7 +97,7 @@ class Func extends Model
'type' => self::TYPE_INTEGER,
'description' => 'Function execution timeout in seconds.',
'default' => 15,
'example' => 1592981237,
'example' => 300,
])
->addRule('entrypoint', [
'type' => self::TYPE_STRING,
@@ -113,9 +113,9 @@ class Func extends Model
])
->addRule('vcsInstallationId', [
'type' => self::TYPE_STRING,
'description' => 'Function vcs installation id.',
'description' => 'Function VCS (Version Control System) installation id.',
'default' => '',
'example' => '644051bd6572792165cc',
'example' => '6m40at4ejk5h2u9s1hboo',
])
->addRule('vcsRepositoryId', [
'type' => self::TYPE_STRING,
@@ -125,19 +125,19 @@ class Func extends Model
])
->addRule('vcsBranch', [
'type' => self::TYPE_STRING,
'description' => 'Git branch name',
'description' => 'VCS (Version Control System) branch name',
'default' => '',
'example' => 'main',
])
->addRule('vcsRootDirectory', [
'type' => self::TYPE_STRING,
'description' => 'Path to function in git repository',
'description' => 'Path to function in VCS (Version Control System) repository',
'default' => '',
'example' => 'functions/helloWorld',
])
->addRule('vcsSilentMode', [
'type' => self::TYPE_BOOLEAN,
'description' => 'Is VCS connection is in silent mode?',
'description' => 'Is VCS (Version Control System) connection is in silent mode? When in silence mode, no comments will be posted on the repository pull or merge requests',
'default' => false,
'example' => false,
])