Skip to content

Commit 3e79c25

Browse files
Update New-GitHubBranch pipeline support (#277)
* Updates `New-GitHubBranch` to be able to take a `GitHub.Branch` object as pipeline input (for the base branch) * Updates `New-GitHubBranch` to be able to take in a `Sha` so that a branch can be created from an arbitrary commit (which also enables a new branch to be created from a `GitHub.Branch` pipeline input value without needing to perform an additional query on `BranchName` to get its `Sha`) * Updates `GitHub.Branch` to have `Sha` as a top-level property. * Updated existing tests and added additional tests. Reference: [GitHub Refs API](https://developer.github.com/v3/git/refs/) Fixes #261
1 parent db11155 commit 3e79c25

File tree

2 files changed

+134
-30
lines changed

2 files changed

+134
-30
lines changed

GitHubBranches.ps1

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ filter New-GitHubRepositoryBranch
175175
.PARAMETER TargetBranchName
176176
Name of the branch to be created.
177177
178+
.PARAMETER Sha
179+
The SHA1 value of the commit that this branch should be based on.
180+
If not specified, will use the head of BranchName.
181+
178182
.PARAMETER AccessToken
179183
If provided, this will be used as the AccessToken for authentication with the
180184
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
@@ -212,6 +216,17 @@ filter New-GitHubRepositoryBranch
212216
$repo | New-GitHubRepositoryBranch -TargetBranchName new-branch
213217
214218
You can also pipe in a repo that was returned from a previous command.
219+
220+
.EXAMPLE
221+
$branch = Get-GitHubRepositoryBranch -OwnerName microsoft -RepositoryName PowerShellForGitHub -BranchName main
222+
$branch | New-GitHubRepositoryBranch -TargetBranchName beta
223+
224+
You can also pipe in a branch that was returned from a previous command.
225+
226+
.EXAMPLE
227+
New-GitHubRepositoryBranch -Uri 'https://github.com/microsoft/PowerShellForGitHub' -Sha 1c3b80b754a983f4da20e77cfb9bd7f0e4cb5da6 -TargetBranchName new-branch
228+
229+
You can also create a new branch based off of a specific SHA1 commit value.
215230
#>
216231
[CmdletBinding(
217232
SupportsShouldProcess,
@@ -235,6 +250,7 @@ filter New-GitHubRepositoryBranch
235250
[Alias('RepositoryUrl')]
236251
[string] $Uri,
237252

253+
[Parameter(ValueFromPipelineByPropertyName)]
238254
[string] $BranchName = 'master',
239255

240256
[Parameter(
@@ -243,6 +259,9 @@ filter New-GitHubRepositoryBranch
243259
Position = 2)]
244260
[string] $TargetBranchName,
245261

262+
[Parameter(ValueFromPipelineByPropertyName)]
263+
[string] $Sha,
264+
246265
[string] $AccessToken
247266
)
248267

@@ -259,51 +278,55 @@ filter New-GitHubRepositoryBranch
259278

260279
$originBranch = $null
261280

262-
try
263-
{
264-
$getGitHubRepositoryBranchParms = @{
265-
OwnerName = $OwnerName
266-
RepositoryName = $RepositoryName
267-
BranchName = $BranchName
268-
}
269-
if ($PSBoundParameters.ContainsKey('AccessToken'))
270-
{
271-
$getGitHubRepositoryBranchParms['AccessToken'] = $AccessToken
272-
}
273-
274-
Write-Log -Level Verbose "Getting $BranchName branch for sha reference"
275-
$originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms
276-
}
277-
catch
281+
if (-not $PSBoundParameters.ContainsKey('Sha'))
278282
{
279-
# Temporary code to handle current differences in exception object between PS5 and PS7
280-
$throwObject = $_
281-
282-
if ($PSVersionTable.PSedition -eq 'Core')
283+
try
283284
{
284-
if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException] -and
285-
($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Branch not found')
285+
$getGitHubRepositoryBranchParms = @{
286+
OwnerName = $OwnerName
287+
RepositoryName = $RepositoryName
288+
BranchName = $BranchName
289+
}
290+
if ($PSBoundParameters.ContainsKey('AccessToken'))
286291
{
287-
$throwObject = "Origin branch $BranchName not found"
292+
$getGitHubRepositoryBranchParms['AccessToken'] = $AccessToken
288293
}
294+
295+
Write-Log -Level Verbose "Getting $BranchName branch for sha reference"
296+
$originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms
297+
$Sha = $originBranch.commit.sha
289298
}
290-
else
299+
catch
291300
{
292-
if ($_.Exception.Message -like '*Not Found*')
301+
# Temporary code to handle current differences in exception object between PS5 and PS7
302+
$throwObject = $_
303+
304+
if ($PSVersionTable.PSedition -eq 'Core')
293305
{
294-
$throwObject = "Origin branch $BranchName not found"
306+
if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException] -and
307+
($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Branch not found')
308+
{
309+
$throwObject = "Origin branch $BranchName not found"
310+
}
311+
}
312+
else
313+
{
314+
if ($_.Exception.Message -like '*Not Found*')
315+
{
316+
$throwObject = "Origin branch $BranchName not found"
317+
}
295318
}
296-
}
297319

298-
Write-Log -Message $throwObject -Level Error
299-
throw $throwObject
320+
Write-Log -Message $throwObject -Level Error
321+
throw $throwObject
322+
}
300323
}
301324

302325
$uriFragment = "repos/$OwnerName/$RepositoryName/git/refs"
303326

304327
$hashBody = @{
305328
ref = "refs/heads/$TargetBranchName"
306-
sha = $originBranch.commit.sha
329+
sha = $Sha
307330
}
308331

309332
if (-not $PSCmdlet.ShouldProcess($BranchName, 'Create Repository Branch'))
@@ -1106,6 +1129,15 @@ filter Add-GitHubBranchAdditionalProperties
11061129
}
11071130

11081131
Add-Member -InputObject $item -Name 'BranchName' -Value $branchName -MemberType NoteProperty -Force
1132+
1133+
if ($null -ne $item.commit)
1134+
{
1135+
Add-Member -InputObject $item -Name 'Sha' -Value $item.commit.sha -MemberType NoteProperty -Force
1136+
}
1137+
elseif ($null -ne $item.object)
1138+
{
1139+
Add-Member -InputObject $item -Name 'Sha' -Value $item.object.sha -MemberType NoteProperty -Force
1140+
}
11091141
}
11101142

11111143
Write-Output $item

Tests/GitHubBranches.tests.ps1

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ try
4545
$branches[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
4646
$branches[0].RepositoryUrl | Should -Be $repo.RepositoryUrl
4747
$branches[0].BranchName | Should -Be $branches[0].name
48+
$branches[0].Sha | Should -Be $branches[0].commit.sha
4849
}
4950
}
5051

@@ -63,6 +64,7 @@ try
6364
$branches[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
6465
$branches[0].RepositoryUrl | Should -Be $repo.RepositoryUrl
6566
$branches[0].BranchName | Should -Be $branches[0].name
67+
$branches[0].Sha | Should -Be $branches[0].commit.sha
6668
}
6769
}
6870

@@ -77,6 +79,7 @@ try
7779
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
7880
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
7981
$branch.BranchName | Should -Be $branch.name
82+
$branch.Sha | Should -Be $branch.commit.sha
8083
}
8184
}
8285

@@ -91,6 +94,7 @@ try
9194
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
9295
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
9396
$branch.BranchName | Should -Be $branch.name
97+
$branch.Sha | Should -Be $branch.commit.sha
9498
}
9599
}
96100

@@ -106,6 +110,7 @@ try
106110
$branchAgain.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
107111
$branchAgain.RepositoryUrl | Should -Be $repo.RepositoryUrl
108112
$branchAgain.BranchName | Should -Be $branchAgain.name
113+
$branchAgain.Sha | Should -Be $branchAgain.commit.sha
109114
}
110115
}
111116
}
@@ -140,6 +145,7 @@ try
140145
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
141146
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
142147
$branch.BranchName | Should -Be $newBranchName
148+
$branch.Sha | Should -Be $branch.object.sha
143149
}
144150

145151
It 'Should have created the branch' {
@@ -165,6 +171,7 @@ try
165171
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
166172
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
167173
$branch.BranchName | Should -Be $newBranchName
174+
$branch.Sha | Should -Be $branch.object.sha
168175
}
169176

170177
It 'Should have created the branch' {
@@ -189,6 +196,71 @@ try
189196
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
190197
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
191198
$branch.BranchName | Should -Be $newBranchName
199+
$branch.Sha | Should -Be $branch.object.sha
200+
}
201+
202+
It 'Should have created the branch' {
203+
$getGitHubRepositoryBranchParms = @{
204+
OwnerName = $script:ownerName
205+
RepositoryName = $repoName
206+
BranchName = $newBranchName
207+
}
208+
209+
{ Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } |
210+
Should -Not -Throw
211+
}
212+
}
213+
214+
Context 'When providing the GitHub.Branch on the pipeline' {
215+
BeforeAll {
216+
$baseBranchName = 'develop4'
217+
$baseBranch = $baseBranchName | New-GitHubRepositoryBranch -Uri $repo.html_url
218+
219+
$newBranchName = 'develop5'
220+
$branch = $baseBranch | New-GitHubRepositoryBranch -TargetBranchName $newBranchName
221+
}
222+
223+
It 'Should have been created from the right Sha' {
224+
$branch.Sha | Should -Be $baseBranch.Sha
225+
}
226+
227+
It 'Should have the expected type and addititional properties' {
228+
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
229+
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
230+
$branch.BranchName | Should -Be $newBranchName
231+
$branch.Sha | Should -Be $branch.object.sha
232+
}
233+
234+
It 'Should have created the branch' {
235+
$getGitHubRepositoryBranchParms = @{
236+
OwnerName = $script:ownerName
237+
RepositoryName = $repoName
238+
BranchName = $newBranchName
239+
}
240+
241+
{ Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } |
242+
Should -Not -Throw
243+
}
244+
}
245+
246+
Context 'When providing the Repo on the pipeline and specifying the Sha' {
247+
BeforeAll {
248+
$baseBranchName = 'sha1'
249+
$baseBranch = $baseBranchName | New-GitHubRepositoryBranch -Uri $repo.html_url
250+
251+
$newBranchName = 'sha2'
252+
$branch = $repo | New-GitHubRepositoryBranch -Sha $baseBranch.Sha -TargetBranchName $newBranchName
253+
}
254+
255+
It 'Should have been created from the right Sha' {
256+
$branch.Sha | Should -Be $baseBranch.Sha
257+
}
258+
259+
It 'Should have the expected type and addititional properties' {
260+
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
261+
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
262+
$branch.BranchName | Should -Be $newBranchName
263+
$branch.Sha | Should -Be $branch.object.sha
192264
}
193265

194266
It 'Should have created the branch' {

0 commit comments

Comments
 (0)