Skip to content

Commit 6aac4b6

Browse files
committed
Improves Azure DevOps PR search for a branch
1. users search criteria to list PRs related to the branch instead of listing all 2. if there are no open PRs, then select a closed one (#4478, #4516)
1 parent d124839 commit 6aac4b6

File tree

1 file changed

+20
-2
lines changed
  • src/plus/integrations/providers/azure

1 file changed

+20
-2
lines changed

src/plus/integrations/providers/azure/azure.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,32 @@ export class AzureDevOpsApi implements Disposable {
9898
provider,
9999
token,
100100
options?.baseUrl,
101-
`${owner}/${projectName}/_apis/git/repositories/${repoName}/pullRequests`,
101+
`${owner}/${projectName}/_apis/git/repositories/${repoName}/pullRequests?searchCriteria.status=all&searchCriteria.sourceRefName=refs/heads/${branch}`,
102102
{
103103
method: 'GET',
104104
},
105105
scope,
106106
);
107107

108-
const pr = prResult?.value.find(pr => pr.sourceRefName.endsWith(branch));
108+
// Sort PRs: open PRs first, then by most recent activity (creation or closure date)
109+
const sortedPRs = prResult?.value.sort((a, b) => {
110+
// First, prioritize open PRs (active/notSet) over closed ones (abandoned/completed)
111+
const aIsOpen = a.status === 'active' || a.status === 'notSet';
112+
const bIsOpen = b.status === 'active' || b.status === 'notSet';
113+
114+
if (aIsOpen !== bIsOpen) {
115+
return aIsOpen ? -1 : 1; // Open PRs come first
116+
}
117+
118+
// Among PRs with the same status, sort by most recent activity
119+
// Use closedDate if available, otherwise use creationDate
120+
const aDate = new Date(a.closedDate || a.creationDate);
121+
const bDate = new Date(b.closedDate || b.creationDate);
122+
123+
return bDate.getTime() - aDate.getTime(); // Most recent first
124+
});
125+
126+
const pr = sortedPRs?.[0];
109127
if (pr == null) return undefined;
110128

111129
return fromAzurePullRequest(pr, provider, owner);

0 commit comments

Comments
 (0)