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
This commit is contained in:
pablohashescobar 2023-02-23 23:56:48 +05:30 committed by GitHub
parent b53b0bc3f0
commit 517600ac89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 7 deletions

View File

@ -733,7 +733,7 @@ urlpatterns = [
name="workspace-integrations", name="workspace-integrations",
), ),
path( path(
"workspaces/<str:slug>/workspace-integrations/<uuid:pk>/", "workspaces/<str:slug>/workspace-integrations/<uuid:pk>/provider/",
WorkspaceIntegrationViewSet.as_view( WorkspaceIntegrationViewSet.as_view(
{ {
"get": "retrieve", "get": "retrieve",

View File

@ -21,7 +21,10 @@ from plane.db.models import (
APIToken, APIToken,
) )
from plane.api.serializers import IntegrationSerializer, WorkspaceIntegrationSerializer 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): class IntegrationViewSet(BaseViewSet):
@ -77,6 +80,14 @@ class WorkspaceIntegrationViewSet(BaseViewSet):
serializer_class = WorkspaceIntegrationSerializer serializer_class = WorkspaceIntegrationSerializer
model = WorkspaceIntegration 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): def create(self, request, slug, provider):
try: try:
installation_id = request.data.get("installation_id", None) installation_id = request.data.get("installation_id", None)
@ -157,3 +168,31 @@ class WorkspaceIntegrationViewSet(BaseViewSet):
{"error": "Something went wrong please try again later"}, {"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST, 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,
)

View File

@ -46,6 +46,14 @@ class GithubRepositorySyncViewSet(BaseViewSet):
def perform_create(self, serializer): def perform_create(self, serializer):
serializer.save(project_id=self.kwargs.get("project_id")) 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): def create(self, request, slug, project_id, workspace_integration_id):
try: try:
name = request.data.get("name", False) name = request.data.get("name", False)
@ -60,6 +68,23 @@ class GithubRepositorySyncViewSet(BaseViewSet):
status=status.HTTP_400_BAD_REQUEST, 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 # Create repository
repo = GithubRepository.objects.create( repo = GithubRepository.objects.create(
name=name, name=name,
@ -70,11 +95,6 @@ class GithubRepositorySyncViewSet(BaseViewSet):
project_id=project_id, project_id=project_id,
) )
# Get the workspace integration
workspace_integration = WorkspaceIntegration.objects.get(
pk=workspace_integration_id
)
# Create a Label for github # Create a Label for github
label = Label.objects.filter( label = Label.objects.filter(
name="GitHub", name="GitHub",

View File

@ -60,3 +60,15 @@ def get_github_repos(access_tokens_url, repositories_url):
headers=headers, headers=headers,
).json() ).json()
return response 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