mirror of
https://github.com/makeplane/plane
synced 2024-06-14 14:31:34 +00:00
feat: slack integration (#874)
* feat: init slack integration * dev: create model and update existing view for slack * dev: update slack sync model and create view to install slack * dev: workspace integration query * dev: update the metadata validation for access_token and team_id and save config to database * dev: update validation for team_id * dev: update validation * dev: update validations * dev: remove bot access token field from sync * dev: handle integrity exception
This commit is contained in:
parent
33a904bc3e
commit
fb4535b294
@ -62,6 +62,7 @@ from .integration import (
|
|||||||
GithubRepositorySerializer,
|
GithubRepositorySerializer,
|
||||||
GithubRepositorySyncSerializer,
|
GithubRepositorySyncSerializer,
|
||||||
GithubCommentSyncSerializer,
|
GithubCommentSyncSerializer,
|
||||||
|
SlackProjectSyncSerializer,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .importer import ImporterSerializer
|
from .importer import ImporterSerializer
|
||||||
|
@ -5,3 +5,4 @@ from .github import (
|
|||||||
GithubIssueSyncSerializer,
|
GithubIssueSyncSerializer,
|
||||||
GithubCommentSyncSerializer,
|
GithubCommentSyncSerializer,
|
||||||
)
|
)
|
||||||
|
from .slack import SlackProjectSyncSerializer
|
14
apiserver/plane/api/serializers/integration/slack.py
Normal file
14
apiserver/plane/api/serializers/integration/slack.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# Module imports
|
||||||
|
from plane.api.serializers import BaseSerializer
|
||||||
|
from plane.db.models import SlackProjectSync
|
||||||
|
|
||||||
|
|
||||||
|
class SlackProjectSyncSerializer(BaseSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = SlackProjectSync
|
||||||
|
fields = "__all__"
|
||||||
|
read_only_fields = [
|
||||||
|
"project",
|
||||||
|
"workspace",
|
||||||
|
"workspace_integration",
|
||||||
|
]
|
@ -132,6 +132,7 @@ from plane.api.views import (
|
|||||||
GithubIssueSyncViewSet,
|
GithubIssueSyncViewSet,
|
||||||
GithubCommentSyncViewSet,
|
GithubCommentSyncViewSet,
|
||||||
BulkCreateGithubIssueSyncEndpoint,
|
BulkCreateGithubIssueSyncEndpoint,
|
||||||
|
SlackProjectSyncViewSet,
|
||||||
## End Integrations
|
## End Integrations
|
||||||
# Importer
|
# Importer
|
||||||
ServiceIssueImportSummaryEndpoint,
|
ServiceIssueImportSummaryEndpoint,
|
||||||
@ -1216,6 +1217,26 @@ urlpatterns = [
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
## End Github Integrations
|
## End Github Integrations
|
||||||
|
# Slack Integration
|
||||||
|
path(
|
||||||
|
"workspaces/<str:slug>/projects/<uuid:project_id>/workspace-integrations/<uuid:workspace_integration_id>/project-slack-sync/",
|
||||||
|
SlackProjectSyncViewSet.as_view(
|
||||||
|
{
|
||||||
|
"post": "create",
|
||||||
|
"get": "list",
|
||||||
|
}
|
||||||
|
),
|
||||||
|
),
|
||||||
|
path(
|
||||||
|
"workspaces/<str:slug>/projects/<uuid:project_id>/workspace-integrations/<uuid:workspace_integration_id>/project-slack-sync/<uuid:pk>/",
|
||||||
|
SlackProjectSyncViewSet.as_view(
|
||||||
|
{
|
||||||
|
"delete": "destroy",
|
||||||
|
"get": "retrieve",
|
||||||
|
}
|
||||||
|
),
|
||||||
|
),
|
||||||
|
## End Slack Integration
|
||||||
## End Integrations
|
## End Integrations
|
||||||
# Importer
|
# Importer
|
||||||
path(
|
path(
|
||||||
|
@ -106,6 +106,7 @@ from .integration import (
|
|||||||
GithubCommentSyncViewSet,
|
GithubCommentSyncViewSet,
|
||||||
GithubRepositoriesEndpoint,
|
GithubRepositoriesEndpoint,
|
||||||
BulkCreateGithubIssueSyncEndpoint,
|
BulkCreateGithubIssueSyncEndpoint,
|
||||||
|
SlackProjectSyncViewSet,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .importer import (
|
from .importer import (
|
||||||
|
@ -6,3 +6,4 @@ from .github import (
|
|||||||
GithubCommentSyncViewSet,
|
GithubCommentSyncViewSet,
|
||||||
GithubRepositoriesEndpoint,
|
GithubRepositoriesEndpoint,
|
||||||
)
|
)
|
||||||
|
from .slack import SlackProjectSyncViewSet
|
||||||
|
@ -27,6 +27,7 @@ from plane.utils.integrations.github import (
|
|||||||
)
|
)
|
||||||
from plane.api.permissions import WorkSpaceAdminPermission
|
from plane.api.permissions import WorkSpaceAdminPermission
|
||||||
|
|
||||||
|
|
||||||
class IntegrationViewSet(BaseViewSet):
|
class IntegrationViewSet(BaseViewSet):
|
||||||
serializer_class = IntegrationSerializer
|
serializer_class = IntegrationSerializer
|
||||||
model = Integration
|
model = Integration
|
||||||
@ -101,7 +102,6 @@ class WorkspaceIntegrationViewSet(BaseViewSet):
|
|||||||
WorkSpaceAdminPermission,
|
WorkSpaceAdminPermission,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return (
|
return (
|
||||||
super()
|
super()
|
||||||
@ -112,21 +112,30 @@ class WorkspaceIntegrationViewSet(BaseViewSet):
|
|||||||
|
|
||||||
def create(self, request, slug, provider):
|
def create(self, request, slug, provider):
|
||||||
try:
|
try:
|
||||||
installation_id = request.data.get("installation_id", None)
|
|
||||||
|
|
||||||
if not installation_id:
|
|
||||||
return Response(
|
|
||||||
{"error": "Installation ID is required"},
|
|
||||||
status=status.HTTP_400_BAD_REQUEST,
|
|
||||||
)
|
|
||||||
|
|
||||||
workspace = Workspace.objects.get(slug=slug)
|
workspace = Workspace.objects.get(slug=slug)
|
||||||
integration = Integration.objects.get(provider=provider)
|
integration = Integration.objects.get(provider=provider)
|
||||||
config = {}
|
config = {}
|
||||||
if provider == "github":
|
if provider == "github":
|
||||||
|
installation_id = request.data.get("installation_id", None)
|
||||||
|
if not installation_id:
|
||||||
|
return Response(
|
||||||
|
{"error": "Installation ID is required"},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
metadata = get_github_metadata(installation_id)
|
metadata = get_github_metadata(installation_id)
|
||||||
config = {"installation_id": installation_id}
|
config = {"installation_id": installation_id}
|
||||||
|
|
||||||
|
if provider == "slack":
|
||||||
|
metadata = request.data.get("metadata", {})
|
||||||
|
access_token = metadata.get("access_token", False)
|
||||||
|
team_id = metadata.get("team", {}).get("id", False)
|
||||||
|
if not metadata or not access_token or not team_id:
|
||||||
|
return Response(
|
||||||
|
{"error": "Access token and team id is required"},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
|
config = {"team_id": team_id, "access_token": access_token}
|
||||||
|
|
||||||
# Create a bot user
|
# Create a bot user
|
||||||
bot_user = User.objects.create(
|
bot_user = User.objects.create(
|
||||||
email=f"{uuid.uuid4().hex}@plane.so",
|
email=f"{uuid.uuid4().hex}@plane.so",
|
||||||
|
59
apiserver/plane/api/views/integration/slack.py
Normal file
59
apiserver/plane/api/views/integration/slack.py
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
# Django import
|
||||||
|
from django.db import IntegrityError
|
||||||
|
|
||||||
|
# Third party imports
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from sentry_sdk import capture_exception
|
||||||
|
|
||||||
|
# Module imports
|
||||||
|
from plane.api.views import BaseViewSet, BaseAPIView
|
||||||
|
from plane.db.models import SlackProjectSync, WorkspaceIntegration, ProjectMember
|
||||||
|
from plane.api.serializers import SlackProjectSyncSerializer
|
||||||
|
from plane.api.permissions import ProjectBasePermission, ProjectEntityPermission
|
||||||
|
|
||||||
|
|
||||||
|
class SlackProjectSyncViewSet(BaseViewSet):
|
||||||
|
permission_classes = [
|
||||||
|
ProjectBasePermission,
|
||||||
|
]
|
||||||
|
serializer_class = SlackProjectSyncSerializer
|
||||||
|
model = SlackProjectSync
|
||||||
|
|
||||||
|
def create(self, request, slug, project_id, workspace_integration_id):
|
||||||
|
try:
|
||||||
|
serializer = SlackProjectSyncSerializer(data=request.data)
|
||||||
|
|
||||||
|
workspace_integration = WorkspaceIntegration.objects.get(
|
||||||
|
workspace__slug=slug, pk=workspace_integration_id
|
||||||
|
)
|
||||||
|
|
||||||
|
if serializer.is_valid():
|
||||||
|
serializer.save(
|
||||||
|
project_id=project_id,
|
||||||
|
workspace_integration_id=workspace_integration_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
workspace_integration = WorkspaceIntegration.objects.get(
|
||||||
|
pk=workspace_integration_id, workspace__slug=slug
|
||||||
|
)
|
||||||
|
|
||||||
|
_ = ProjectMember.objects.get_or_create(
|
||||||
|
member=workspace_integration.actor, role=20, project_id=project_id
|
||||||
|
)
|
||||||
|
|
||||||
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
except IntegrityError:
|
||||||
|
return Response({"error": "Slack is already enabled for the project"}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
except WorkspaceIntegration.DoesNotExist:
|
||||||
|
return Response(
|
||||||
|
{"error": "Workspace Integration does not exist"},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return Response(
|
||||||
|
{"error": "Something went wrong please try again later"},
|
||||||
|
status=status.HTTP_400_BAD_REQUEST,
|
||||||
|
)
|
@ -59,6 +59,7 @@ from .integration import (
|
|||||||
GithubRepositorySync,
|
GithubRepositorySync,
|
||||||
GithubIssueSync,
|
GithubIssueSync,
|
||||||
GithubCommentSync,
|
GithubCommentSync,
|
||||||
|
SlackProjectSync,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .importer import Importer
|
from .importer import Importer
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
from .base import Integration, WorkspaceIntegration
|
from .base import Integration, WorkspaceIntegration
|
||||||
from .github import GithubRepository, GithubRepositorySync, GithubIssueSync, GithubCommentSync
|
from .github import GithubRepository, GithubRepositorySync, GithubIssueSync, GithubCommentSync
|
||||||
|
from .slack import SlackProjectSync
|
32
apiserver/plane/db/models/integration/slack.py
Normal file
32
apiserver/plane/db/models/integration/slack.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# Python imports
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
# Django imports
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
# Module imports
|
||||||
|
from plane.db.models import ProjectBaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class SlackProjectSync(ProjectBaseModel):
|
||||||
|
access_token = models.CharField(max_length=300)
|
||||||
|
scopes = models.TextField()
|
||||||
|
bot_user_id = models.CharField(max_length=50)
|
||||||
|
webhook_url = models.URLField(max_length=1000)
|
||||||
|
data = models.JSONField(default=dict)
|
||||||
|
team_id = models.CharField(max_length=30)
|
||||||
|
team_name = models.CharField(max_length=300)
|
||||||
|
workspace_integration = models.ForeignKey(
|
||||||
|
"db.WorkspaceIntegration", related_name="slack_syncs", on_delete=models.CASCADE
|
||||||
|
)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
"""Return the repo name"""
|
||||||
|
return f"{self.project.name}"
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
unique_together = ["team_id", "project"]
|
||||||
|
verbose_name = "Slack Project Sync"
|
||||||
|
verbose_name_plural = "Slack Project Syncs"
|
||||||
|
db_table = "slack_project_syncs"
|
||||||
|
ordering = ("-created_at",)
|
Loading…
Reference in New Issue
Block a user