Some checks failed
CodeQL / Analyze (javascript) (push) Failing after 16s
CodeQL / Analyze (python) (push) Failing after 3s
i18n sync check / check:sync (push) Failing after 3s
React Doctor / react-doctor (push) Failing after 16s
Branch Build CE / Build-Push Admin Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Admin Manifest (push) Has been cancelled
Branch Build CE / Build-Push Web Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Web Manifest (push) Has been cancelled
Branch Build CE / Build-Push Space Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Proxy Manifest (push) Has been cancelled
Branch Build CE / Merge Space Manifest (push) Has been cancelled
Branch Build CE / Prepare AIO Assets (push) Has been cancelled
Branch Build CE / Build Setup (push) Has been cancelled
Branch Build CE / Build-Push Live Collaboration Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge Live Collaboration Manifest (push) Has been cancelled
Branch Build CE / Build-Push API Server Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge API Server Manifest (push) Has been cancelled
Branch Build CE / Build-Push Proxy Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Build-Push AIO Docker Image (${{ matrix.arch }}) (push) Has been cancelled
Branch Build CE / Merge AIO Manifest (push) Has been cancelled
Branch Build CE / Upload Build Assets (push) Has been cancelled
Branch Build CE / Build Release (push) Has been cancelled
130 lines
4.7 KiB
Python
130 lines
4.7 KiB
Python
# Copyright (c) 2023-present Plane Software, Inc. and contributors
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# See the LICENSE file for details.
|
|
|
|
# Django imports
|
|
from django.db import IntegrityError
|
|
|
|
# Third party imports
|
|
from rest_framework import status
|
|
from rest_framework.response import Response
|
|
|
|
# Module imports
|
|
from plane.db.models import Webhook, WebhookLog, Workspace
|
|
from plane.db.models.webhook import generate_token
|
|
from ..base import BaseAPIView
|
|
from plane.app.permissions import allow_permission, ROLE
|
|
from plane.app.serializers import WebhookSerializer, WebhookLogSerializer
|
|
|
|
|
|
class WebhookEndpoint(BaseAPIView):
|
|
@allow_permission(allowed_roles=[ROLE.ADMIN], level="WORKSPACE")
|
|
def post(self, request, slug):
|
|
workspace = Workspace.objects.get(slug=slug)
|
|
try:
|
|
serializer = WebhookSerializer(
|
|
data=request.data,
|
|
context={"request": request, "show_secret_key": True},
|
|
)
|
|
if serializer.is_valid():
|
|
serializer.save(workspace_id=workspace.id)
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
except IntegrityError as e:
|
|
if "already exists" in str(e):
|
|
return Response(
|
|
{"error": "URL already exists for the workspace"},
|
|
status=status.HTTP_409_CONFLICT,
|
|
)
|
|
raise IntegrityError
|
|
|
|
@allow_permission(allowed_roles=[ROLE.ADMIN], level="WORKSPACE")
|
|
def get(self, request, slug, pk=None):
|
|
if pk is None:
|
|
webhooks = Webhook.objects.filter(workspace__slug=slug)
|
|
serializer = WebhookSerializer(
|
|
webhooks,
|
|
fields=(
|
|
"id",
|
|
"url",
|
|
"is_active",
|
|
"created_at",
|
|
"updated_at",
|
|
"project",
|
|
"issue",
|
|
"cycle",
|
|
"module",
|
|
"issue_comment",
|
|
),
|
|
many=True,
|
|
)
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
else:
|
|
webhook = Webhook.objects.get(workspace__slug=slug, pk=pk)
|
|
serializer = WebhookSerializer(
|
|
webhook,
|
|
fields=(
|
|
"id",
|
|
"url",
|
|
"is_active",
|
|
"created_at",
|
|
"updated_at",
|
|
"project",
|
|
"issue",
|
|
"cycle",
|
|
"module",
|
|
"issue_comment",
|
|
),
|
|
)
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
|
|
@allow_permission(allowed_roles=[ROLE.ADMIN], level="WORKSPACE")
|
|
def patch(self, request, slug, pk):
|
|
webhook = Webhook.objects.get(workspace__slug=slug, pk=pk)
|
|
serializer = WebhookSerializer(
|
|
webhook,
|
|
data=request.data,
|
|
context={"request": request},
|
|
partial=True,
|
|
fields=(
|
|
"id",
|
|
"url",
|
|
"is_active",
|
|
"created_at",
|
|
"updated_at",
|
|
"project",
|
|
"issue",
|
|
"cycle",
|
|
"module",
|
|
"issue_comment",
|
|
),
|
|
)
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
@allow_permission(allowed_roles=[ROLE.ADMIN], level="WORKSPACE")
|
|
def delete(self, request, slug, pk):
|
|
webhook = Webhook.objects.get(pk=pk, workspace__slug=slug)
|
|
webhook.delete()
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
|
class WebhookSecretRegenerateEndpoint(BaseAPIView):
|
|
@allow_permission(allowed_roles=[ROLE.ADMIN], level="WORKSPACE")
|
|
def post(self, request, slug, pk):
|
|
webhook = Webhook.objects.get(workspace__slug=slug, pk=pk)
|
|
webhook.secret_key = generate_token()
|
|
webhook.save()
|
|
serializer = WebhookSerializer(webhook, context={"show_secret_key": True})
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
|
|
|
|
class WebhookLogsEndpoint(BaseAPIView):
|
|
@allow_permission(allowed_roles=[ROLE.ADMIN], level="WORKSPACE")
|
|
def get(self, request, slug, webhook_id):
|
|
webhook_logs = WebhookLog.objects.filter(workspace__slug=slug, webhook=webhook_id)
|
|
serializer = WebhookLogSerializer(webhook_logs, many=True)
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|