-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAtlassian.Bitbucket.PullRequest.psm1
171 lines (143 loc) · 6.57 KB
/
Atlassian.Bitbucket.PullRequest.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using module .\Atlassian.Bitbucket.Authentication.psm1
<#
.SYNOPSIS
Returns pull requests against a repository.
.DESCRIPTION
Returns all the pull requests in the specified state against the repository. Defaults to PR's in an OPEN state.
.EXAMPLE
C:\PS> Get-BitbucketPullRequest -RepoSlug 'Repo'
Returns all open PR's against the `Repo` repository.
.EXAMPLE
C:\PS> Get-BitbucketPullRequest -RepoSlug 'Repo' -State 'MERGED'
Returns all merged PR's against the `Repo` repository.
.PARAMETER Workspace
Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.
.PARAMETER RepoSlug
Name of the repo in Bitbucket.
.PARAMETER State
State of the PR. Options: ['OPEN', 'MERGED', 'SUPERSEDED', 'DECLINED']
#>
function Get-BitbucketPullRequest {
[CmdletBinding()]
param(
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.')]
[Alias("Team")]
[string]$Workspace = (Get-BitbucketSelectedWorkspace),
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug,
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The state of the PR. Defaults to OPEN')]
[ValidateSet('OPEN', 'MERGED', 'SUPERSEDED', 'DECLINED')]
[string]$State = 'OPEN'
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/pullrequests"
# To Do - Add Filtering as needed https://developer.atlassian.com/bitbucket/api/2/reference/meta/filtering#query-pullreq
# Most Repos have small sets of PR's so client side will work for now until a use case comes up besides state.
if ($State) {
$endpoint += "?q=state=%22$State%22"
}
return Invoke-BitbucketAPI -Path $endpoint -Paginated
}
}
<#
.SYNOPSIS
Creates a pull request against a repository.
.DESCRIPTION
Creates a pull request against a repository.
.EXAMPLE
C:\PS> New-BitbucketPullRequest -RepoSlug 'Repo' -Title 'PR Title' -SourceBranch 'BranchName'
Creates a new PR against the `Repo` repository from BranchName to the repositories main branch.
.EXAMPLE
C:\PS> New-BitbucketPullRequest -RepoSlug 'Repo' -Title 'Markdown' -SourceBranch 'BranchName' -Description "# Heading1 `n * Item1 `n * Item2"
Creates a PR with markdown in the Description. Includes an h1 heading and bullet items.
.EXAMPLE
C:\PS> New-BitbucketPullRequest -RepoSlug 'Repo' -Title 'Reviewers' -SourceBranch 'BranchName' -Description "..." -Reviewers (Get-BitbucketRepositoryReviewer <repo>)
Creates a PR and includes the default reviewers for the repo on the PR.
The user creating the PR can not be included in the reviewers list.
.PARAMETER Workspace
Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.
.PARAMETER RepoSlug
Name of the repo in Bitbucket.
.PARAMETER Title
Title of the PR.
.PARAMETER SourceBranch
The source branch for the PR.
.PARAMETER Description
Description for the PR. Supports Markdown.
.PARAMETER CloseBranch
Specifies if the source branch should be closed when the PR is merged. Defaults to True.
.PARAMETER DestinationBranch
The destination branch for the PR. Defaults to the repositories main branch specified in Bitbucket.
.PARAMETER Reviewers
Array of user objects of the reviewers to add to the PR. Uses the uuid property on the object. Defaults to no reviewers. To include the default list use the (Get-BitbucketRepositoryReviewer <repo>) command.
#>
function New-BitbucketPullRequest {
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
param(
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Name of the workspace in Bitbucket. Defaults to selected workspace if not provided.')]
[Alias("Team")]
[string]$Workspace = (Get-BitbucketSelectedWorkspace),
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The repository slug.')]
[Alias('Slug')]
[string]$RepoSlug,
[Parameter( Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Title of the PR.')]
[string]$Title,
[Parameter( Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The source branch for the PR.')]
[string]$SourceBranch,
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Description for the PR.')]
[string]$Description,
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'Should the source branch be closed after PR is merged. Defaults to True.')]
[bool]$CloseBranch = $true,
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'The destination branch for the PR. Defaults to the repositories main branch specified in Bitbucket.')]
[string]$DestinationBranch,
[Parameter( ValueFromPipelineByPropertyName = $true,
HelpMessage = 'An array of user objects to include on the PR. Only needs the uuid property on the object.')]
[psobject[]]$Reviewers = @()
)
Process {
$endpoint = "repositories/$Workspace/$RepoSlug/pullrequests"
$body = [ordered]@{
title = $Title
description = $Description
close_source_branch = $CloseBranch
source = [ordered]@{
branch = [ordered]@{
name = $SourceBranch
}
}
reviewers = $Reviewers
}
if ($DestinationBranch) {
$body += [ordered]@{
destination = @{
branch = @{
name = $DestinationBranch
}
}
}
}
$body = $body | ConvertTo-Json -Depth 3 -Compress
if ($pscmdlet.ShouldProcess($RepoSlug, 'Create pull request')) {
return Invoke-BitbucketAPI -Path $endpoint -Body $body -Method Post
}
}
}