Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/Migration/Destinations/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
use Utopia\Migration\Resources\Messaging\Subscriber;
use Utopia\Migration\Resources\Messaging\Topic;
use Utopia\Migration\Resources\Settings\ProjectVariable;
use Utopia\Migration\Resources\Settings\Webhook;
use Utopia\Migration\Resources\Sites\Deployment as SiteDeployment;
use Utopia\Migration\Resources\Sites\EnvVar as SiteEnvVar;
use Utopia\Migration\Resources\Sites\Site;
Expand Down Expand Up @@ -281,6 +282,7 @@ public static function getSupportedResources(): array

// Settings
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,

// Backups
Resource::TYPE_BACKUP_POLICY,
Expand Down Expand Up @@ -3101,6 +3103,10 @@ public function importSettingsResource(Resource $resource): Resource
/** @var ProjectVariable $resource */
$this->createProjectVariable($resource);
break;
case Resource::TYPE_WEBHOOK:
/** @var Webhook $resource */
$this->createWebhook($resource);
break;
}

if ($resource->getStatus() !== Resource::STATUS_SKIPPED) {
Expand Down Expand Up @@ -3149,6 +3155,50 @@ protected function createProjectVariable(ProjectVariable $resource): bool
return true;
}

protected function createWebhook(Webhook $resource): bool
{
$existing = $this->dbForPlatform->findOne('webhooks', [
Query::equal('projectInternalId', [$this->projectInternalId]),
Query::equal('name', [$resource->getWebhookName()]),
]);

if ($existing !== false && !$existing->isEmpty()) {
$resource->setStatus(Resource::STATUS_SKIPPED, 'Webhook already exists');
return false;
}

$createdAt = $this->normalizeDateTime($resource->getCreatedAt());
$updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt);

try {
$this->dbForPlatform->createDocument('webhooks', new UtopiaDocument([
'$id' => ID::unique(),
'$permissions' => $resource->getPermissions(),
'projectInternalId' => $this->projectInternalId,
'projectId' => $this->project,
'name' => $resource->getWebhookName(),
'events' => $resource->getEvents(),
'url' => $resource->getUrl(),
'security' => $resource->getSecurity(),
'httpUser' => $resource->getHttpUser(),
'httpPass' => $resource->getHttpPass(),
// SDK only returns the signing secret on creation, never on list — regenerate
// a fresh one on the destination to match upstream createWebhook behavior.
'signatureKey' => \bin2hex(\random_bytes(64)),
'enabled' => $resource->isEnabled(),
'$createdAt' => $createdAt,
'$updatedAt' => $updatedAt,
]));
} catch (DuplicateException) {
$resource->setStatus(Resource::STATUS_SKIPPED, 'Webhook already exists');
return false;
}

$this->dbForPlatform->purgeCachedDocument('projects', $this->project);

return true;
}

/**
* @throws \Throwable
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ abstract class Resource implements \JsonSerializable

// Settings
public const TYPE_PROJECT_VARIABLE = 'project-variable';
public const TYPE_WEBHOOK = 'webhook';

// Messaging
public const TYPE_SUBSCRIBER = 'subscriber';
Expand Down Expand Up @@ -119,6 +120,7 @@ abstract class Resource implements \JsonSerializable
self::TYPE_PLATFORM,
self::TYPE_API_KEY,
self::TYPE_PROJECT_VARIABLE,
self::TYPE_WEBHOOK,
self::TYPE_PROVIDER,
self::TYPE_TOPIC,
self::TYPE_SUBSCRIBER,
Expand Down
116 changes: 116 additions & 0 deletions src/Migration/Resources/Settings/Webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Utopia\Migration\Resources\Settings;

use Utopia\Migration\Resource;
use Utopia\Migration\Transfer;

class Webhook extends Resource
{
/**
* @param array<string> $events
*/
public function __construct(
string $id,
private readonly string $name,
private readonly string $url,
private readonly array $events = [],
private readonly bool $security = false,
private readonly string $httpUser = '',
private readonly string $httpPass = '',
private readonly bool $enabled = true,
string $createdAt = '',
string $updatedAt = '',
) {
$this->id = $id;
$this->createdAt = $createdAt;
$this->updatedAt = $updatedAt;
}

/**
* @param array<string, mixed> $array
* @return self
*/
public static function fromArray(array $array): self
{
return new self(
$array['id'],
$array['name'],
$array['url'],
$array['events'] ?? [],
(bool) ($array['security'] ?? false),
$array['httpUser'] ?? '',
$array['httpPass'] ?? '',
(bool) ($array['enabled'] ?? true),
createdAt: $array['createdAt'] ?? '',
updatedAt: $array['updatedAt'] ?? '',
);
}

/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'url' => $this->url,
'events' => $this->events,
'security' => $this->security,
'httpUser' => $this->httpUser,
'httpPass' => $this->httpPass,
'enabled' => $this->enabled,
'createdAt' => $this->createdAt,
'updatedAt' => $this->updatedAt,
];
}

public static function getName(): string
{
return Resource::TYPE_WEBHOOK;
}

public function getGroup(): string
{
return Transfer::GROUP_SETTINGS;
}

public function getWebhookName(): string
{
return $this->name;
}

public function getUrl(): string
{
return $this->url;
}

/**
* @return array<string>
*/
public function getEvents(): array
{
return $this->events;
}

public function getSecurity(): bool
{
return $this->security;
}

public function getHttpUser(): string
{
return $this->httpUser;
}

public function getHttpPass(): string
{
return $this->httpPass;
}

public function isEnabled(): bool
{
return $this->enabled;
}
}
84 changes: 84 additions & 0 deletions src/Migration/Sources/Appwrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Appwrite\Services\TablesDB;
use Appwrite\Services\Teams;
use Appwrite\Services\Users;
use Appwrite\Services\Webhooks;
use Utopia\Database\Database as UtopiaDatabase;
use Utopia\Database\DateTime as UtopiaDateTime;
use Utopia\Database\Document as UtopiaDocument;
Expand Down Expand Up @@ -62,6 +63,7 @@
use Utopia\Migration\Resources\Messaging\Subscriber;
use Utopia\Migration\Resources\Messaging\Topic;
use Utopia\Migration\Resources\Settings\ProjectVariable;
use Utopia\Migration\Resources\Settings\Webhook;
use Utopia\Migration\Resources\Sites\Deployment as SiteDeployment;
use Utopia\Migration\Resources\Sites\EnvVar as SiteEnvVar;
use Utopia\Migration\Resources\Sites\Site;
Expand Down Expand Up @@ -98,6 +100,8 @@ class Appwrite extends Source

private Project $project;

private Webhooks $webhooks;

/**
* @var callable(UtopiaDocument $database|null): UtopiaDatabase
*/
Expand Down Expand Up @@ -127,6 +131,7 @@ public function __construct(
$this->messaging = new Messaging($this->client);
$this->sites = new Sites($this->client);
$this->project = new Project($this->client);
$this->webhooks = new Webhooks($this->client);

$this->headers['x-appwrite-project'] = $this->projectId;
$this->headers['x-appwrite-key'] = $this->key;
Expand Down Expand Up @@ -216,6 +221,7 @@ public static function getSupportedResources(): array

// Settings
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
];
}

Expand Down Expand Up @@ -1466,6 +1472,19 @@ private function reportSettings(array $resources, array &$report, array $resourc
$report[Resource::TYPE_PROJECT_VARIABLE] = 0;
}
}

if (\in_array(Resource::TYPE_WEBHOOK, $resources)) {
$webhookQueries = $this->buildQueries(
resourceType: Resource::TYPE_WEBHOOK,
resourceIds: $resourceIds,
limit: 1
);
try {
$report[Resource::TYPE_WEBHOOK] = $this->webhooks->list($webhookQueries)->total;
} catch (\Throwable) {
$report[Resource::TYPE_WEBHOOK] = 0;
}
}
}

/**
Expand All @@ -1487,6 +1506,20 @@ protected function exportGroupSettings(int $batchSize, array $resources): void
));
}
}

if (\in_array(Resource::TYPE_WEBHOOK, $resources)) {
try {
$this->exportWebhooks($batchSize);
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_WEBHOOK,
Transfer::GROUP_SETTINGS,
message: $e->getMessage(),
code: $e->getCode(),
previous: $e
));
}
}
}

/**
Expand Down Expand Up @@ -1536,6 +1569,57 @@ private function exportProjectVariables(int $batchSize): void
}
}

/**
* @throws AppwriteException
*/
private function exportWebhooks(int $batchSize): void
{
$lastId = null;

while (true) {
$queries = [Query::limit($batchSize)];

if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_WEBHOOK) {
$queries[] = Query::equal('$id', $this->rootResourceId);
$queries[] = Query::limit(1);
}

if ($lastId !== null) {
$queries[] = Query::cursorAfter($lastId);
}

$response = $this->webhooks->list($queries);
if ($response->total === 0) {
break;
}

$webhooks = [];

foreach ($response->webhooks as $webhook) {
$webhooks[] = new Webhook(
$webhook->id,
$webhook->name,
$webhook->url,
$webhook->events,
$webhook->tls,
$webhook->authUsername,
$webhook->authPassword,
$webhook->enabled,
createdAt: $webhook->createdAt,
updatedAt: $webhook->updatedAt,
);

$lastId = $webhook->id;
}

$this->callback($webhooks);

if (\count($response->webhooks) < $batchSize) {
break;
}
}
}

/**
* @throws AppwriteException
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Migration/Transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class Transfer

public const GROUP_SETTINGS_RESOURCES = [
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
];

public const GROUP_BACKUPS_RESOURCES = [
Expand Down Expand Up @@ -138,6 +139,7 @@ class Transfer

// Settings
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,

// legacy
Resource::TYPE_DOCUMENT,
Expand Down
Loading