Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev: validation for external id and external source #3552

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
dev: error response for duplicate items created through external apis
  • Loading branch information
pablohashescobar committed Feb 1, 2024
commit 314d4494e58163d18c8047c6a092e213687761cf
37 changes: 37 additions & 0 deletions apiserver/plane/api/views/cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,22 @@ def post(self, request, slug, project_id):
):
serializer = CycleSerializer(data=request.data)
if serializer.is_valid():
if (
request.data.get("external_id")
and request.data.get("external_source")
and Cycle.objects.filter(
project_id=project_id,
workspace__slug=slug,
external_source=request.data.get("external_source"),
external_id=request.data.get("external_id"),
).exists()
):
return Response(
{
"error": "Cycle with the same external id and external source already exists"
},
status=status.HTTP_410_GONE,
)
serializer.save(
project_id=project_id,
owned_by=request.user,
Expand Down Expand Up @@ -289,6 +305,27 @@ def patch(self, request, slug, project_id, pk):

serializer = CycleSerializer(cycle, data=request.data, partial=True)
if serializer.is_valid():
if (
request.data.get("external_id")
and (cycle.external_id != request.data.get("external_id"))
and request.data.get("external_source")
and (
cycle.external_source
!= request.data.get("external_source")
)
and Cycle.objects.filter(
project_id=project_id,
workspace__slug=slug,
external_source=request.data.get("external_source"),
external_id=request.data.get("external_id"),
).exists()
):
return Response(
{
"error": "Cycle with the same external id and external source already exists"
},
status=status.HTTP_410_GONE,
)
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Expand Down
39 changes: 39 additions & 0 deletions apiserver/plane/api/views/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ def post(self, request, slug, project_id):
)

if serializer.is_valid():
if (
request.data.get("external_id")
and request.data.get("external_source")
and Issue.objects.filter(
project_id=project_id,
workspace__slug=slug,
external_source=request.data.get("external_source"),
external_id=request.data.get("external_id"),
).exists()
):
return Response(
{
"error": "Issue with the same external id and external source already exists"
},
status=status.HTTP_410_GONE,
)

serializer.save()

# Track the issue
Expand Down Expand Up @@ -256,6 +273,28 @@ def patch(self, request, slug, project_id, pk=None):
partial=True,
)
if serializer.is_valid():
if (
str(request.data.get("external_id"))
and (issue.external_id != str(request.data.get("external_id")))
and request.data.get("external_source")
and (
issue.external_source
!= request.data.get("external_source")
)
and Issue.objects.filter(
project_id=project_id,
workspace__slug=slug,
external_source=request.data.get("external_source"),
external_id=request.data.get("external_id"),
).exists()
):
return Response(
{
"error": "Issue with the same external id and external source already exists"
},
status=status.HTTP_410_GONE,
)

serializer.save()
issue_activity.delay(
type="issue.activity.updated",
Expand Down
37 changes: 37 additions & 0 deletions apiserver/plane/api/views/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ def post(self, request, slug, project_id):
},
)
if serializer.is_valid():
if (
request.data.get("external_id")
and request.data.get("external_source")
and Module.objects.filter(
project_id=project_id,
workspace__slug=slug,
external_source=request.data.get("external_source"),
external_id=request.data.get("external_id"),
).exists()
):
return Response(
{
"error": "Module with the same external id and external source already exists"
},
status=status.HTTP_410_GONE,
)
serializer.save()
module = Module.objects.get(pk=serializer.data["id"])
serializer = ModuleSerializer(module)
Expand All @@ -149,6 +165,27 @@ def patch(self, request, slug, project_id, pk):
partial=True,
)
if serializer.is_valid():
if (
request.data.get("external_id")
and (module.external_id != request.data.get("external_id"))
and request.data.get("external_source")
and (
module.external_source
!= request.data.get("external_source")
)
and Module.objects.filter(
project_id=project_id,
workspace__slug=slug,
external_source=request.data.get("external_source"),
external_id=request.data.get("external_id"),
).exists()
):
return Response(
{
"error": "Module with the same external id and external source already exists"
},
status=status.HTTP_410_GONE,
)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Expand Down