Skip to content

Commit

Permalink
Update New-GitHubBranch pipeline support (#277)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
HowardWolosky committed Aug 13, 2020
1 parent db11155 commit 3e79c25
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 30 deletions.
92 changes: 62 additions & 30 deletions GitHubBranches.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ filter New-GitHubRepositoryBranch
.PARAMETER TargetBranchName
Name of the branch to be created.
.PARAMETER Sha
The SHA1 value of the commit that this branch should be based on.
If not specified, will use the head of BranchName.
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
Expand Down Expand Up @@ -212,6 +216,17 @@ filter New-GitHubRepositoryBranch
$repo | New-GitHubRepositoryBranch -TargetBranchName new-branch
You can also pipe in a repo that was returned from a previous command.
.EXAMPLE
$branch = Get-GitHubRepositoryBranch -OwnerName microsoft -RepositoryName PowerShellForGitHub -BranchName main
$branch | New-GitHubRepositoryBranch -TargetBranchName beta
You can also pipe in a branch that was returned from a previous command.
.EXAMPLE
New-GitHubRepositoryBranch -Uri 'https://github.com/microsoft/PowerShellForGitHub' -Sha 1c3b80b754a983f4da20e77cfb9bd7f0e4cb5da6 -TargetBranchName new-branch
You can also create a new branch based off of a specific SHA1 commit value.
#>
[CmdletBinding(
SupportsShouldProcess,
Expand All @@ -235,6 +250,7 @@ filter New-GitHubRepositoryBranch
[Alias('RepositoryUrl')]
[string] $Uri,

[Parameter(ValueFromPipelineByPropertyName)]
[string] $BranchName = 'master',

[Parameter(
Expand All @@ -243,6 +259,9 @@ filter New-GitHubRepositoryBranch
Position = 2)]
[string] $TargetBranchName,

[Parameter(ValueFromPipelineByPropertyName)]
[string] $Sha,

[string] $AccessToken
)

Expand All @@ -259,51 +278,55 @@ filter New-GitHubRepositoryBranch

$originBranch = $null

try
{
$getGitHubRepositoryBranchParms = @{
OwnerName = $OwnerName
RepositoryName = $RepositoryName
BranchName = $BranchName
}
if ($PSBoundParameters.ContainsKey('AccessToken'))
{
$getGitHubRepositoryBranchParms['AccessToken'] = $AccessToken
}

Write-Log -Level Verbose "Getting $BranchName branch for sha reference"
$originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms
}
catch
if (-not $PSBoundParameters.ContainsKey('Sha'))
{
# Temporary code to handle current differences in exception object between PS5 and PS7
$throwObject = $_

if ($PSVersionTable.PSedition -eq 'Core')
try
{
if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException] -and
($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Branch not found')
$getGitHubRepositoryBranchParms = @{
OwnerName = $OwnerName
RepositoryName = $RepositoryName
BranchName = $BranchName
}
if ($PSBoundParameters.ContainsKey('AccessToken'))
{
$throwObject = "Origin branch $BranchName not found"
$getGitHubRepositoryBranchParms['AccessToken'] = $AccessToken
}

Write-Log -Level Verbose "Getting $BranchName branch for sha reference"
$originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms
$Sha = $originBranch.commit.sha
}
else
catch
{
if ($_.Exception.Message -like '*Not Found*')
# Temporary code to handle current differences in exception object between PS5 and PS7
$throwObject = $_

if ($PSVersionTable.PSedition -eq 'Core')
{
$throwObject = "Origin branch $BranchName not found"
if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException] -and
($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Branch not found')
{
$throwObject = "Origin branch $BranchName not found"
}
}
else
{
if ($_.Exception.Message -like '*Not Found*')
{
$throwObject = "Origin branch $BranchName not found"
}
}
}

Write-Log -Message $throwObject -Level Error
throw $throwObject
Write-Log -Message $throwObject -Level Error
throw $throwObject
}
}

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

$hashBody = @{
ref = "refs/heads/$TargetBranchName"
sha = $originBranch.commit.sha
sha = $Sha
}

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

Add-Member -InputObject $item -Name 'BranchName' -Value $branchName -MemberType NoteProperty -Force

if ($null -ne $item.commit)
{
Add-Member -InputObject $item -Name 'Sha' -Value $item.commit.sha -MemberType NoteProperty -Force
}
elseif ($null -ne $item.object)
{
Add-Member -InputObject $item -Name 'Sha' -Value $item.object.sha -MemberType NoteProperty -Force
}
}

Write-Output $item
Expand Down
72 changes: 72 additions & 0 deletions Tests/GitHubBranches.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ try
$branches[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
$branches[0].RepositoryUrl | Should -Be $repo.RepositoryUrl
$branches[0].BranchName | Should -Be $branches[0].name
$branches[0].Sha | Should -Be $branches[0].commit.sha
}
}

Expand All @@ -63,6 +64,7 @@ try
$branches[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
$branches[0].RepositoryUrl | Should -Be $repo.RepositoryUrl
$branches[0].BranchName | Should -Be $branches[0].name
$branches[0].Sha | Should -Be $branches[0].commit.sha
}
}

Expand All @@ -77,6 +79,7 @@ try
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
$branch.BranchName | Should -Be $branch.name
$branch.Sha | Should -Be $branch.commit.sha
}
}

Expand All @@ -91,6 +94,7 @@ try
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
$branch.BranchName | Should -Be $branch.name
$branch.Sha | Should -Be $branch.commit.sha
}
}

Expand All @@ -106,6 +110,7 @@ try
$branchAgain.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
$branchAgain.RepositoryUrl | Should -Be $repo.RepositoryUrl
$branchAgain.BranchName | Should -Be $branchAgain.name
$branchAgain.Sha | Should -Be $branchAgain.commit.sha
}
}
}
Expand Down Expand Up @@ -140,6 +145,7 @@ try
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
$branch.BranchName | Should -Be $newBranchName
$branch.Sha | Should -Be $branch.object.sha
}

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

It 'Should have created the branch' {
Expand All @@ -189,6 +196,71 @@ try
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
$branch.BranchName | Should -Be $newBranchName
$branch.Sha | Should -Be $branch.object.sha
}

It 'Should have created the branch' {
$getGitHubRepositoryBranchParms = @{
OwnerName = $script:ownerName
RepositoryName = $repoName
BranchName = $newBranchName
}

{ Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } |
Should -Not -Throw
}
}

Context 'When providing the GitHub.Branch on the pipeline' {
BeforeAll {
$baseBranchName = 'develop4'
$baseBranch = $baseBranchName | New-GitHubRepositoryBranch -Uri $repo.html_url

$newBranchName = 'develop5'
$branch = $baseBranch | New-GitHubRepositoryBranch -TargetBranchName $newBranchName
}

It 'Should have been created from the right Sha' {
$branch.Sha | Should -Be $baseBranch.Sha
}

It 'Should have the expected type and addititional properties' {
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
$branch.BranchName | Should -Be $newBranchName
$branch.Sha | Should -Be $branch.object.sha
}

It 'Should have created the branch' {
$getGitHubRepositoryBranchParms = @{
OwnerName = $script:ownerName
RepositoryName = $repoName
BranchName = $newBranchName
}

{ Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } |
Should -Not -Throw
}
}

Context 'When providing the Repo on the pipeline and specifying the Sha' {
BeforeAll {
$baseBranchName = 'sha1'
$baseBranch = $baseBranchName | New-GitHubRepositoryBranch -Uri $repo.html_url

$newBranchName = 'sha2'
$branch = $repo | New-GitHubRepositoryBranch -Sha $baseBranch.Sha -TargetBranchName $newBranchName
}

It 'Should have been created from the right Sha' {
$branch.Sha | Should -Be $baseBranch.Sha
}

It 'Should have the expected type and addititional properties' {
$branch.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch'
$branch.RepositoryUrl | Should -Be $repo.RepositoryUrl
$branch.BranchName | Should -Be $newBranchName
$branch.Sha | Should -Be $branch.object.sha
}

It 'Should have created the branch' {
Expand Down

0 comments on commit 3e79c25

Please sign in to comment.