diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 37c1614241..df1817c8d6 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -60,6 +60,7 @@ use Utopia\Migration\Resources\Messaging\Message; use Utopia\Migration\Resources\Messaging\Provider; use Utopia\Migration\Resources\Messaging\Subscriber; use Utopia\Migration\Resources\Messaging\Topic; +use Utopia\Migration\Resources\Settings\Labels; use Utopia\Migration\Resources\Settings\ProjectVariable; use Utopia\Migration\Resources\Settings\Protocols; use Utopia\Migration\Resources\Settings\Webhook; @@ -292,6 +293,7 @@ class Appwrite extends Destination Resource::TYPE_PROJECT_VARIABLE, Resource::TYPE_WEBHOOK, Resource::TYPE_PROTOCOLS, + Resource::TYPE_LABELS, // Backups Resource::TYPE_BACKUP_POLICY, @@ -3124,6 +3126,10 @@ class Appwrite extends Destination /** @var Protocols $resource */ $this->createProtocols($resource); break; + case Resource::TYPE_LABELS: + /** @var Labels $resource */ + $this->createLabels($resource); + break; } if ($resource->getStatus() !== Resource::STATUS_SKIPPED) { @@ -3192,6 +3198,17 @@ class Appwrite extends Destination return true; } + /** + * Overwrite destination labels with the source array. Project::updateLabels + * is a wholesale replace, so the source's array is authoritative. + */ + protected function createLabels(Labels $resource): bool + { + $this->project->updateLabels($resource->getLabels()); + + return true; + } + protected function createWebhook(Webhook $resource): bool { $existing = $this->dbForPlatform->findOne('webhooks', [ diff --git a/src/Migration/Resource.php b/src/Migration/Resource.php index 17150628b9..ddbed66718 100644 --- a/src/Migration/Resource.php +++ b/src/Migration/Resource.php @@ -81,6 +81,7 @@ abstract class Resource implements \JsonSerializable public const TYPE_PROJECT_VARIABLE = 'project-variable'; public const TYPE_WEBHOOK = 'webhook'; public const TYPE_PROTOCOLS = 'protocols'; + public const TYPE_LABELS = 'labels'; // Messaging public const TYPE_SUBSCRIBER = 'subscriber'; @@ -126,6 +127,7 @@ abstract class Resource implements \JsonSerializable self::TYPE_PROJECT_VARIABLE, self::TYPE_WEBHOOK, self::TYPE_PROTOCOLS, + self::TYPE_LABELS, self::TYPE_PROVIDER, self::TYPE_TOPIC, self::TYPE_SUBSCRIBER, diff --git a/src/Migration/Resources/Settings/Labels.php b/src/Migration/Resources/Settings/Labels.php new file mode 100644 index 0000000000..a2da658dd1 --- /dev/null +++ b/src/Migration/Resources/Settings/Labels.php @@ -0,0 +1,72 @@ + $labels + */ + public function __construct( + string $id, + private readonly array $labels = [], + string $createdAt = '', + string $updatedAt = '', + ) { + $this->id = $id; + $this->createdAt = $createdAt; + $this->updatedAt = $updatedAt; + } + + /** + * @param array $array + */ + public static function fromArray(array $array): self + { + return new self( + $array['id'], + (array) ($array['labels'] ?? []), + createdAt: $array['createdAt'] ?? '', + updatedAt: $array['updatedAt'] ?? '', + ); + } + + /** + * @return array + */ + public function jsonSerialize(): array + { + return [ + 'id' => $this->id, + 'labels' => $this->labels, + 'createdAt' => $this->createdAt, + 'updatedAt' => $this->updatedAt, + ]; + } + + public static function getName(): string + { + return Resource::TYPE_LABELS; + } + + public function getGroup(): string + { + return Transfer::GROUP_SETTINGS; + } + + /** + * @return array + */ + public function getLabels(): array + { + return $this->labels; + } +} diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index 0155099bcb..e166a6e54b 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -63,6 +63,7 @@ use Utopia\Migration\Resources\Messaging\Message; use Utopia\Migration\Resources\Messaging\Provider; use Utopia\Migration\Resources\Messaging\Subscriber; use Utopia\Migration\Resources\Messaging\Topic; +use Utopia\Migration\Resources\Settings\Labels; use Utopia\Migration\Resources\Settings\ProjectVariable; use Utopia\Migration\Resources\Settings\Protocols; use Utopia\Migration\Resources\Settings\Webhook; @@ -226,6 +227,7 @@ class Appwrite extends Source Resource::TYPE_PROJECT_VARIABLE, Resource::TYPE_WEBHOOK, Resource::TYPE_PROTOCOLS, + Resource::TYPE_LABELS, ]; } @@ -1541,6 +1543,11 @@ class Appwrite extends Source // Singleton — there is exactly one protocols config per project. $report[Resource::TYPE_PROTOCOLS] = 1; } + + if (\in_array(Resource::TYPE_LABELS, $resources)) { + // Singleton — one labels array per project. + $report[Resource::TYPE_LABELS] = 1; + } } /** @@ -1590,6 +1597,41 @@ class Appwrite extends Source previous: $e )); } + + try { + if (\in_array(Resource::TYPE_LABELS, $resources)) { + $this->exportLabels(); + } + } catch (\Throwable $e) { + $this->addError(new Exception( + Resource::TYPE_LABELS, + Transfer::GROUP_SETTINGS, + message: $e->getMessage(), + code: $e->getCode(), + previous: $e + )); + } + } + + /** + * Read project-level labels from /v1/project. No SDK Project.get() exposed. + */ + private function exportLabels(): void + { + $response = $this->call('GET', '/v1/project'); + + if (!\is_array($response)) { + return; + } + + $labels = new Labels( + $this->projectId, + (array) ($response['labels'] ?? []), + createdAt: $response['$createdAt'] ?? '', + updatedAt: $response['$updatedAt'] ?? '', + ); + + $this->callback([$labels]); } /** diff --git a/src/Migration/Transfer.php b/src/Migration/Transfer.php index ccd3a8daad..1e096efced 100644 --- a/src/Migration/Transfer.php +++ b/src/Migration/Transfer.php @@ -99,6 +99,7 @@ class Transfer Resource::TYPE_PROJECT_VARIABLE, Resource::TYPE_WEBHOOK, Resource::TYPE_PROTOCOLS, + Resource::TYPE_LABELS, ]; public const GROUP_BACKUPS_RESOURCES = [ @@ -144,6 +145,7 @@ class Transfer Resource::TYPE_PROJECT_VARIABLE, Resource::TYPE_WEBHOOK, Resource::TYPE_PROTOCOLS, + Resource::TYPE_LABELS, // legacy Resource::TYPE_DOCUMENT,