@@ -15,31 +15,37 @@ The owner of the branch we want to create a pull request for.
15
15
The branch which we want to create a pull request for.
16
16
. PARAMETER AuthToken
17
17
A personal access token
18
+ . PARAMETER PRTitle
19
+ The title of the pull request.
20
+ . PARAMETER PRLabels
21
+ The labels added to the PRs. Multple labels seperated by comma, e.g "bug, service"
18
22
#>
19
23
[CmdletBinding (SupportsShouldProcess = $true )]
20
24
param (
21
25
[Parameter (Mandatory = $true )]
22
- $RepoOwner ,
26
+ [ string ] $RepoOwner ,
23
27
24
28
[Parameter (Mandatory = $true )]
25
- $RepoName ,
29
+ [ string ] $RepoName ,
26
30
27
31
[Parameter (Mandatory = $true )]
28
- $BaseBranch ,
32
+ [ string ] $BaseBranch ,
29
33
30
34
[Parameter (Mandatory = $true )]
31
- $PROwner ,
35
+ [ string ] $PROwner ,
32
36
33
37
[Parameter (Mandatory = $true )]
34
- $PRBranch ,
38
+ [ string ] $PRBranch ,
35
39
36
40
[Parameter (Mandatory = $true )]
37
- $AuthToken ,
41
+ [ string ] $AuthToken ,
38
42
39
43
[Parameter (Mandatory = $true )]
40
- $PRTitle ,
44
+ [string ]$PRTitle ,
45
+ $PRBody = $PRTitle ,
41
46
42
- $PRBody = $PRTitle
47
+ [Parameter (Mandatory = $false )]
48
+ [string ]$PRLabels
43
49
)
44
50
45
51
$headers = @ {
@@ -48,6 +54,31 @@ $headers = @{
48
54
49
55
$query = " state=open&head=${PROwner} :${PRBranch} &base=${BaseBranch} "
50
56
57
+ function AddLabels ([int ] $prNumber , [string ] $prLabelString )
58
+ {
59
+ # Adding labels to the pr.
60
+ if (-not $prLabelString ) {
61
+ Write-Verbose " There are no labels added to the PR."
62
+ return
63
+ }
64
+
65
+ # Parse the labels from string to array
66
+ $prLabelArray = @ ($prLabelString.Split (" ," ) | % { $_.Trim () } | ? { return $_ })
67
+ $prLabelUri = " https://api.github.com/repos/$RepoOwner /$RepoName /issues/$prNumber "
68
+ $labelRequestData = @ {
69
+ labels = $prLabelArray
70
+ }
71
+ try {
72
+ $resp = Invoke-RestMethod - Method PATCH - Headers $headers $prLabelUri - Body ($labelRequestData | ConvertTo-Json )
73
+ }
74
+ catch {
75
+ Write-Error " Invoke-RestMethod $prLabelUri failed with exception:`n $_ "
76
+ }
77
+
78
+ $resp | Write-Verbose
79
+ Write-Host -f green " Label(s) [$prLabelArray ] added to pull request: https://github.com/$RepoOwner /$RepoName /pull/$prNumber "
80
+ }
81
+
51
82
try {
52
83
$resp = Invoke-RestMethod - Headers $headers " https://api.github.com/repos/$RepoOwner /$RepoName /pulls?$query "
53
84
}
@@ -62,6 +93,7 @@ if ($resp.Count -gt 0) {
62
93
63
94
# setting variable to reference the pull request by number
64
95
Write-Host " ##vso[task.setvariable variable=Submitted.PullRequest.Number]$ ( $resp [0 ].number) "
96
+ AddLabels $resp [0 ].number $PRLabels
65
97
}
66
98
else {
67
99
$data = @ {
@@ -87,4 +119,6 @@ else {
87
119
88
120
# setting variable to reference the pull request by number
89
121
Write-Host " ##vso[task.setvariable variable=Submitted.PullRequest.Number]$ ( $resp.number ) "
122
+
123
+ AddLabels $resp.number $PRLabels
90
124
}
0 commit comments