From 517600ac89ffb18096a3dc19fa2d2001b867c3f9 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Thu, 23 Feb 2023 23:56:48 +0530 Subject: [PATCH] fix: add filter for workspace integrations (#325) * fix: add filter for workspace integrations * fix: update url for delete * fix: remove github installation when deleted * fix: delete old repos * fix: add filter on repository endpoints --- apiserver/plane/api/urls.py | 2 +- apiserver/plane/api/views/integration/base.py | 41 ++++++++++++++++++- .../plane/api/views/integration/github.py | 30 +++++++++++--- apiserver/plane/utils/integrations/github.py | 12 ++++++ 4 files changed, 78 insertions(+), 7 deletions(-) diff --git a/apiserver/plane/api/urls.py b/apiserver/plane/api/urls.py index e44579cb7..f267ff16a 100644 --- a/apiserver/plane/api/urls.py +++ b/apiserver/plane/api/urls.py @@ -733,7 +733,7 @@ urlpatterns = [ name="workspace-integrations", ), path( - "workspaces//workspace-integrations//", + "workspaces//workspace-integrations//provider/", WorkspaceIntegrationViewSet.as_view( { "get": "retrieve", diff --git a/apiserver/plane/api/views/integration/base.py b/apiserver/plane/api/views/integration/base.py index bded732ec..4f15c347f 100644 --- a/apiserver/plane/api/views/integration/base.py +++ b/apiserver/plane/api/views/integration/base.py @@ -21,7 +21,10 @@ from plane.db.models import ( APIToken, ) from plane.api.serializers import IntegrationSerializer, WorkspaceIntegrationSerializer -from plane.utils.integrations.github import get_github_metadata +from plane.utils.integrations.github import ( + get_github_metadata, + delete_github_installation, +) class IntegrationViewSet(BaseViewSet): @@ -77,6 +80,14 @@ class WorkspaceIntegrationViewSet(BaseViewSet): serializer_class = WorkspaceIntegrationSerializer model = WorkspaceIntegration + def get_queryset(self): + return ( + super() + .get_queryset() + .filter(workspace__slug=self.kwargs.get("slug")) + .select_related("integration") + ) + def create(self, request, slug, provider): try: installation_id = request.data.get("installation_id", None) @@ -157,3 +168,31 @@ class WorkspaceIntegrationViewSet(BaseViewSet): {"error": "Something went wrong please try again later"}, status=status.HTTP_400_BAD_REQUEST, ) + + def destroy(self, request, slug, pk): + try: + workspace_integration = WorkspaceIntegration.objects.get( + pk=pk, workspace__slug=slug + ) + + if workspace_integration.integration.provider == "github": + installation_id = workspace_integration.config.get( + "installation_id", False + ) + if installation_id: + delete_github_installation(installation_id=installation_id) + + workspace_integration.delete() + return Response(status=status.HTTP_204_NO_CONTENT) + + except WorkspaceIntegration.DoesNotExist: + return Response( + {"error": "Workspace Integration Does not exists"}, + status=status.HTTP_400_BAD_REQUEST, + ) + except Exception as e: + capture_exception(e) + return Response( + {"error": "Something went wrong please try again later"}, + status=status.HTTP_400_BAD_REQUEST, + ) diff --git a/apiserver/plane/api/views/integration/github.py b/apiserver/plane/api/views/integration/github.py index 7486ce7b9..df8f1d0c2 100644 --- a/apiserver/plane/api/views/integration/github.py +++ b/apiserver/plane/api/views/integration/github.py @@ -46,6 +46,14 @@ class GithubRepositorySyncViewSet(BaseViewSet): def perform_create(self, serializer): serializer.save(project_id=self.kwargs.get("project_id")) + def get_queryset(self): + return ( + super() + .get_queryset() + .filter(workspace__slug=self.kwargs.get("slug")) + .filter(project_id=self.kwargs.get("project_id")) + ) + def create(self, request, slug, project_id, workspace_integration_id): try: name = request.data.get("name", False) @@ -60,6 +68,23 @@ class GithubRepositorySyncViewSet(BaseViewSet): status=status.HTTP_400_BAD_REQUEST, ) + # Get the workspace integration + workspace_integration = WorkspaceIntegration.objects.get( + pk=workspace_integration_id + ) + + # Delete the old repository object + GithubRepositorySync.objects.filter( + project_id=project_id, workspace__slug=slug + ).delete() + GithubRepository.objects.filter( + project_id=project_id, workspace__slug=slug + ).delete() + # Project member delete + ProjectMember.objects.filter( + member=workspace_integration.actor, role=20, project_id=project_id + ).delete() + # Create repository repo = GithubRepository.objects.create( name=name, @@ -70,11 +95,6 @@ class GithubRepositorySyncViewSet(BaseViewSet): project_id=project_id, ) - # Get the workspace integration - workspace_integration = WorkspaceIntegration.objects.get( - pk=workspace_integration_id - ) - # Create a Label for github label = Label.objects.filter( name="GitHub", diff --git a/apiserver/plane/utils/integrations/github.py b/apiserver/plane/utils/integrations/github.py index ba9cb0ae0..e06ac31f7 100644 --- a/apiserver/plane/utils/integrations/github.py +++ b/apiserver/plane/utils/integrations/github.py @@ -60,3 +60,15 @@ def get_github_repos(access_tokens_url, repositories_url): headers=headers, ).json() return response + + +def delete_github_installation(installation_id): + token = get_jwt_token() + + url = f"https://api.github.com/app/installations/{installation_id}" + headers = { + "Authorization": "Bearer " + token, + "Accept": "application/vnd.github+json", + } + response = requests.delete(url, headers=headers) + return response