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