-
Notifications
You must be signed in to change notification settings - Fork 5.6k
fix(api): avoid re-crawling issue link on update when url is unchanged #9763
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
Tyagiquamar
wants to merge
2
commits into
makeplane:preview
Choose a base branch
from
Tyagiquamar:fix/issue-link-preserve-metadata-on-update
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.
+221
−2
Open
Changes from all commits
Commits
Show all changes
2 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| # Copyright (c) 2023-present Plane Software, Inc. and contributors | ||
| # SPDX-License-Identifier: AGPL-3.0-only | ||
| # See the LICENSE file for details. | ||
|
|
||
| """ | ||
| Unit regression tests for IssueLinkViewSet.partial_update. | ||
|
|
||
| Verifies that crawl_work_item_link_title is only scheduled when the URL | ||
| actually changes, preventing overwriting custom link metadata and unnecessary crawls. | ||
| See: https://github.com/makeplane/plane/issues/9674 | ||
| """ | ||
|
|
||
| import uuid | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
| from rest_framework import status | ||
| from rest_framework.parsers import JSONParser | ||
| from rest_framework.request import Request | ||
| from rest_framework.test import APIRequestFactory | ||
|
|
||
| from plane.app.views.issue.link import IssueLinkViewSet | ||
| from plane.api.views.issue import IssueLinkDetailAPIEndpoint | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| class TestIssueLinkPartialUpdate: | ||
| @pytest.fixture(autouse=True) | ||
| def setup_view(self): | ||
| self.factory = APIRequestFactory() | ||
| self.view = IssueLinkViewSet() | ||
| self.workspace_slug = "test-workspace" | ||
| self.project_id = str(uuid.uuid4()) | ||
| self.issue_id = str(uuid.uuid4()) | ||
| self.link_id = str(uuid.uuid4()) | ||
| self.user_id = str(uuid.uuid4()) | ||
|
|
||
| def _create_mock_request(self, data): | ||
| wsgi_request = self.factory.patch("/api/test/", data=data, format="json") | ||
| request = Request(wsgi_request, parsers=[JSONParser()]) | ||
| request.user = MagicMock() | ||
| request.user.id = self.user_id | ||
| return request | ||
|
|
||
| @patch("plane.app.views.issue.link.base_host", return_value="https://app.plane.so") | ||
| @patch("plane.app.views.issue.link.issue_activity") | ||
| @patch("plane.app.views.issue.link.crawl_work_item_link_title") | ||
| @patch("plane.app.views.issue.link.IssueLinkSerializer") | ||
| @patch("plane.app.views.issue.link.IssueLink.objects.get") | ||
| def test_partial_update_does_not_recrawl_when_url_unchanged( | ||
| self, mock_get_link, mock_serializer_cls, mock_crawl, mock_activity, mock_base_host | ||
| ): | ||
| """Updating link title or metadata without changing url must NOT trigger crawler.""" | ||
| initial_url = "https://github.com/makeplane/plane" | ||
| mock_instance = MagicMock() | ||
| mock_instance.id = self.link_id | ||
| mock_instance.url = initial_url | ||
| mock_get_link.return_value = mock_instance | ||
|
|
||
| # Mock serializer behavior | ||
| mock_serializer_instance = MagicMock() | ||
| mock_serializer_instance.is_valid.return_value = True | ||
| mock_serializer_instance.data = { | ||
| "id": self.link_id, | ||
| "title": "Custom Renamed Title", | ||
| "url": initial_url, | ||
| "metadata": {"custom": "data"}, | ||
| } | ||
| mock_serializer_cls.return_value = mock_serializer_instance | ||
|
|
||
| # Mock get_queryset for self.get_queryset().get(id=...) | ||
| mock_queryset = MagicMock() | ||
| mock_queryset.get.return_value = mock_instance | ||
| self.view.get_queryset = MagicMock(return_value=mock_queryset) | ||
|
|
||
| request = self._create_mock_request({"title": "Custom Renamed Title"}) | ||
| response = self.view.partial_update( | ||
| request, | ||
| slug=self.workspace_slug, | ||
| project_id=self.project_id, | ||
| issue_id=self.issue_id, | ||
| pk=self.link_id, | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_200_OK | ||
| mock_crawl.delay.assert_not_called() | ||
| mock_activity.delay.assert_called_once() | ||
|
|
||
| @patch("plane.app.views.issue.link.base_host", return_value="https://app.plane.so") | ||
| @patch("plane.app.views.issue.link.issue_activity") | ||
| @patch("plane.app.views.issue.link.crawl_work_item_link_title") | ||
| @patch("plane.app.views.issue.link.IssueLinkSerializer") | ||
| @patch("plane.app.views.issue.link.IssueLink.objects.get") | ||
| def test_partial_update_triggers_crawl_when_url_changed( | ||
| self, mock_get_link, mock_serializer_cls, mock_crawl, mock_activity, mock_base_host | ||
| ): | ||
| """Updating url to a new value MUST trigger crawl_work_item_link_title.""" | ||
| initial_url = "https://github.com/makeplane/plane" | ||
| new_url = "https://github.com/makeplane/plane/pull/123" | ||
|
|
||
| mock_instance = MagicMock() | ||
| mock_instance.id = self.link_id | ||
| mock_instance.url = initial_url | ||
| mock_get_link.return_value = mock_instance | ||
|
|
||
| mock_serializer_instance = MagicMock() | ||
| mock_serializer_instance.is_valid.return_value = True | ||
| mock_serializer_instance.data = { | ||
| "id": self.link_id, | ||
| "title": "Old Title", | ||
| "url": new_url, | ||
| "metadata": {}, | ||
| } | ||
| mock_serializer_cls.return_value = mock_serializer_instance | ||
|
|
||
| mock_queryset = MagicMock() | ||
| mock_queryset.get.return_value = mock_instance | ||
| self.view.get_queryset = MagicMock(return_value=mock_queryset) | ||
|
|
||
| request = self._create_mock_request({"url": new_url}) | ||
| response = self.view.partial_update( | ||
| request, | ||
| slug=self.workspace_slug, | ||
| project_id=self.project_id, | ||
| issue_id=self.issue_id, | ||
| pk=self.link_id, | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_200_OK | ||
| mock_crawl.delay.assert_called_once_with(self.link_id, new_url) | ||
| mock_activity.delay.assert_called_once() | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| class TestIssueLinkDetailPatch: | ||
| @pytest.fixture(autouse=True) | ||
| def setup_view(self): | ||
| self.factory = APIRequestFactory() | ||
| self.view = IssueLinkDetailAPIEndpoint() | ||
| self.workspace_slug = "test-workspace" | ||
| self.project_id = str(uuid.uuid4()) | ||
| self.issue_id = str(uuid.uuid4()) | ||
| self.link_id = str(uuid.uuid4()) | ||
| self.user_id = str(uuid.uuid4()) | ||
|
|
||
| def _create_mock_request(self, data): | ||
| wsgi_request = self.factory.patch("/api/test/", data=data, format="json") | ||
| request = Request(wsgi_request, parsers=[JSONParser()]) | ||
| request.user = MagicMock() | ||
| request.user.id = self.user_id | ||
| return request | ||
|
|
||
| @patch("plane.api.views.issue.issue_activity") | ||
| @patch("plane.api.views.issue.crawl_work_item_link_title") | ||
| @patch("plane.api.views.issue.IssueLinkSerializer") | ||
| @patch("plane.api.views.issue.IssueLink.objects.get") | ||
| def test_patch_does_not_recrawl_when_url_unchanged( | ||
| self, mock_get_link, mock_serializer_cls, mock_crawl, mock_activity | ||
| ): | ||
| initial_url = "https://github.com/makeplane/plane" | ||
| mock_instance = MagicMock() | ||
| mock_instance.id = self.link_id | ||
| mock_instance.url = initial_url | ||
| mock_get_link.return_value = mock_instance | ||
|
|
||
| mock_serializer_instance = MagicMock() | ||
| mock_serializer_instance.is_valid.return_value = True | ||
| mock_serializer_instance.data = {"id": self.link_id, "url": initial_url} | ||
| mock_serializer_cls.return_value = mock_serializer_instance | ||
|
|
||
| response = self.view.patch( | ||
| self._create_mock_request({"title": "Custom Renamed Title"}), | ||
| slug=self.workspace_slug, | ||
| project_id=self.project_id, | ||
| issue_id=self.issue_id, | ||
| pk=self.link_id, | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_200_OK | ||
| mock_crawl.delay.assert_not_called() | ||
| mock_activity.delay.assert_called_once() | ||
|
|
||
| @patch("plane.api.views.issue.issue_activity") | ||
| @patch("plane.api.views.issue.crawl_work_item_link_title") | ||
| @patch("plane.api.views.issue.IssueLinkSerializer") | ||
| @patch("plane.api.views.issue.IssueLink.objects.get") | ||
| def test_patch_triggers_crawl_when_url_changed( | ||
| self, mock_get_link, mock_serializer_cls, mock_crawl, mock_activity | ||
| ): | ||
| initial_url = "https://github.com/makeplane/plane" | ||
| new_url = "https://github.com/makeplane/plane/pull/123" | ||
| mock_instance = MagicMock() | ||
| mock_instance.id = self.link_id | ||
| mock_instance.url = initial_url | ||
| mock_get_link.return_value = mock_instance | ||
|
|
||
| mock_serializer_instance = MagicMock() | ||
| mock_serializer_instance.is_valid.return_value = True | ||
| mock_serializer_instance.data = {"id": self.link_id, "url": new_url} | ||
| mock_serializer_cls.return_value = mock_serializer_instance | ||
|
|
||
| response = self.view.patch( | ||
| self._create_mock_request({"url": new_url}), | ||
| slug=self.workspace_slug, | ||
| project_id=self.project_id, | ||
| issue_id=self.issue_id, | ||
| pk=self.link_id, | ||
| ) | ||
|
|
||
| assert response.status_code == status.HTTP_200_OK | ||
| mock_crawl.delay.assert_called_once_with(self.link_id, new_url) | ||
| mock_activity.delay.assert_called_once() |
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.