From 57d0dade1f362d6871850875231c8cc49068380a Mon Sep 17 00:00:00 2001 From: NarayanBavisetti Date: Fri, 15 Sep 2023 12:54:40 +0530 Subject: [PATCH] chore: changed issue relation history logs --- apiserver/plane/api/serializers/issue.py | 4 +- apiserver/plane/api/views/issue.py | 20 ++++-- .../plane/bgtasks/issue_activites_task.py | 68 +++++++++++++++---- .../db/migrations/0045_auto_20230915_0655.py | 23 +++++++ 4 files changed, 92 insertions(+), 23 deletions(-) create mode 100644 apiserver/plane/db/migrations/0045_auto_20230915_0655.py diff --git a/apiserver/plane/api/serializers/issue.py b/apiserver/plane/api/serializers/issue.py index 113b54d0e..57539f24c 100644 --- a/apiserver/plane/api/serializers/issue.py +++ b/apiserver/plane/api/serializers/issue.py @@ -293,12 +293,12 @@ class IssueLabelSerializer(BaseSerializer): class IssueRelationSerializer(BaseSerializer): - related_issue_detail = IssueProjectLiteSerializer(read_only=True, source="related_issue") + issue_detail = IssueProjectLiteSerializer(read_only=True, source="related_issue") class Meta: model = IssueRelation fields = [ - "related_issue_detail", + "issue_detail", "relation_type", "related_issue", "issue", diff --git a/apiserver/plane/api/views/issue.py b/apiserver/plane/api/views/issue.py index 16dce6f47..79141d78d 100644 --- a/apiserver/plane/api/views/issue.py +++ b/apiserver/plane/api/views/issue.py @@ -53,6 +53,7 @@ from plane.api.serializers import ( CommentReactionSerializer, IssueVoteSerializer, IssueRelationSerializer, + RelatedIssueSerializer, IssuePublicSerializer, ) from plane.api.permissions import ( @@ -2085,9 +2086,10 @@ class IssueRelationViewSet(BaseViewSet): def create(self, request, slug, project_id, issue_id): try: related_list = request.data.get("related_list", []) + relation = request.data.get("relation", None) project = Project.objects.get(pk=project_id) - issueRelation = IssueRelation.objects.bulk_create( + issue_relation = IssueRelation.objects.bulk_create( [ IssueRelation( issue_id=related_issue["issue"], @@ -2112,11 +2114,17 @@ class IssueRelationViewSet(BaseViewSet): project_id=str(project_id), current_instance=None, ) - - return Response( - IssueRelationSerializer(issueRelation, many=True).data, - status=status.HTTP_201_CREATED, - ) + + if relation == "blocking": + return Response( + RelatedIssueSerializer(issue_relation, many=True).data, + status=status.HTTP_201_CREATED, + ) + else: + return Response( + IssueRelationSerializer(issue_relation, many=True).data, + status=status.HTTP_201_CREATED, + ) except IntegrityError as e: if "already exists" in str(e): return Response( diff --git a/apiserver/plane/bgtasks/issue_activites_task.py b/apiserver/plane/bgtasks/issue_activites_task.py index 73fd54a7e..3f543c479 100644 --- a/apiserver/plane/bgtasks/issue_activites_task.py +++ b/apiserver/plane/bgtasks/issue_activites_task.py @@ -1048,6 +1048,25 @@ def create_issue_relation_activity( ) if current_instance is None and requested_data.get("related_list") is not None: for issue_relation in requested_data.get("related_list"): + if issue_relation.get("relation_type") == "blocked_by": + custom_field = "blocking" + else: + custom_field = issue_relation.get("relation_type") + issue = Issue.objects.get(pk=issue_relation.get("issue")) + issue_activities.append( + IssueActivity( + issue_id=issue_relation.get("related_issue"), + actor=actor, + verb="created", + old_value="", + new_value=f"{project.identifier}-{issue.sequence_id}", + field=custom_field, + project=project, + workspace=project.workspace, + comment=f'added {custom_field} relation', + old_identifier=issue_relation.get("issue"), + ) + ) issue = Issue.objects.get(pk=issue_relation.get("related_issue")) issue_activities.append( IssueActivity( @@ -1060,7 +1079,7 @@ def create_issue_relation_activity( project=project, workspace=project.workspace, comment=f'added {issue_relation.get("relation_type")} relation', - old_identifier=issue_relation.get("issue"), + old_identifier=issue_relation.get("related_issue"), ) ) @@ -1073,21 +1092,40 @@ def delete_issue_relation_activity( json.loads(current_instance) if current_instance is not None else None ) if current_instance is not None and requested_data.get("related_list") is None: - issue = Issue.objects.get(pk=current_instance.get("issue")) - issue_activities.append( - IssueActivity( - issue_id=current_instance.get("issue"), - actor=actor, - verb="deleted", - old_value=f"{project.identifier}-{issue.sequence_id}", - new_value="", - field=f'{current_instance.get("relation_type")}', - project=project, - workspace=project.workspace, - comment=f'deleted the {current_instance.get("relation_type")} relation', - old_identifier=current_instance.get("issue"), + if current_instance.get("relation_type") == "blocked_by": + custom_field = "blocking" + else: + custom_field = current_instance.get("relation_type") + issue = Issue.objects.get(pk=current_instance.get("issue")) + issue_activities.append( + IssueActivity( + issue_id=current_instance.get("related_issue"), + actor=actor, + verb="deleted", + old_value="", + new_value=f"{project.identifier}-{issue.sequence_id}", + field=custom_field, + project=project, + workspace=project.workspace, + comment=f'deleted {custom_field} relation', + old_identifier=current_instance.get("issue"), + ) + ) + issue = Issue.objects.get(pk=current_instance.get("related_issue")) + issue_activities.append( + IssueActivity( + issue_id=current_instance.get("issue"), + actor=actor, + verb="deleted", + old_value="", + new_value=f"{project.identifier}-{issue.sequence_id}", + field=f'{current_instance.get("relation_type")}', + project=project, + workspace=project.workspace, + comment=f'deleted {current_instance.get("relation_type")} relation', + old_identifier=current_instance.get("related_issue"), + ) ) - ) def create_draft_issue_activity( diff --git a/apiserver/plane/db/migrations/0045_auto_20230915_0655.py b/apiserver/plane/db/migrations/0045_auto_20230915_0655.py new file mode 100644 index 000000000..7bd907e29 --- /dev/null +++ b/apiserver/plane/db/migrations/0045_auto_20230915_0655.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.3 on 2023-09-15 06:55 + +from django.db import migrations + +def update_issue_activity(apps, schema_editor): + IssueActivityModel = apps.get_model("db", "IssueActivity") + updated_issue_activity = [] + for obj in IssueActivityModel.objects.all(): + if obj.field == "blocks": + obj.field = "blocked_by" + updated_issue_activity.append(obj) + IssueActivityModel.objects.bulk_update(updated_issue_activity, ["field"], batch_size=100) + + +class Migration(migrations.Migration): + + dependencies = [ + ('db', '0044_auto_20230913_0709'), + ] + + operations = [ + migrations.RunPython(update_issue_activity), + ]