From dd2003bfa44055983b69ce8e5f327a449ef641a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Sat, 15 Feb 2025 04:36:38 +0100 Subject: [PATCH] Initial branch rule implementation --- app/controllers/api/vcs.php | 28 +++++++++++++++++-- app/controllers/general.php | 2 +- .../Platform/Modules/Compute/Base.php | 2 +- .../Modules/Functions/Workers/Builds.php | 16 +++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/vcs.php b/app/controllers/api/vcs.php index 921a840c0a..72377ecf74 100644 --- a/app/controllers/api/vcs.php +++ b/app/controllers/api/vcs.php @@ -19,6 +19,7 @@ use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; +use Utopia\Database\Exception\Duplicate; use Utopia\Database\Exception\Query as QueryException; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; @@ -233,10 +234,8 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId // Preview deployments for sites if ($resource->getCollection() === 'sites') { - $projectId = $project->getId(); - $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); - $domain = "{$deploymentId}-{$projectId}.{$sitesDomain}"; + $domain = "{$deploymentId}-{$resourceId}.{$sitesDomain}"; $ruleId = md5($domain); $rule = Authorization::skip( @@ -252,6 +251,29 @@ $createGitDeployments = function (GitHub $github, string $providerInstallationId 'certificateId' => '', ])) ); + + // Branch preview + if (!empty($providerBranch)) { + $domain = "git-{$providerBranch}-{$resource->getId()}.{$sitesDomain}"; + $ruleId = md5($domain); + try { + Authorization::skip( + fn () => $dbForPlatform->createDocument('rules', new Document([ + '$id' => $ruleId, + 'projectId' => $project->getId(), + 'projectInternalId' => $project->getInternalId(), + 'domain' => $domain, + 'resourceType' => 'deployment', + 'resourceId' => $deployment->getId(), + 'resourceInternalId' => $deployment->getInternalId(), + 'status' => 'verified', + 'certificateId' => '', + ])) + ); + } catch (Duplicate $err) { + // Ignore, rule already exists; will be updated by builds worker + } + } } if (!empty($providerCommitHash) && $resource->getAttribute('providerSilentMode', false) === false) { diff --git a/app/controllers/general.php b/app/controllers/general.php index c9efc90426..72fde9b1a8 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -739,7 +739,7 @@ App::init() $refDomainOrigin = $origin; } else { // Auto-allow domains with linked rule - $rule = Authorization::skip(fn () => $dbForPlatform->getDocument('rules', md5($origin))); + $rule = Authorization::skip(fn () => $dbForPlatform->getDocument('rules', md5($origin ?? ''))); if (!$rule->isEmpty() && $rule->getAttribute('projectInternalId') === $project->getInternalId()) { $refDomainOrigin = $origin; } diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index 9ea0ef86c5..89fe070723 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -165,7 +165,7 @@ class Base extends Action $projectId = $project->getId(); $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); - $domain = "{$deploymentId}-{$projectId}.{$sitesDomain}"; + $domain = "{$deploymentId}-{$site->getId()}-{$projectId}.{$sitesDomain}"; $ruleId = md5($domain); $rule = Authorization::skip( diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 809cb96cc2..ec57105ae9 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -19,6 +19,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; @@ -700,6 +701,21 @@ class Builds extends Action Console::success("Build id: $buildId created"); + // Git branch preview + $providerBranch = $deployment->getAttribute('providerBranch', ''); + if(!empty($providerBranch)) { + $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + $domain = "git-{$providerBranch}-{$resource->getId()}.{$sitesDomain}"; + $ruleId = md5($domain); + $rule = Authorization::skip(fn () => $dbForPlatform->getDocument('rules', $ruleId)); + if(!$rule->isEmpty()) { + $rule = $rule + ->setAttribute('resourceId', $deployment->getId()) + ->setAttribute('resourceInternalId', $deployment->getInternalId()); + Authorization::skip(fn () => $dbForPlatform->updateDocument('rules', $rule->getId(), $rule)); + } + } + /** Set auto deploy */ if ($deployment->getAttribute('activate') === true) { $resource->setAttribute('deploymentInternalId', $deployment->getInternalId());