Skip to content

Commit da1870f

Browse files
committed
feat: add 2 new flags to the WorkItem Class telling the user, whether the list of attachments and links are loaded completely
1 parent 55f706b commit da1870f

File tree

6 files changed

+347
-18
lines changed

6 files changed

+347
-18
lines changed

polarion_rest_api_client/client.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,13 @@ def get_work_item(
582582
work_item_id: str,
583583
retry: bool = True,
584584
) -> base_client.WorkItemType | None:
585-
"""Return one specific work item with all fields."""
585+
"""Return one specific work item with all fields.
586+
587+
This also includes all linked work items and attachments. If
588+
there are to many of these to get them in one request, the
589+
truncated flags for linked_work_items and attachments will be
590+
set to True.
591+
"""
586592
response = get_work_item.sync_detailed(
587593
self.project_id,
588594
work_item_id,
@@ -616,33 +622,39 @@ def _generate_work_item(
616622
work_item_id = work_item.id.split("/")[-1]
617623
work_item_links = []
618624
work_item_attachments = []
619-
if (
620-
work_item.relationships
621-
and work_item.relationships.linked_work_items
622-
and work_item.relationships.linked_work_items.data
623-
):
624-
for link in work_item.relationships.linked_work_items.data:
625-
work_item_links.append(
625+
links_truncated = True
626+
attachments_truncated = True
627+
if work_item.relationships:
628+
if links := work_item.relationships.linked_work_items:
629+
if not links.meta or links.meta.total_count is oa_types.UNSET:
630+
links_truncated = False
631+
632+
work_item_links = [
626633
self._parse_work_item_link(
627634
link.id,
628635
link.additional_properties.get("suspect", False),
629636
work_item_id,
630637
)
631-
)
632-
if (
633-
work_item.relationships
634-
and work_item.relationships.attachments
635-
and work_item.relationships.attachments.data
636-
):
637-
for attachment in work_item.relationships.attachments.data:
638-
assert attachment.id
639-
work_item_attachments.append(
638+
for link in links.data or []
639+
]
640+
641+
if attachments := work_item.relationships.attachments:
642+
if (
643+
not attachments.meta
644+
or attachments.meta.total_count is oa_types.UNSET
645+
):
646+
attachments_truncated = False
647+
648+
work_item_attachments = [
640649
dm.WorkItemAttachment(
641650
work_item_id,
642651
attachment.id.split("/")[-1],
643652
None, # title isn't provided
644653
)
645-
)
654+
for attachment in attachments.data or []
655+
if attachment.id
656+
]
657+
646658
work_item_obj = self._work_item(
647659
work_item_id,
648660
unset_str_builder(work_item.attributes.title),
@@ -661,6 +673,8 @@ def _generate_work_item(
661673
work_item.attributes.additional_properties,
662674
work_item_links,
663675
work_item_attachments,
676+
links_truncated,
677+
attachments_truncated,
664678
)
665679
return work_item_obj
666680

polarion_rest_api_client/data_models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ class WorkItem(BaseItem):
6767
description: str | None = None
6868
additional_attributes: dict[str, t.Any] = {}
6969
linked_work_items: list[WorkItemLink] = []
70+
linked_work_items_truncated: bool = False
7071
attachments: list[WorkItemAttachment] = []
72+
attachments_truncated: bool = False
7173

7274
def __init__(
7375
self,
@@ -80,6 +82,8 @@ def __init__(
8082
additional_attributes: dict[str, t.Any] | None = None,
8183
linked_work_items: list[WorkItemLink] | None = None,
8284
attachments: list[WorkItemAttachment] | None = None,
85+
linked_work_items_truncated: bool = False,
86+
attachments_truncated: bool = False,
8387
**kwargs,
8488
):
8589
super().__init__(id, type, status)
@@ -89,6 +93,8 @@ def __init__(
8993
self.additional_attributes = (additional_attributes or {}) | kwargs
9094
self.linked_work_items = linked_work_items or []
9195
self.attachments = attachments or []
96+
self.linked_work_items_truncated = linked_work_items_truncated
97+
self.attachments_truncated = attachments_truncated
9298

9399
def __getattribute__(self, item: str) -> t.Any:
94100
"""Return all non WorkItem attributes from additional_properties."""

tests/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
)
6161
TEST_WI_NEXT_PAGE_RESPONSE = TEST_RESPONSES / "workitems_next_page.json"
6262
TEST_WI_SINGLE_RESPONSE = TEST_RESPONSES / "get_work_item.json"
63+
TEST_WI_NOT_TRUNCATED_RESPONSE = (
64+
TEST_RESPONSES / "get_work_item_not_truncated.json"
65+
)
6366
TEST_DOCUMENT_RESPONSE = TEST_RESPONSES / "get_document.json"
6467
TEST_ERROR_RESPONSE = TEST_RESPONSES / "error.json"
6568
TEST_PROJECT_RESPONSE_JSON = TEST_RESPONSES / "project.json"
Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
{
2+
"data": {
3+
"type": "workitems",
4+
"id": "MyProjectId/MyWorkItemId",
5+
"revision": "1234",
6+
"attributes": {
7+
"created": "1970-01-01T00:00:00Z",
8+
"description": {
9+
"type": "text/html",
10+
"value": "My text value"
11+
},
12+
"test_custom_field": {
13+
"type": "text/html",
14+
"value": "My text value"
15+
},
16+
"dueDate": "1970-01-01",
17+
"hyperlinks": [
18+
{
19+
"role": "ref_ext",
20+
"uri": "https://polarion.plm.automation.siemens.com"
21+
}
22+
],
23+
"id": "MyWorkItemId",
24+
"initialEstimate": "5 1/2d",
25+
"outlineNumber": "1.11",
26+
"plannedEnd": "1970-01-01T00:00:00Z",
27+
"plannedStart": "1970-01-01T00:00:00Z",
28+
"priority": "90.0",
29+
"remainingEstimate": "5 1/2d",
30+
"resolution": "done",
31+
"resolvedOn": "1970-01-01T00:00:00Z",
32+
"severity": "blocker",
33+
"status": "open",
34+
"timeSpent": "5 1/2d",
35+
"title": "Title",
36+
"type": "task",
37+
"updated": "1970-01-01T00:00:00Z"
38+
},
39+
"relationships": {
40+
"approvals": {
41+
"data": [
42+
{
43+
"type": "workrecords",
44+
"id": "MyProjectId/MyWorkItemId/MyWorkRecordId",
45+
"revision": "1234"
46+
}
47+
],
48+
"meta": {
49+
"totalCount": 0
50+
}
51+
},
52+
"assignee": {
53+
"data": [
54+
{
55+
"type": "users",
56+
"id": "MyUserId",
57+
"revision": "1234"
58+
}
59+
],
60+
"meta": {
61+
"totalCount": 0
62+
}
63+
},
64+
"attachments": {
65+
"data": [
66+
{
67+
"type": "workitem_attachments",
68+
"id": "MyProjectId/MyWorkItemId/MyAttachmentId",
69+
"revision": "1234"
70+
}
71+
],
72+
"links": {
73+
"related": "server-host-name/application-path/projects/MyProjectId/workitems/MyWorkItemId/attachments?revision=1234"
74+
}
75+
},
76+
"author": {
77+
"data": {
78+
"type": "users",
79+
"id": "MyUserId",
80+
"revision": "1234"
81+
}
82+
},
83+
"backlinkedWorkItems": {
84+
"data": [
85+
{
86+
"type": "linkedworkitems",
87+
"id": "MyProjectId/MyWorkItemId/parent/MyProjectId/MyLinkedWorkItemId",
88+
"revision": "1234"
89+
}
90+
],
91+
"meta": {
92+
"totalCount": 0
93+
}
94+
},
95+
"categories": {
96+
"data": [
97+
{
98+
"type": "categories",
99+
"id": "MyProjectId/MyCategoryId",
100+
"revision": "1234"
101+
}
102+
],
103+
"meta": {
104+
"totalCount": 0
105+
}
106+
},
107+
"comments": {
108+
"data": [
109+
{
110+
"type": "workitem_comments",
111+
"id": "MyProjectId/MyWorkItemId/MyCommentId",
112+
"revision": "1234"
113+
}
114+
],
115+
"meta": {
116+
"totalCount": 0
117+
},
118+
"links": {
119+
"related": "server-host-name/application-path/projects/MyProjectId/workitems/MyWorkItemId/comments?revision=1234"
120+
}
121+
},
122+
"externallyLinkedWorkItems": {
123+
"data": [
124+
{
125+
"type": "externallylinkedworkitems",
126+
"id": "MyProjectId/MyWorkItemId/parent/hostname/MyProjectId/MyLinkedWorkItemId",
127+
"revision": "1234"
128+
}
129+
],
130+
"meta": {
131+
"totalCount": 0
132+
}
133+
},
134+
"linkedOslcResources": {
135+
"data": [
136+
{
137+
"type": "linkedoslcresources",
138+
"id": "MyProjectId/MyWorkItemId/http://server-host-name/ns/cm#relatedChangeRequest/http://server-host-name/application-path/oslc/services/projects/MyProjectId/workitems/MyWorkItemId",
139+
"revision": "1234"
140+
}
141+
],
142+
"meta": {
143+
"totalCount": 0
144+
}
145+
},
146+
"linkedRevisions": {
147+
"data": [
148+
{
149+
"type": "revisions",
150+
"id": "default/1234",
151+
"revision": "1234"
152+
}
153+
],
154+
"meta": {
155+
"totalCount": 0
156+
}
157+
},
158+
"linkedWorkItems": {
159+
"data": [
160+
{
161+
"type": "linkedworkitems",
162+
"id": "MyProjectId/MyWorkItemId/parent/MyProjectId/MyLinkedWorkItemId",
163+
"revision": "1234"
164+
}
165+
],
166+
"links": {
167+
"related": "server-host-name/application-path/projects/MyProjectId/workitems/MyWorkItemId/linkedworkitems?revision=1234"
168+
}
169+
},
170+
"module": {
171+
"data": {
172+
"type": "documents",
173+
"id": "MyProjectId/MySpaceId/MyDocumentId",
174+
"revision": "1234"
175+
}
176+
},
177+
"plannedIn": {
178+
"data": [
179+
{
180+
"type": "plans",
181+
"id": "MyProjectId/MyPlanId",
182+
"revision": "1234"
183+
}
184+
],
185+
"meta": {
186+
"totalCount": 0
187+
}
188+
},
189+
"project": {
190+
"data": {
191+
"type": "projects",
192+
"id": "MyProjectId",
193+
"revision": "1234"
194+
}
195+
},
196+
"testSteps": {
197+
"data": [
198+
{
199+
"type": "teststeps",
200+
"id": "MyProjectId/MyWorkItemId/MyTestStepIndex",
201+
"revision": "1234"
202+
}
203+
],
204+
"meta": {
205+
"totalCount": 0
206+
}
207+
},
208+
"votes": {
209+
"data": [
210+
{
211+
"type": "users",
212+
"id": "MyUserId",
213+
"revision": "1234"
214+
}
215+
],
216+
"meta": {
217+
"totalCount": 0
218+
}
219+
},
220+
"watches": {
221+
"data": [
222+
{
223+
"type": "users",
224+
"id": "MyUserId",
225+
"revision": "1234"
226+
}
227+
],
228+
"meta": {
229+
"totalCount": 0
230+
}
231+
},
232+
"workRecords": {
233+
"data": [
234+
{
235+
"type": "workrecords",
236+
"id": "MyProjectId/MyWorkItemId/MyWorkRecordId",
237+
"revision": "1234"
238+
}
239+
],
240+
"meta": {
241+
"totalCount": 0
242+
}
243+
}
244+
},
245+
"meta": {
246+
"errors": [
247+
{
248+
"status": "400",
249+
"title": "Bad Request",
250+
"detail": "Unexpected token, BEGIN_ARRAY expected, but was : BEGIN_OBJECT (at $.data)",
251+
"source": {
252+
"pointer": "$.data",
253+
"parameter": "revision",
254+
"resource": {
255+
"id": "MyProjectId/id",
256+
"type": "type"
257+
}
258+
}
259+
}
260+
]
261+
},
262+
"links": {
263+
"self": "server-host-name/application-path/projects/MyProjectId/workitems/MyWorkItemId?revision=1234",
264+
"portal": "server-host-name/application-path/polarion/redirect/project/MyProjectId/workitem?id=MyWorkItemId&revision=1234"
265+
}
266+
},
267+
"included": [
268+
{}
269+
],
270+
"links": {
271+
"self": "server-host-name/application-path/projects/MyProjectId/workitems/MyWorkItemId?revision=1234"
272+
}
273+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Copyright DB InfraGO AG and contributors
2+
SPDX-License-Identifier: Apache-2.0

0 commit comments

Comments
 (0)