Skip to content

Commit b7f5b02

Browse files
committed
updated Get-AzDoPull~ and Set-AzDoPull~
1 parent a28858b commit b7f5b02

File tree

7 files changed

+353
-7
lines changed

7 files changed

+353
-7
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Don't check in the Output dir
22
Output/
33
site/
4+
baswijdenes/
5+
test.ps1
6+
.vs

AzureDevOpsPowerShell/AzureDevOpsPowerShell.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
} # End of PrivateData hashtable
127127

128128
# HelpInfo URI of this module
129-
# HelpInfoURI = ''
129+
HelpInfoURI = 'https://github.com/WeAreInSpark/AzureDevOpsPowerShellAPI'
130130

131131
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
132132
# DefaultCommandPrefix = ''

AzureDevOpsPowerShell/AzureDevOpsPowerShell.psm1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
$public = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Public/*.ps1') -Recurse -ErrorAction Stop)
33
$private = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Private/*.ps1') -Recurse -ErrorAction Stop)
44
foreach ($import in @($private + $public)) {
5-
try {
6-
. $import.FullName
7-
} catch {
8-
throw "Unable to dot source [$($import.FullName)]"
9-
}
5+
try {
6+
. $import.FullName
7+
} catch {
8+
throw "Unable to dot source [$($import.FullName)]"
9+
}
1010
}
1111

1212
Export-ModuleMember -Function $public.Basename

AzureDevOpsPowerShell/Private/Invoke-AzDoRestMethod.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ function Invoke-AzDoRestMethod {
8282
}
8383

8484
if ($QueryParameters) {
85+
if ($QueryParameters -notlike "?*") {
86+
$QueryParameters = "?$QueryParameters"
87+
}
8588
$params.Uri = "$($Uri)?$($QueryParameters)&api-version=$($Version)"
8689
} else {
8790
$params.Uri = "$($Uri)?api-version=$($Version)"

AzureDevOpsPowerShell/Private/New-AzDoAuthHeader.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function New-AzDoAuthHeader {
88
)
99
if ($PSCmdlet.ShouldProcess("Creating new authentication header")) {
1010
Write-Verbose "Function: New-AzDoAuthHeader"
11-
if ($Pat -eq '') {
11+
if ([string]::IsNullOrEmpty($Pat)) {
1212
# validate if user is logged in to Azure PowerShell
1313
Write-Verbose "Using Access Token"
1414
try {
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
function Get-AzDoPullRequest {
2+
<#
3+
.SYNOPSIS
4+
Retrieves pull request information from Azure DevOps.
5+
6+
.DESCRIPTION
7+
This function fetches pull request details using Azure DevOps REST API.
8+
9+
.PARAMETER CollectionUri
10+
The base URL of the Azure DevOps organization (e.g., https://dev.azure.com/my-org).
11+
12+
.PARAMETER ProjectName
13+
The name of the Azure DevOps project.
14+
15+
.PARAMETER RepositoryName
16+
The name of the repository (optional for project-wide pull request queries).
17+
18+
.PARAMETER PullRequestId
19+
The ID of a specific pull request (optional for listing all pull requests).
20+
21+
.PARAMETER Query
22+
A query string to filter the results (optional).
23+
Can only be used without PullRequestId:
24+
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-requests?view=azure-devops-rest-7.2&tabs=HTTP#uri-parameters
25+
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-requests-by-project?view=azure-devops-rest-7.2&tabs=HTTP#uri-parameters
26+
27+
.EXAMPLE
28+
Get-AzDoPullRequest -CollectionUri "https://dev.azure.com/my-org" -ProjectName "MyProject"
29+
30+
.EXAMPLE
31+
Get-AzDoPullRequest -CollectionUri "https://dev.azure.com/my-org" -ProjectName "MyProject" -RepositoryName "RepositoryName"
32+
33+
.EXAMPLE
34+
Get-AzDoPullRequest -CollectionUri "https://dev.azure.com/my-org" -ProjectName "MyProject" -RepositoryName "RepositoryName" -Query "searchCriteria.status=completed"
35+
36+
.EXAMPLE
37+
Get-AzDoPullRequest -CollectionUri "https://dev.azure.com/my-org" -ProjectName "MyProject" -RepositoryName "RepositoryName" -PullRequestId "6789"
38+
39+
.EXAMPLE
40+
Get-AzDoPullRequest -CollectionUri "https://dev.azure.com/my-org" -ProjectName "MyProject" -PullRequestId "6789"
41+
42+
.OUTPUTS
43+
PSCustomObject with pull request details.
44+
45+
.NOTES
46+
Requires authentication with Azure DevOps REST API.
47+
48+
.LINK
49+
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-request?view=azure-devops-rest-7.2
50+
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-request-by-id?view=azure-devops-rest-7.2
51+
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-requests?view=azure-devops-rest-7.2
52+
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/get-pull-requests-by-project?view=azure-devops-rest-7.2
53+
#>
54+
[CmdletBinding(DefaultParameterSetName = "AllProjectPullRequests", SupportsShouldProcess)]
55+
param (
56+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "RepoSpecificPullRequest")]
57+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "ProjectSpecificPullRequest")]
58+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "AllRepoPullRequests")]
59+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "AllProjectPullRequests")]
60+
[ValidateScript({ Validate-CollectionUri -CollectionUri $_ })]
61+
[string]
62+
$CollectionUri,
63+
64+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "RepoSpecificPullRequest")]
65+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "ProjectSpecificPullRequest")]
66+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "AllRepoPullRequests")]
67+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = "AllProjectPullRequests")]
68+
[string]
69+
$ProjectName,
70+
71+
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = "RepoSpecificPullRequest")]
72+
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = "AllRepoPullRequests")]
73+
[string]
74+
$RepoName,
75+
76+
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = "RepoSpecificPullRequest")]
77+
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = "ProjectSpecificPullRequest")]
78+
[string]
79+
$PullRequestId,
80+
81+
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName = "AllRepoPullRequests")]
82+
[string]
83+
$Query
84+
)
85+
86+
87+
begin {
88+
$result = @()
89+
Write-Verbose "Starting function: Get-AzDoPullRequest"
90+
}
91+
92+
process {
93+
$uriBase = "$CollectionUri/$ProjectName/_apis/git"
94+
$apiVersion = "7.2-preview.2"
95+
$uri = ""
96+
97+
switch ($PSCmdlet.ParameterSetName) {
98+
"RepoSpecificPullRequest" {
99+
$uri = "$uriBase/repositories/$RepoName/pullrequests/$PullRequestId"
100+
}
101+
"ProjectSpecificPullRequest" {
102+
$uri = "$uriBase/pullrequests/$PullRequestId"
103+
}
104+
"AllRepoPullRequests" {
105+
$uri = "$uriBase/repositories/$RepoName/pullrequests"
106+
}
107+
"AllProjectPullRequests" {
108+
$uri = "$uriBase/pullrequests"
109+
}
110+
}
111+
112+
if ($PSCmdlet.ShouldProcess($CollectionUri, "Get Pull Request from: $ProjectName")) {
113+
Write-Verbose "Calling API: $uri"
114+
115+
$InvokeAzDoRestMethodSplat = @{
116+
Uri = $uri
117+
Method = "GET"
118+
Version = $apiVersion
119+
}
120+
if (-not([string]::IsNullOrEmpty($Query))) {
121+
$InvokeAzDoRestMethodSplat.QueryParameters = $Query
122+
}
123+
$response = Invoke-AzDoRestMethod @InvokeAzDoRestMethodSplat
124+
if ($response.value) {
125+
$result += $response.value
126+
} else {
127+
$result += $response
128+
}
129+
if ($result) {
130+
$result | ForEach-Object {
131+
[PSCustomObject]@{
132+
CollectionUri = $CollectionUri
133+
ProjectName = $ProjectName
134+
RepoName = $RepoName
135+
PullRequest = $PSItem
136+
}
137+
}
138+
}
139+
}
140+
}
141+
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
function Set-AzDoPullRequest {
2+
<#
3+
.SYNOPSIS
4+
Retrieves pull request information from Azure DevOps.
5+
6+
.DESCRIPTION
7+
This function fetches pull request details using Azure DevOps REST API.
8+
9+
.PARAMETER CollectionUri
10+
The base URL of the Azure DevOps organization (e.g., https://dev.azure.com/my-org).
11+
12+
.PARAMETER ProjectName
13+
The name of the Azure DevOps project.
14+
15+
.PARAMETER RepositoryName
16+
The name of the repository (optional for project-wide pull request queries).
17+
18+
.PARAMETER PullRequestId
19+
The ID of a specific pull request (optional for listing all pull requests).
20+
21+
.PARAMETER Status
22+
The new status of the pull request. Allowed values: active, abandoned, completed.
23+
24+
.PARAMETER Title
25+
The new title for the pull request (max 256 characters).
26+
27+
.PARAMETER Description
28+
The new description for the pull request (max 4000 characters).
29+
30+
.PARAMETER CompletionOptions
31+
Specifies how the PR should be completed. Example: @{ deleteSourceBranch = $true; mergeCommitMessage = "Merged PR" }
32+
33+
.PARAMETER MergeOptions
34+
Specifies how the PR should be merged. Allowed values: noMerge, squash, rebase, rebaseMerge.
35+
36+
.PARAMETER AutoCompleteSetBy
37+
The Azure DevOps user ID who sets the PR to auto-complete.
38+
39+
.PARAMETER TargetRefName
40+
The new target branch for the pull request. Example: "main" (automatically prefixed with "refs/heads/").
41+
Retargeting a pull request means changing the destination branch where the pull request will be merged.
42+
43+
.EXAMPLE
44+
# Update only the title and description of a pull request
45+
Set-AzDoPullRequest -CollectionUri "https://dev.azure.com/my-org" -ProjectName "MyProject" -RepositoryName "Repo" -PullRequestId "123" -Title "Updated PR Title" -Description "New description"
46+
47+
.EXAMPLE
48+
# Set auto-complete with completion options
49+
$completionOptions = @{
50+
deleteSourceBranch = $true
51+
mergeCommitMessage = "Auto-merging PR"
52+
}
53+
Set-AzDoPullRequest -CollectionUri "https://dev.azure.com/my-org" -ProjectName "MyProject" -RepositoryName "Repo" -PullRequestId "123" -AutoCompleteSetBy "user-id-123" -CompletionOptions $completionOptions
54+
55+
.EXAMPLE
56+
# Change the merge strategy to squash and complete the PR
57+
Set-AzDoPullRequest -CollectionUri "https://dev.azure.com/my-org" -ProjectName "MyProject" -RepositoryName "Repo" -PullRequestId "123" -MergeOptions "squash" -Status "completed"
58+
59+
.EXAMPLE
60+
# Retarget a pull request to a different branch
61+
Set-AzDoPullRequest -CollectionUri "https://dev.azure.com/my-org" -ProjectName "MyProject" -RepositoryName "Repo" -PullRequestId "123" -TargetRefName "develop"
62+
63+
.EXAMPLE
64+
# Set auto-complete for a pull request with a transition work item option
65+
$completionOptions = @{
66+
transitionWorkItems = $true
67+
deleteSourceBranch = $true
68+
}
69+
Set-AzDoPullRequest -CollectionUri "https://dev.azure.com/my-org" -ProjectName "MyProject" -RepositoryName "Repo" -PullRequestId "123" -AutoCompleteSetBy "user-id-123" -CompletionOptions $completionOptions
70+
71+
72+
.OUTPUTS
73+
PSCustomObject with pull request details.
74+
75+
.NOTES
76+
Requires authentication with Azure DevOps REST API.
77+
78+
I have currently added the 'extra parameters' to the function, that are registered in the first paragraph on this page:
79+
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/update?view=azure-devops-rest-7.2&tabs=HTTP#request-body
80+
81+
With the text currently being:
82+
These are the properties that can be updated with the API:
83+
Status
84+
Title
85+
Description (up to 4000 characters)
86+
CompletionOptions
87+
MergeOptions
88+
AutoCompleteSetBy.Id
89+
TargetRefName (when the PR retargeting feature is enabled) Attempting to update other properties outside of this list will either cause the server to throw an InvalidArgumentValueException, or to silently ignore the update.
90+
91+
.LINK
92+
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull-requests/update?view=azure-devops-rest-7.2&tabs=HTTP#request-body
93+
#>
94+
[CmdletBinding(SupportsShouldProcess)]
95+
param (
96+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
97+
[ValidateScript({ Validate-CollectionUri -CollectionUri $_ })]
98+
[string]
99+
$CollectionUri,
100+
101+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
102+
[string]
103+
$ProjectName,
104+
105+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
106+
[string]
107+
$RepositoryName,
108+
109+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
110+
[string]
111+
$PullRequestId,
112+
113+
[Parameter( ValueFromPipelineByPropertyName, ValueFromPipeline)]
114+
[ValidateSet('active', 'abandoned', 'completed', 'all')]
115+
[string]
116+
$Status,
117+
118+
[Parameter( ValueFromPipelineByPropertyName, ValueFromPipeline)]
119+
[ValidateLength(0, 256)]
120+
[string]
121+
$Title,
122+
123+
[Parameter( ValueFromPipelineByPropertyName, ValueFromPipeline)]
124+
[ValidateLength(0, 4000)]
125+
[string]
126+
$Description,
127+
128+
[Parameter( ValueFromPipelineByPropertyName, ValueFromPipeline)]
129+
[ValidateSet('setAutoComplete', 'setAutoCompleteSetBy')]
130+
[string]
131+
$CompletionOptions,
132+
133+
[Parameter( ValueFromPipelineByPropertyName, ValueFromPipeline)]
134+
[ValidateSet('noMerge', 'squash', 'rebase', 'rebaseMerge')]
135+
[string]
136+
$MergeOptions,
137+
138+
[Parameter( ValueFromPipelineByPropertyName, ValueFromPipeline)]
139+
[string]
140+
$AutoCompleteSetBy,
141+
142+
[Parameter( ValueFromPipelineByPropertyName, ValueFromPipeline)]
143+
[string]
144+
$TargetRefName
145+
)
146+
147+
begin {
148+
$result = @()
149+
Write-Verbose "Starting function: Set-AzDoPullRequest"
150+
}
151+
152+
process {
153+
$uriBase = "$CollectionUri/$ProjectName/_apis/git"
154+
$apiVersion = "7.2-preview.2"
155+
156+
$body = @{}
157+
158+
if ($Status) { $body["status"] = $Status }
159+
if ($Title) { $body["title"] = $Title }
160+
if ($Description) { $body["description"] = $Description }
161+
if ($MergeOptions) { $body["mergeOptions"] = $MergeOptions }
162+
if ($TargetRefName) { $body["targetRefName"] = "refs/heads/$TargetRefName" }
163+
164+
if ($CompletionOptions -and $CompletionOptions.Count -gt 0) {
165+
$body["completionOptions"] = $CompletionOptions
166+
}
167+
168+
if ($AutoCompleteSetBy) {
169+
$body["autoCompleteSetBy"] = @{ id = $AutoCompleteSetBy }
170+
}
171+
172+
if ($PSCmdlet.ShouldProcess($CollectionUri, "Get Pull Request from: $ProjectName")) {
173+
Write-Verbose "Calling API: $uri"
174+
175+
$InvokeAzDoRestMethodSplat = @{
176+
Uri = "$uriBase/repositories/$RepositoryName/pullrequests/$PullRequestId"
177+
Method = "PATCH"
178+
Version = $apiVersion
179+
Body = $Body
180+
}
181+
$response = Invoke-AzDoRestMethod @InvokeAzDoRestMethodSplat
182+
if ($response.value) {
183+
$result += $response.value
184+
} else {
185+
$result += $response
186+
}
187+
if ($result) {
188+
$result | ForEach-Object {
189+
[PSCustomObject]@{
190+
CollectionUri = $CollectionUri
191+
ProjectName = $ProjectName
192+
RepoName = $RepoName
193+
PullRequest = $PSItem
194+
}
195+
}
196+
}
197+
}
198+
}
199+
}

0 commit comments

Comments
 (0)