-
Notifications
You must be signed in to change notification settings - Fork 14
dev: Implemented workspace roles endpoint and pagination support #54
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
Merged
Merged
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
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,59 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any, Literal | ||
|
|
||
| from ..models.roles import PaginatedRoleResponse, Role | ||
| from .base_resource import BaseResource | ||
|
|
||
|
|
||
| class Roles(BaseResource): | ||
| """API client for workspace and project role definitions. | ||
|
|
||
| Roles are defined at the workspace level. ``namespace="workspace"`` returns | ||
| workspace-level roles (Owner / Admin / Member / Guest); ``namespace="project"`` | ||
| returns the project-role definitions available across the workspace (Admin / | ||
| Contributor / Commenter / Guest). The project-role definitions are shared by | ||
| every project in the workspace — there is no per-project roles endpoint, so | ||
| this resource takes no project id. | ||
| """ | ||
|
|
||
| def __init__(self, config: Any) -> None: | ||
| super().__init__(config, "/workspaces/") | ||
|
|
||
| def list( | ||
| self, | ||
| workspace_slug: str, | ||
| namespace: Literal["workspace", "project"] | None = None, | ||
| per_page: int | None = None, | ||
| cursor: str | None = None, | ||
| ) -> PaginatedRoleResponse: | ||
| """List role definitions in a workspace. | ||
|
|
||
| Args: | ||
| workspace_slug: The workspace slug identifier | ||
| namespace: Optional filter — ``"workspace"`` for workspace-level | ||
| roles, ``"project"`` for project-role definitions. When omitted, | ||
| both are returned, ordered by namespace, sort order, then name. | ||
| per_page: Number of results per page (server default 20) | ||
| cursor: Pagination cursor from a previous response's ``next_cursor`` | ||
| """ | ||
| params: dict[str, Any] = {} | ||
| if namespace is not None: | ||
| params["namespace"] = namespace | ||
| if per_page is not None: | ||
| params["per_page"] = per_page | ||
| if cursor is not None: | ||
| params["cursor"] = cursor | ||
|
|
||
| response = self._get(f"{workspace_slug}/roles", params=params if params else None) | ||
| return PaginatedRoleResponse.model_validate(response) | ||
|
|
||
| def retrieve(self, workspace_slug: str, role_id: str) -> Role: | ||
| """Retrieve a single role definition by id. | ||
|
|
||
| Args: | ||
| workspace_slug: The workspace slug identifier | ||
| role_id: UUID of the role | ||
| """ | ||
| response = self._get(f"{workspace_slug}/roles/{role_id}") | ||
| return Role.model_validate(response) |
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,28 @@ | ||
| from pydantic import BaseModel, ConfigDict | ||
|
|
||
| from .pagination import PaginatedResponse | ||
|
|
||
|
|
||
| class Role(BaseModel): | ||
| """Workspace or project role definition. | ||
|
|
||
| The API returns additional fields (``id``, ``permissions``, ``level``, | ||
| ``member_count``, ...); only the three below are modeled and the rest are | ||
| ignored. ``slug`` is the stable identifier to use in code, but it is **not** | ||
| globally unique — e.g. ``admin``/``guest`` exist in both namespaces — so key | ||
| roles by ``(namespace, slug)`` when indexing them. | ||
| """ | ||
|
|
||
| model_config = ConfigDict(extra="allow", populate_by_name=True) | ||
|
|
||
| name: str | None = None | ||
| slug: str | None = None | ||
| namespace: str | None = None | ||
|
|
||
|
|
||
| class PaginatedRoleResponse(PaginatedResponse): | ||
| """Paginated response for workspace/project role definitions.""" | ||
|
|
||
| model_config = ConfigDict(extra="allow", populate_by_name=True) | ||
|
|
||
| results: list[Role] | ||
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,46 @@ | ||
| """Unit tests for Roles API resource (smoke tests with real HTTP requests).""" | ||
|
|
||
| from plane.client import PlaneClient | ||
| from plane.models.roles import Role | ||
|
|
||
|
|
||
| class TestRolesAPI: | ||
| """Test Roles API resource.""" | ||
|
|
||
| def test_list_roles(self, client: PlaneClient, workspace_slug: str) -> None: | ||
| """Listing roles returns a paginated envelope of Role objects.""" | ||
| page = client.roles.list(workspace_slug) | ||
| assert isinstance(page.results, list) | ||
| assert isinstance(page.next_page_results, bool) | ||
| for role in page.results: | ||
| assert isinstance(role, Role) | ||
| assert role.slug is not None | ||
| assert role.namespace in ("workspace", "project") | ||
|
|
||
| def test_list_roles_namespace_workspace(self, client: PlaneClient, workspace_slug: str) -> None: | ||
| """Filtering by namespace=workspace returns only workspace-level roles.""" | ||
| page = client.roles.list(workspace_slug, namespace="workspace") | ||
| assert isinstance(page.results, list) | ||
| for role in page.results: | ||
| assert role.namespace == "workspace" | ||
|
|
||
| def test_list_roles_namespace_project(self, client: PlaneClient, workspace_slug: str) -> None: | ||
| """Filtering by namespace=project returns only project-level roles.""" | ||
| page = client.roles.list(workspace_slug, namespace="project") | ||
| assert isinstance(page.results, list) | ||
| for role in page.results: | ||
| assert role.namespace == "project" | ||
|
|
||
| def test_retrieve_role(self, client: PlaneClient, workspace_slug: str) -> None: | ||
| """Retrieving a role by id returns a single Role matching the listed one.""" | ||
| page = client.roles.list(workspace_slug, per_page=1) | ||
| if not page.results: | ||
| return | ||
| listed = page.results[0] | ||
| role_id = getattr(listed, "id", None) | ||
| if role_id is None: | ||
| return | ||
| retrieved = client.roles.retrieve(workspace_slug, role_id) | ||
| assert isinstance(retrieved, Role) | ||
| assert retrieved.slug == listed.slug | ||
| assert retrieved.namespace == listed.namespace |
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.
📐 Maintainability & Code Quality | 🟡 Minor
🧩 Analysis chain
🌐 Web query:
In Pydantic v2.4, when model_config uses ConfigDict(extra="allow"), are unknown fields preserved and included in model_dump by default?💡 Result:
Yes, in Pydantic v2.4 (and throughout the v2 series), when a model is configured with ConfigDict(extra="allow"), unknown fields are preserved and included in the output of model_dump by default [1][2][3]. When extra="allow" is set, any data provided to the model that does not correspond to a defined field is stored in a special dictionary attribute called pydantic_extra [1][2]. During serialization, Pydantic's model_dump method automatically includes the contents of pydantic_extra alongside the standard fields [1][2][3]. Example behavior: from pydantic import BaseModel, ConfigDict class Model(BaseModel): x: int model_config = ConfigDict(extra='allow') m = Model(x=1, y='a') assert m.model_dump == {'x': 1, 'y': 'a'} # Unknown field 'y' is included [1][3] assert m.pydantic_extra == {'y': 'a'} # Extra data is stored here [1][2] Note that while this is the standard behavior for model_dump, recent discussions in the Pydantic ecosystem have highlighted that this integration can sometimes be complex when using model_construct or specific serialization overrides [4][5]. However, under normal instantiation and serialization, these extra fields are included [1][2][3].
Citations:
model_dumppydantic/pydantic#12937Docstring should say extras are accepted, not ignored
plane/models/roles.py:9-11— The API returns additional fields (id,permissions,level,member_count, ...); only the three below are explicitly typed, and the rest are accepted and preserved for forward compatibility.slugis the stable identifier to use in code, but it is not ...🤖 Prompt for AI Agents