forked from github/plane
dev: webhooks restructuring
This commit is contained in:
parent
bc0c9404a4
commit
5186990426
@ -38,6 +38,7 @@ class TimezoneMixin:
|
||||
|
||||
class WebhookMixin:
|
||||
webhook_event = None
|
||||
bulk = False
|
||||
|
||||
def finalize_response(self, request, response, *args, **kwargs):
|
||||
response = super().finalize_response(request, response, *args, **kwargs)
|
||||
@ -45,17 +46,17 @@ class WebhookMixin:
|
||||
# Check for the case should webhook be sent
|
||||
if (
|
||||
self.webhook_event
|
||||
and self.request.method in ["DELETE"]
|
||||
and response.status_code in [204]
|
||||
and self.request.method in ["POST", "PATCH", "DELETE"]
|
||||
and response.status_code in [200, 201, 204]
|
||||
):
|
||||
# Get the id
|
||||
object_id = self.kwargs.get("pk")
|
||||
# Push the object to delay
|
||||
send_webhook.delay(
|
||||
event=self.webhook_event,
|
||||
event_id=object_id,
|
||||
event_data=response.data,
|
||||
kw=self.kwargs,
|
||||
action=self.request.method,
|
||||
slug=self.workspace_slug,
|
||||
bulk=self.bulk,
|
||||
)
|
||||
|
||||
return response
|
||||
|
@ -195,6 +195,7 @@ class ModuleIssueAPIEndpoint(WebhookMixin, BaseAPIView):
|
||||
serializer_class = ModuleIssueSerializer
|
||||
model = ModuleIssue
|
||||
webhook_event = "module_issue"
|
||||
bulk = True
|
||||
|
||||
permission_classes = [
|
||||
ProjectEntityPermission,
|
||||
|
@ -54,11 +54,16 @@ MODEL_MAPPER = {
|
||||
}
|
||||
|
||||
|
||||
def get_model_data(event, event_id):
|
||||
def get_model_data(event, event_id, many=False):
|
||||
model = MODEL_MAPPER.get(event)
|
||||
queryset = model.objects.get(pk=event_id)
|
||||
if many:
|
||||
queryset = model.objects.get(pk__in=event_id)
|
||||
else:
|
||||
queryset = model.objects.get(pk__in=event_id)
|
||||
|
||||
serializer = SERIALIZER_MAPPER.get(event)
|
||||
return serializer(queryset).data
|
||||
data = serializer(queryset, many=many).data
|
||||
return data
|
||||
|
||||
|
||||
@shared_task(
|
||||
@ -79,7 +84,10 @@ def webhook_task(self, webhook, slug, event, event_id, action):
|
||||
"X-Plane-Event": event,
|
||||
}
|
||||
|
||||
event_data = get_model_data(event=event, event_id=event_id)
|
||||
if action == "DELETE":
|
||||
event_data = {"id": str(event_id)}
|
||||
else:
|
||||
event_data = get_model_data(event=event, event_id=event_id, many=isinstance(event_id, list))
|
||||
|
||||
# # Your secret key
|
||||
event_data = (
|
||||
@ -166,7 +174,7 @@ def webhook_task(self, webhook, slug, event, event_id, action):
|
||||
|
||||
|
||||
@shared_task()
|
||||
def send_webhook(event, event_id, action, slug):
|
||||
def send_webhook(event, event_data, kw, action, slug, bulk):
|
||||
try:
|
||||
webhooks = Webhook.objects.filter(workspace__slug=slug, is_active=True)
|
||||
|
||||
@ -185,8 +193,20 @@ def send_webhook(event, event_id, action, slug):
|
||||
if event == "issue_comment":
|
||||
webhooks = webhooks.filter(issue_comment=True)
|
||||
|
||||
for webhook in webhooks:
|
||||
webhook_task.delay(webhook.id, slug, event, event_id, action)
|
||||
|
||||
if webhooks:
|
||||
|
||||
if action in ["POST", "PATCH"]:
|
||||
if bulk:
|
||||
event_id = []
|
||||
else:
|
||||
event_id = event_data.get("id") if isinstance(event_data, dict) else None
|
||||
if action == "DELETE":
|
||||
event_id = kw.get("pk")
|
||||
|
||||
|
||||
for webhook in webhooks:
|
||||
webhook_task.delay(webhook.id, slug, event, event_id, action)
|
||||
|
||||
except Exception as e:
|
||||
if settings.DEBUG:
|
||||
|
Loading…
Reference in New Issue
Block a user