diff --git a/app/controllers/general.php b/app/controllers/general.php index 82587b3f50..1d8f49a954 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -162,19 +162,19 @@ function router(App $utopia, Database $dbForPlatform, callable $getProjectDB, Sw /** @var Database $dbForProject */ $dbForProject = $getProjectDB($project); - $deployment = $dbForProject->getDocument('deployments', $rule->getAttribute('value')); + $deployment = Authorization::skip(fn () => $dbForProject->getDocument('deployments', $rule->getAttribute('value'))); - if ($deployment->getAttribute('resourceType', '') === 'function') { + if ($deployment->getAttribute('resourceType', '') === 'functions') { $type = 'function'; - } elseif ($deployment->getAttribute('resourceType', '') === 'site') { + } elseif ($deployment->getAttribute('resourceType', '') === 'sites') { $type = 'site'; } $resource = $type === 'function' ? - $dbForProject->getDocument('functions', $deployment->getAttribute('resourceId', '')) : - $dbForProject->getDocument('sites', $deployment->getAttribute('resourceId', '')); + Authorization::skip(fn () => $dbForProject->getDocument('functions', $deployment->getAttribute('resourceId', ''))) : + Authorization::skip(fn () => $dbForProject->getDocument('sites', $deployment->getAttribute('resourceId', ''))); - $isPreview = $type === 'function' ? false : ($resource->getAttribute('deploymentId', '') === $deployment->getId()); + $isPreview = $type === 'function' ? false : (!\str_starts_with($rule->getAttribute('automation', ''), 'site=')); $path = ($swooleRequest->server['request_uri'] ?? '/'); $query = ($swooleRequest->server['query_string'] ?? ''); diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 717b138176..c04b412f56 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -831,14 +831,26 @@ class Builds extends Action case 'functions': $resource->setAttribute('deployment', $deployment->getId()); $resource = $dbForProject->updateDocument('functions', $resource->getId(), $resource); + + $this->listRules($project, [ + Query::equal("automation", ["function=" . $resource->getId()]), + ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { + $rule = $rule->setAttribute('value', $deployment->getId()); + $dbForPlatform->updateDocument('rules', $rule->getId(), $rule); + }); break; case 'sites': $resource->setAttribute('deploymentId', $deployment->getId()); $resource = $dbForProject->updateDocument('sites', $resource->getId(), $resource); + + $this->listRules($project, [ + Query::equal("automation", ["site=" . $resource->getId()]), + ], $dbForPlatform, function (Document $rule) use ($dbForPlatform, $deployment) { + $rule = $rule->setAttribute('value', $deployment->getId()); + $dbForPlatform->updateDocument('rules', $rule->getId(), $rule); + }); break; } - - // TODO: @Meldiron DO NOT FORGET!!! Update rules with correct automation (function=$functionId, site=$siteId) } @@ -1134,4 +1146,34 @@ class Builds extends Action } } } + + protected function listRules(Document $project, array $queries, Database $database, callable $callback = null): void + { + $cursor = null; + + do { + $queries = \array_merge([ + Query::limit(100), + Query::equal("projectInternalId", [$project->getInternalId()]) + ], $queries); + + if ($cursor !== null) { + $queries[] = Query::cursorAfter($cursor); + } + + $results = $database->find('rules', $queries); + + if (\count($results) > 0) { + $cursor = $results[\count($results) - 1]; + } else { + $cursor = null; + } + + foreach ($results as $document) { + if (is_callable($callback)) { + $callback($document); + } + } + } while (!\is_null($cursor)); + } } diff --git a/tests/e2e/Services/Functions/FunctionsBase.php b/tests/e2e/Services/Functions/FunctionsBase.php index 77c5e5888f..167094aec7 100644 --- a/tests/e2e/Services/Functions/FunctionsBase.php +++ b/tests/e2e/Services/Functions/FunctionsBase.php @@ -298,8 +298,8 @@ trait FunctionsBase 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'queries' => [ - Query::equal('resourceId', [$functionId])->toString(), - Query::equal('resourceType', ['function'])->toString(), + Query::equal('automation', ['function=' . $functionId])->toString(), + Query::equal('type', ['deployment'])->toString(), ], ]); diff --git a/tests/e2e/Services/Proxy/ProxyCustomServerTest.php b/tests/e2e/Services/Proxy/ProxyCustomServerTest.php index d2f380c4a6..5af9106fe4 100644 --- a/tests/e2e/Services/Proxy/ProxyCustomServerTest.php +++ b/tests/e2e/Services/Proxy/ProxyCustomServerTest.php @@ -14,13 +14,15 @@ class ProxyCustomServerTest extends Scope public function testCreateRule(): void { - $rule = $this->createAPIRule('api.myapp.com'); + $domain = \uniqid() . '-api.myapp.com'; + $rule = $this->createAPIRule($domain); $this->assertEquals(201, $rule['headers']['status-code']); - $this->assertEquals('api.myapp.com', $rule['body']['domain']); + $this->assertEquals($domain, $rule['body']['domain']); $this->assertArrayHasKey('$id', $rule['body']); - $this->assertArrayHasKey('resourceType', $rule['body']); - $this->assertArrayHasKey('resourceId', $rule['body']); + $this->assertArrayHasKey('type', $rule['body']); + $this->assertArrayHasKey('value', $rule['body']); + $this->assertArrayHasKey('automation', $rule['body']); $this->assertArrayHasKey('status', $rule['body']); $this->assertArrayHasKey('logs', $rule['body']); $this->assertArrayHasKey('renewAt', $rule['body']); @@ -34,7 +36,7 @@ class ProxyCustomServerTest extends Scope public function testCreateRuleSetup(): void { - $ruleId = $this->setupAPIRule('api2.myapp.com'); + $ruleId = $this->setupAPIRule(\uniqid() . '-api2.myapp.com'); $this->cleanupRule($ruleId); } diff --git a/tests/e2e/Services/Sites/SitesBase.php b/tests/e2e/Services/Sites/SitesBase.php index 437bc7e4bf..7d65e40db6 100644 --- a/tests/e2e/Services/Sites/SitesBase.php +++ b/tests/e2e/Services/Sites/SitesBase.php @@ -332,6 +332,7 @@ trait SitesBase 'queries' => [ Query::equal('value', [$deploymentId])->toString(), Query::equal('type', ['deployment'])->toString(), + Query::equal('automation', [''])->toString(), ], ]); diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index cd78fb279a..f66e7de0f5 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -273,6 +273,8 @@ class SitesCustomServerTest extends Scope $this->cleanupSite($siteId); } + // This is first Sites test with Proxy + // If this fails, it may not be related to variables; but Router flow failing public function testVariablesE2E(): void { $siteId = $this->setupSite([