Skip to content

Commit 36ffa77

Browse files
author
Dale Myers
committed
Add basic replacements for getting work items
1 parent 866e9c2 commit 36ffa77

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

simple_ado/workitems.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
WorkItemRelationType,
2222
ADOWorkItemBuiltInFields,
2323
)
24+
from simple_ado.work_item import ADOWorkItem
2425

2526

2627
class BatchRequest:
@@ -75,6 +76,7 @@ class ADOWorkItemsClient(ADOBaseClient):
7576
def __init__(self, http_client: ADOHTTPClient, log: logging.Logger) -> None:
7677
super().__init__(http_client, log.getChild("workitems"))
7778

79+
# TODO: Switch this to the default on next major version bump
7880
def get(self, identifier: str, project_id: str) -> ADOResponse:
7981
"""Get the data about a work item.
8082
@@ -92,6 +94,25 @@ def get(self, identifier: str, project_id: str) -> ADOResponse:
9294
response = self.http_client.get(request_url)
9395
return self.http_client.decode_response(response)
9496

97+
def get_work_item(self, identifier: str, project_id: str) -> ADOWorkItem:
98+
"""Get a work item as an ADOWorkItem object.
99+
100+
:param identifier: The identifier of the work item
101+
:param project_id: The ID of the project
102+
103+
:returns: An ADOWorkItem object wrapping the work item data
104+
"""
105+
106+
self.log.debug(f"Getting work item: {identifier}")
107+
request_url = (
108+
self.http_client.api_endpoint(project_id=project_id)
109+
+ f"/wit/workitems/{identifier}?api-version=4.1&$expand=all"
110+
)
111+
response = self.http_client.get(request_url)
112+
data = self.http_client.decode_response(response)
113+
return ADOWorkItem(data, self, project_id, self.log)
114+
115+
# TODO: Switch this to the default on next major version bump
95116
def list(self, identifiers: List[int], project_id: str) -> ADOResponse:
96117
"""Get a list of work items.
97118
@@ -149,6 +170,44 @@ def batched(sequence: List[T], n: int) -> Iterator[List[T]]:
149170

150171
yield from data.get("value", [])
151172

173+
def list_work_items(self, identifiers: List[int], project_id: str) -> Iterator[ADOWorkItem]:
174+
"""Get a list of work items as ADOWorkItem objects with automatic chunking.
175+
176+
:param identifiers: The list of requested work item ids
177+
:param project_id: The ID of the project
178+
179+
:returns: An iterator of ADOWorkItem objects
180+
"""
181+
182+
T = TypeVar("T")
183+
184+
# batched is only available in Python 3.12+
185+
def batched(sequence: List[T], n: int) -> Iterator[List[T]]:
186+
"""Batch data into lists of length n.
187+
188+
:param sequence: The iterable to batch
189+
:param n: The batch size
190+
191+
:returns: An iterator of lists of size n
192+
"""
193+
for i in range(0, len(sequence), n):
194+
yield sequence[i : i + n]
195+
196+
for id_chunk in batched(identifiers, 200):
197+
198+
ids = ",".join(map(str, id_chunk))
199+
200+
self.log.debug(f"Getting work items: {ids}")
201+
request_url = (
202+
self.http_client.api_endpoint(project_id=project_id)
203+
+ f"/wit/workitems?api-version=4.1&ids={ids}&$expand=all"
204+
)
205+
response = self.http_client.get(request_url)
206+
data = self.http_client.decode_response(response)
207+
208+
for item_data in data.get("value", []):
209+
yield ADOWorkItem(item_data, self, project_id, self.log)
210+
152211
def get_work_item_types(self, project_id: str) -> ADOResponse:
153212
"""Get the types of work items supported by the project.
154213

0 commit comments

Comments
 (0)