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
This commit is contained in:
Howard Wolosky 2020-08-13 00:11:44 -07:00 коммит произвёл GitHub
Родитель db111559f9
Коммит 3e79c2592c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 137 добавлений и 33 удалений

Просмотреть файл

@ -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
if (-not $PSBoundParameters.ContainsKey('Sha'))
{
$getGitHubRepositoryBranchParms = @{
OwnerName = $OwnerName
RepositoryName = $RepositoryName
BranchName = $BranchName
}
if ($PSBoundParameters.ContainsKey('AccessToken'))
try
{
$getGitHubRepositoryBranchParms['AccessToken'] = $AccessToken
}
Write-Log -Level Verbose "Getting $BranchName branch for sha reference"
$originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms
}
catch
{
# Temporary code to handle current differences in exception object between PS5 and PS7
$throwObject = $_
if ($PSVersionTable.PSedition -eq 'Core')
{
if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException] -and
($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Branch not found')
{
$throwObject = "Origin branch $BranchName not found"
$getGitHubRepositoryBranchParms = @{
OwnerName = $OwnerName
RepositoryName = $RepositoryName
BranchName = $BranchName
}
}
else
{
if ($_.Exception.Message -like '*Not Found*')
if ($PSBoundParameters.ContainsKey('AccessToken'))
{
$throwObject = "Origin branch $BranchName not found"
$getGitHubRepositoryBranchParms['AccessToken'] = $AccessToken
}
}
Write-Log -Message $throwObject -Level Error
throw $throwObject
Write-Log -Level Verbose "Getting $BranchName branch for sha reference"
$originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms
$Sha = $originBranch.commit.sha
}
catch
{
# Temporary code to handle current differences in exception object between PS5 and PS7
$throwObject = $_
if ($PSVersionTable.PSedition -eq 'Core')
{
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
}
}
$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

Просмотреть файл

@ -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' {