Skip to content

Commit 2c00364

Browse files
author
Dale Myers
committed
Support more parameters for listing pull requests
1 parent 55e0e17 commit 2c00364

File tree

2 files changed

+75
-15
lines changed

2 files changed

+75
-15
lines changed

simple_ado/__init__.py

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from simple_ado.graph import ADOGraphClient
2424
from simple_ado.governance import ADOGovernanceClient
2525
from simple_ado.http_client import ADOHTTPClient, ADOResponse
26+
from simple_ado.models.pull_requests import ADOPullRequestTimeRangeType
2627
from simple_ado.pipelines import ADOPipelineClient
2728
from simple_ado.pools import ADOPoolsClient
2829
from simple_ado.pull_requests import ADOPullRequestClient, ADOPullRequestStatus
@@ -184,21 +185,39 @@ def pull_request(
184185
def list_all_pull_requests(
185186
self,
186187
*,
187-
branch_name: str | None = None,
188188
project_id: str,
189-
repository_id: str,
190189
top: int | None = None,
190+
creator_id: str | None = None,
191+
include_links: bool | None = None,
192+
max_time: datetime.datetime | None = None,
193+
min_time: datetime.datetime | None = None,
194+
query_time_range_type: ADOPullRequestTimeRangeType | None = None,
195+
repository_id: str | None = None,
196+
reviewer_id: str | None = None,
197+
branch_name: str | None = None, # TODO: Rename to source_ref_name
198+
source_repo_id: str | None = None,
191199
pr_status: ADOPullRequestStatus | None = None,
200+
target_ref_name: str | None = None,
201+
title: str | None = None,
192202
) -> Iterator[Any]:
193-
"""Get the pull requests for a branch from ADO.
194-
195-
:param branch_name: The name of the branch to fetch the pull requests for.
196-
:param project_id: The ID of the project
197-
:param repository_id: The ID for the repository
198-
:param top: How many PRs to retrieve
199-
:param pr_status: Set to filter by only PRs with that status
200-
201-
:returns: The ADO Response with the pull request data
203+
"""Get the pull requests matching the specified criteria from ADO.
204+
205+
:param project_id: The ID of the project.
206+
:param top: The number of pull requests to retrieve per page.
207+
:param creator_id: If set, search for pull requests that were created by this identity (Team Foundation ID).
208+
:param include_links: Whether to include the _links field on the shallow references.
209+
:param max_time: If specified, filters pull requests that were created/closed before this date based on the queryTimeRangeType specified.
210+
:param min_time: If specified, filters pull requests that were created/closed after this date based on the queryTimeRangeType specified.
211+
:param query_time_range_type: The type of time range to use for min_time and max_time filtering. Defaults to Created if unset.
212+
:param repository_id: If set, search for pull requests whose target branch is in this repository.
213+
:param reviewer_id: If set, search for pull requests that have this identity as a reviewer (Team Foundation ID).
214+
:param branch_name: If set, search for pull requests from this source branch.
215+
:param source_repo_id: If set, search for pull requests whose source branch is in this repository.
216+
:param pr_status: If set, search for pull requests that are in this state. Defaults to Active if unset.
217+
:param target_ref_name: If set, search for pull requests into this target branch.
218+
:param title: If set, filters pull requests that contain the specified text in the title.
219+
220+
:returns: An iterator yielding pull request data dictionaries.
202221
"""
203222

204223
self.log.debug("Fetching PRs")
@@ -207,28 +226,59 @@ def list_all_pull_requests(
207226

208227
while True:
209228
request_url = (
210-
self.http_client.api_endpoint(project_id=project_id)
211-
+ f"/git/repositories/{repository_id}/pullRequests?"
229+
self.http_client.api_endpoint(project_id=project_id) + f"/git/pullrequests?"
212230
)
213231

214-
parameters: dict[str, Any] = {"$skip": offset}
232+
parameters: dict[str, Any] = {"$skip": offset, "api-version": "7.2-preview.2"}
215233

216234
if top:
217235
parameters["$top"] = top
218236

237+
if creator_id:
238+
parameters["searchCriteria.creatorId"] = creator_id
239+
240+
if include_links is not None:
241+
parameters["searchCriteria.includeLinks"] = str(include_links).lower()
242+
243+
if max_time:
244+
parameters["searchCriteria.maxTime"] = max_time.strftime("%Y-%m-%dT%H:%M:%S.000Z")
245+
246+
if min_time:
247+
parameters["searchCriteria.minTime"] = min_time.strftime("%Y-%m-%dT%H:%M:%S.000Z")
248+
249+
if query_time_range_type:
250+
parameters["searchCriteria.queryTimeRangeType"] = query_time_range_type.value
251+
252+
if repository_id:
253+
parameters["searchCriteria.repositoryId"] = repository_id
254+
255+
if reviewer_id:
256+
parameters["searchCriteria.reviewerId"] = reviewer_id
257+
258+
if source_repo_id:
259+
parameters["searchCriteria.sourceRepositoryId"] = source_repo_id
260+
219261
if pr_status:
220262
parameters["searchCriteria.status"] = pr_status.value
221263

264+
if title:
265+
parameters["searchCriteria.title"] = title
266+
222267
encoded_parameters = urllib.parse.urlencode(parameters)
223268

224269
request_url += encoded_parameters
225270

271+
# ADO doesn't like it if the `/` in branch references are encoded, so we just append them manually
272+
226273
if branch_name is not None:
227274
request_url += (
228275
f"&searchCriteria.sourceRefName={_canonicalize_branch_name(branch_name)}"
229276
)
230277

231-
request_url += "&api-version=3.0-preview"
278+
if target_ref_name is not None:
279+
request_url += (
280+
f"&searchCriteria.targetRefName={_canonicalize_branch_name(target_ref_name)}"
281+
)
232282

233283
response = self.http_client.get(request_url)
234284
response_data = self.http_client.decode_response(response)

simple_ado/models/pull_requests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""ADO Pull Request Models."""
2+
3+
import enum
4+
5+
6+
class ADOPullRequestTimeRangeType(enum.Enum):
7+
"""An ADO operation."""
8+
9+
CREATED = "created"
10+
CLOSED = "closed"

0 commit comments

Comments
 (0)