Finalize rules overhauls

This commit is contained in:
Matej Bačo
2025-02-24 11:55:59 +01:00
parent a56086f8bf
commit eb7daf3edd
6 changed files with 62 additions and 15 deletions
+6 -6
View File
@@ -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'] ?? '');
@@ -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));
}
}
@@ -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(),
],
]);
@@ -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);
}
+1
View File
@@ -332,6 +332,7 @@ trait SitesBase
'queries' => [
Query::equal('value', [$deploymentId])->toString(),
Query::equal('type', ['deployment'])->toString(),
Query::equal('automation', [''])->toString(),
],
]);
@@ -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([