-
Notifications
You must be signed in to change notification settings - Fork 5.3k
feat: add optional state propagation from parent to sub-work items #9616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Iyamokuma
wants to merge
3
commits into
makeplane:preview
Choose a base branch
from
Iyamokuma:feat/cascade-sub-issue-status
base: preview
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
apps/api/plane/tests/unit/utils/test_sub_issue_state_propagation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from plane.db.models import Issue, Project, ProjectMember, State, Workspace | ||
| from plane.utils.sub_issue_state_propagation import ( | ||
| propagate_state_to_sub_issues, | ||
| resolve_target_state, | ||
| user_can_edit_issue, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def project(workspace, create_user): | ||
| return Project.objects.create( | ||
| name="Test Project", | ||
| identifier="TP", | ||
| workspace=workspace, | ||
| created_by=create_user, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def todo_state(project): | ||
| return State.objects.create( | ||
| name="Todo", | ||
| project=project, | ||
| group="unstarted", | ||
| color="#60646C", | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def done_state(project): | ||
| return State.objects.create( | ||
| name="Done", | ||
| project=project, | ||
| group="completed", | ||
| color="#46A758", | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def parent_issue(workspace, project, todo_state, create_user): | ||
| return Issue.objects.create( | ||
| name="Parent Issue", | ||
| workspace=workspace, | ||
| project=project, | ||
| state=todo_state, | ||
| created_by=create_user, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sub_issue(workspace, project, todo_state, create_user, parent_issue): | ||
| return Issue.objects.create( | ||
| name="Sub Issue", | ||
| workspace=workspace, | ||
| project=project, | ||
| state=todo_state, | ||
| parent=parent_issue, | ||
| created_by=create_user, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| class TestResolveTargetState: | ||
| @pytest.mark.django_db | ||
| def test_returns_same_state_for_same_project(self, project, todo_state): | ||
| assert resolve_target_state(todo_state, project.id) == todo_state | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_resolves_state_by_group_and_name(self, workspace, create_user, todo_state): | ||
| other_project = Project.objects.create( | ||
| name="Other Project", | ||
| identifier="OP", | ||
| workspace=workspace, | ||
| created_by=create_user, | ||
| ) | ||
| other_todo = State.objects.create( | ||
| name="Todo", | ||
| project=other_project, | ||
| group="unstarted", | ||
| color="#60646C", | ||
| ) | ||
| assert resolve_target_state(todo_state, other_project.id) == other_todo | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_falls_back_to_group_state(self, workspace, create_user, todo_state): | ||
| other_project = Project.objects.create( | ||
| name="Other Project", | ||
| identifier="OP", | ||
| workspace=workspace, | ||
| created_by=create_user, | ||
| ) | ||
| fallback_state = State.objects.create( | ||
| name="Backlog", | ||
| project=other_project, | ||
| group="unstarted", | ||
| color="#60646C", | ||
| sequence=1000, | ||
| ) | ||
| assert resolve_target_state(todo_state, other_project.id) == fallback_state | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| class TestPropagateStateToSubIssues: | ||
| @pytest.mark.django_db | ||
| @patch("plane.utils.sub_issue_state_propagation.issue_activity.delay") | ||
| def test_propagates_state_to_direct_sub_issues( | ||
| self, mock_issue_activity, workspace, create_user, parent_issue, sub_issue, done_state | ||
| ): | ||
| ProjectMember.objects.create( | ||
| workspace=workspace, | ||
| project=parent_issue.project, | ||
| member=create_user, | ||
| role=20, | ||
| ) | ||
|
|
||
| updated_ids = propagate_state_to_sub_issues( | ||
| parent=parent_issue, | ||
| new_state=done_state, | ||
| actor=create_user, | ||
| workspace_slug=workspace.slug, | ||
| origin="http://localhost", | ||
| ) | ||
|
|
||
| sub_issue.refresh_from_db() | ||
| assert updated_ids == [str(sub_issue.id)] | ||
| assert sub_issue.state_id == done_state.id | ||
| assert sub_issue.completed_at is not None | ||
| mock_issue_activity.assert_called_once() | ||
|
|
||
| @pytest.mark.django_db | ||
| @patch("plane.utils.sub_issue_state_propagation.issue_activity.delay") | ||
| def test_skips_sub_issues_user_cannot_edit( | ||
| self, mock_issue_activity, workspace, create_user, parent_issue, sub_issue, done_state, user_data | ||
| ): | ||
| from plane.db.models import User | ||
|
|
||
| other_user = User.objects.create( | ||
| email="other@plane.so", | ||
| first_name="Other", | ||
| last_name="User", | ||
| ) | ||
| sub_issue.created_by = other_user | ||
| sub_issue.save() | ||
|
|
||
| updated_ids = propagate_state_to_sub_issues( | ||
| parent=parent_issue, | ||
| new_state=done_state, | ||
| actor=create_user, | ||
| workspace_slug=workspace.slug, | ||
| origin="http://localhost", | ||
| ) | ||
|
|
||
| sub_issue.refresh_from_db() | ||
| assert updated_ids == [] | ||
| assert sub_issue.state_id != done_state.id | ||
| mock_issue_activity.assert_not_called() | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_user_can_edit_issue_for_creator(self, workspace, create_user, sub_issue): | ||
| assert user_can_edit_issue(create_user, workspace.slug, sub_issue) is True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| import json | ||
|
|
||
| from django.utils import timezone | ||
|
|
||
| from plane.app.permissions.base import ROLE | ||
| from plane.bgtasks.issue_activities_task import issue_activity | ||
| from plane.db.models import Issue, ProjectMember, State, WorkspaceMember | ||
|
|
||
|
|
||
| def user_can_edit_issue(user, workspace_slug, issue): | ||
| """Check if the user can edit an issue, mirroring partial_update permissions.""" | ||
| if issue.created_by_id == user.id: | ||
| return True | ||
|
|
||
| allowed_roles = [ROLE.ADMIN.value, ROLE.MEMBER.value] | ||
| if ProjectMember.objects.filter( | ||
| member=user, | ||
| workspace__slug=workspace_slug, | ||
| project_id=issue.project_id, | ||
| role__in=allowed_roles, | ||
| is_active=True, | ||
| ).exists(): | ||
| return True | ||
|
|
||
| return ( | ||
| ProjectMember.objects.filter( | ||
| member=user, | ||
| workspace__slug=workspace_slug, | ||
| project_id=issue.project_id, | ||
| is_active=True, | ||
| ).exists() | ||
| and WorkspaceMember.objects.filter( | ||
| member=user, | ||
| workspace__slug=workspace_slug, | ||
| role=ROLE.ADMIN.value, | ||
| is_active=True, | ||
| ).exists() | ||
| ) | ||
|
|
||
|
|
||
| def resolve_target_state(new_state, target_project_id): | ||
| """Resolve the equivalent state for a sub-issue's project.""" | ||
| if str(new_state.project_id) == str(target_project_id): | ||
| return new_state | ||
|
|
||
| # Prefer a state with the same group and name in the target project | ||
| matching_state = State.objects.filter( | ||
| project_id=target_project_id, | ||
| group=new_state.group, | ||
| name=new_state.name, | ||
| ).first() | ||
| if matching_state: | ||
| return matching_state | ||
|
|
||
| # Fall back to any state in the same group | ||
| return State.objects.filter(project_id=target_project_id, group=new_state.group).order_by("sequence").first() | ||
|
|
||
|
|
||
| def propagate_state_to_sub_issues(parent, new_state, actor, workspace_slug, origin): | ||
| """ | ||
| Propagate a state change from a parent issue to its direct sub-issues. | ||
| Returns the list of updated sub-issue IDs. | ||
| """ | ||
| if not new_state: | ||
| return [] | ||
|
|
||
| sub_issues = Issue.issue_objects.filter(parent_id=parent.id, workspace=parent.workspace).select_related("state") | ||
| updated_sub_issue_ids = [] | ||
|
|
||
| for sub_issue in sub_issues: | ||
| if not user_can_edit_issue(actor, workspace_slug, sub_issue): | ||
| continue | ||
|
|
||
| target_state = resolve_target_state(new_state, sub_issue.project_id) | ||
| if not target_state or sub_issue.state_id == target_state.id: | ||
| continue | ||
|
|
||
| current_instance = json.dumps({"state_id": str(sub_issue.state_id)}) | ||
| sub_issue.state = target_state | ||
| sub_issue.updated_by = actor | ||
| sub_issue.save() | ||
|
|
||
| issue_activity.delay( | ||
| type="issue.activity.updated", | ||
| requested_data=json.dumps({"state_id": str(target_state.id)}), | ||
| actor_id=str(actor.id), | ||
| issue_id=str(sub_issue.id), | ||
| project_id=str(sub_issue.project_id), | ||
| current_instance=current_instance, | ||
| epoch=int(timezone.now().timestamp()), | ||
| notification=True, | ||
| origin=origin, | ||
| ) | ||
| updated_sub_issue_ids.append(str(sub_issue.id)) | ||
|
|
||
| return updated_sub_issue_ids |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.