mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #185 from utopia-php/add-webhook-migration
Add webhook migration support
This commit is contained in:
7 files changed
+258
No files matched your search
@@ -57,6 +57,7 @@ use Utopia\Migration\Resources\Messaging\Provider;
|
||||
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;
|
||||
@@ -278,6 +279,7 @@ class Appwrite extends Destination
|
||||
// Integrations
|
||||
Resource::TYPE_PLATFORM,
|
||||
Resource::TYPE_API_KEY,
|
||||
Resource::TYPE_WEBHOOK,
|
||||
|
||||
// Settings
|
||||
Resource::TYPE_PROJECT_VARIABLE,
|
||||
@@ -3085,6 +3087,10 @@ class Appwrite extends Destination
|
||||
/** @var ApiKey $resource */
|
||||
$this->createApiKey($resource);
|
||||
break;
|
||||
case Resource::TYPE_WEBHOOK:
|
||||
/** @var Webhook $resource */
|
||||
$this->createWebhook($resource);
|
||||
break;
|
||||
}
|
||||
|
||||
if ($resource->getStatus() !== Resource::STATUS_SKIPPED) {
|
||||
@@ -3149,6 +3155,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
|
||||
*/
|
||||
|
||||
@@ -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_INTEGRATIONS;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
@@ -62,6 +63,7 @@ use Utopia\Migration\Resources\Messaging\Provider;
|
||||
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;
|
||||
@@ -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;
|
||||
@@ -210,6 +215,7 @@ class Appwrite extends Source
|
||||
// Integrations
|
||||
Resource::TYPE_PLATFORM,
|
||||
Resource::TYPE_API_KEY,
|
||||
Resource::TYPE_WEBHOOK,
|
||||
|
||||
// Backups
|
||||
Resource::TYPE_BACKUP_POLICY,
|
||||
@@ -1536,6 +1542,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
|
||||
*/
|
||||
@@ -2341,6 +2398,19 @@ class Appwrite extends Source
|
||||
$report[Resource::TYPE_API_KEY] = 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2400,6 +2470,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_INTEGRATIONS,
|
||||
message: $e->getMessage(),
|
||||
code: $e->getCode(),
|
||||
previous: $e
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -65,6 +65,7 @@ class Transfer
|
||||
public const GROUP_INTEGRATIONS_RESOURCES = [
|
||||
Resource::TYPE_PLATFORM,
|
||||
Resource::TYPE_API_KEY,
|
||||
Resource::TYPE_WEBHOOK,
|
||||
];
|
||||
public const GROUP_DOCUMENTSDB_RESOURCES = [
|
||||
Resource::TYPE_DATABASE_DOCUMENTSDB,
|
||||
@@ -135,6 +136,7 @@ class Transfer
|
||||
// Integrations
|
||||
Resource::TYPE_PLATFORM,
|
||||
Resource::TYPE_API_KEY,
|
||||
Resource::TYPE_WEBHOOK,
|
||||
|
||||
// Settings
|
||||
Resource::TYPE_PROJECT_VARIABLE,
|
||||
|
||||
@@ -53,6 +53,8 @@ class MockDestination extends Destination
|
||||
Resource::TYPE_MEMBERSHIP,
|
||||
Resource::TYPE_PLATFORM,
|
||||
Resource::TYPE_API_KEY,
|
||||
Resource::TYPE_PROJECT_VARIABLE,
|
||||
Resource::TYPE_WEBHOOK,
|
||||
Resource::TYPE_PROVIDER,
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
|
||||
@@ -82,6 +82,8 @@ class MockSource extends Source
|
||||
Resource::TYPE_MEMBERSHIP,
|
||||
Resource::TYPE_PLATFORM,
|
||||
Resource::TYPE_API_KEY,
|
||||
Resource::TYPE_PROJECT_VARIABLE,
|
||||
Resource::TYPE_WEBHOOK,
|
||||
Resource::TYPE_PROVIDER,
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
|
||||
Reference in new issue
Block a user