Add webhook migration support (in GROUP_SETTINGS)

- Resource::TYPE_WEBHOOK joins project-variable under the Settings group.
- Source uses Appwrite SDK Webhooks::list() (separate service from
  Project) with cursor pagination.
- Destination writes to dbForPlatform.webhooks matching upstream
  createWebhook payload. Signing secret regenerates on the destination
  because the SDK strips it from list responses (same caveat as api keys).
This commit is contained in:
Prem Palanisamy committed 2026-05-14 20:27:19 +01:00
1 parent 3376bed6f7
commit 7fca4226ae
5 files changed
+254

No files matched your search

+50
View File
@@ -53,6 +53,7 @@ use Utopia\Migration\Resources\Functions\Func;
use Utopia\Migration\Resources\Integrations\ApiKey;
use Utopia\Migration\Resources\Integrations\Platform;
use Utopia\Migration\Resources\Settings\ProjectVariable;
use Utopia\Migration\Resources\Settings\Webhook;
use Utopia\Migration\Resources\Messaging\Message;
use Utopia\Migration\Resources\Messaging\Provider;
use Utopia\Migration\Resources\Messaging\Subscriber;
@@ -281,6 +282,7 @@ class Appwrite extends Destination
// Settings
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
// Backups
Resource::TYPE_BACKUP_POLICY,
@@ -3101,6 +3103,10 @@ class Appwrite extends Destination
/** @var ProjectVariable $resource */
$this->createProjectVariable($resource);
break;
case Resource::TYPE_WEBHOOK:
/** @var Webhook $resource */
$this->createWebhook($resource);
break;
}
if ($resource->getStatus() !== Resource::STATUS_SKIPPED) {
@@ -3151,6 +3157,50 @@ class Appwrite extends Destination
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
*/
+2
View File
@@ -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';
@@ -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,
@@ -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
View File
@@ -13,6 +13,7 @@ use Appwrite\Services\Storage;
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;
@@ -57,6 +58,7 @@ use Utopia\Migration\Resources\Functions\EnvVar;
use Utopia\Migration\Resources\Functions\Func;
use Utopia\Migration\Resources\Integrations\ApiKey;
use Utopia\Migration\Resources\Settings\ProjectVariable;
use Utopia\Migration\Resources\Settings\Webhook;
use Utopia\Migration\Resources\Integrations\Platform;
use Utopia\Migration\Resources\Messaging\Message;
use Utopia\Migration\Resources\Messaging\Provider;
@@ -98,6 +100,8 @@ class Appwrite extends Source
private Project $project;
private Webhooks $webhooks;
/**
* @var callable(UtopiaDocument $database|null): UtopiaDatabase
*/
@@ -127,6 +131,7 @@ class Appwrite extends Source
$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;
@@ -216,6 +221,7 @@ class Appwrite extends Source
// Settings
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
];
}
@@ -1466,6 +1472,19 @@ class Appwrite extends Source
$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;
}
}
}
/**
@@ -1487,6 +1506,20 @@ class Appwrite extends Source
));
}
}
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
));
}
}
}
/**
@@ -1536,6 +1569,57 @@ class Appwrite extends Source
}
}
/**
* @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
*/
+2
View File
@@ -96,6 +96,7 @@ class Transfer
public const GROUP_SETTINGS_RESOURCES = [
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
];
public const GROUP_BACKUPS_RESOURCES = [
@@ -138,6 +139,7 @@ class Transfer
// Settings
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
// legacy
Resource::TYPE_DOCUMENT,