-
Notifications
You must be signed in to change notification settings - Fork 5.1k
fix: track and subscribe inline assignees when work items are created via the external API #9450
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
stella-nova
wants to merge
1
commit into
makeplane:preview
Choose a base branch
from
stella-nova:fix/create-issue-activity-assignees-key
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.
+117
−1
Open
Changes from all commits
Commits
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
116 changes: 116 additions & 0 deletions
116
apps/api/plane/tests/unit/bg_tasks/test_issue_activities_task.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,116 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| """ | ||
| Unit tests for the issue activity background task. | ||
|
|
||
| Verifies that creating a work item with inline assignees tracks the assignee | ||
| activities and auto-subscribes the assignees regardless of whether the payload | ||
| uses the web app field name (``assignee_ids``) or the external REST API field | ||
| name (``assignees``). With only ``assignee_ids`` accepted, work items created | ||
| through the external API with assignees produced no assignee activity and no | ||
| subscription, so the assignees were never notified. | ||
| """ | ||
|
|
||
| import json | ||
|
|
||
| import pytest | ||
| from django.utils import timezone | ||
|
|
||
| from plane.bgtasks.issue_activities_task import create_issue_activity | ||
| from plane.db.models import Issue, IssueActivity, IssueSubscriber, User | ||
| from plane.tests.factories import ProjectFactory, WorkspaceFactory | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def workspace(db): | ||
| return WorkspaceFactory() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def project(db, workspace): | ||
| return ProjectFactory(workspace=workspace) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def assignee(db): | ||
| # Not UserFactory: it leaves username empty, and a second factory user | ||
| # would collide with the unique username constraint. | ||
| user = User.objects.create( | ||
| email="assignee@plane.so", | ||
| username="assignee-user", | ||
| first_name="Assignee", | ||
| last_name="User", | ||
| ) | ||
| user.set_password("assignee-password") | ||
| user.save() | ||
| return user | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def author(db): | ||
| user = User.objects.create( | ||
| email="author@plane.so", | ||
| username="author-user", | ||
| first_name="Author", | ||
| last_name="User", | ||
| ) | ||
| user.set_password("author-password") | ||
| user.save() | ||
| return user | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def issue(db, workspace, project, author): | ||
| return Issue.objects.create( | ||
| name="Created via external API", | ||
| project=project, | ||
| workspace=workspace, | ||
| created_by=author, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| class TestCreateIssueActivityAssignees: | ||
| def _run(self, issue, project, workspace, actor, requested_data): | ||
| # actor is passed explicitly: BaseModel.save() derives created_by from | ||
| # crum's current user, which is None outside a request, so | ||
| # issue.created_by_id cannot be used as the actor here. | ||
| issue_activities = [] | ||
| create_issue_activity( | ||
| requested_data=json.dumps(requested_data), | ||
| current_instance=None, | ||
| issue_id=issue.id, | ||
| project_id=project.id, | ||
| workspace_id=workspace.id, | ||
| actor_id=str(actor.id), | ||
| issue_activities=issue_activities, | ||
| epoch=int(timezone.now().timestamp()), | ||
| ) | ||
| return issue_activities | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_create_with_assignee_ids_tracks_and_subscribes(self, workspace, project, issue, author, assignee): | ||
| """Web app payload key: assignee activity is collected, assignee subscribed.""" | ||
| activities = self._run(issue, project, workspace, author, {"assignee_ids": [str(assignee.id)]}) | ||
|
|
||
| assert any(activity.field == "assignees" for activity in activities) | ||
| assert IssueSubscriber.objects.filter(issue_id=issue.id, subscriber_id=assignee.id).exists() | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_create_with_assignees_tracks_and_subscribes(self, workspace, project, issue, author, assignee): | ||
| """External API payload key: must behave exactly like ``assignee_ids``.""" | ||
| activities = self._run(issue, project, workspace, author, {"assignees": [str(assignee.id)]}) | ||
|
|
||
| assert any(activity.field == "assignees" for activity in activities) | ||
| assert IssueSubscriber.objects.filter(issue_id=issue.id, subscriber_id=assignee.id).exists() | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_create_without_assignees_only_creates_created_activity(self, workspace, project, issue, author): | ||
| """No assignee payload: no assignee activities, no subscriptions.""" | ||
| activities = self._run(issue, project, workspace, author, {"name": "Created via external API"}) | ||
|
|
||
| assert activities == [] | ||
| assert not IssueSubscriber.objects.filter(issue_id=issue.id).exists() | ||
| assert IssueActivity.objects.filter(issue_id=issue.id, verb="created").exists() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Guard against non-dictionary types to prevent crashes.
If
requested_dataevaluates toNone(which is explicitly allowed by the preceding line) or if it parses as a JSON array instead of a JSON object, calling.get()will raise anAttributeErrorand crash the background task.Guard the dictionary access by verifying the payload type to ensure the task gracefully skips assignee tracking when the payload is missing or invalid.
🛡️ Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents