Skip to content

Saleor Core: the high performance, composable, headless commerce API.

[SALEOR-6404] Webhooks definitions in app manifest

Tomasz Magulski05606a749ce4
diff --git a/saleor/app/installation_utils.py b/saleor/app/installation_utils.py
index 63899cd779..e5ac223f15 100644
--- a/saleor/app/installation_utils.py
+++ b/saleor/app/installation_utils.py
@@ -1,7 +1,9 @@
import requests
from django.contrib.sites.models import Site
+from django.db import transaction
from ..core.permissions import get_permission_names
+from ..webhook.models import Webhook, WebhookEvent
from .manifest_validations import clean_manifest_data
from .models import App, AppExtension, AppInstallation
from .types import AppExtensionTarget, AppType
@@ -62,6 +64,25 @@ def install_app(
)
extension.permissions.set(extension_data.get("permissions", []))
+ for webhook_data in manifest_data.get("webhooks", []):
+ with transaction.atomic():
+ webhook = Webhook(
+ app=app,
+ name=webhook_data["name"],
+ target_url=webhook_data["targetUrl"],
+ is_active=webhook_data["isActive"],
+ subscription_query=webhook_data["query"],
+ )
+ webhook.full_clean()
+ webhook.save()
+ WebhookEvent.objects.bulk_create(
+ [
+ WebhookEvent(webhook=webhook, event_type=event)
+ for event in webhook_data["events"]
+ ]
+ )
+
+
_, token = app.tokens.create(name="Default token")
try:
diff --git a/saleor/app/manifest_validations.py b/saleor/app/manifest_validations.py
index cdc0c12cdc..0ad3167ce5 100644
--- a/saleor/app/manifest_validations.py
+++ b/saleor/app/manifest_validations.py
@@ -4,9 +4,12 @@ from typing import Dict, Iterable, List
from django.contrib.auth.models import Permission
from django.core.exceptions import ValidationError
+from django.core.validators import URLValidator
from django.db.models import Value
from django.db.models.functions import Concat
+from saleor.graphql.webhook.mutations import validate_query
+
from ..core.permissions import (
get_permissions,
get_permissions_enum_list,
@@ -120,6 +123,7 @@ def clean_manifest_data(manifest_data):
if not errors:
clean_extensions(manifest_data, app_permissions, errors)
+ clean_webhooks(manifest_data, app_permissions, errors)
if errors:
raise ValidationError(errors)
@@ -178,13 +182,16 @@ def clean_extensions(manifest_data, app_permissions, errors):
_clean_extension_permissions(extension, app_permissions, errors)
+def clean_webhooks(manifest_data, app_permissions, errors):
+ webhooks = manifest_data.get("webhooks", [])
+ for webhook in webhooks:
+ validate_query(webhook["query"])
+
+
def validate_required_fields(manifest_data, errors):
manifest_required_fields = {"id", "version", "name", "tokenTargetUrl"}
- extension_required_fields = {
- "label",
- "url",
- "mount",
- }
+ extension_required_fields = {"label", "url", "mount"}
+ webhook_required_fields = {"name", "targetUrl", "isActive", "events", "query"}
manifest_missing_fields = manifest_required_fields.difference(manifest_data)
if manifest_missing_fields:
[
@@ -206,3 +213,16 @@ def validate_required_fields(manifest_data, errors):
code=AppErrorCode.REQUIRED.value,
)
)
+
+ webhooks = manifest_data.get("webhooks", [])
+ for webhook in webhooks:
+ webhook_fields = set(webhook.keys())
+ missing_fields = webhook_required_fields.difference(webhook_fields)
+ if missing_fields:
+ errors["webhooks"].append(
+ ValidationError(
+ "Missing required fields for webhook: %s."
+ % ", ".join(missing_fields),
+ code=AppErrorCode.REQUIRED.value,
+ )
+ )