-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbase.py
More file actions
361 lines (309 loc) · 12.3 KB
/
Copy pathbase.py
File metadata and controls
361 lines (309 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from __future__ import annotations
import json
from collections.abc import Mapping
from typing import Any
from ...models.query_params import RetrieveQueryParams, WorkItemQueryParams
from ...models.work_items import (
AdvancedSearchResult,
AdvancedSearchWorkItem,
CreateWorkItem,
PaginatedWorkItemResponse,
UpdateWorkItem,
WorkItem,
WorkItemDetail,
WorkItemSearch,
)
from ..base_resource import BaseResource
from .activities import WorkItemActivities
from .attachments import WorkItemAttachments
from .comments import WorkItemComments
from .links import WorkItemLinks
from .pages import WorkItemPages
from .relations import WorkItemRelations
from .work_logs import WorkLogs
def prepare_work_item_params(
params: WorkItemQueryParams | Mapping[str, Any] | None,
) -> dict[str, Any] | None:
"""Serialize work-item query params for use as HTTP query params.
Accepts either a :class:`WorkItemQueryParams` DTO or a plain mapping,
and normalises the ``filters`` field: the API expects it as a JSON
string in a single ``filters=`` query parameter, but callers are free
to pass it as a dict for ergonomics. Everything else is passed through
as-is by ``requests``' query-string encoder.
"""
if params is None:
return None
if isinstance(params, WorkItemQueryParams):
payload: dict[str, Any] = params.model_dump(exclude_none=True)
else:
payload = {k: v for k, v in params.items() if v is not None}
if "filters" in payload and isinstance(payload["filters"], dict):
payload["filters"] = json.dumps(payload["filters"], separators=(",", ":"))
return payload
class WorkItems(BaseResource):
def __init__(self, config: Any) -> None:
super().__init__(config, "/workspaces/")
# Initialize sub-resources
self.relations = WorkItemRelations(config)
self.links = WorkItemLinks(config)
self.attachments = WorkItemAttachments(config)
self.comments = WorkItemComments(config)
self.activities = WorkItemActivities(config)
self.work_logs = WorkLogs(config)
self.pages = WorkItemPages(config)
def create(self, workspace_slug: str, project_id: str, data: CreateWorkItem) -> WorkItem:
"""Create a new work item.
Args:
workspace_slug: The workspace slug identifier
project_id: UUID of the project
data: Work item data
"""
response = self._post(
f"{workspace_slug}/projects/{project_id}/work-items",
data.model_dump(exclude_none=True),
)
return WorkItem.model_validate(response)
def retrieve(
self,
workspace_slug: str,
project_id: str,
work_item_id: str,
params: RetrieveQueryParams | None = None,
) -> WorkItemDetail:
"""Retrieve a work item by ID.
Args:
workspace_slug: The workspace slug identifier
project_id: UUID of the project
work_item_id: UUID of the work item
params: Optional query parameters for expand, fields, etc.
Example:
# Get work item with expanded relationships
from plane.models.schemas import RetrieveQueryParams
work_item = client.work_items.retrieve(
"my-workspace",
"project-id",
"work-item-id",
params=RetrieveQueryParams(expand="assignees,labels,state")
)
# Get specific fields only
work_item = client.work_items.retrieve(
"my-workspace",
"project-id",
"work-item-id",
params=RetrieveQueryParams(fields="id,name,priority,state")
)
"""
query_params = params.model_dump(exclude_none=True) if params else None
response = self._get(
f"{workspace_slug}/projects/{project_id}/work-items/{work_item_id}",
params=query_params,
)
return WorkItemDetail.model_validate(response)
def retrieve_by_identifier(
self,
workspace_slug: str,
project_identifier: str,
issue_identifier: int,
params: RetrieveQueryParams | None = None,
) -> WorkItemDetail:
"""Retrieve a work item by project and issue identifiers.
Args:
workspace_slug: The workspace slug identifier
project_identifier: Project identifier string
issue_identifier: Issue sequence number
params: Optional query parameters for expand, fields, etc.
"""
query_params = params.model_dump(exclude_none=True) if params else None
response = self._get(
f"{workspace_slug}/work-items/{project_identifier}-{issue_identifier}",
params=query_params,
)
return WorkItemDetail.model_validate(response)
def update(
self,
workspace_slug: str,
project_id: str,
work_item_id: str,
data: UpdateWorkItem,
) -> WorkItem:
"""Update a work item by ID.
Args:
workspace_slug: The workspace slug identifier
project_id: UUID of the project
work_item_id: UUID of the work item
data: Updated work item data
"""
response = self._patch(
f"{workspace_slug}/projects/{project_id}/work-items/{work_item_id}",
data.model_dump(exclude_none=True),
)
return WorkItem.model_validate(response)
def delete(self, workspace_slug: str, project_id: str, work_item_id: str) -> None:
"""Delete a work item by ID.
Args:
workspace_slug: The workspace slug identifier
project_id: UUID of the project
work_item_id: UUID of the work item
"""
return self._delete(f"{workspace_slug}/projects/{project_id}/work-items/{work_item_id}")
def list(
self,
workspace_slug: str,
project_id: str,
params: WorkItemQueryParams | None = None,
) -> PaginatedWorkItemResponse:
"""List work items with optional filtering parameters.
Args:
workspace_slug: The workspace slug identifier
project_id: UUID of the project
params: Optional query parameters for filtering, ordering, and pagination
Example::
from plane.models.query_params import WorkItemQueryParams
# PQL filter (human-readable)
work_items = client.work_items.list(
"my-workspace",
"project-id",
params=WorkItemQueryParams(pql='priority = "urgent"'),
)
# Structured `filters` (JSON-encoded into the query string)
work_items = client.work_items.list(
"my-workspace",
"project-id",
params=WorkItemQueryParams(
filters={"and": [
{"priority": "urgent"},
{"state_group__in": ["unstarted", "started"]},
]},
),
)
"""
response = self._get(
f"{workspace_slug}/projects/{project_id}/work-items",
params=prepare_work_item_params(params),
)
return PaginatedWorkItemResponse.model_validate(response)
def list_workspace(
self,
workspace_slug: str,
params: WorkItemQueryParams | None = None,
) -> PaginatedWorkItemResponse:
"""List work items across an entire workspace.
Returns a paginated envelope of work items the caller can view,
spanning every project in the workspace (per-project authorization
and conditional grants are honored server-side).
Args:
workspace_slug: The workspace slug identifier
params: Optional query parameters — supports ``filters``, ``pql``,
``order_by``, ``cursor``, ``per_page``, ``fields``, ``expand``.
Example::
from plane.models.query_params import WorkItemQueryParams
results = client.work_items.list_workspace(
"my-workspace",
params=WorkItemQueryParams(
filters={"priority": "urgent"},
order_by="-created_at",
per_page=50,
),
)
"""
response = self._get(
f"{workspace_slug}/work-items",
params=prepare_work_item_params(params),
)
return PaginatedWorkItemResponse.model_validate(response)
def search(
self,
workspace_slug: str,
query: str,
params: RetrieveQueryParams | None = None,
) -> WorkItemSearch:
"""Search work items.
Args:
workspace_slug: The workspace slug identifier
query: Search query string
params: Optional query parameters for expand, fields, etc.
"""
search_params = {"q": query}
if params:
search_params.update(params.model_dump(exclude_none=True))
response = self._get(f"{workspace_slug}/work-items/search", params=search_params)
return WorkItemSearch.model_validate(response)
def advanced_search(
self,
workspace_slug: str,
data: AdvancedSearchWorkItem,
) -> list[AdvancedSearchResult]:
"""Perform advanced search on work items with filters.
Supports text-based search via ``query`` and/or structured filters
using recursive AND/OR groups.
Args:
workspace_slug: The workspace slug identifier
data: Advanced search request with query, filters, and limit
Example::
from plane.models.work_items import AdvancedSearchWorkItem
results = client.work_items.advanced_search(
"my-workspace",
AdvancedSearchWorkItem(
query="new",
project_id="project-uuid",
workspace_search=True,
filters={
"and": [
{"state_id": "state-uuid"},
{"or": [
{"priority": "high"},
{"state_id": "other-state-uuid"},
]},
]
},
limit=100,
),
)
"""
response = self._post(
f"{workspace_slug}/work-items/advanced-search",
data.model_dump(exclude_none=True),
)
return [AdvancedSearchResult.model_validate(item) for item in response]
def list_archived(
self,
workspace_slug: str,
project_id: str,
params: WorkItemQueryParams | None = None,
) -> PaginatedWorkItemResponse:
"""List archived work items in a project.
Supports the same ``filters`` and ``pql`` query parameters as
:meth:`list`.
Args:
workspace_slug: The workspace slug identifier
project_id: UUID of the project
params: Optional query parameters for filtering, ordering, and pagination
"""
response = self._get(
f"{workspace_slug}/projects/{project_id}/archived-work-items",
params=prepare_work_item_params(params),
)
return PaginatedWorkItemResponse.model_validate(response)
def archive(self, workspace_slug: str, project_id: str, work_item_id: str) -> None:
"""Archive a work item.
Only work items in a completed or cancelled state can be archived.
Args:
workspace_slug: The workspace slug identifier
project_id: UUID of the project
work_item_id: UUID of the work item
"""
self._post(
f"{workspace_slug}/projects/{project_id}/work-items/{work_item_id}/archive",
{},
)
def unarchive(self, workspace_slug: str, project_id: str, work_item_id: str) -> None:
"""Unarchive a work item.
Restore an archived work item to active status.
Args:
workspace_slug: The workspace slug identifier
project_id: UUID of the project
work_item_id: UUID of the work item
Returns:
None (HTTP 204 No Content)
"""
self._delete(f"{workspace_slug}/projects/{project_id}/work-items/{work_item_id}/unarchive")