diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php index dafeeda2e7..9467a1858a 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php @@ -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), ]); } diff --git a/tests/unit/Platform/Modules/Functions/Workers/ActivateRulesTest.php b/tests/unit/Platform/Modules/Functions/Workers/ActivateRulesTest.php new file mode 100644 index 0000000000..ca7ced84b2 --- /dev/null +++ b/tests/unit/Platform/Modules/Functions/Workers/ActivateRulesTest.php @@ -0,0 +1,147 @@ +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 $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 $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); + } +}