Skip to content

Commit 002339c

Browse files
authored
Merge pull request #69 from magusdevs/feature/create-pull-request
feat: Add feature New-AzDoPullRequest
2 parents 384a631 + dc2427c commit 002339c

File tree

2 files changed

+379
-0
lines changed

2 files changed

+379
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
function New-AzDoPullRequest {
2+
<#
3+
.SYNOPSIS
4+
Creates a pull request in Azure DevOps.
5+
.DESCRIPTION
6+
Creates a pull request in Azure DevOps.
7+
.EXAMPLE
8+
$params = @{
9+
CollectionUri = "https://dev.azure.com/contoso"
10+
RepoName = "Repo1"
11+
ProjectName = "Project 1"
12+
Title = "New Pull Request"
13+
Description = "This is a new pull request"
14+
SourceRefName = "refs/heads/feature1"
15+
TargetRefName = "refs/heads/main"
16+
}
17+
New-AzDoPullRequest @params
18+
19+
This example creates a new Azure DevOps Pull Request with splatting parameters
20+
.EXAMPLE
21+
$params = @{
22+
CollectionUri = "https://dev.azure.com/contoso"
23+
RepoName = "Repo1"
24+
ProjectName = "Project 1"
25+
Title = "New Pull Request"
26+
Description = "This is a new pull request"
27+
SourceRefName = "refs/heads/feature1"
28+
TargetRefName = "refs/heads/main"
29+
}
30+
$params | New-AzDoPullRequest
31+
32+
This example creates a new Azure DevOps Pull Request with pipeline parameters
33+
.OUTPUTS
34+
[PSCustomObject]@{
35+
CollectionUri = $CollectionUri
36+
ProjectName = $ProjectName
37+
RepoName = $RepoName
38+
PullRequestId = $res.pullRequestId
39+
PullRequestURL = $res.url
40+
}
41+
.NOTES
42+
#>
43+
44+
[CmdletBinding(SupportsShouldProcess)]
45+
param (
46+
# Collection Uri of the organization
47+
[Parameter(Mandatory)]
48+
[ValidateScript({ Validate-CollectionUri -CollectionUri $_ })]
49+
[string]
50+
$CollectionUri,
51+
52+
# Id of the repository
53+
[Parameter(Mandatory)]
54+
[string]
55+
$RepoName,
56+
57+
# Name of the project where the new repository has to be created
58+
[Parameter(Mandatory)]
59+
[string]
60+
$ProjectName,
61+
62+
# Title of the pull request
63+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
64+
[string[]]
65+
$Title,
66+
67+
# Description of the pull request
68+
[Parameter(ValueFromPipelineByPropertyName)]
69+
[string[]]
70+
$Description = 'Describe the changes made in this pull request',
71+
72+
# Source ref name
73+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
74+
[string[]]
75+
$SourceRefName,
76+
77+
# Target ref name
78+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
79+
[string[]]
80+
$TargetRefName
81+
)
82+
83+
begin {
84+
Write-Verbose "Starting function: New-AzDoPullRequest"
85+
$CollectionUri = $CollectionUri.TrimEnd('/')
86+
$result = New-Object System.Collections.Generic.List[System.Object]
87+
}
88+
89+
process {
90+
foreach ($pr in $Title) {
91+
$prTitle = $pr
92+
$prDescription = $Description[$Title.IndexOf($pr)]
93+
$prSourceRefName = $SourceRefName[$Title.IndexOf($pr)]
94+
$prTargetRefName = $TargetRefName[$Title.IndexOf($pr)]
95+
96+
# If the ref name is not in the format refs/heads/branch, then add it
97+
if ($prSourceRefName -notlike "refs/*") {
98+
$prSourceRefName = "refs/heads/$prSourceRefName"
99+
}
100+
if ($prTargetRefName -notlike "refs/*") {
101+
$prTargetRefName = "refs/heads/$prTargetRefName"
102+
}
103+
104+
$params = @{
105+
uri = "$CollectionUri/$ProjectName/_apis/git/repositories/$RepoName/pullrequests"
106+
version = '7.2-preview.2'
107+
method = 'POST'
108+
body = @{
109+
sourceRefName = $prSourceRefName
110+
targetRefName = $prTargetRefName
111+
title = $prTitle
112+
description = $prDescription
113+
}
114+
}
115+
116+
if ($PSCmdlet.ShouldProcess($CollectionUri, "Create pull request named: $($PSStyle.Bold)$prTitle$($PSStyle.Reset)")) {
117+
try {
118+
$result += Invoke-AzDoRestMethod @params
119+
} catch {
120+
if ($_ -match 'TF401179') {
121+
Write-Warning "Pull request between those branches already exists, trying to get it"
122+
$getParams = @{
123+
uri = "$CollectionUri/$ProjectName/_apis/git/repositories/$RepoName/pullrequests"
124+
version = '7.1-preview.1'
125+
method = 'GET'
126+
}
127+
$result += (Invoke-AzDoRestMethod @getParams).value | Where-Object { $_.sourceRefName -eq $prSourceRefName -and $_.targetRefName -eq $prTargetRefName }
128+
} else {
129+
Write-AzDoError -message $_
130+
}
131+
}
132+
} else {
133+
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
134+
}
135+
}
136+
}
137+
138+
end {
139+
if ($result) {
140+
$result | ForEach-Object {
141+
[PSCustomObject]@{
142+
CollectionUri = $CollectionUri
143+
ProjectName = $ProjectName
144+
RepoName = $RepoName
145+
PullRequestTitle = $_.title
146+
PullRequestId = $_.pullRequestId
147+
PullRequestURL = $_.url
148+
PullRequestWebUrl = "$CollectionUri/$ProjectName/_git/$RepoName/pullrequest/$($_.pullRequestId)"
149+
PullRequestStatus = $_.status
150+
}
151+
}
152+
}
153+
}
154+
}

docs/en-US/New-AzDoPullRequest.md

+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
---
2+
external help file: AzureDevOpsPowerShell-help.xml
3+
Module Name: AzureDevOpsPowerShell
4+
online version:
5+
schema: 2.0.0
6+
---
7+
8+
# New-AzDoPullRequest
9+
10+
## SYNOPSIS
11+
Creates a pull request in Azure DevOps.
12+
13+
## SYNTAX
14+
15+
```
16+
New-AzDoPullRequest [-CollectionUri] <String> [-RepoName] <String> [-ProjectName] <String> [-Title] <String> [-Description] <String> [-SourceRefName] <String> [-TargetRefName] <String>
17+
[-ProgressAction <ActionPreference>] [-WhatIf] [-Confirm] [<CommonParameters>]
18+
```
19+
20+
## DESCRIPTION
21+
Creates a pull request in Azure DevOps.
22+
23+
## EXAMPLES
24+
25+
### EXAMPLE 1
26+
```
27+
$params = @{
28+
CollectionUri = "https://dev.azure.com/contoso"
29+
RepoName = "Repo 1"
30+
ProjectName = "Project 1"
31+
Title = "New Pull Request"
32+
Description = "This is a new pull request"
33+
SourceRefName = "refs/heads/feature1"
34+
TargetRefName = "refs/heads/main"
35+
}
36+
New-AzDoPullRequest @params
37+
```
38+
39+
This example creates a new Azure DevOps Pull Request with splatting parameters
40+
41+
### Example 2
42+
```
43+
$params = @{
44+
CollectionUri = "https://dev.azure.com/contoso"
45+
RepoName = "Repo1"
46+
ProjectName = "Project 1"
47+
Title = "New Pull Request"
48+
Description = "This is a new pull request"
49+
SourceRefName = "refs/heads/feature1"
50+
TargetRefName = "refs/heads/main"
51+
}
52+
$params | New-AzDoPullRequest
53+
54+
This example creates a new Azure DevOps Pull Request with pipeline parameters
55+
```
56+
## PARAMETERS
57+
58+
### -CollectionUri
59+
Collection Uri of the organization
60+
61+
```yaml
62+
Type: String
63+
Parameter Sets: (All)
64+
Aliases:
65+
66+
Required: True
67+
Position: 1
68+
Default value: None
69+
Accept pipeline input: True (ByPropertyName)
70+
Accept wildcard characters: False
71+
```
72+
73+
### -RepoName
74+
Name of the repo
75+
76+
```yaml
77+
Type: String[]
78+
Parameter Sets: (All)
79+
Aliases:
80+
81+
Required: True
82+
Position: 2
83+
Default value: None
84+
Accept pipeline input: True (ByPropertyName, ByValue)
85+
Accept wildcard characters: False
86+
```
87+
88+
### -ProjectName
89+
Name of the project where the repository is hosted
90+
91+
```yaml
92+
Type: String
93+
Parameter Sets: (All)
94+
Aliases:
95+
96+
Required: True
97+
Position: 3
98+
Default value: None
99+
Accept pipeline input: True (ByPropertyName)
100+
Accept wildcard characters: False
101+
```
102+
103+
### -Title
104+
The title of the new pull request
105+
106+
```yaml
107+
Type: String
108+
Parameter Sets: (All)
109+
Aliases:
110+
111+
Required: True
112+
Position: 4
113+
Default value: None
114+
Accept pipeline input: True (ByPropertyName)
115+
Accept wildcard characters: False
116+
```
117+
118+
### -Description
119+
The description of the new pull request
120+
121+
```yaml
122+
Type: String
123+
Parameter Sets: (All)
124+
Aliases:
125+
126+
Required: True
127+
Position: 5
128+
Default value: None
129+
Accept pipeline input: True (ByPropertyName)
130+
Accept wildcard characters: False
131+
```
132+
133+
### -SourceRefName
134+
The source branch path of the pull request
135+
136+
```yaml
137+
Type: String
138+
Parameter Sets: (All)
139+
Aliases:
140+
141+
Required: True
142+
Position: 6
143+
Default value: None
144+
Accept pipeline input: True (ByPropertyName)
145+
Accept wildcard characters: False
146+
```
147+
148+
### -TargetRefName
149+
The target branch path of the pull request
150+
151+
```yaml
152+
Type: String
153+
Parameter Sets: (All)
154+
Aliases:
155+
156+
Required: True
157+
Position: 7
158+
Default value: None
159+
Accept pipeline input: True (ByPropertyName)
160+
Accept wildcard characters: False
161+
```
162+
163+
### -WhatIf
164+
Shows what would happen if the cmdlet runs.
165+
The cmdlet is not run.
166+
167+
```yaml
168+
Type: SwitchParameter
169+
Parameter Sets: (All)
170+
Aliases: wi
171+
172+
Required: False
173+
Position: Named
174+
Default value: None
175+
Accept pipeline input: False
176+
Accept wildcard characters: False
177+
```
178+
179+
### -Confirm
180+
Prompts you for confirmation before running the cmdlet.
181+
182+
```yaml
183+
Type: SwitchParameter
184+
Parameter Sets: (All)
185+
Aliases: cf
186+
187+
Required: False
188+
Position: Named
189+
Default value: None
190+
Accept pipeline input: False
191+
Accept wildcard characters: False
192+
```
193+
194+
### -ProgressAction
195+
{{ Fill ProgressAction Description }}
196+
197+
```yaml
198+
Type: ActionPreference
199+
Parameter Sets: (All)
200+
Aliases: proga
201+
202+
Required: False
203+
Position: Named
204+
Default value: None
205+
Accept pipeline input: False
206+
Accept wildcard characters: False
207+
```
208+
209+
### CommonParameters
210+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
211+
212+
## INPUTS
213+
214+
## OUTPUTS
215+
216+
### [PSCustomObject]@{
217+
### CollectionUri = $CollectionUri
218+
### ProjectName = $ProjectName
219+
### RepoId = $RepoId
220+
### PullRequestId = $res.pullRequestId
221+
### PullRequestURL = $res.url
222+
### }
223+
## NOTES
224+
225+
## RELATED LINKS

0 commit comments

Comments
 (0)