-
Notifications
You must be signed in to change notification settings - Fork 702
UN-3057 [FIX] Repair Prompt Studio projects left ownerless by the clone path #2239
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
pk-zipstack
wants to merge
1
commit into
main
Choose a base branch
from
fix/clone-owner-membership
base: main
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
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
34 changes: 34 additions & 0 deletions
34
backend/prompt_studio/prompt_studio_core_v2/migrations/0011_repair_ownerless_custom_tools.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,34 @@ | ||
| """UN-3057: grant an OWNER row to custom tools left ownerless by the clone path. | ||
|
|
||
| The Prompt Studio clone helper never created the OWNER ``ResourceMembership`` | ||
| that UN-2202 made authoritative, so every project cloned after | ||
| ``0009_absorb_shared_users`` ran has no owner: visible (the clone copies the | ||
| parent's ``shared_to_org``) but unmanageable by anyone except an org admin. | ||
| The helper is fixed going forward; this repairs the rows already written. | ||
|
|
||
| Idempotent and non-destructive — only resources with zero OWNER rows are | ||
| touched, so it is safe to re-run and reverses to a no-op. | ||
| """ | ||
|
|
||
| from django.db import migrations | ||
| from tenant_account_v2.migrations._membership_backfill import ( | ||
| repair_ownerless_owner_rows, | ||
| ) | ||
|
|
||
| APP_LABEL = "prompt_studio_core_v2" | ||
| MODEL_NAME = "CustomTool" | ||
|
|
||
|
|
||
| def _forward(apps, schema_editor): | ||
| repair_ownerless_owner_rows(apps, APP_LABEL, MODEL_NAME) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("prompt_studio_core_v2", "0010_customtool_custtool_org_modified_idx"), | ||
| ("tenant_account_v2", "0005_resource_membership"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(_forward, migrations.RunPython.noop), | ||
| ] |
86 changes: 86 additions & 0 deletions
86
backend/prompt_studio/prompt_studio_core_v2/tests/test_ownerless_owner_repair.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,86 @@ | ||
| """Repair of ownerless ``CustomTool`` rows (UN-3057). | ||
|
|
||
| The Prompt Studio clone path created projects without the OWNER | ||
| ``ResourceMembership`` that UN-2202 made authoritative, so every project cloned | ||
| after the UN-2202 backfill ran is ownerless: visible, but unmanageable by anyone | ||
| except an org admin. Fixing the clone helper stops new breakage; these already | ||
| broken rows need a repair pass. | ||
|
|
||
| Exercises the migration helper against the real models (``django.apps.apps`` | ||
| satisfies the ``apps.get_model`` interface the migration passes in), so the | ||
| behaviour is pinned without driving the migration executor. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import secrets | ||
|
|
||
| from account_v2.models import Organization, User | ||
| from django.apps import apps as django_apps | ||
| from django.test import TestCase | ||
| from permissions.roles import ResourceRole | ||
| from tenant_account_v2.migrations._membership_backfill import ( | ||
| repair_ownerless_owner_rows, | ||
| ) | ||
|
|
||
| from prompt_studio.prompt_studio_core_v2.models import CustomTool | ||
|
|
||
| APP_LABEL = "prompt_studio_core_v2" | ||
| MODEL_NAME = "CustomTool" | ||
|
|
||
|
|
||
| def _make_user(email: str) -> User: | ||
| return User.objects.create_user( | ||
| username=email, email=email, password=secrets.token_urlsafe() | ||
| ) | ||
|
|
||
|
|
||
| class RepairOwnerlessOwnerRowsTests(TestCase): | ||
| def setUp(self) -> None: | ||
| self.org = Organization.objects.create( | ||
| name="org-a", display_name="Org A", organization_id="org-a" | ||
| ) | ||
| self.creator = _make_user("creator@example.com") | ||
| self.other = _make_user("other@example.com") | ||
|
|
||
| def _tool(self, name: str, creator: User | None) -> CustomTool: | ||
| return CustomTool.objects.create( | ||
| tool_name=name, | ||
| description="", | ||
| organization=self.org, | ||
| created_by=creator, | ||
| ) | ||
|
|
||
| def _repair(self) -> int: | ||
| return repair_ownerless_owner_rows(django_apps, APP_LABEL, MODEL_NAME) | ||
|
|
||
| def _owner_ids(self, tool: CustomTool) -> set: | ||
| return set( | ||
| tool.memberships.filter(role=ResourceRole.OWNER).values_list( | ||
| "user_id", flat=True | ||
| ) | ||
| ) | ||
|
|
||
| def test_ownerless_tool_gets_an_owner_row_for_its_creator(self) -> None: | ||
| tool = self._tool("cloned-project", self.creator) | ||
| self.assertEqual(self._owner_ids(tool), set()) | ||
|
|
||
| self._repair() | ||
|
|
||
| self.assertEqual(self._owner_ids(tool), {self.creator.id}) | ||
|
|
||
| def test_tool_that_already_has_an_owner_is_left_alone(self) -> None: | ||
| """A creator deliberately replaced by a co-owner must not be re-added.""" | ||
| tool = self._tool("handed-over-project", self.creator) | ||
| tool.memberships.create(user=self.other, role=ResourceRole.OWNER) | ||
|
|
||
| self._repair() | ||
|
|
||
| self.assertEqual(self._owner_ids(tool), {self.other.id}) | ||
|
|
||
| def test_tool_with_no_creator_is_skipped(self) -> None: | ||
| tool = self._tool("orphan-project", None) | ||
|
|
||
| self._repair() | ||
|
|
||
| self.assertEqual(self._owner_ids(tool), set()) |
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.
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.
When an ownerless tool's creator already has a VIEWER membership,
get_or_createreturns that row without applying the OWNER default, causing ownership-gated PUT, PATCH, and DELETE operations to continue returning 403 after the repair.Knowledge Base Used: Django Core Scaffold: Settings, Routing, Auth, Tenancy
Prompt To Fix With AI