dev: updated webhook configuration

This commit is contained in:
pablohashescobar 2023-11-23 14:52:19 +05:30
parent 5186990426
commit c8706a166b
4 changed files with 36 additions and 21 deletions

View File

@ -9,6 +9,7 @@ from .issue import (
IssueCommentSerializer, IssueCommentSerializer,
IssueAttachmentSerializer, IssueAttachmentSerializer,
IssueActivitySerializer, IssueActivitySerializer,
IssueExpandSerializer,
) )
from .state import StateLiteSerializer, StateSerializer from .state import StateLiteSerializer, StateSerializer
from .cycle import CycleSerializer, CycleIssueSerializer from .cycle import CycleSerializer, CycleIssueSerializer

View File

@ -19,7 +19,8 @@ from plane.db.models import (
ProjectMember, ProjectMember,
) )
from .base import BaseSerializer from .base import BaseSerializer
from .cycle import CycleSerializer
from .module import ModuleSerializer
class IssueSerializer(BaseSerializer): class IssueSerializer(BaseSerializer):
assignees = serializers.ListField( assignees = serializers.ListField(
@ -309,3 +310,22 @@ class IssueActivitySerializer(BaseSerializer):
"created_by", "created_by",
"updated_by", "updated_by",
] ]
class IssueExpandSerializer(BaseSerializer):
cycle = CycleSerializer(source="issue_cycle.cycle", many=True)
module = ModuleSerializer(source="issue_module.module", many=True)
class Meta:
model = Issue
fields = "__all__"
read_only_fields = [
"id",
"workspace",
"project",
"created_by",
"updated_by",
"created_at",
"updated_at",
]

View File

@ -58,6 +58,7 @@ class ConfigurationEndpoint(BaseAPIView):
) and get_configuration_value( ) and get_configuration_value(
instance_configuration, "ENABLE_MAGIC_LINK_LOGIN", "0" instance_configuration, "ENABLE_MAGIC_LINK_LOGIN", "0"
) == "1" ) == "1"
data["email_password_login"] = ( data["email_password_login"] = (
get_configuration_value( get_configuration_value(
instance_configuration, "ENABLE_EMAIL_PASSWORD", "0" instance_configuration, "ENABLE_EMAIL_PASSWORD", "0"

View File

@ -31,11 +31,12 @@ from plane.api.serializers import (
CycleIssueSerializer, CycleIssueSerializer,
ModuleIssueSerializer, ModuleIssueSerializer,
IssueCommentSerializer, IssueCommentSerializer,
IssueExpandSerializer,
) )
SERIALIZER_MAPPER = { SERIALIZER_MAPPER = {
"project": ProjectSerializer, "project": ProjectSerializer,
"issue": IssueSerializer, "issue": IssueExpandSerializer,
"cycle": CycleSerializer, "cycle": CycleSerializer,
"module": ModuleSerializer, "module": ModuleSerializer,
"cycle_issue": CycleIssueSerializer, "cycle_issue": CycleIssueSerializer,
@ -54,16 +55,11 @@ MODEL_MAPPER = {
} }
def get_model_data(event, event_id, many=False): def get_model_data(event, event_id, many=True):
model = MODEL_MAPPER.get(event) model = MODEL_MAPPER.get(event)
if many: queryset = model.objects.get(pk=event_id)
queryset = model.objects.get(pk__in=event_id)
else:
queryset = model.objects.get(pk__in=event_id)
serializer = SERIALIZER_MAPPER.get(event) serializer = SERIALIZER_MAPPER.get(event)
data = serializer(queryset, many=many).data return serializer(queryset).data
return data
@shared_task( @shared_task(
@ -73,7 +69,7 @@ def get_model_data(event, event_id, many=False):
max_retries=5, max_retries=5,
retry_jitter=True, retry_jitter=True,
) )
def webhook_task(self, webhook, slug, event, event_id, action): def webhook_task(self, webhook, slug, event, event_data, action):
try: try:
webhook = Webhook.objects.get(id=webhook, workspace__slug=slug) webhook = Webhook.objects.get(id=webhook, workspace__slug=slug)
@ -84,11 +80,6 @@ def webhook_task(self, webhook, slug, event, event_id, action):
"X-Plane-Event": event, "X-Plane-Event": event,
} }
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 # # Your secret key
event_data = ( event_data = (
json.loads(json.dumps(event_data, cls=DjangoJSONEncoder)) json.loads(json.dumps(event_data, cls=DjangoJSONEncoder))
@ -195,18 +186,20 @@ def send_webhook(event, event_data, kw, action, slug, bulk):
if webhooks: if webhooks:
if action in ["POST", "PATCH"]: if action in ["POST", "PATCH"]:
if bulk: if bulk:
event_id = [] event_data= []
if event in ["cycle_issue", "module_issue"]:
pass
else: else:
event_id = event_data.get("id") if isinstance(event_data, dict) else None event_id = event_data.get("id") if isinstance(event_data, dict) else None
event_data = [get_model_data(event=event, event_id=event_id, many=isinstance(event_id, list))]
if action == "DELETE": if action == "DELETE":
event_id = kw.get("pk") event_data = [{"id": kw.get("pk")}]
for webhook in webhooks: for webhook in webhooks:
webhook_task.delay(webhook.id, slug, event, event_id, action) for data in event_data:
webhook_task.delay(webhook=webhook.id, slug=slug, event=event, event_data=data, action=action,)
except Exception as e: except Exception as e:
if settings.DEBUG: if settings.DEBUG: