fix(functions): rebind generated branch domain on deployment activation

A Git-connected function's generated "Deployed from <branch>" domain is a
manual rule stored under its branch. When it is created before the first
successful build — e.g. after an initial build fails — the rule is born with
an empty deploymentId, and activate() only rebound rules with an empty
deploymentVcsProviderBranch, so the branch rule was skipped and the domain
stayed stuck on router_rule_not_found even after a later build activated.

Include the activating deployment's own branch in the rule rebind query so the
generated branch domain follows activation alongside the branch-agnostic
custom and per-deployment domains.
This commit is contained in:
ArnabChatterjee20k committed 2026-09-07 11:50:55 +05:30
1 parent 4729c7ba45
commit 847a257d8a
2 files changed
+151 -1

No files matched your search

@@ -661,6 +661,9 @@ class Jobs extends Action
'deploymentCreatedAt' => $deployment->getCreatedAt(),
]));
$branch = $deployment->getAttribute('providerBranch', '');
$branches = $branch === '' ? [''] : ['', $branch];
$dbForPlatform->forEach('rules', function (Document $rule) use ($dbForPlatform, $deployment, $bus) {
$rule = $dbForPlatform->updateDocument('rules', $rule->getId(), new Document([
'deploymentId' => $deployment->getId(),
@@ -673,7 +676,7 @@ class Jobs extends Action
Query::equal('deploymentResourceInternalId', [$resource->getSequence()]),
Query::equal('deploymentResourceType', [$resource->getCollection() === 'sites' ? 'site' : 'function']),
Query::equal('trigger', ['manual']),
Query::equal('deploymentVcsProviderBranch', ['']),
Query::equal('deploymentVcsProviderBranch', $branches),
]);
}
@@ -0,0 +1,147 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Platform\Modules\Functions\Workers;
use Appwrite\Platform\Modules\Functions\Workers\Jobs;
use PHPUnit\Framework\TestCase;
use Utopia\Bus\Bus;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Query;
final class ActivateRulesTest extends TestCase
{
public function testActivateRebindsGeneratedBranchDomain(): void
{
$captured = [];
$dbForPlatform = $this->platformDatabaseCapturing($captured);
$this->activate($dbForPlatform, deploymentBranch: 'main');
$branchQuery = $this->queryFor($captured, 'deploymentVcsProviderBranch');
$this->assertNotNull($branchQuery);
$this->assertSame(['', 'main'], $branchQuery->getValues());
$resourceType = $this->queryFor($captured, 'deploymentResourceType');
$this->assertNotNull($resourceType);
$this->assertSame(['function'], $resourceType->getValues());
}
public function testActivateOfNonVcsDeploymentOnlyTouchesBranchAgnosticRules(): void
{
$captured = [];
$dbForPlatform = $this->platformDatabaseCapturing($captured);
$this->activate($dbForPlatform, deploymentBranch: '');
$branchQuery = $this->queryFor($captured, 'deploymentVcsProviderBranch');
$this->assertNotNull($branchQuery);
$this->assertSame([''], $branchQuery->getValues());
}
public function testActivateRepointsMatchedRuleAtTheNewDeployment(): void
{
$updated = [];
$dispatched = 0;
$dbForPlatform = $this->createStub(Database::class);
$dbForPlatform->method('forEach')->willReturnCallback(
function (string $collection, callable $callback): void {
$callback(new Document(['$id' => 'rule-branch', 'deploymentId' => '']));
}
);
$dbForPlatform->method('updateDocument')->willReturnCallback(
function (string $collection, string $id, Document $data) use (&$updated): Document {
$updated[$id] = $data->getAttribute('deploymentId');
return new Document(['$id' => $id, ...$data->getArrayCopy()]);
}
);
$bus = $this->createStub(Bus::class);
$bus->method('dispatch')->willReturnCallback(function () use (&$dispatched) {
$dispatched++;
return;
});
$this->activate($dbForPlatform, deploymentBranch: 'main', bus: $bus);
$this->assertArrayHasKey('rule-branch', $updated);
$this->assertSame('dep-active', $updated['rule-branch']);
$this->assertSame(1, $dispatched);
}
private function activate(Database $dbForPlatform, string $deploymentBranch, ?Bus $bus = null): void
{
$resource = new Document([
'$id' => 'func-1',
'$sequence' => '100',
'$collection' => 'functions',
]);
$dbForProject = $this->createStub(Database::class);
$dbForProject->method('updateDocument')->willReturn($resource);
$project = new Document(['$id' => 'project-1', '$sequence' => '7']);
$deployment = new Document([
'$id' => 'dep-active',
'$sequence' => '55',
'providerBranch' => $deploymentBranch,
]);
(new ActivateRulesTestJobs())->exposeActivate(
$dbForProject,
$dbForPlatform,
$project,
$resource,
$deployment,
$bus ?? $this->createStub(Bus::class),
);
}
/**
* @param array<Query> $captured
*/
private function platformDatabaseCapturing(array &$captured): Database
{
$dbForPlatform = $this->createStub(Database::class);
$dbForPlatform->method('forEach')->willReturnCallback(
function (string $collection, callable $callback, array $queries = []) use (&$captured): void {
$captured = $queries;
}
);
return $dbForPlatform;
}
/**
* @param array<Query> $queries
*/
private function queryFor(array $queries, string $attribute): ?Query
{
foreach ($queries as $query) {
if ($query->getMethod() === Query::TYPE_EQUAL && $query->getAttribute() === $attribute) {
return $query;
}
}
return null;
}
}
final class ActivateRulesTestJobs extends Jobs
{
public function exposeActivate(
Database $dbForProject,
Database $dbForPlatform,
Document $project,
Document $resource,
Document $deployment,
Bus $bus,
): void {
$this->activate($dbForProject, $dbForPlatform, $project, $resource, $deployment, $bus);
}
}