From 92b696ea21d3e2c543a037f75d9b5ffa770524b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 7 Mar 2025 10:14:45 +0100 Subject: [PATCH 1/8] Introduce type-relevant attributes to rules --- app/config/collections/platform.php | 132 ++++++++++++++++-- app/controllers/api/vcs.php | 26 +++- app/controllers/general.php | 8 +- .../Platform/Modules/Compute/Base.php | 8 +- .../Http/Functions/Deployment/Update.php | 9 +- .../Modules/Proxy/Http/Rules/API/Create.php | 1 - .../Proxy/Http/Rules/Function/Create.php | 12 +- .../Proxy/Http/Rules/Redirect/Create.php | 16 +-- .../Modules/Proxy/Http/Rules/Site/Create.php | 11 +- .../Modules/Sites/Http/Deployments/Create.php | 6 +- .../Http/Deployments/Duplicate/Create.php | 6 +- .../Http/Deployments/Template/Create.php | 6 +- .../Sites/Http/Sites/Deployment/Update.php | 9 +- src/Appwrite/Platform/Workers/Deletes.php | 24 +--- .../Database/Validator/Queries/Rules.php | 8 +- src/Appwrite/Utopia/Response/Model/Rule.php | 39 ++++-- 16 files changed, 248 insertions(+), 73 deletions(-) diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 58867bf2ba..3ebb2eb7a6 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1024,13 +1024,10 @@ return [ 'filters' => [], ], [ - // If 'api', then (empty) - // If 'redirect', then URL - // If 'deployment', then deployment ID - '$id' => ID::custom('value'), + '$id' => ID::custom('redirectUrl'), 'type' => Database::VAR_STRING, 'format' => '', - 'size' => 512, + 'size' => 2048, 'signed' => true, 'required' => false, 'default' => '', @@ -1038,8 +1035,29 @@ return [ 'filters' => [], ], [ - // Examples: branch=main - '$id' => ID::custom('automation'), + '$id' => ID::custom('redirectStatusCode'), + 'type' => Database::VAR_INTEGER, + 'format' => '', + 'size' => 0, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('deploymentResourceType'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 32, + 'signed' => true, + 'required' => false, + 'default' => '', + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('deploymentId'), 'type' => Database::VAR_STRING, 'format' => '', 'size' => Database::LENGTH_KEY, @@ -1049,6 +1067,61 @@ return [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('deploymentInternalId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => '', + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('deploymentResourceId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => '', + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('deploymentResourceInternalId'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => '', + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('deploymentVcsProviderBranch'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => Database::LENGTH_KEY, + 'signed' => true, + 'required' => false, + 'default' => '', + 'array' => false, + 'filters' => [], + ], + [ + '$id' => ID::custom('deploymentUpdatePolicy'), // 'active' or 'branch' + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 32, + 'signed' => true, + 'required' => false, + 'default' => '', + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('status'), 'type' => Database::VAR_STRING, @@ -1120,19 +1193,54 @@ return [ 'orders' => [Database::ORDER_ASC], ], [ - '$id' => '_key_value', + '$id' => '_key_deploymentResourceType', 'type' => Database::INDEX_KEY, - 'attributes' => ['value'], - 'lengths' => [512], + 'attributes' => ['deploymentResourceType'], + 'lengths' => [32], 'orders' => [Database::ORDER_ASC], ], [ - '$id' => '_key_automation', + '$id' => '_key_deploymentResourceId', 'type' => Database::INDEX_KEY, - 'attributes' => ['automation'], + 'attributes' => ['deploymentResourceId'], 'lengths' => [Database::LENGTH_KEY], 'orders' => [Database::ORDER_ASC], ], + [ + '$id' => '_key_deploymentResourceInternalId', + 'type' => Database::INDEX_KEY, + 'attributes' => ['deploymentResourceInternalId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => '_key_deploymentId', + 'type' => Database::INDEX_KEY, + 'attributes' => ['deploymentId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => '_key_deploymentInternalId', + 'type' => Database::INDEX_KEY, + 'attributes' => ['deploymentInternalId'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => '_key_deploymentVcsProviderBranch', + 'type' => Database::INDEX_KEY, + 'attributes' => ['deploymentVcsProviderBranch'], + 'lengths' => [Database::LENGTH_KEY], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => '_key_deploymentUpdatePolicy', + 'type' => Database::INDEX_KEY, + 'attributes' => ['deploymentUpdatePolicy'], + 'lengths' => [32], + 'orders' => [Database::ORDER_ASC], + ], ], ], diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index 0f277a4661..9c95f8acd5 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -246,7 +246,13 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', - 'value' => $deployment->getId(), + 'deploymentId' => $deployment->getId(), + 'deploymentInternalId' => $deployment->getInternalId(), + 'deploymentResourceType' => 'site', + 'deploymentResourceId' => $resourceId, + 'deploymentResourceInternalId' => $resourceInternalId, + 'deploymentVcsProviderBranch' => $providerBranch, + 'deploymentUpdatePolicy' => '', 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), @@ -265,8 +271,13 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', - 'value' => $deployment->getId(), - 'automation' => 'branch=' . $providerBranch, + 'deploymentId' => $deployment->getId(), + 'deploymentInternalId' => $deployment->getInternalId(), + 'deploymentResourceType' => 'site', + 'deploymentResourceId' => $resourceId, + 'deploymentResourceInternalId' => $resourceInternalId, + 'deploymentVcsProviderBranch' => $providerBranch, + 'deploymentUpdatePolicy' => 'branch', 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), @@ -289,8 +300,13 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', - 'value' => $deployment->getId(), - 'automation' => 'commit=' . $providerCommitHash, + 'deploymentId' => $deployment->getId(), + 'deploymentInternalId' => $deployment->getInternalId(), + 'deploymentResourceType' => 'site', + 'deploymentResourceId' => $resourceId, + 'deploymentResourceInternalId' => $resourceInternalId, + 'deploymentVcsProviderBranch' => $providerBranch, + 'deploymentUpdatePolicy' => '', 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), diff --git a/app/controllers/general.php b/app/controllers/general.php index d4728fb5af..90ce8f8956 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -135,7 +135,7 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw /** @var Database $dbForProject */ $dbForProject = $getProjectDB($project); - $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $rule->getAttribute('value'))); + $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $rule->getAttribute('deploymentId'))); if ($deployment->getAttribute('resourceType', '') === 'functions') { $type = 'function'; @@ -147,7 +147,7 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw Authorization::skip(fn () => $dbForProject->getDocument('functions', $deployment->getAttribute('resourceId', ''))) : Authorization::skip(fn () => $dbForProject->getDocument('sites', $deployment->getAttribute('resourceId', ''))); - $isPreview = $type === 'function' ? false : (!\str_starts_with($rule->getAttribute('automation', ''), 'site=')); + $isPreview = $type === 'function' ? false : ($rule->getAttribute('deploymentUpdatePolicy', '') !== 'active'); $path = ($swooleRequest->server['request_uri'] ?? '/'); $query = ($swooleRequest->server['query_string'] ?? ''); @@ -530,9 +530,9 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw $path .= '?' . $query; } - $url = 'https://' . $rule->getAttribute('value', '') . $path; + $url = 'https://' . $rule->getAttribute('redirectUrl', '') . $path; - $response->redirect($url); + $response->redirect($url, \intval($rule->getAttribute('redirectStatusCode', 301))); return true; } else { throw new AppwriteException(AppwriteException::GENERAL_SERVER_ERROR, 'Unknown resource type ' . $type); diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index 7880df189e..8097592e19 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -183,7 +183,13 @@ class Base extends Action 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', - 'value' => $deployment->getId(), + 'deploymentId' => $deployment->getId(), + 'deploymentInternalId' => $deployment->getInternalId(), + 'deploymentResourceType' => 'site', + 'deploymentResourceId' => $site->getId(), + 'deploymentResourceInternalId' => $site->getInternalId(), + 'deploymentVcsProviderBranch' => $providerBranch, + 'deploymentUpdatePolicy' => '', 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php index f45aa0a108..35339b3bbb 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php @@ -100,9 +100,14 @@ class Update extends Base Authorization::skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), $schedule)); $this->listRules($project, [ - Query::equal("automation", ["function=" . $function->getId()]), + Query::equal("type", ["deployment"]), + Query::equal("deploymentResourceType", ["function"]), + Query::equal("deploymentResourceInternalId", [$function->getInternalId()]), + Query::equal("deploymentUpdatePolicy", ["active"]), ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { - $rule = $rule->setAttribute('value', $deployment->getId()); + $rule = $rule + ->setAttribute('deploymentId', $deployment->getId()) + ->setAttribute('deploymentInternalId', $deployment->getInternalId()); $dbForPlatform->updateDocument('rules', $rule->getId(), $rule); }); diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php index 36e2bcdf12..fc1dcdd724 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php @@ -124,7 +124,6 @@ class Create extends Action 'domain' => $domain->get(), 'status' => $status, 'type' => 'api', - 'value' => '', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain->get()]), ]); diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php index bcc3b91e8f..5aeff9db7b 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php @@ -108,6 +108,8 @@ class Create extends Action throw new Exception(Exception::RULE_RESOURCE_NOT_FOUND); } + $deployment = $dbForProject->getDocument('deployments', $function->getAttribute('deployment', '')); + // TODO: @christyjacob remove once we migrate the rules in 1.7.x $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain->get()) : ID::unique(); @@ -130,10 +132,14 @@ class Create extends Action 'domain' => $domain->get(), 'status' => $status, 'type' => 'deployment', - 'value' => $function->getAttribute('deployment', ''), + 'deploymentId' => $deployment->isEmpty() ? '' : $deployment->getId(), + 'deploymentInternalId' => $deployment->isEmpty() ? '' : $deployment->getInternalId(), + 'deploymentResourceType' => 'function', + 'deploymentResourceId' => $function->getId(), + 'deploymentResourceInternalId' => $function->getInternalId(), + 'deploymentVcsProviderBranch' => $branch, + 'deploymentUpdatePolicy' => empty($branch) ? 'active' : 'branch', 'certificateId' => '', - 'automation' => 'function=' . $function->getId(), - 'automation' => !empty($branch) ? ('branch=' . $branch) : ('function=' . $function->getId()), 'search' => implode(' ', [$ruleId, $domain->get(), $branch]), ]); diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php index ac23cca168..9a36b1345f 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php @@ -20,6 +20,8 @@ use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; use Utopia\Validator\Domain as ValidatorDomain; +use Utopia\Validator\URL; +use Utopia\Validator\WhiteList; class Create extends Action { @@ -59,7 +61,8 @@ class Create extends Action ->label('abuse-key', 'userId:{userId}, url:{url}') ->label('abuse-time', 60) ->param('domain', null, new ValidatorDomain(), 'Domain name.') - ->param('target', null, new ValidatorDomain(), 'Target domain (hostname) of redirection') + ->param('url', null, new URL(), 'Target URL of redirection') + ->param('statusCode', null, new WhiteList([301, 302, 307, 308]), 'Status code of redirection') ->inject('response') ->inject('project') ->inject('queueForCertificates') @@ -68,7 +71,7 @@ class Create extends Action ->callback([$this, 'action']); } - public function action(string $domain, string $target, Response $response, Document $project, Certificate $queueForCertificates, Event $queueForEvents, Database $dbForPlatform) + public function action(string $domain, string $url, int $statusCode, Response $response, Document $project, Certificate $queueForCertificates, Event $queueForEvents, Database $dbForPlatform) { $mainDomain = System::getEnv('_APP_DOMAIN', ''); $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); @@ -92,12 +95,6 @@ class Create extends Action throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); } - try { - $target = new Domain($target); - } catch (\Throwable) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Target may not start with http:// or https://.'); - } - // Apex domain prevention due to CNAME limitations if (empty(App::getEnv('_APP_DOMAINS_NAMESERVERS', ''))) { if ($domain->get() === $domain->getRegisterable()) { @@ -127,7 +124,8 @@ class Create extends Action 'domain' => $domain->get(), 'status' => $status, 'type' => 'redirect', - 'value' => $target->get(), + 'redirectUrl' => $url, + 'redirectStatusCode' => $statusCode, 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain->get()]), ]); diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php index b6e88be00c..14a859d1ad 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php @@ -108,6 +108,8 @@ class Create extends Action throw new Exception(Exception::RULE_RESOURCE_NOT_FOUND); } + $deployment = $dbForProject->getDocument('deployments', $site->getAttribute('deployment', '')); + // TODO: @christyjacob remove once we migrate the rules in 1.7.x $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain->get()) : ID::unique(); @@ -130,9 +132,14 @@ class Create extends Action 'domain' => $domain->get(), 'status' => $status, 'type' => 'deployment', - 'value' => $site->getAttribute('deploymentId', ''), + 'deploymentId' => $deployment->isEmpty() ? '' : $deployment->getId(), + 'deploymentInternalId' => $deployment->isEmpty() ? '' : $deployment->getInternalId(), + 'deploymentResourceType' => 'site', + 'deploymentResourceId' => $site->getId(), + 'deploymentResourceInternalId' => $site->getInternalId(), + 'deploymentVcsProviderBranch' => $branch, + 'deploymentUpdatePolicy' => empty($branch) ? 'active' : 'branch', 'certificateId' => '', - 'automation' => !empty($branch) ? ('branch=' . $branch) : ('site=' . $site->getId()), 'search' => implode(' ', [$ruleId, $domain->get(), $branch]), ]); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php index fc70f2fe25..7b24a4377a 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php @@ -238,7 +238,11 @@ class Create extends Action 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', - 'value' => $deployment->getId(), + 'deploymentId' => $deployment->isEmpty() ? '' : $deployment->getId(), + 'deploymentInternalId' => $deployment->isEmpty() ? '' : $deployment->getInternalId(), + 'deploymentResourceType' => 'site', + 'deploymentResourceId' => $site->getId(), + 'deploymentResourceInternalId' => $site->getInternalId(), 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php index b30c07838f..512d1b6e5c 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php @@ -114,7 +114,11 @@ class Create extends Action 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', - 'value' => $deployment->getId(), + 'deploymentId' => $deployment->isEmpty() ? '' : $deployment->getId(), + 'deploymentInternalId' => $deployment->isEmpty() ? '' : $deployment->getInternalId(), + 'deploymentResourceType' => 'site', + 'deploymentResourceId' => $site->getId(), + 'deploymentResourceInternalId' => $site->getInternalId(), 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index 5fa7a68e82..528df58c5f 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php @@ -150,7 +150,11 @@ class Create extends Base 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', - 'value' => $deployment->getId(), + 'deploymentId' => $deployment->isEmpty() ? '' : $deployment->getId(), + 'deploymentInternalId' => $deployment->isEmpty() ? '' : $deployment->getInternalId(), + 'deploymentResourceType' => 'site', + 'deploymentResourceId' => $site->getId(), + 'deploymentResourceInternalId' => $site->getInternalId(), 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php index fcb0532929..93d5e85048 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php @@ -89,9 +89,14 @@ class Update extends Base ]))); $this->listRules($project, [ - Query::equal("automation", ["site=" . $site->getId()]), + Query::equal("type", ["deployment"]), + Query::equal("deploymentResourceType", ["site"]), + Query::equal("deploymentResourceInternalId", [$site->getInternalId()]), + Query::equal("deploymentUpdatePolicy", ["active"]), ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { - $rule = $rule->setAttribute('value', $deployment->getId()); + $rule = $rule + ->setAttribute('deploymentId', $deployment->getId()) + ->setAttribute('deploymentInternalId', $deployment->getInternalId()); $dbForPlatform->updateDocument('rules', $rule->getId(), $rule); }); diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 09fe98dd7f..de385bb646 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -761,7 +761,8 @@ class Deletes extends Action Console::info("Deleting rules for site " . $siteId); $this->deleteByGroup('rules', [ Query::equal('type', ['deployment']), - Query::equal('automation', ['site=' . $siteId]), + Query::equal('deploymentResourceType', ['site']), + Query::equal('deploymentResourceInternalId', [$siteInternalId]), Query::equal('projectInternalId', [$project->getInternalId()]) ], $dbForPlatform, function (Document $document) use ($dbForPlatform, $certificates) { $this->deleteRule($dbForPlatform, $document, $certificates); @@ -792,20 +793,6 @@ class Deletes extends Action $this->deleteDeploymentRules($dbForPlatform, $document, $project, $certificates); }); - /** - * Delete rules for all deployments of the site - */ - foreach ($deploymentIds as $deploymentId) { - Console::info("Deleting rules for site " . $siteId . "'s deployment " . $deploymentId); - $this->deleteByGroup('rules', [ - Query::equal('type', ['deployment']), - Query::equal('value', [$deploymentId]), - Query::equal('projectInternalId', [$project->getInternalId()]) - ], $dbForPlatform, function (Document $document) use ($dbForPlatform, $certificates) { - $this->deleteRule($dbForPlatform, $document, $certificates); - }); - } - /** * Delete builds */ @@ -858,7 +845,8 @@ class Deletes extends Action Console::info("Deleting rules for function " . $functionId); $this->deleteByGroup('rules', [ Query::equal('type', ['deployment']), - Query::equal('automation', ['function=' . $functionId]), + Query::equal('deploymentResourceType', ['site']), + Query::equal('deploymentResourceInternalId', [$functionInternalId]), Query::equal('projectInternalId', [$project->getInternalId()]) ], $dbForPlatform, function (Document $document) use ($project, $dbForPlatform, $certificates) { $this->deleteRule($dbForPlatform, $document, $certificates); @@ -937,7 +925,7 @@ class Deletes extends Action Console::info("Deleting rules for site " . $deployment->getId()); $this->deleteByGroup('rules', [ Query::equal('type', ['deployment']), - Query::equal('value', [$deployment->getId()]), + Query::equal('deploymentInternalId', [$deployment->getInternalId()]), Query::equal('projectInternalId', [$project->getInternalId()]) ], $dbForPlatform, function (Document $document) use ($dbForPlatform, $certificates) { $this->deleteRule($dbForPlatform, $document, $certificates); @@ -1101,7 +1089,7 @@ class Deletes extends Action Console::info("Deleting rules for deployment " . $deploymentId); $this->deleteByGroup('rules', [ Query::equal('type', ['deployment']), - Query::equal('value', [$deploymentId]), + Query::equal('deploymentResourceInternalId', [$deploymentInternalId]), Query::equal('projectInternalId', [$project->getInternalId()]) ], $dbForPlatform, function (Document $document) use ($dbForPlatform, $certificates) { $this->deleteRule($dbForPlatform, $document, $certificates); diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Rules.php b/src/Appwrite/Utopia/Database/Validator/Queries/Rules.php index 61701f0b2c..4367861b4d 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Rules.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Rules.php @@ -7,9 +7,11 @@ class Rules extends Base public const ALLOWED_ATTRIBUTES = [ 'domain', 'type', - 'value', - 'automation', - 'url' + 'deploymentResourceType', + 'deploymentResourceId', + 'deploymentId', + 'deploymentVcsProviderBranch', + 'deploymentUpdatePolicy' ]; /** diff --git a/src/Appwrite/Utopia/Response/Model/Rule.php b/src/Appwrite/Utopia/Response/Model/Rule.php index c365f241f8..6ff2c056b9 100644 --- a/src/Appwrite/Utopia/Response/Model/Rule.php +++ b/src/Appwrite/Utopia/Response/Model/Rule.php @@ -40,18 +40,41 @@ class Rule extends Model 'default' => '', 'example' => 'deployment', ]) - ->addRule('value', [ + ->addRule('redirectUrl', [ 'type' => self::TYPE_STRING, - 'description' => 'Detail specification for the type. If type is "api", this is empty. If type is "redirect", this is URL. If type is "deployment", this is deployment ID.', + 'description' => 'URL to redirect to. Used if type is "redirect"', 'default' => '', - 'example' => '67a9cf1a00150ee93abd', + 'example' => 'https://appwrite.io/docs', ]) - ->addRule('automation', [ - 'type' => self::TYPE_STRING, - 'description' => 'Action that results in a rule update. If VCS branch, value can be of syntax "branch=[name]"', - 'array' => false, + ->addRule('redirectStatusCode', [ + 'type' => self::TYPE_INTEGER, + 'description' => 'Status code to apply during redirect. Used if type is "redirect"', 'default' => '', - 'example' => 'branch=dev', + 'example' => 301, + ]) + ->addRule('deploymentResourceType', [ + 'type' => self::TYPE_STRING, + 'description' => 'Type of deployment. Possible values are "function", "site". Used if rule\'s type is "deployment".', + 'default' => '', + 'example' => 'function', + ]) + ->addRule('deploymentResourceId', [ + 'type' => self::TYPE_STRING, + 'description' => 'ID of deployment. Used if type is "deployment"', + 'default' => '', + 'example' => 'function', + ]) + ->addRule('deploymentVcsProviderBranch', [ + 'type' => self::TYPE_STRING, + 'description' => 'Name of Git branch that updates rule. Used if type is "deployment"', + 'default' => '', + 'example' => 'function', + ]) + ->addRule('deploymentUpdatePolicy', [ + 'type' => self::TYPE_STRING, + 'description' => 'Describes when to update deployment ID of this rule. Can be "active" or "branch". Used if type is "deployment"', + 'default' => '', + 'example' => 'function', ]) ->addRule('status', [ 'type' => self::TYPE_STRING, From 9400fdbbe5d6fcc049f2c6e7a36fd457d5701fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 7 Mar 2025 10:43:54 +0100 Subject: [PATCH 2/8] Leftover bugs after rework --- .../Platform/Modules/Compute/Base.php | 5 +- .../Modules/Functions/Workers/Builds.php | 64 +++++++++++-------- .../Modules/Sites/Http/Deployments/Create.php | 5 +- .../Http/Deployments/Duplicate/Create.php | 5 +- .../Http/Deployments/Template/Create.php | 5 +- .../e2e/Services/Functions/FunctionsBase.php | 3 +- tests/e2e/Services/Sites/SitesBase.php | 7 +- .../Services/Sites/SitesCustomServerTest.php | 2 +- 8 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index 8097592e19..9a92bfb5cc 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -175,7 +175,10 @@ class Base extends Action $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); $domain = ID::unique() . "." . $sitesDomain; - $ruleId = md5($domain); + + // TODO: @christyjacob remove once we migrate the rules in 1.7.x + $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain) : ID::unique(); + Authorization::skip( fn () => $dbForPlatform->createDocument('rules', new Document([ '$id' => $ruleId, diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 53556f60ba..33f2468f43 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -732,7 +732,7 @@ class Builds extends Action $rule = Authorization::skip(fn () => $dbForPlatform->findOne('rules', [ Query::equal("projectInternalId", [$project->getInternalId()]), Query::equal("type", ["deployment"]), - Query::equal("value", [$deployment->getId()]) + Query::equal('deploymentInternalId', [$deployment->getInternalId()]), ])); if ($rule->isEmpty()) { @@ -841,9 +841,15 @@ class Builds extends Action $resource = $dbForProject->updateDocument('functions', $resource->getId(), $resource); $this->listRules($project, [ - Query::equal("automation", ["function=" . $resource->getId()]), + Query::equal("projectInternalId", [$project->getInternalId()]), + Query::equal("type", ["deployment"]), + Query::equal("deploymentResourceInternalId", [$resource->getInternalId()]), + Query::equal('deploymentResourceType', ['function']), + Query::equal("deploymentUpdatePolicy", ['active']), ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { - $rule = $rule->setAttribute('value', $deployment->getId()); + $rule = $rule + ->setAttribute('deploymentId', $deployment->getId()) + ->setAttribute('deploymentInternalId', $deployment->getInternalId()); $dbForPlatform->updateDocument('rules', $rule->getId(), $rule); }); break; @@ -852,37 +858,41 @@ class Builds extends Action $resource = $dbForProject->updateDocument('sites', $resource->getId(), $resource); $this->listRules($project, [ - Query::equal("automation", ["site=" . $resource->getId()]), + Query::equal("projectInternalId", [$project->getInternalId()]), + Query::equal("type", ["deployment"]), + Query::equal("deploymentResourceInternalId", [$resource->getInternalId()]), + Query::equal('deploymentResourceType', ['site']), + Query::equal("deploymentUpdatePolicy", ['active']), ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { - $rule = $rule->setAttribute('value', $deployment->getId()); + $rule = $rule + ->setAttribute('deploymentId', $deployment->getId()) + ->setAttribute('deploymentInternalId', $deployment->getInternalId()); $dbForPlatform->updateDocument('rules', $rule->getId(), $rule); }); - // VCS branch - $branchName = $deployment->getAttribute('providerBranch'); - if (!empty($branchName)) { - $this->listRules($project, [ - Query::equal("automation", ["branch=" . $branchName]), - ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { - $rule = $rule->setAttribute('value', $deployment->getId()); - $dbForPlatform->updateDocument('rules', $rule->getId(), $rule); - }); - } - - // VCS commit - $commitHash = $deployment->getAttribute('providerCommitHash', ''); - if (!empty($commitHash)) { - $this->listRules($project, [ - Query::equal("automation", ["commit=" . $commitHash]), - ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { - $rule = $rule->setAttribute('value', $deployment->getId()); - $dbForPlatform->updateDocument('rules', $rule->getId(), $rule); - }); - } break; } } + if ($resource->getCollection() === 'sites') { + // VCS branch + $branchName = $deployment->getAttribute('providerBranch'); + if (!empty($branchName)) { + $this->listRules($project, [ + Query::equal("projectInternalId", [$project->getInternalId()]), + Query::equal("type", ["deployment"]), + Query::equal("deploymentResourceInternalId", [$resource->getInternalId()]), + Query::equal('deploymentResourceType', ['site']), + Query::equal("deploymentUpdatePolicy", ['branch']), + Query::equal("_key_deploymentVcsProviderBranch", [$branchName]), + ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { + $rule = $rule + ->setAttribute('deploymentId', $deployment->getId()) + ->setAttribute('deploymentInternalId', $deployment->getInternalId()); + $dbForPlatform->updateDocument('rules', $rule->getId(), $rule); + }); + } + } if ($dbForProject->getDocument('builds', $buildId)->getAttribute('status') === 'canceled') { Console::info('Build has been canceled'); @@ -1151,7 +1161,7 @@ class Builds extends Action $rule = Authorization::skip(fn () => $dbForPlatform->findOne('rules', [ Query::equal("projectInternalId", [$project->getInternalId()]), Query::equal("type", ["deployment"]), - Query::equal("value", [$deployment->getId()]) + Query::equal("deploymentInternalId", [$deployment->getInternalId()]), ])); $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') == 'disabled' ? 'http' : 'https'; diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php index 7b24a4377a..c4e0b1226f 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php @@ -230,7 +230,10 @@ class Create extends Action $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); $domain = ID::unique() . "." . $sitesDomain; - $ruleId = md5($domain); + + // TODO: @christyjacob remove once we migrate the rules in 1.7.x + $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain) : ID::unique(); + Authorization::skip( fn () => $dbForPlatform->createDocument('rules', new Document([ '$id' => $ruleId, diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php index 512d1b6e5c..27c2358b83 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php @@ -106,7 +106,10 @@ class Create extends Action // Preview deployments for sites $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); $domain = ID::unique() . "." . $sitesDomain; - $ruleId = md5($domain); + + // TODO: @christyjacob remove once we migrate the rules in 1.7.x + $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain) : ID::unique(); + Authorization::skip( fn () => $dbForPlatform->createDocument('rules', new Document([ '$id' => $ruleId, diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index 528df58c5f..d434e1565f 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php @@ -142,7 +142,10 @@ class Create extends Base $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); $domain = ID::unique() . "." . $sitesDomain; - $ruleId = md5($domain); + + // TODO: @christyjacob remove once we migrate the rules in 1.7.x + $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain) : ID::unique(); + Authorization::skip( fn () => $dbForPlatform->createDocument('rules', new Document([ '$id' => $ruleId, diff --git a/tests/e2e/Services/Functions/FunctionsBase.php b/tests/e2e/Services/Functions/FunctionsBase.php index 9f0a5903f3..760013e827 100644 --- a/tests/e2e/Services/Functions/FunctionsBase.php +++ b/tests/e2e/Services/Functions/FunctionsBase.php @@ -308,7 +308,8 @@ trait FunctionsBase 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => [ - Query::equal('automation', ['function=' . $functionId])->toString(), + Query::equal('deploymentResourceId', [$functionId])->toString(), + Query::equal('deploymentUpdatePolicy', ['active'])->toString(), Query::equal('type', ['deployment'])->toString(), ], ]); diff --git a/tests/e2e/Services/Sites/SitesBase.php b/tests/e2e/Services/Sites/SitesBase.php index a7e0293409..7758cce5bc 100644 --- a/tests/e2e/Services/Sites/SitesBase.php +++ b/tests/e2e/Services/Sites/SitesBase.php @@ -343,7 +343,8 @@ trait SitesBase 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => [ - Query::equal('automation', ['site=' . $siteId])->toString(), + Query::equal('deploymentResourceId', [$siteId])->toString(), + Query::equal('deploymentUpdatePolicy', ['active'])->toString(), Query::equal('type', ['deployment'])->toString(), ], ]); @@ -365,9 +366,9 @@ trait SitesBase 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => [ - Query::equal('value', [$deploymentId])->toString(), + Query::equal('deploymentId', [$deploymentId])->toString(), Query::equal('type', ['deployment'])->toString(), - Query::equal('automation', [''])->toString(), + Query::equal('deploymentUpdatePolicy', [''])->toString(), ], ]); diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index c9adc3a116..d307cf6a74 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -116,7 +116,7 @@ class SitesCustomServerTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => [ - Query::equal('automation', ['site=' . $siteId]) + Query::equal('deploymentResourceId', [$siteId]) ] ]); From a42522feaa6e06e2e2ee4dfe1dc217104d081125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 7 Mar 2025 11:11:32 +0100 Subject: [PATCH 3/8] Fix test failures --- src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php index 14a859d1ad..95eacf6e00 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php @@ -108,7 +108,7 @@ class Create extends Action throw new Exception(Exception::RULE_RESOURCE_NOT_FOUND); } - $deployment = $dbForProject->getDocument('deployments', $site->getAttribute('deployment', '')); + $deployment = $dbForProject->getDocument('deployments', $site->getAttribute('deploymentId', '')); // TODO: @christyjacob remove once we migrate the rules in 1.7.x $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain->get()) : ID::unique(); From f07c351c94add51e64b9030786da53c71d559791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 7 Mar 2025 11:22:13 +0100 Subject: [PATCH 4/8] Linter fix --- src/Appwrite/Platform/Modules/Compute/Base.php | 2 +- .../Platform/Modules/Functions/Workers/Builds.php | 10 +++++----- .../Platform/Modules/Sites/Http/Deployments/Create.php | 2 +- .../Sites/Http/Deployments/Duplicate/Create.php | 2 +- .../Modules/Sites/Http/Deployments/Template/Create.php | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index 9a92bfb5cc..fccb85fb2f 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -178,7 +178,7 @@ class Base extends Action // TODO: @christyjacob remove once we migrate the rules in 1.7.x $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain) : ID::unique(); - + Authorization::skip( fn () => $dbForPlatform->createDocument('rules', new Document([ '$id' => $ruleId, diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 33f2468f43..b63f772c75 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -841,11 +841,11 @@ class Builds extends Action $resource = $dbForProject->updateDocument('functions', $resource->getId(), $resource); $this->listRules($project, [ - Query::equal("projectInternalId", [$project->getInternalId()]), - Query::equal("type", ["deployment"]), - Query::equal("deploymentResourceInternalId", [$resource->getInternalId()]), - Query::equal('deploymentResourceType', ['function']), - Query::equal("deploymentUpdatePolicy", ['active']), + Query::equal("projectInternalId", [$project->getInternalId()]), + Query::equal("type", ["deployment"]), + Query::equal("deploymentResourceInternalId", [$resource->getInternalId()]), + Query::equal('deploymentResourceType', ['function']), + Query::equal("deploymentUpdatePolicy", ['active']), ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { $rule = $rule ->setAttribute('deploymentId', $deployment->getId()) diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php index c4e0b1226f..7736f78328 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php @@ -233,7 +233,7 @@ class Create extends Action // TODO: @christyjacob remove once we migrate the rules in 1.7.x $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain) : ID::unique(); - + Authorization::skip( fn () => $dbForPlatform->createDocument('rules', new Document([ '$id' => $ruleId, diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php index 27c2358b83..2c1fd6bef7 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php @@ -109,7 +109,7 @@ class Create extends Action // TODO: @christyjacob remove once we migrate the rules in 1.7.x $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain) : ID::unique(); - + Authorization::skip( fn () => $dbForPlatform->createDocument('rules', new Document([ '$id' => $ruleId, diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index d434e1565f..08f09e8a9c 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php @@ -145,7 +145,7 @@ class Create extends Base // TODO: @christyjacob remove once we migrate the rules in 1.7.x $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain) : ID::unique(); - + Authorization::skip( fn () => $dbForPlatform->createDocument('rules', new Document([ '$id' => $ruleId, From 777fb3fcddb6d22915a12aac08cd0d912632f6a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 7 Mar 2025 18:00:20 +0100 Subject: [PATCH 5/8] Update proxy rule tests --- app/controllers/general.php | 7 +- src/Appwrite/Utopia/Response/Model/Rule.php | 10 ++- tests/e2e/Client.php | 4 +- tests/e2e/Services/Proxy/ProxyBase.php | 9 +-- .../Services/Proxy/ProxyCustomServerTest.php | 70 ++++++++++++++----- 5 files changed, 72 insertions(+), 28 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 90ce8f8956..0df4b2b0c7 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -524,14 +524,13 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw $utopia->getRoute()?->label('error', ''); return false; } elseif ($type === 'redirect') { - $path = ($swooleRequest->server['request_uri'] ?? '/'); + $url = $rule->getAttribute('redirectUrl', ''); + $query = ($swooleRequest->server['query_string'] ?? ''); if (!empty($query)) { - $path .= '?' . $query; + $url .= '?' . $query; } - $url = 'https://' . $rule->getAttribute('redirectUrl', '') . $path; - $response->redirect($url, \intval($rule->getAttribute('redirectStatusCode', 301))); return true; } else { diff --git a/src/Appwrite/Utopia/Response/Model/Rule.php b/src/Appwrite/Utopia/Response/Model/Rule.php index 6ff2c056b9..192b68a856 100644 --- a/src/Appwrite/Utopia/Response/Model/Rule.php +++ b/src/Appwrite/Utopia/Response/Model/Rule.php @@ -52,6 +52,12 @@ class Rule extends Model 'default' => '', 'example' => 301, ]) + ->addRule('deploymentId', [ + 'type' => self::TYPE_STRING, + 'description' => 'ID of deployment. Used if type is "deployment"', + 'default' => '', + 'example' => 'n3u9feiwmf', + ]) ->addRule('deploymentResourceType', [ 'type' => self::TYPE_STRING, 'description' => 'Type of deployment. Possible values are "function", "site". Used if rule\'s type is "deployment".', @@ -60,9 +66,9 @@ class Rule extends Model ]) ->addRule('deploymentResourceId', [ 'type' => self::TYPE_STRING, - 'description' => 'ID of deployment. Used if type is "deployment"', + 'description' => 'ID deployment\'s resource. Used if type is "deployment"', 'default' => '', - 'example' => 'function', + 'example' => 'n3u9feiwmf', ]) ->addRule('deploymentVcsProviderBranch', [ 'type' => self::TYPE_STRING, diff --git a/tests/e2e/Client.php b/tests/e2e/Client.php index dc80808b14..278d1cd0d5 100644 --- a/tests/e2e/Client.php +++ b/tests/e2e/Client.php @@ -164,7 +164,7 @@ class Client * @return array * @throws Exception */ - public function call(string $method, string $path = '', array $headers = [], mixed $params = [], bool $decode = true): array + public function call(string $method, string $path = '', array $headers = [], mixed $params = [], bool $decode = true, bool $followRedirects = true): array { $headers = array_merge($this->headers, $headers); $ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : '')); @@ -192,7 +192,7 @@ class Client curl_setopt($ch, CURLOPT_PATH_AS_IS, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $followRedirects); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'); curl_setopt($ch, CURLOPT_HTTPHEADER, $formattedHeaders); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); diff --git a/tests/e2e/Services/Proxy/ProxyBase.php b/tests/e2e/Services/Proxy/ProxyBase.php index ed289ea8a5..40f16b5f8e 100644 --- a/tests/e2e/Services/Proxy/ProxyBase.php +++ b/tests/e2e/Services/Proxy/ProxyBase.php @@ -68,14 +68,15 @@ trait ProxyBase return $rule; } - protected function createRedirectRule(string $domain, string $target): mixed + protected function createRedirectRule(string $domain, string $url, int $statusCode): mixed { $rule = $this->client->call(Client::METHOD_POST, '/proxy/rules/redirect', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'domain' => $domain, - 'target' => $target, + 'url' => $url, + 'statusCode' => $statusCode, ]); return $rule; @@ -114,9 +115,9 @@ trait ProxyBase return $rule['body']['$id']; } - protected function setupRedirectRule(string $domain, string $target): string + protected function setupRedirectRule(string $domain, string $url, int $statusCode): string { - $rule = $this->createRedirectRule($domain, $target); + $rule = $this->createRedirectRule($domain, $url, $statusCode); $this->assertEquals(201, $rule['headers']['status-code'], 'Failed to setup rule: ' . \json_encode($rule)); diff --git a/tests/e2e/Services/Proxy/ProxyCustomServerTest.php b/tests/e2e/Services/Proxy/ProxyCustomServerTest.php index 78faffebd0..809c918bbe 100644 --- a/tests/e2e/Services/Proxy/ProxyCustomServerTest.php +++ b/tests/e2e/Services/Proxy/ProxyCustomServerTest.php @@ -23,10 +23,15 @@ class ProxyCustomServerTest extends Scope $this->assertEquals(201, $rule['headers']['status-code']); $this->assertEquals($domain, $rule['body']['domain']); $this->assertArrayHasKey('$id', $rule['body']); + $this->assertArrayHasKey('domain', $rule['body']); $this->assertArrayHasKey('type', $rule['body']); - $this->assertArrayHasKey('value', $rule['body']); - $this->assertArrayHasKey('automation', $rule['body']); - $this->assertArrayHasKey('status', $rule['body']); + $this->assertArrayHasKey('redirectUrl', $rule['body']); + $this->assertArrayHasKey('redirectStatusCode', $rule['body']); + $this->assertArrayHasKey('deploymentResourceType', $rule['body']); + $this->assertArrayHasKey('deploymentId', $rule['body']); + $this->assertArrayHasKey('deploymentResourceId', $rule['body']); + $this->assertArrayHasKey('deploymentVcsProviderBranch', $rule['body']); + $this->assertArrayHasKey('deploymentUpdatePolicy', $rule['body']); $this->assertArrayHasKey('logs', $rule['body']); $this->assertArrayHasKey('renewAt', $rule['body']); @@ -117,13 +122,33 @@ class ProxyCustomServerTest extends Scope $response = $proxyClient->call(Client::METHOD_GET, '/todos/1'); $this->assertEquals(404, $response['headers']['status-code']); - $ruleId = $this->setupRedirectRule($domain, 'jsonplaceholder.typicode.com'); + $ruleId = $this->setupRedirectRule($domain, 'https://jsonplaceholder.typicode.com/todos/1', 301); $this->assertNotEmpty($ruleId); $response = $proxyClient->call(Client::METHOD_GET, '/todos/1'); $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(1, $response['body']['id']); + $response = $proxyClient->call(Client::METHOD_GET, '/'); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(1, $response['body']['id']); + + $response = $proxyClient->call(Client::METHOD_GET, '/', followRedirects: false); + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertEquals('https://jsonplaceholder.typicode.com/todos/1', $response['headers']['location']); + + $domain = \uniqid() . '-redirect-307.custom.localhost'; + $ruleId = $this->setupRedirectRule($domain, 'https://jsonplaceholder.typicode.com/todos/1', 307); + $this->assertNotEmpty($ruleId); + + $proxyClient = new Client(); + $proxyClient->setEndpoint('http://appwrite'); + $proxyClient->addHeader('x-appwrite-hostname', $domain); + + $response = $proxyClient->call(Client::METHOD_GET, '/', followRedirects: false); + $this->assertEquals(307, $response['headers']['status-code']); + $this->assertEquals('https://jsonplaceholder.typicode.com/todos/1', $response['headers']['location']); + $this->cleanupRule($ruleId); } @@ -161,7 +186,8 @@ class ProxyCustomServerTest extends Scope 'queries' => [ Query::limit(1)->toString(), Query::equal('type', ['deployment'])->toString(), - Query::equal('automation', ['function=' . $functionId])->toString() + Query::equal('deploymentResourceType', ['function'])->toString(), + Query::equal('deploymentResourceId', [$functionId])->toString(), ] ]); $this->assertEquals(200, $rules['headers']['status-code']); @@ -172,7 +198,7 @@ class ProxyCustomServerTest extends Scope 'queries' => [ Query::limit(1)->toString(), Query::equal('type', ['deployment'])->toString(), - Query::equal('value', [$deploymentId])->toString() + Query::equal('deploymentId', [$deploymentId])->toString() ] ]); $this->assertEquals(200, $rules['headers']['status-code']); @@ -215,7 +241,8 @@ class ProxyCustomServerTest extends Scope 'queries' => [ Query::limit(1)->toString(), Query::equal('type', ['deployment'])->toString(), - Query::equal('automation', ['site=' . $siteId])->toString() + Query::equal('deploymentResourceType', ['site'])->toString(), + Query::equal('deploymentResourceId', [$siteId])->toString(), ] ]); $this->assertEquals(200, $rules['headers']['status-code']); @@ -226,7 +253,7 @@ class ProxyCustomServerTest extends Scope 'queries' => [ Query::limit(1)->toString(), Query::equal('type', ['deployment'])->toString(), - Query::equal('value', [$deploymentId])->toString() + Query::equal('deploymentId', [$deploymentId])->toString() ] ]); $this->assertEquals(200, $rules['headers']['status-code']); @@ -251,7 +278,7 @@ class ProxyCustomServerTest extends Scope $rule = $this->getRule($ruleId); $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('branch=dev', $rule['body']['automation']); + $this->assertEquals('branch', $rule['body']['deploymentUpdatePolicy']); $this->cleanupRule($ruleId); } @@ -272,7 +299,7 @@ class ProxyCustomServerTest extends Scope $rule = $this->getRule($ruleId); $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('branch=dev', $rule['body']['automation']); + $this->assertEquals('branch', $rule['body']['deploymentUpdatePolicy']); $this->cleanupRule($ruleId); } @@ -323,10 +350,15 @@ class ProxyCustomServerTest extends Scope $this->assertEquals(200, $rule['headers']['status-code']); $this->assertEquals($domain, $rule['body']['domain']); $this->assertArrayHasKey('$id', $rule['body']); + $this->assertArrayHasKey('domain', $rule['body']); $this->assertArrayHasKey('type', $rule['body']); - $this->assertArrayHasKey('value', $rule['body']); - $this->assertArrayHasKey('automation', $rule['body']); - $this->assertArrayHasKey('status', $rule['body']); + $this->assertArrayHasKey('redirectUrl', $rule['body']); + $this->assertArrayHasKey('redirectStatusCode', $rule['body']); + $this->assertArrayHasKey('deploymentResourceType', $rule['body']); + $this->assertArrayHasKey('deploymentId', $rule['body']); + $this->assertArrayHasKey('deploymentResourceId', $rule['body']); + $this->assertArrayHasKey('deploymentVcsProviderBranch', $rule['body']); + $this->assertArrayHasKey('deploymentUpdatePolicy', $rule['body']); $this->assertArrayHasKey('logs', $rule['body']); $this->assertArrayHasKey('renewAt', $rule['body']); @@ -356,11 +388,17 @@ class ProxyCustomServerTest extends Scope $this->assertEquals(1, $rules['body']['total']); $this->assertCount(1, $rules['body']['rules']); $this->assertEquals($rule1Domain, $rules['body']['rules'][0]['domain']); + $this->assertArrayHasKey('$id', $rules['body']['rules'][0]); + $this->assertArrayHasKey('domain', $rules['body']['rules'][0]); $this->assertArrayHasKey('type', $rules['body']['rules'][0]); - $this->assertArrayHasKey('value', $rules['body']['rules'][0]); - $this->assertArrayHasKey('automation', $rules['body']['rules'][0]); - $this->assertArrayHasKey('status', $rules['body']['rules'][0]); + $this->assertArrayHasKey('redirectUrl', $rules['body']['rules'][0]); + $this->assertArrayHasKey('redirectStatusCode', $rules['body']['rules'][0]); + $this->assertArrayHasKey('deploymentResourceType', $rules['body']['rules'][0]); + $this->assertArrayHasKey('deploymentId', $rules['body']['rules'][0]); + $this->assertArrayHasKey('deploymentResourceId', $rules['body']['rules'][0]); + $this->assertArrayHasKey('deploymentVcsProviderBranch', $rules['body']['rules'][0]); + $this->assertArrayHasKey('deploymentUpdatePolicy', $rules['body']['rules'][0]); $this->assertArrayHasKey('logs', $rules['body']['rules'][0]); $this->assertArrayHasKey('renewAt', $rules['body']['rules'][0]); From f75acfa5030163091ab59d86123b946cff8da34b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 8 Mar 2025 19:19:05 +0100 Subject: [PATCH 6/8] Remove deploymentUpdatePolicy, add trigger --- app/config/collections/platform.php | 36 +++++----- app/controllers/api/vcs.php | 6 +- app/controllers/general.php | 2 +- .../Platform/Modules/Compute/Base.php | 2 +- .../Http/Functions/Deployment/Update.php | 15 ++++- .../Modules/Functions/Workers/Builds.php | 65 ++++++++++++++++--- .../Modules/Proxy/Http/Rules/API/Create.php | 1 + .../Proxy/Http/Rules/Function/Create.php | 2 +- .../Proxy/Http/Rules/Redirect/Create.php | 1 + .../Modules/Proxy/Http/Rules/Site/Create.php | 2 +- .../Modules/Sites/Http/Deployments/Create.php | 2 + .../Http/Deployments/Duplicate/Create.php | 1 + .../Http/Deployments/Template/Create.php | 1 + .../Sites/Http/Sites/Deployment/Update.php | 15 ++++- .../Database/Validator/Queries/Rules.php | 4 +- src/Appwrite/Utopia/Response/Model/Rule.php | 6 -- .../e2e/Services/Functions/FunctionsBase.php | 2 +- .../Services/Proxy/ProxyCustomServerTest.php | 5 -- tests/e2e/Services/Sites/SitesBase.php | 4 +- 19 files changed, 117 insertions(+), 55 deletions(-) diff --git a/app/config/collections/platform.php b/app/config/collections/platform.php index 3ebb2eb7a6..cac75fe9dc 100644 --- a/app/config/collections/platform.php +++ b/app/config/collections/platform.php @@ -1023,6 +1023,17 @@ return [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('trigger'), // 'manual', 'deployment', '' (empty) + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 32, + 'signed' => true, + 'required' => false, + 'default' => '', + 'array' => false, + 'filters' => [], + ], [ '$id' => ID::custom('redirectUrl'), 'type' => Database::VAR_STRING, @@ -1111,17 +1122,6 @@ return [ 'array' => false, 'filters' => [], ], - [ - '$id' => ID::custom('deploymentUpdatePolicy'), // 'active' or 'branch' - 'type' => Database::VAR_STRING, - 'format' => '', - 'size' => 32, - 'signed' => true, - 'required' => false, - 'default' => '', - 'array' => false, - 'filters' => [], - ], [ '$id' => ID::custom('status'), 'type' => Database::VAR_STRING, @@ -1192,6 +1192,13 @@ return [ 'lengths' => [32], 'orders' => [Database::ORDER_ASC], ], + [ + '$id' => '_key_trigger', + 'type' => Database::INDEX_KEY, + 'attributes' => ['trigger'], + 'lengths' => [32], + 'orders' => [Database::ORDER_ASC], + ], [ '$id' => '_key_deploymentResourceType', 'type' => Database::INDEX_KEY, @@ -1234,13 +1241,6 @@ return [ 'lengths' => [Database::LENGTH_KEY], 'orders' => [Database::ORDER_ASC], ], - [ - '$id' => '_key_deploymentUpdatePolicy', - 'type' => Database::INDEX_KEY, - 'attributes' => ['deploymentUpdatePolicy'], - 'lengths' => [32], - 'orders' => [Database::ORDER_ASC], - ], ], ], diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index 9c95f8acd5..4adfb73908 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -246,13 +246,13 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', + 'trigger' => 'deployment', 'deploymentId' => $deployment->getId(), 'deploymentInternalId' => $deployment->getInternalId(), 'deploymentResourceType' => 'site', 'deploymentResourceId' => $resourceId, 'deploymentResourceInternalId' => $resourceInternalId, 'deploymentVcsProviderBranch' => $providerBranch, - 'deploymentUpdatePolicy' => '', 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), @@ -271,13 +271,13 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', + 'trigger' => 'deployment', 'deploymentId' => $deployment->getId(), 'deploymentInternalId' => $deployment->getInternalId(), 'deploymentResourceType' => 'site', 'deploymentResourceId' => $resourceId, 'deploymentResourceInternalId' => $resourceInternalId, 'deploymentVcsProviderBranch' => $providerBranch, - 'deploymentUpdatePolicy' => 'branch', 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), @@ -300,13 +300,13 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', + 'trigger' => 'deployment', 'deploymentId' => $deployment->getId(), 'deploymentInternalId' => $deployment->getInternalId(), 'deploymentResourceType' => 'site', 'deploymentResourceId' => $resourceId, 'deploymentResourceInternalId' => $resourceInternalId, 'deploymentVcsProviderBranch' => $providerBranch, - 'deploymentUpdatePolicy' => '', 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), diff --git a/app/controllers/general.php b/app/controllers/general.php index 0df4b2b0c7..d319a1a3cd 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -147,7 +147,7 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw Authorization::skip(fn () => $dbForProject->getDocument('functions', $deployment->getAttribute('resourceId', ''))) : Authorization::skip(fn () => $dbForProject->getDocument('sites', $deployment->getAttribute('resourceId', ''))); - $isPreview = $type === 'function' ? false : ($rule->getAttribute('deploymentUpdatePolicy', '') !== 'active'); + $isPreview = $type === 'function' ? false : ($rule->getAttribute('deploymentId', '') !== $resource->getAttribute('deploymentId', '')); $path = ($swooleRequest->server['request_uri'] ?? '/'); $query = ($swooleRequest->server['query_string'] ?? ''); diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index fccb85fb2f..196963ad44 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -185,6 +185,7 @@ class Base extends Action 'projectId' => $project->getId(), 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, + 'trigger' => 'deployment', 'type' => 'deployment', 'deploymentId' => $deployment->getId(), 'deploymentInternalId' => $deployment->getInternalId(), @@ -192,7 +193,6 @@ class Base extends Action 'deploymentResourceId' => $site->getId(), 'deploymentResourceInternalId' => $site->getInternalId(), 'deploymentVcsProviderBranch' => $providerBranch, - 'deploymentUpdatePolicy' => '', 'status' => 'verified', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain]), diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php index 35339b3bbb..96fced8b3c 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php @@ -86,6 +86,8 @@ class Update extends Base throw new Exception(Exception::BUILD_NOT_READY); } + $oldDeploymentInternalId = $function->getAttribute('deploymentInternalId', ''); + $function = $dbForProject->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [ 'deploymentInternalId' => $deployment->getInternalId(), 'deployment' => $deployment->getId(), @@ -99,12 +101,19 @@ class Update extends Base ->setAttribute('active', !empty($function->getAttribute('schedule')) && !empty($function->getAttribute('deployment'))); Authorization::skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), $schedule)); - $this->listRules($project, [ + $queries = [ Query::equal("type", ["deployment"]), Query::equal("deploymentResourceType", ["function"]), Query::equal("deploymentResourceInternalId", [$function->getInternalId()]), - Query::equal("deploymentUpdatePolicy", ["active"]), - ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { + ]; + + if (empty($oldDeploymentInternalId)) { + $queries[] = Query::equal("deploymentInternalId", [""]); + } else { + $queries[] = Query::equal("deploymentInternalId", [$oldDeploymentInternalId]); + } + + $this->listRules($project, $queries, $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { $rule = $rule ->setAttribute('deploymentId', $deployment->getId()) ->setAttribute('deploymentInternalId', $deployment->getInternalId()); diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index b63f772c75..9bbbbdba36 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -21,6 +21,7 @@ use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Exception\Conflict; +use Utopia\Database\Exception\Duplicate; use Utopia\Database\Exception\Restricted; use Utopia\Database\Exception\Structure; use Utopia\Database\Helpers\ID; @@ -837,16 +838,25 @@ class Builds extends Action $resource->setAttribute('live', true); switch ($resource->getCollection()) { case 'functions': + $oldDeploymentInternalId = $resource->getAttribute('deploymentInternalId', ''); + $resource->setAttribute('deployment', $deployment->getId()); $resource = $dbForProject->updateDocument('functions', $resource->getId(), $resource); - $this->listRules($project, [ + $queries = [ Query::equal("projectInternalId", [$project->getInternalId()]), Query::equal("type", ["deployment"]), Query::equal("deploymentResourceInternalId", [$resource->getInternalId()]), Query::equal('deploymentResourceType', ['function']), - Query::equal("deploymentUpdatePolicy", ['active']), - ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { + ]; + + if (empty($oldDeploymentInternalId)) { + $queries[] = Query::equal("deploymentInternalId", [""]); + } else { + $queries[] = Query::equal("deploymentInternalId", [$oldDeploymentInternalId]); + } + + $this->listRules($project, $queries, $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { $rule = $rule ->setAttribute('deploymentId', $deployment->getId()) ->setAttribute('deploymentInternalId', $deployment->getInternalId()); @@ -854,16 +864,25 @@ class Builds extends Action }); break; case 'sites': + $oldDeploymentInternalId = $resource->getAttribute('deploymentInternalId', ''); + $resource->setAttribute('deploymentId', $deployment->getId()); $resource = $dbForProject->updateDocument('sites', $resource->getId(), $resource); - $this->listRules($project, [ + $queries = [ Query::equal("projectInternalId", [$project->getInternalId()]), Query::equal("type", ["deployment"]), Query::equal("deploymentResourceInternalId", [$resource->getInternalId()]), Query::equal('deploymentResourceType', ['site']), - Query::equal("deploymentUpdatePolicy", ['active']), - ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { + ]; + + if (empty($oldDeploymentInternalId)) { + $queries[] = Query::equal("deploymentInternalId", [""]); + } else { + $queries[] = Query::equal("deploymentInternalId", [$oldDeploymentInternalId]); + } + + $this->listRules($project, $queries, $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { $rule = $rule ->setAttribute('deploymentId', $deployment->getId()) ->setAttribute('deploymentInternalId', $deployment->getInternalId()); @@ -878,13 +897,43 @@ class Builds extends Action // VCS branch $branchName = $deployment->getAttribute('providerBranch'); if (!empty($branchName)) { + $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + $domain = "branch-{$branchName}-{$resource->getId()}-{$project->getId()}.{$sitesDomain}"; + $ruleId = md5($domain); + + try { + $dbForPlatform->createDocument('rules', new Document([ + '$id' => $ruleId, + 'projectId' => $project->getId(), + 'projectInternalId' => $project->getInternalId(), + 'domain' => $domain, + 'type' => 'deployment', + 'trigger' => 'deployment', + 'deploymentId' => $deployment->getId(), + 'deploymentInternalId' => $deployment->getInternalId(), + 'deploymentResourceType' => 'site', + 'deploymentResourceId' => $deployment->getId(), + 'deploymentResourceInternalId' => $deployment->getInternalId(), + 'deploymentVcsProviderBranch' => $branchName, + 'status' => 'verified', + 'certificateId' => '', + 'search' => implode(' ', [$ruleId, $domain]), + ])); + } catch (Duplicate $err) { + $rule = $dbForPlatform->getDocument('rules', $ruleId); + $rule = $rule + ->setAttribute('deploymentId', $deployment->getId()) + ->setAttribute('deploymentInternalId', $deployment->getInternalId()); + $dbForPlatform->updateDocument('rules', $rule->getId(), $rule); + } + $this->listRules($project, [ Query::equal("projectInternalId", [$project->getInternalId()]), Query::equal("type", ["deployment"]), Query::equal("deploymentResourceInternalId", [$resource->getInternalId()]), Query::equal('deploymentResourceType', ['site']), - Query::equal("deploymentUpdatePolicy", ['branch']), - Query::equal("_key_deploymentVcsProviderBranch", [$branchName]), + Query::equal("deploymentVcsProviderBranch", [$branchName]), + Query::equal("trigger", ['manual']), ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { $rule = $rule ->setAttribute('deploymentId', $deployment->getId()) diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php index fc1dcdd724..4c7b38d80f 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php @@ -124,6 +124,7 @@ class Create extends Action 'domain' => $domain->get(), 'status' => $status, 'type' => 'api', + 'trigger' => 'manual', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain->get()]), ]); diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php index 5aeff9db7b..5f3d6382d5 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php @@ -132,13 +132,13 @@ class Create extends Action 'domain' => $domain->get(), 'status' => $status, 'type' => 'deployment', + 'trigger' => 'manual', 'deploymentId' => $deployment->isEmpty() ? '' : $deployment->getId(), 'deploymentInternalId' => $deployment->isEmpty() ? '' : $deployment->getInternalId(), 'deploymentResourceType' => 'function', 'deploymentResourceId' => $function->getId(), 'deploymentResourceInternalId' => $function->getInternalId(), 'deploymentVcsProviderBranch' => $branch, - 'deploymentUpdatePolicy' => empty($branch) ? 'active' : 'branch', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain->get(), $branch]), ]); diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php index 9a36b1345f..26e1dd8809 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php @@ -124,6 +124,7 @@ class Create extends Action 'domain' => $domain->get(), 'status' => $status, 'type' => 'redirect', + 'trigger' => 'manual', 'redirectUrl' => $url, 'redirectStatusCode' => $statusCode, 'certificateId' => '', diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php index 95eacf6e00..4090750285 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php @@ -132,13 +132,13 @@ class Create extends Action 'domain' => $domain->get(), 'status' => $status, 'type' => 'deployment', + 'trigger' => 'manual', 'deploymentId' => $deployment->isEmpty() ? '' : $deployment->getId(), 'deploymentInternalId' => $deployment->isEmpty() ? '' : $deployment->getInternalId(), 'deploymentResourceType' => 'site', 'deploymentResourceId' => $site->getId(), 'deploymentResourceInternalId' => $site->getInternalId(), 'deploymentVcsProviderBranch' => $branch, - 'deploymentUpdatePolicy' => empty($branch) ? 'active' : 'branch', 'certificateId' => '', 'search' => implode(' ', [$ruleId, $domain->get(), $branch]), ]); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php index 7736f78328..73f3372de2 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Create.php @@ -241,6 +241,7 @@ class Create extends Action 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', + 'trigger' => 'deployment', 'deploymentId' => $deployment->isEmpty() ? '' : $deployment->getId(), 'deploymentInternalId' => $deployment->isEmpty() ? '' : $deployment->getInternalId(), 'deploymentResourceType' => 'site', @@ -296,6 +297,7 @@ class Create extends Action 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', + 'trigger' => 'deployment', 'value' => $deployment->getId(), 'status' => 'verified', 'certificateId' => '', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php index 2c1fd6bef7..af1d6bee14 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php @@ -117,6 +117,7 @@ class Create extends Action 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', + 'trigger' => 'deployment', 'deploymentId' => $deployment->isEmpty() ? '' : $deployment->getId(), 'deploymentInternalId' => $deployment->isEmpty() ? '' : $deployment->getInternalId(), 'deploymentResourceType' => 'site', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php index 08f09e8a9c..7900f09075 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Template/Create.php @@ -153,6 +153,7 @@ class Create extends Base 'projectInternalId' => $project->getInternalId(), 'domain' => $domain, 'type' => 'deployment', + 'trigger' => 'deployment', 'deploymentId' => $deployment->isEmpty() ? '' : $deployment->getId(), 'deploymentInternalId' => $deployment->isEmpty() ? '' : $deployment->getInternalId(), 'deploymentResourceType' => 'site', diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php index 93d5e85048..27376f6a42 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php @@ -83,17 +83,26 @@ class Update extends Base throw new Exception(Exception::BUILD_NOT_READY); } + $oldDeploymentInternalId = $site->getAttribute('deploymentInternalId', ''); + $site = $dbForProject->updateDocument('sites', $site->getId(), new Document(array_merge($site->getArrayCopy(), [ 'deploymentInternalId' => $deployment->getInternalId(), 'deploymentId' => $deployment->getId(), ]))); - $this->listRules($project, [ + $queries = [ Query::equal("type", ["deployment"]), Query::equal("deploymentResourceType", ["site"]), Query::equal("deploymentResourceInternalId", [$site->getInternalId()]), - Query::equal("deploymentUpdatePolicy", ["active"]), - ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { + ]; + + if (empty($oldDeploymentInternalId)) { + $queries[] = Query::equal("deploymentInternalId", [""]); + } else { + $queries[] = Query::equal("deploymentInternalId", [$oldDeploymentInternalId]); + } + + $this->listRules($project, $queries, $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { $rule = $rule ->setAttribute('deploymentId', $deployment->getId()) ->setAttribute('deploymentInternalId', $deployment->getInternalId()); diff --git a/src/Appwrite/Utopia/Database/Validator/Queries/Rules.php b/src/Appwrite/Utopia/Database/Validator/Queries/Rules.php index 4367861b4d..c8057162a8 100644 --- a/src/Appwrite/Utopia/Database/Validator/Queries/Rules.php +++ b/src/Appwrite/Utopia/Database/Validator/Queries/Rules.php @@ -7,11 +7,11 @@ class Rules extends Base public const ALLOWED_ATTRIBUTES = [ 'domain', 'type', + 'trigger', 'deploymentResourceType', 'deploymentResourceId', 'deploymentId', - 'deploymentVcsProviderBranch', - 'deploymentUpdatePolicy' + 'deploymentVcsProviderBranch' ]; /** diff --git a/src/Appwrite/Utopia/Response/Model/Rule.php b/src/Appwrite/Utopia/Response/Model/Rule.php index 192b68a856..7aff104efe 100644 --- a/src/Appwrite/Utopia/Response/Model/Rule.php +++ b/src/Appwrite/Utopia/Response/Model/Rule.php @@ -76,12 +76,6 @@ class Rule extends Model 'default' => '', 'example' => 'function', ]) - ->addRule('deploymentUpdatePolicy', [ - 'type' => self::TYPE_STRING, - 'description' => 'Describes when to update deployment ID of this rule. Can be "active" or "branch". Used if type is "deployment"', - 'default' => '', - 'example' => 'function', - ]) ->addRule('status', [ 'type' => self::TYPE_STRING, 'description' => 'Domain verification status. Possible values are "created", "verifying", "verified" and "unverified"', diff --git a/tests/e2e/Services/Functions/FunctionsBase.php b/tests/e2e/Services/Functions/FunctionsBase.php index 760013e827..0f831b5e38 100644 --- a/tests/e2e/Services/Functions/FunctionsBase.php +++ b/tests/e2e/Services/Functions/FunctionsBase.php @@ -309,7 +309,7 @@ trait FunctionsBase ], $this->getHeaders()), [ 'queries' => [ Query::equal('deploymentResourceId', [$functionId])->toString(), - Query::equal('deploymentUpdatePolicy', ['active'])->toString(), + Query::equal('trigger', ['manual'])->toString(), Query::equal('type', ['deployment'])->toString(), ], ]); diff --git a/tests/e2e/Services/Proxy/ProxyCustomServerTest.php b/tests/e2e/Services/Proxy/ProxyCustomServerTest.php index 809c918bbe..6dc3d00c36 100644 --- a/tests/e2e/Services/Proxy/ProxyCustomServerTest.php +++ b/tests/e2e/Services/Proxy/ProxyCustomServerTest.php @@ -31,7 +31,6 @@ class ProxyCustomServerTest extends Scope $this->assertArrayHasKey('deploymentId', $rule['body']); $this->assertArrayHasKey('deploymentResourceId', $rule['body']); $this->assertArrayHasKey('deploymentVcsProviderBranch', $rule['body']); - $this->assertArrayHasKey('deploymentUpdatePolicy', $rule['body']); $this->assertArrayHasKey('logs', $rule['body']); $this->assertArrayHasKey('renewAt', $rule['body']); @@ -278,7 +277,6 @@ class ProxyCustomServerTest extends Scope $rule = $this->getRule($ruleId); $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('branch', $rule['body']['deploymentUpdatePolicy']); $this->cleanupRule($ruleId); } @@ -299,7 +297,6 @@ class ProxyCustomServerTest extends Scope $rule = $this->getRule($ruleId); $this->assertEquals(200, $rule['headers']['status-code']); - $this->assertEquals('branch', $rule['body']['deploymentUpdatePolicy']); $this->cleanupRule($ruleId); } @@ -358,7 +355,6 @@ class ProxyCustomServerTest extends Scope $this->assertArrayHasKey('deploymentId', $rule['body']); $this->assertArrayHasKey('deploymentResourceId', $rule['body']); $this->assertArrayHasKey('deploymentVcsProviderBranch', $rule['body']); - $this->assertArrayHasKey('deploymentUpdatePolicy', $rule['body']); $this->assertArrayHasKey('logs', $rule['body']); $this->assertArrayHasKey('renewAt', $rule['body']); @@ -398,7 +394,6 @@ class ProxyCustomServerTest extends Scope $this->assertArrayHasKey('deploymentId', $rules['body']['rules'][0]); $this->assertArrayHasKey('deploymentResourceId', $rules['body']['rules'][0]); $this->assertArrayHasKey('deploymentVcsProviderBranch', $rules['body']['rules'][0]); - $this->assertArrayHasKey('deploymentUpdatePolicy', $rules['body']['rules'][0]); $this->assertArrayHasKey('logs', $rules['body']['rules'][0]); $this->assertArrayHasKey('renewAt', $rules['body']['rules'][0]); diff --git a/tests/e2e/Services/Sites/SitesBase.php b/tests/e2e/Services/Sites/SitesBase.php index 7758cce5bc..303b70393d 100644 --- a/tests/e2e/Services/Sites/SitesBase.php +++ b/tests/e2e/Services/Sites/SitesBase.php @@ -344,7 +344,7 @@ trait SitesBase ], $this->getHeaders()), [ 'queries' => [ Query::equal('deploymentResourceId', [$siteId])->toString(), - Query::equal('deploymentUpdatePolicy', ['active'])->toString(), + Query::equal('trigger', ['manual'])->toString(), Query::equal('type', ['deployment'])->toString(), ], ]); @@ -368,7 +368,7 @@ trait SitesBase 'queries' => [ Query::equal('deploymentId', [$deploymentId])->toString(), Query::equal('type', ['deployment'])->toString(), - Query::equal('deploymentUpdatePolicy', [''])->toString(), + Query::equal('trigger', ['deployment'])->toString(), ], ]); From 1943e00e402f4812099ad154311664e5b54b1c9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 8 Mar 2025 19:58:54 +0100 Subject: [PATCH 7/8] Fix tests --- .../Modules/Functions/Workers/Builds.php | 5 +++- src/Appwrite/Utopia/Response/Model/Rule.php | 6 +++++ .../Services/Proxy/ProxyCustomServerTest.php | 21 +++++++++++++-- .../Services/Sites/SitesCustomServerTest.php | 26 +++++++++---------- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 9bbbbdba36..d2d01409c3 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -834,13 +834,13 @@ class Builds extends Action /** Set auto deploy */ if ($deployment->getAttribute('activate') === true) { - $resource->setAttribute('deploymentInternalId', $deployment->getInternalId()); $resource->setAttribute('live', true); switch ($resource->getCollection()) { case 'functions': $oldDeploymentInternalId = $resource->getAttribute('deploymentInternalId', ''); $resource->setAttribute('deployment', $deployment->getId()); + $resource->setAttribute('deploymentInternalId', $deployment->getInternalId()); $resource = $dbForProject->updateDocument('functions', $resource->getId(), $resource); $queries = [ @@ -848,6 +848,7 @@ class Builds extends Action Query::equal("type", ["deployment"]), Query::equal("deploymentResourceInternalId", [$resource->getInternalId()]), Query::equal('deploymentResourceType', ['function']), + Query::equal('trigger', ['manual']), ]; if (empty($oldDeploymentInternalId)) { @@ -867,6 +868,7 @@ class Builds extends Action $oldDeploymentInternalId = $resource->getAttribute('deploymentInternalId', ''); $resource->setAttribute('deploymentId', $deployment->getId()); + $resource->setAttribute('deploymentInternalId', $deployment->getInternalId()); $resource = $dbForProject->updateDocument('sites', $resource->getId(), $resource); $queries = [ @@ -874,6 +876,7 @@ class Builds extends Action Query::equal("type", ["deployment"]), Query::equal("deploymentResourceInternalId", [$resource->getInternalId()]), Query::equal('deploymentResourceType', ['site']), + Query::equal('trigger', ['manual']), ]; if (empty($oldDeploymentInternalId)) { diff --git a/src/Appwrite/Utopia/Response/Model/Rule.php b/src/Appwrite/Utopia/Response/Model/Rule.php index 7aff104efe..12903b270e 100644 --- a/src/Appwrite/Utopia/Response/Model/Rule.php +++ b/src/Appwrite/Utopia/Response/Model/Rule.php @@ -40,6 +40,12 @@ class Rule extends Model 'default' => '', 'example' => 'deployment', ]) + ->addRule('trigger', [ + 'type' => self::TYPE_STRING, + 'description' => 'Defines how the rule was created. Possible values are "manual" or "deployment"', + 'default' => '', + 'example' => 'manual', + ]) ->addRule('redirectUrl', [ 'type' => self::TYPE_STRING, 'description' => 'URL to redirect to. Used if type is "redirect"', diff --git a/tests/e2e/Services/Proxy/ProxyCustomServerTest.php b/tests/e2e/Services/Proxy/ProxyCustomServerTest.php index 6dc3d00c36..cdc95bf5ed 100644 --- a/tests/e2e/Services/Proxy/ProxyCustomServerTest.php +++ b/tests/e2e/Services/Proxy/ProxyCustomServerTest.php @@ -22,6 +22,7 @@ class ProxyCustomServerTest extends Scope $this->assertEquals(201, $rule['headers']['status-code']); $this->assertEquals($domain, $rule['body']['domain']); + $this->assertEquals('manual', $rule['body']['trigger']); $this->assertArrayHasKey('$id', $rule['body']); $this->assertArrayHasKey('domain', $rule['body']); $this->assertArrayHasKey('type', $rule['body']); @@ -231,6 +232,18 @@ class ProxyCustomServerTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); $this->assertStringContainsString('Contact page', $response['body']); + $rules = $this->listRules([ + 'queries' => [ + Query::limit(1)->toString(), + Query::equal('trigger', ['deployment'])->toString(), + Query::equal('type', ['deployment'])->toString(), + Query::equal('deploymentResourceType', ['site'])->toString(), + Query::equal('deploymentResourceId', [$siteId])->toString(), + ] + ]); + $this->assertEquals(200, $rules['headers']['status-code']); + $this->assertGreaterThan(0, $rules['body']['total']); + $this->cleanupRule($ruleId); $this->cleanupSite($siteId); @@ -261,7 +274,7 @@ class ProxyCustomServerTest extends Scope }); } - public function testCreatSiteBranchRule(): void + public function testCreateSiteBranchRule(): void { $domain = \uniqid() . '-site-branch.custom.localhost'; @@ -281,7 +294,7 @@ class ProxyCustomServerTest extends Scope $this->cleanupRule($ruleId); } - public function testCreatFunctionBranchRule(): void + public function testCreateFunctionBranchRule(): void { $domain = \uniqid() . '-function-branch.custom.localhost'; @@ -299,6 +312,8 @@ class ProxyCustomServerTest extends Scope $this->assertEquals(200, $rule['headers']['status-code']); $this->cleanupRule($ruleId); + + $this->cleanupFunction($functionId); } public function testUpdateRule(): void @@ -346,6 +361,7 @@ class ProxyCustomServerTest extends Scope $rule = $this->getRule($ruleId); $this->assertEquals(200, $rule['headers']['status-code']); $this->assertEquals($domain, $rule['body']['domain']); + $this->assertEquals('manual', $rule['body']['trigger']); $this->assertArrayHasKey('$id', $rule['body']); $this->assertArrayHasKey('domain', $rule['body']); $this->assertArrayHasKey('type', $rule['body']); @@ -385,6 +401,7 @@ class ProxyCustomServerTest extends Scope $this->assertCount(1, $rules['body']['rules']); $this->assertEquals($rule1Domain, $rules['body']['rules'][0]['domain']); + $this->assertEquals('manual', $rules['body']['rules'][0]['trigger']); $this->assertArrayHasKey('$id', $rules['body']['rules'][0]); $this->assertArrayHasKey('domain', $rules['body']['rules'][0]); $this->assertArrayHasKey('type', $rules['body']['rules'][0]); diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index e7c3803d66..bf3287f309 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -1453,37 +1453,35 @@ class SitesCustomServerTest extends Scope $this->assertNotEmpty($siteId); - $domain = $this->setupSiteDomain($siteId); + $deploymentId = $this->setupDeployment($siteId, [ + 'code' => $this->packageSite('static'), + 'activate' => 'true' + ]); + $this->assertNotEmpty($deploymentId); + + $oldDeploymentDomain = $this->getDeploymentDomain($deploymentId); + $this->assertNotEmpty($oldDeploymentDomain); $deploymentId = $this->setupDeployment($siteId, [ 'code' => $this->packageSite('static'), 'activate' => 'true' ]); - $this->assertNotEmpty($deploymentId); - $domain = $this->getSiteDomain($siteId); - $previewDomain = $this->getDeploymentDomain($deploymentId); - - $this->assertNotEmpty($domain); - $this->assertNotEmpty($previewDomain); + $newDeploymentDomain = $this->getDeploymentDomain($deploymentId); + $this->assertNotEmpty($newDeploymentDomain); $proxyClient = new Client(); - $proxyClient->setEndpoint('http://' . $domain); - + $proxyClient->setEndpoint('http://' . $newDeploymentDomain); $response = $proxyClient->call(Client::METHOD_GET, '/'); - $this->assertEquals(200, $response['headers']['status-code']); $this->assertStringContainsString("Hello Appwrite", $response['body']); $this->assertStringNotContainsString("Preview by", $response['body']); - $contentLength = $response['headers']['content-length']; $proxyClient = new Client(); - $proxyClient->setEndpoint('http://' . $previewDomain); - + $proxyClient->setEndpoint('http://' . $oldDeploymentDomain); $response = $proxyClient->call(Client::METHOD_GET, '/'); - $this->assertEquals(200, $response['headers']['status-code']); $this->assertStringContainsString("Hello Appwrite", $response['body']); $this->assertStringContainsString("Preview by", $response['body']); From 12e3d700325bd5903a9345d81dde36aa074c873f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 8 Mar 2025 21:33:43 +0100 Subject: [PATCH 8/8] Post-merge fixes --- src/Appwrite/Platform/Workers/Deletes.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 63a69d2ad7..c985513073 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -797,15 +797,15 @@ class Deletes extends Action /** * Delete builds */ - Console::info("Deleting builds for site " . $siteId); - foreach ($deploymentInternalIds as $deploymentInternalId) { - $this->deleteByGroup('builds', [ - Query::equal('deploymentInternalId', [$deploymentInternalId]) - ], $dbForProject, function (Document $document) use ($deviceForBuilds) { - $this->deleteBuildFiles($deviceForBuilds, $document); - }); + Console::info("Deleting builds for site " . $siteId); + foreach ($deploymentInternalIds as $deploymentInternalId) { + $this->deleteByGroup('builds', [ + Query::equal('deploymentInternalId', [$deploymentInternalId]) + ], $dbForProject, function (Document $document) use ($deviceForBuilds) { + $this->deleteBuildFiles($deviceForBuilds, $document); + }); } - + /** * Delete rules for all deployments of the site */