|
13 | 13 | use Appwrite\Services\TablesDB; |
14 | 14 | use Appwrite\Services\Teams; |
15 | 15 | use Appwrite\Services\Users; |
| 16 | +use Appwrite\Services\Webhooks; |
16 | 17 | use Utopia\Database\Database as UtopiaDatabase; |
17 | 18 | use Utopia\Database\DateTime as UtopiaDateTime; |
18 | 19 | use Utopia\Database\Document as UtopiaDocument; |
|
62 | 63 | use Utopia\Migration\Resources\Messaging\Subscriber; |
63 | 64 | use Utopia\Migration\Resources\Messaging\Topic; |
64 | 65 | use Utopia\Migration\Resources\Settings\ProjectVariable; |
| 66 | +use Utopia\Migration\Resources\Settings\Webhook; |
65 | 67 | use Utopia\Migration\Resources\Sites\Deployment as SiteDeployment; |
66 | 68 | use Utopia\Migration\Resources\Sites\EnvVar as SiteEnvVar; |
67 | 69 | use Utopia\Migration\Resources\Sites\Site; |
@@ -98,6 +100,8 @@ class Appwrite extends Source |
98 | 100 |
|
99 | 101 | private Project $project; |
100 | 102 |
|
| 103 | + private Webhooks $webhooks; |
| 104 | + |
101 | 105 | /** |
102 | 106 | * @var callable(UtopiaDocument $database|null): UtopiaDatabase |
103 | 107 | */ |
@@ -127,6 +131,7 @@ public function __construct( |
127 | 131 | $this->messaging = new Messaging($this->client); |
128 | 132 | $this->sites = new Sites($this->client); |
129 | 133 | $this->project = new Project($this->client); |
| 134 | + $this->webhooks = new Webhooks($this->client); |
130 | 135 |
|
131 | 136 | $this->headers['x-appwrite-project'] = $this->projectId; |
132 | 137 | $this->headers['x-appwrite-key'] = $this->key; |
@@ -210,6 +215,7 @@ public static function getSupportedResources(): array |
210 | 215 | // Integrations |
211 | 216 | Resource::TYPE_PLATFORM, |
212 | 217 | Resource::TYPE_API_KEY, |
| 218 | + Resource::TYPE_WEBHOOK, |
213 | 219 |
|
214 | 220 | // Backups |
215 | 221 | Resource::TYPE_BACKUP_POLICY, |
@@ -1536,6 +1542,57 @@ private function exportProjectVariables(int $batchSize): void |
1536 | 1542 | } |
1537 | 1543 | } |
1538 | 1544 |
|
| 1545 | + /** |
| 1546 | + * @throws AppwriteException |
| 1547 | + */ |
| 1548 | + private function exportWebhooks(int $batchSize): void |
| 1549 | + { |
| 1550 | + $lastId = null; |
| 1551 | + |
| 1552 | + while (true) { |
| 1553 | + $queries = [Query::limit($batchSize)]; |
| 1554 | + |
| 1555 | + if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_WEBHOOK) { |
| 1556 | + $queries[] = Query::equal('$id', $this->rootResourceId); |
| 1557 | + $queries[] = Query::limit(1); |
| 1558 | + } |
| 1559 | + |
| 1560 | + if ($lastId !== null) { |
| 1561 | + $queries[] = Query::cursorAfter($lastId); |
| 1562 | + } |
| 1563 | + |
| 1564 | + $response = $this->webhooks->list($queries); |
| 1565 | + if ($response->total === 0) { |
| 1566 | + break; |
| 1567 | + } |
| 1568 | + |
| 1569 | + $webhooks = []; |
| 1570 | + |
| 1571 | + foreach ($response->webhooks as $webhook) { |
| 1572 | + $webhooks[] = new Webhook( |
| 1573 | + $webhook->id, |
| 1574 | + $webhook->name, |
| 1575 | + $webhook->url, |
| 1576 | + $webhook->events, |
| 1577 | + $webhook->tls, |
| 1578 | + $webhook->authUsername, |
| 1579 | + $webhook->authPassword, |
| 1580 | + $webhook->enabled, |
| 1581 | + createdAt: $webhook->createdAt, |
| 1582 | + updatedAt: $webhook->updatedAt, |
| 1583 | + ); |
| 1584 | + |
| 1585 | + $lastId = $webhook->id; |
| 1586 | + } |
| 1587 | + |
| 1588 | + $this->callback($webhooks); |
| 1589 | + |
| 1590 | + if (\count($response->webhooks) < $batchSize) { |
| 1591 | + break; |
| 1592 | + } |
| 1593 | + } |
| 1594 | + } |
| 1595 | + |
1539 | 1596 | /** |
1540 | 1597 | * @throws AppwriteException |
1541 | 1598 | */ |
@@ -2341,6 +2398,19 @@ private function reportIntegrations(array $resources, array &$report, array $res |
2341 | 2398 | $report[Resource::TYPE_API_KEY] = 0; |
2342 | 2399 | } |
2343 | 2400 | } |
| 2401 | + |
| 2402 | + if (\in_array(Resource::TYPE_WEBHOOK, $resources)) { |
| 2403 | + $webhookQueries = $this->buildQueries( |
| 2404 | + resourceType: Resource::TYPE_WEBHOOK, |
| 2405 | + resourceIds: $resourceIds, |
| 2406 | + limit: 1 |
| 2407 | + ); |
| 2408 | + try { |
| 2409 | + $report[Resource::TYPE_WEBHOOK] = $this->webhooks->list($webhookQueries)->total; |
| 2410 | + } catch (\Throwable) { |
| 2411 | + $report[Resource::TYPE_WEBHOOK] = 0; |
| 2412 | + } |
| 2413 | + } |
2344 | 2414 | } |
2345 | 2415 |
|
2346 | 2416 | /** |
@@ -2400,6 +2470,20 @@ protected function exportGroupIntegrations(int $batchSize, array $resources): vo |
2400 | 2470 | )); |
2401 | 2471 | } |
2402 | 2472 | } |
| 2473 | + |
| 2474 | + if (\in_array(Resource::TYPE_WEBHOOK, $resources)) { |
| 2475 | + try { |
| 2476 | + $this->exportWebhooks($batchSize); |
| 2477 | + } catch (\Throwable $e) { |
| 2478 | + $this->addError(new Exception( |
| 2479 | + Resource::TYPE_WEBHOOK, |
| 2480 | + Transfer::GROUP_INTEGRATIONS, |
| 2481 | + message: $e->getMessage(), |
| 2482 | + code: $e->getCode(), |
| 2483 | + previous: $e |
| 2484 | + )); |
| 2485 | + } |
| 2486 | + } |
2403 | 2487 | } |
2404 | 2488 |
|
2405 | 2489 | /** |
|
0 commit comments