dev: update importer task to create user automatically and adding it to project and workspace (#467)

This commit is contained in:
pablohashescobar 2023-03-17 22:57:10 +05:30 committed by GitHub
parent e7ef6275cd
commit 350e183375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,12 +1,14 @@
# Python imports # Python imports
import json import json
import requests import requests
import uuid
import jwt import jwt
from datetime import datetime from datetime import datetime
# Django imports # Django imports
from django.conf import settings from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.json import DjangoJSONEncoder
from django.contrib.auth.hashers import make_password
# Third Party imports # Third Party imports
from django_rq import job from django_rq import job
@ -16,12 +18,13 @@ from sentry_sdk import capture_exception
from plane.api.serializers import ImporterSerializer from plane.api.serializers import ImporterSerializer
from plane.db.models import ( from plane.db.models import (
Importer, Importer,
WorkspaceMemberInvite, WorkspaceMember,
GithubRepositorySync, GithubRepositorySync,
GithubRepository, GithubRepository,
ProjectMember, ProjectMember,
WorkspaceIntegration, WorkspaceIntegration,
Label, Label,
User,
) )
from .workspace_invitation_task import workspace_invitation from .workspace_invitation_task import workspace_invitation
@ -35,40 +38,42 @@ def service_importer(service, importer_id):
users = importer.data.get("users", []) users = importer.data.get("users", [])
workspace_invitations = WorkspaceMemberInvite.objects.bulk_create( # For all invited users create the uers
new_users = User.objects.bulk_create(
[ [
WorkspaceMemberInvite( User(
email=user.get("email").strip().lower(), email=user.get("email").strip().lower(),
workspace_id=importer.workspace_id, username=uuid.uuid4().hex,
token=jwt.encode( password=make_password(uuid.uuid4().hex),
{ is_password_autoset=True,
"email": user.get("email").strip().lower(),
"timestamp": datetime.now().timestamp(),
},
settings.SECRET_KEY,
algorithm="HS256",
),
role=10,
) )
for user in users for user in users
if user.get("import", False) == "invite" if user.get("import", False) == "invite"
or user.get("import", False) == "map" ],
batch_size=10,
)
# Add new users to Workspace and project automatically
WorkspaceMember.objects.bulk_create(
[
WorkspaceMember(member=user, workspace_id=importer.workspace_id)
for user in new_users
],
batch_size=100,
ignore_conflicts=True,
)
ProjectMember.objects.bulk_create(
[
ProjectMember(
project_id=importer.project_id,
workspace_id=importer.workspace_id,
member=user,
)
for user in new_users
], ],
batch_size=100, batch_size=100,
ignore_conflicts=True, ignore_conflicts=True,
) )
# Send the invites
[
workspace_invitation.delay(
invitation.email,
importer.workspace_id,
invitation.token,
settings.WEB_URL,
importer.initiated_by.email,
)
for invitation in workspace_invitations
]
# Check if sync config is on for github importers # Check if sync config is on for github importers
if service == "github" and importer.config.get("sync", False): if service == "github" and importer.config.get("sync", False):
@ -85,7 +90,6 @@ def service_importer(service, importer_id):
# Delete the old repository object # Delete the old repository object
GithubRepositorySync.objects.filter(project_id=importer.project_id).delete() GithubRepositorySync.objects.filter(project_id=importer.project_id).delete()
GithubRepository.objects.filter(project_id=importer.project_id).delete() GithubRepository.objects.filter(project_id=importer.project_id).delete()
# Project member delete
# Create a Label for github # Create a Label for github
label = Label.objects.filter( label = Label.objects.filter(