Initial branch rule implementation

This commit is contained in:
Matej Bačo
2025-02-15 04:36:38 +01:00
parent fb2649ab41
commit 81a7ba536c
4 changed files with 43 additions and 5 deletions
+25 -3
View File
@@ -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) {
+1 -1
View File
@@ -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;
}
@@ -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(
@@ -23,6 +23,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;
@@ -788,6 +789,21 @@ class Builds extends Action
}
}
// 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());