diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index 82d693ae..969f7264 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -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. @@ -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, @@ -235,6 +250,7 @@ filter New-GitHubRepositoryBranch [Alias('RepositoryUrl')] [string] $Uri, + [Parameter(ValueFromPipelineByPropertyName)] [string] $BranchName = 'master', [Parameter( @@ -243,6 +259,9 @@ filter New-GitHubRepositoryBranch Position = 2)] [string] $TargetBranchName, + [Parameter(ValueFromPipelineByPropertyName)] + [string] $Sha, + [string] $AccessToken ) @@ -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')) @@ -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 diff --git a/Tests/GitHubBranches.tests.ps1 b/Tests/GitHubBranches.tests.ps1 index 5e770b99..78768d12 100644 --- a/Tests/GitHubBranches.tests.ps1 +++ b/Tests/GitHubBranches.tests.ps1 @@ -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 } } @@ -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 } } @@ -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 } } @@ -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 } } @@ -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 } } } @@ -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' { @@ -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' { @@ -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' {