From 05a06aea60c9e77c94176f6924e04aead150a0d3 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Fri, 29 May 2020 18:53:20 +0100 Subject: [PATCH 01/21] GitHubRepositryChanges: - Add New-GitHubRepositoryBranch and Remove-GitHubRepositoryBranch --- GitHubBranches.ps1 | 230 +++++++++++++++++++++++++++++++++++++++ PowerShellForGitHub.psd1 | 2 + 2 files changed, 232 insertions(+) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index f595ace7..2b2f2720 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -154,6 +154,236 @@ filter Get-GitHubRepositoryBranch return (Invoke-GHRestMethodMultipleResult @params | Add-GitHubBranchAdditionalProperties) } +function New-GitHubRepositoryBranch +{ + <# + .SYNOPSIS + Creates a new branch for a given GitHub repository. + + .DESCRIPTION + Creates a new branch for a given GitHub repository. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER Name + Name of the branch to be created. + + .PARAMETER OriginBranchName + The name of the origin branch to create the new branch from. + + .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. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .OUTPUTS + [PSCustomObject] details of the created branch. + + .EXAMPLE + New-GitHubRepositoryBranch -OwnerName Microsoft -RepositoryName PowerShellForGitHub -Name New-Branch1 + + Creates a new branch in the specified repository from the master branch. + + .EXAMPLE + New-GitHubRepositoryBranch -Uri 'https://github.com/PowerShell/PowerShellForGitHub' -Name New-Branch2 -OriginBranchName 'New-Branch1' + + Creates a new branch in the specified repository from the specified origin branch. +#> + [CmdletBinding( + SupportsShouldProcess, + DefaultParameterSetName = 'Elements')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification = "One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")] + [Alias('New-GitHubBranch')] + param( + [Parameter(ParameterSetName = 'Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName = 'Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ParameterSetName = 'Uri')] + [string] $Uri, + + [Parameter(Mandatory)] + [string] $Name, + + [string] $OriginBranchName = 'master', + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog + + $elements = Resolve-RepositoryElements + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + try + { + $originBranch = Get-GitHubRepositoryBranch -OwnerName $OwnerName -RepositoryName $RepositoryName -Name $OriginBranchName + } + catch [System.Management.Automation.RemoteException] + { + if (($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Branch not found') + { + throw "Origin branch $OriginBranchName not found" + } + else + { + throw $_ + } + } + catch + { + throw $_ + } + + $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs" + + $hashBody = @{ + ref = "refs/heads/$Name" + sha = $originBranch.commit.sha + } + + $params = @{ + 'UriFragment' = $uriFragment + 'Body' = (ConvertTo-Json -InputObject $hashBody) + 'Method' = 'Post' + 'Description' = "Creating branch $Name for $RepositoryName" + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} + +function Remove-GitHubRepositoryBranch +{ + <# + .SYNOPSIS + Removes a branch from a given GitHub repository. + + .DESCRIPTION + Removes a branch from a given GitHub repository. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER Name + Name of the branch to be removed. + + .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. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .OUTPUTS + None + + .EXAMPLE + Remove-GitHubRepositoryBranch -OwnerName Microsoft -RepositoryName PowerShellForGitHub -Name Test + + Removes the Test branch from the specified repository. +#> + [CmdletBinding( + SupportsShouldProcess, + DefaultParameterSetName = 'Elements')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification = "One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")] + [Alias('Remove-GitHubBranch')] + param( + [Parameter(ParameterSetName = 'Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName = 'Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ParameterSetName = 'Uri')] + [string] $Uri, + + [Parameter(Mandatory)] + [string] $Name, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog + + $elements = Resolve-RepositoryElements + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/heads/$Name" + + $params = @{ + 'UriFragment' = $uriFragment + 'Method' = 'Delete' + 'Description' = "Deleting branch $Name from $RepositoryName" + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethod @params +} + filter Add-GitHubBranchAdditionalProperties { <# diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index cb7fcf06..47e865d8 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -121,6 +121,7 @@ 'New-GitHubPullRequest', 'New-GitHubRepository', 'New-GitHubRepositoryFromTemplate', + 'New-GitHubRepositoryBranch', 'New-GitHubRepositoryFork', 'Remove-GitHubAssignee', 'Remove-GitHubIssueComment', @@ -131,6 +132,7 @@ 'Remove-GitHubProjectCard', 'Remove-GitHubProjectColumn', 'Remove-GitHubRepository', + 'Remove-GitHubRepositoryBranch' 'Rename-GitHubRepository', 'Reset-GitHubConfiguration', 'Restore-GitHubConfiguration', From 41ad7db5971d741d509bfb318183ad9972802385 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Tue, 2 Jun 2020 17:54:05 +0100 Subject: [PATCH 02/21] Update review comments --- GitHubBranches.ps1 | 17 ++++++++++++----- PowerShellForGitHub.psd1 | 4 ++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index 2b2f2720..1e3e3aab 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -210,8 +210,10 @@ function New-GitHubRepositoryBranch [CmdletBinding( SupportsShouldProcess, DefaultParameterSetName = 'Elements')] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification = "One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", + Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", + Justification = "One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")] [Alias('New-GitHubBranch')] param( [Parameter(ParameterSetName = 'Elements')] @@ -334,10 +336,15 @@ function Remove-GitHubRepositoryBranch #> [CmdletBinding( SupportsShouldProcess, - DefaultParameterSetName = 'Elements')] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification = "One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")] + DefaultParameterSetName = 'Elements', + ConfirmImpact="High")] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", + Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", + Justification = "One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")] [Alias('Remove-GitHubBranch')] + [Alias('Delete-GitHubRepositoryBranch')] + [Alias('Delete-GitHubBranch')] param( [Parameter(ParameterSetName = 'Elements')] [string] $OwnerName, diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 47e865d8..a15992a6 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -159,6 +159,7 @@ ) AliasesToExport = @( + 'Delete-GitHubBranch', 'Delete-GitHubComment', 'Delete-GitHubIssueComment', 'Delete-GitHubLabel', @@ -167,10 +168,13 @@ 'Delete-GitHubProjectCard', 'Delete-GitHubProjectColumn' 'Delete-GitHubRepository', + 'Delete-GitHubRepositoryBranch', 'Get-GitHubBranch', 'Get-GitHubComment', 'New-GitHubAssignee', + 'New-GitHubBranch', 'New-GitHubComment', + 'Remove-GitHubBranch' 'Remove-GitHubComment', 'Set-GitHubComment', 'Transfer-GitHubRepositoryOwnership' From 17cbc5ce233e5484afbc05a44d6cdd60836463bd Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Fri, 5 Jun 2020 20:16:17 +0100 Subject: [PATCH 03/21] Fix Get-GitHubRepositoryBranch Exception handling --- GitHubBranches.ps1 | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index 1e3e3aab..17850fa1 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -252,9 +252,10 @@ function New-GitHubRepositoryBranch { $originBranch = Get-GitHubRepositoryBranch -OwnerName $OwnerName -RepositoryName $RepositoryName -Name $OriginBranchName } - catch [System.Management.Automation.RemoteException] + catch { - if (($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Branch not found') + if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException] -and + ($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Branch not found') { throw "Origin branch $OriginBranchName not found" } @@ -263,10 +264,6 @@ function New-GitHubRepositoryBranch throw $_ } } - catch - { - throw $_ - } $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs" From 02e018e541d38d8ce0b37c17fdf0cab3b245da66 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Fri, 5 Jun 2020 20:16:32 +0100 Subject: [PATCH 04/21] Add GitHubBranches tests --- Tests/GitHubBranches.tests.ps1 | 128 +++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/Tests/GitHubBranches.tests.ps1 b/Tests/GitHubBranches.tests.ps1 index f0df1ca9..f2bacb0b 100644 --- a/Tests/GitHubBranches.tests.ps1 +++ b/Tests/GitHubBranches.tests.ps1 @@ -107,6 +107,134 @@ try } } } + + Describe 'GitHubBranches\New-GitHubRepositoryBranch' { + Context 'When creating a new GitHub repository branch' { + BeforeAll -Scriptblock { + $repoName = [Guid]::NewGuid().Guid + $originBranchName = 'master' + $newBranchName = 'develop' + $newGitHubRepositoryParms = @{ + RepositoryName = $repoName + AutoInit = $true + } + $repo = New-GitHubRepository @newGitHubRepositoryParms + + $newGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + Name = $newBranchName + OriginBranchName = $originBranchName + } + $branch = New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms + } + + It 'Should return an object of the correct type' { + $branch | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $branch.ref | Should -Be "refs/heads/$newBranchName" + } + + It 'Should have created the branch' { + $getGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + Name = $newBranchName + } + { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | + Should -Not -Throw + } + + Context 'When the origin branch cannot be found' { + BeforeAll -Scriptblock { + $missingOriginBranchName = 'Missing-Branch' + } + + It 'Should throw the correct exception' { + $errorMessage = "Origin branch $missingOriginBranchName not found" + + $newGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + Name = $newBranchName + OriginBranchName = $missingOriginBranchName + } + { New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } | + Should -Throw $errorMessage + } + } + + Context 'When Get-GitHubRepositoryBranch throws an undefined HttpResponseException' { + It 'Should throw the correct exception' { + $newGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = 'test' + Name = 'test' + OriginBranchName = 'test' + } + { New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } | + Should -Throw 'Not Found' + } + } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } + } + } + + Describe 'GitHubBranches\Remove-GitHubRepositoryBranch' { + BeforeAll -Scriptblock { + $repoName = [Guid]::NewGuid().Guid + $originBranchName = 'master' + $newBranchName = 'develop' + $newGitHubRepositoryParms = @{ + RepositoryName = $repoName + AutoInit = $true + } + $repo = New-GitHubRepository @newGitHubRepositoryParms + + $newGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + Name = $newBranchName + OriginBranchName = $originBranchName + } + $branch = New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms + } + + It 'Should not throw an exception' { + $removeGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + Name = $newBranchName + } + { Remove-GitHubRepositoryBranch @removeGitHubRepositoryBranchParms } | + Should -Not -Throw + } + + It 'Should have removed the branch' { + $getGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + Name = $newBranchName + } + { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | + Should -Throw + } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } + } } finally { From ea23414c2d2ca82b26c60a81a35cd35b3aaddf18 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 7 Jun 2020 13:38:19 +0100 Subject: [PATCH 05/21] Fix GitHubBranches tests --- Tests/GitHubBranches.tests.ps1 | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Tests/GitHubBranches.tests.ps1 b/Tests/GitHubBranches.tests.ps1 index f2bacb0b..f1af0172 100644 --- a/Tests/GitHubBranches.tests.ps1 +++ b/Tests/GitHubBranches.tests.ps1 @@ -105,6 +105,13 @@ try $branchAgain.RepositoryUrl | Should -Be $repo.RepositoryUrl $branchAgain.BranchName | Should -Be $branchAgain.name } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } } } @@ -213,6 +220,7 @@ try OwnerName = $script:ownerName RepositoryName = $repoName Name = $newBranchName + Confirm = $false } { Remove-GitHubRepositoryBranch @removeGitHubRepositoryBranchParms } | Should -Not -Throw From e90e6f20a1fb374acb60a59822c210f8111d5b61 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 7 Jun 2020 13:42:44 +0100 Subject: [PATCH 06/21] GitHubBranches updates - Add CBH Inputs - Fix CBH Outputs - Add PositionalBinding - Change Mandatory parameter order - Add Invocation parm to Write-InvocationLog - Fix getGitHubRepositoryBranchParms - Add temp code to handle diffs in exception object between PS5 & PS7 - Add ShouldProcess to Remove-GitHubRepositoryBranch --- GitHubBranches.ps1 | 130 ++++++++++++++++++++++++++++++--------------- 1 file changed, 88 insertions(+), 42 deletions(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index 17850fa1..ec05f7de 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -194,41 +194,51 @@ function New-GitHubRepositoryBranch the background, enabling the command prompt to provide status information. If not supplied here, the DefaultNoStatus configuration property value will be used. + .INPUTS + None + .OUTPUTS - [PSCustomObject] details of the created branch. + PSCustomObject .EXAMPLE - New-GitHubRepositoryBranch -OwnerName Microsoft -RepositoryName PowerShellForGitHub -Name New-Branch1 + New-GitHubRepositoryBranch -Name New-Branch1 -OwnerName Microsoft -RepositoryName PowerShellForGitHub Creates a new branch in the specified repository from the master branch. .EXAMPLE - New-GitHubRepositoryBranch -Uri 'https://github.com/PowerShell/PowerShellForGitHub' -Name New-Branch2 -OriginBranchName 'New-Branch1' + New-GitHubRepositoryBranch -Name New-Branch2 -Uri 'https://github.com/PowerShell/PowerShellForGitHub' -OriginBranchName 'New-Branch1' Creates a new branch in the specified repository from the specified origin branch. #> [CmdletBinding( SupportsShouldProcess, - DefaultParameterSetName = 'Elements')] + DefaultParameterSetName = 'Elements', + PositionalBinding = $false + )] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", - Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + Justification = "Methods called within here make use of PSShouldProcess, and the switch is + passed on to them inherently.")] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", - Justification = "One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")] + Justification = "One or more parameters (like NoStatus) are only referenced by helper + methods which get access to it from the stack via Get-Variable -Scope 1.")] [Alias('New-GitHubBranch')] param( - [Parameter(ParameterSetName = 'Elements')] - [string] $OwnerName, - - [Parameter(ParameterSetName = 'Elements')] - [string] $RepositoryName, + [Parameter( + Mandatory, + Position = 1)] + [string] $Name, [Parameter( Mandatory, + Position = 2, ParameterSetName = 'Uri')] [string] $Uri, - [Parameter(Mandatory)] - [string] $Name, + [Parameter(ParameterSetName = 'Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName = 'Elements')] + [string] $RepositoryName, [string] $OriginBranchName = 'master', @@ -237,7 +247,7 @@ function New-GitHubRepositoryBranch [switch] $NoStatus ) - Write-InvocationLog + Write-InvocationLog -Invocation $MyInvocation $elements = Resolve-RepositoryElements $OwnerName = $elements.ownerName @@ -250,19 +260,43 @@ function New-GitHubRepositoryBranch try { - $originBranch = Get-GitHubRepositoryBranch -OwnerName $OwnerName -RepositoryName $RepositoryName -Name $OriginBranchName + $getGitHubRepositoryBranchParms = @{ + OwnerName = $OwnerName + RepositoryName = $RepositoryName + Name = $OriginBranchName + Whatif = $false + Confirm = $false + } + if ($PSBoundParameters.ContainsKey('AccessToken')) { + $getGitHubRepositoryBranchParms['AccessToken'] = $AccessToken + } + if ($PSBoundParameters.ContainsKey('NoStatus')) { + $getGitHubRepositoryBranchParms['NoStatus'] = $NoStatus + } + $originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } catch { - if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException] -and - ($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Branch not found') + # Temporary code to handle current differences in exception object between PS5 and PS7 + $throwObject = $_ + + if ($PSVersionTable.PSedition -eq 'Core') { - throw "Origin branch $OriginBranchName not found" + if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException] -and + ($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Branch not found') + { + $throwObject = "Origin branch $OriginBranchName not found" + } } else { - throw $_ + if ($_.Exception.Message -like '*Not Found*') + { + $throwObject = "Origin branch $OriginBranchName not found" + } } + + throw $throwObject } $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs" @@ -323,47 +357,54 @@ function Remove-GitHubRepositoryBranch the background, enabling the command prompt to provide status information. If not supplied here, the DefaultNoStatus configuration property value will be used. + .INPUTS + None + .OUTPUTS None .EXAMPLE - Remove-GitHubRepositoryBranch -OwnerName Microsoft -RepositoryName PowerShellForGitHub -Name Test + Remove-GitHubRepositoryBranch -Name TestBranch -OwnerName Microsoft -RepositoryName PowerShellForGitHub - Removes the Test branch from the specified repository. + Removes the specified branch from the specified repository. #> [CmdletBinding( SupportsShouldProcess, DefaultParameterSetName = 'Elements', + PositionalBinding = $false, ConfirmImpact="High")] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", - Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + Justification = "Methods called within here make use of PSShouldProcess, and the switch is + passed on to them inherently.")] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", - Justification = "One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")] + Justification = "One or more parameters (like NoStatus) are only referenced by helper + methods which get access to it from the stack via Get-Variable -Scope 1.")] [Alias('Remove-GitHubBranch')] [Alias('Delete-GitHubRepositoryBranch')] [Alias('Delete-GitHubBranch')] param( - [Parameter(ParameterSetName = 'Elements')] - [string] $OwnerName, - - [Parameter(ParameterSetName = 'Elements')] - [string] $RepositoryName, + [Parameter( + Mandatory, + Position = 1)] + [string] $Name, [Parameter( Mandatory, + Position = 2, ParameterSetName = 'Uri')] [string] $Uri, - [Parameter(Mandatory)] - [string] $Name, + [Parameter(ParameterSetName = 'Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName = 'Elements')] + [string] $RepositoryName, [string] $AccessToken, [switch] $NoStatus ) - Write-InvocationLog - $elements = Resolve-RepositoryElements $OwnerName = $elements.ownerName $RepositoryName = $elements.repositoryName @@ -375,17 +416,22 @@ function Remove-GitHubRepositoryBranch $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/heads/$Name" - $params = @{ - 'UriFragment' = $uriFragment - 'Method' = 'Delete' - 'Description' = "Deleting branch $Name from $RepositoryName" - 'AccessToken' = $AccessToken - 'TelemetryEventName' = $MyInvocation.MyCommand.Name - 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) - } + if ($PSCmdlet.ShouldProcess($Name, "Remove Repository Branch")) + { + Write-InvocationLog -Invocation $MyInvocation + + $params = @{ + 'UriFragment' = $uriFragment + 'Method' = 'Delete' + 'Description' = "Deleting branch $Name from $RepositoryName" + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + } - return Invoke-GHRestMethod @params + Invoke-GHRestMethod @params | Out-Null + } } filter Add-GitHubBranchAdditionalProperties From 953a902e53b831876475ce19aef787924a4b4c94 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 7 Jun 2020 15:16:07 +0100 Subject: [PATCH 07/21] Added Remove-GitHubRepositoryBranch -Force Switch --- GitHubBranches.ps1 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index ec05f7de..bfbe7681 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -347,6 +347,9 @@ function Remove-GitHubRepositoryBranch .PARAMETER Name Name of the branch to be removed. + .PARAMETER Force + If this switch is specified, you will not be prompted for confirmation of command execution. + .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. @@ -400,6 +403,8 @@ function Remove-GitHubRepositoryBranch [Parameter(ParameterSetName = 'Elements')] [string] $RepositoryName, + [switch] $Force, + [string] $AccessToken, [switch] $NoStatus @@ -416,7 +421,7 @@ function Remove-GitHubRepositoryBranch $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/heads/$Name" - if ($PSCmdlet.ShouldProcess($Name, "Remove Repository Branch")) + if ($Force -or $PSCmdlet.ShouldProcess($Name, "Remove Repository Branch")) { Write-InvocationLog -Invocation $MyInvocation From 89c2325032f9030ac845f851f3524d8daa8f6223 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 7 Jun 2020 15:44:08 +0100 Subject: [PATCH 08/21] Update -Force parameter processing. --- GitHubBranches.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index bfbe7681..7f244028 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -421,7 +421,11 @@ function Remove-GitHubRepositoryBranch $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/heads/$Name" - if ($Force -or $PSCmdlet.ShouldProcess($Name, "Remove Repository Branch")) + if ($Force -and -not $Confirm){ + $ConfirmPreference = 'None' + } + + if ($PSCmdlet.ShouldProcess($Name, "Remove Repository Branch")) { Write-InvocationLog -Invocation $MyInvocation From eb353767d199fb3b3d5404ce17f802535bf36bb0 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 7 Jun 2020 22:33:57 +0100 Subject: [PATCH 09/21] Fix formatting --- GitHubBranches.ps1 | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index 7f244028..19800a99 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -267,10 +267,12 @@ function New-GitHubRepositoryBranch Whatif = $false Confirm = $false } - if ($PSBoundParameters.ContainsKey('AccessToken')) { + if ($PSBoundParameters.ContainsKey('AccessToken')) + { $getGitHubRepositoryBranchParms['AccessToken'] = $AccessToken } - if ($PSBoundParameters.ContainsKey('NoStatus')) { + if ($PSBoundParameters.ContainsKey('NoStatus')) + { $getGitHubRepositoryBranchParms['NoStatus'] = $NoStatus } $originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms @@ -421,7 +423,8 @@ function Remove-GitHubRepositoryBranch $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/heads/$Name" - if ($Force -and -not $Confirm){ + if ($Force -and -not $Confirm) + { $ConfirmPreference = 'None' } @@ -436,7 +439,8 @@ function Remove-GitHubRepositoryBranch 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties - 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue ` + -Name NoStatus -ConfigValueName DefaultNoStatus) } Invoke-GHRestMethod @params | Out-Null From 996fb5602c39899c61d9e5cfb8ba3f8e170b94a6 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Wed, 10 Jun 2020 21:29:40 +0100 Subject: [PATCH 10/21] Update USAGE.md --- USAGE.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 441081e7..25e6fd1b 100644 --- a/USAGE.md +++ b/USAGE.md @@ -39,6 +39,9 @@ * [Disable repository vulnerability alerts](#disable-repository-vulnerability-alerts) * [Enable repository automatic security fixes](#enable-repository-automatic-security-fixes) * [Disable repository automatic security fixes](#disable-repository-automatic-security-fixes) + * [Branches](#branches) + * [Adding a new Branch to a Repository](#adding-a-new-branch-to-a-repository) + * [Removing a Branch from a Repository](#removing-a-branch-from-a-repository) * [Forks](#forks) * [Get all the forks for a repository](#get-all-the-forks-for-a-repository) * [Create a new fork](#create-a-new-fork) @@ -432,6 +435,21 @@ Get-GitHubUser ``` > Warning: This will take a while. It's getting _every_ GitHub user. +---------- +### Repositories + +#### Adding a new Branch to a Repository + +```powershell +New-GitHubRepositoryBranch -Name develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub +``` + +#### Removing a Branch from a Repository + +```powershell +Remove-GitHubRepositoryBranch -Name develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub +``` + ---------- ### Repositories @@ -459,7 +477,8 @@ New-GitHubRepository -RepositoryName TestRepo -OrganizationName MyOrg -TeamId $m ```powershell New-GitHubRepositoryFromTemplate -OwnerName MyOrg -RepositoryName MyNewRepo-TemplateOwnerName MyOrg -TemplateRepositoryName MyTemplateRepo -======= +``` + #### Get repository vulnerability alert status ```powershell From 31ee9efaf664927cd8613ebed491ba13cee839af Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Wed, 10 Jun 2020 21:35:22 +0100 Subject: [PATCH 11/21] Remove redundant Invocation parameter --- GitHubBranches.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index 19800a99..841d8d67 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -247,7 +247,7 @@ function New-GitHubRepositoryBranch [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog $elements = Resolve-RepositoryElements $OwnerName = $elements.ownerName @@ -430,7 +430,7 @@ function Remove-GitHubRepositoryBranch if ($PSCmdlet.ShouldProcess($Name, "Remove Repository Branch")) { - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog $params = @{ 'UriFragment' = $uriFragment From 8dd123e7ca2b9dbae5c4823ad70f82150f6e893b Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Wed, 10 Jun 2020 21:36:00 +0100 Subject: [PATCH 12/21] Update Remove-GitHubRepositoryBranch examples --- GitHubBranches.ps1 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index 841d8d67..2dec71f1 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -369,9 +369,14 @@ function Remove-GitHubRepositoryBranch None .EXAMPLE - Remove-GitHubRepositoryBranch -Name TestBranch -OwnerName Microsoft -RepositoryName PowerShellForGitHub + Remove-GitHubRepositoryBranch -Name develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub - Removes the specified branch from the specified repository. + Removes the 'develop' branch from the specified repository. + + .EXAMPLE + Remove-GitHubRepositoryBranch -Name develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub -Force + + Removes the 'develop' branch from the specified repository without prompting for confirmation. #> [CmdletBinding( SupportsShouldProcess, From 9a499447f4fbaa4684fe9d72196c89522e016035 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Wed, 10 Jun 2020 21:36:21 +0100 Subject: [PATCH 13/21] Update force condition --- GitHubBranches.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index 2dec71f1..ebb86b40 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -428,7 +428,7 @@ function Remove-GitHubRepositoryBranch $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/heads/$Name" - if ($Force -and -not $Confirm) + if ($Force -and (-not $Confirm)) { $ConfirmPreference = 'None' } From efebcfbce734f202fc646064c5ad03d3b3b4c525 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Thu, 18 Jun 2020 07:57:46 +0100 Subject: [PATCH 14/21] Remove duplicate AfterAll block in tests. --- Tests/GitHubBranches.tests.ps1 | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Tests/GitHubBranches.tests.ps1 b/Tests/GitHubBranches.tests.ps1 index f1af0172..8cdc0f1e 100644 --- a/Tests/GitHubBranches.tests.ps1 +++ b/Tests/GitHubBranches.tests.ps1 @@ -105,13 +105,6 @@ try $branchAgain.RepositoryUrl | Should -Be $repo.RepositoryUrl $branchAgain.BranchName | Should -Be $branchAgain.name } - - AfterAll -ScriptBlock { - if ($repo) - { - Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false - } - } } } From 884290e7aaff09c83e04cd2e54e62edca7ae006b Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Thu, 18 Jun 2020 18:16:37 +0100 Subject: [PATCH 15/21] Add pipelining support --- GitHubBranches.ps1 | 100 ++++++++++++++++++++++++++------- Tests/GitHubBranches.tests.ps1 | 46 +++++++++------ 2 files changed, 109 insertions(+), 37 deletions(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index ebb86b40..b848e013 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -154,7 +154,7 @@ filter Get-GitHubRepositoryBranch return (Invoke-GHRestMethodMultipleResult @params | Add-GitHubBranchAdditionalProperties) } -function New-GitHubRepositoryBranch +filter New-GitHubRepositoryBranch { <# .SYNOPSIS @@ -195,26 +195,45 @@ function New-GitHubRepositoryBranch If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS - None + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Release + GitHub.Repository .OUTPUTS - PSCustomObject + GitHub.Branch .EXAMPLE - New-GitHubRepositoryBranch -Name New-Branch1 -OwnerName Microsoft -RepositoryName PowerShellForGitHub + New-GitHubRepositoryBranch -BranchName New-Branch1 -OwnerName Microsoft -RepositoryName PowerShellForGitHub Creates a new branch in the specified repository from the master branch. .EXAMPLE - New-GitHubRepositoryBranch -Name New-Branch2 -Uri 'https://github.com/PowerShell/PowerShellForGitHub' -OriginBranchName 'New-Branch1' + New-GitHubRepositoryBranch -BranchName New-Branch2 -Uri 'https://github.com/PowerShell/PowerShellForGitHub' -OriginBranchName 'New-Branch1' Creates a new branch in the specified repository from the specified origin branch. + + .EXAMPLE + $repo = Get-GithubRepository -Uri https://github.com/You/YourRepo + $repo | New-GitHubRepositoryBranch -BranchName 'NewBranch' + + You can also pipe in a repo that was returned from a previous command. #> [CmdletBinding( SupportsShouldProcess, DefaultParameterSetName = 'Elements', PositionalBinding = $false )] + [OutputType({$script:GitHubBranchTypeName})] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] @@ -225,13 +244,16 @@ function New-GitHubRepositoryBranch param( [Parameter( Mandatory, + ValueFromPipeline, Position = 1)] - [string] $Name, + [string] $BranchName, [Parameter( Mandatory, + ValueFromPipelineByPropertyName, Position = 2, ParameterSetName = 'Uri')] + [Alias('RepositoryUrl')] [string] $Uri, [Parameter(ParameterSetName = 'Elements')] @@ -263,7 +285,7 @@ function New-GitHubRepositoryBranch $getGitHubRepositoryBranchParms = @{ OwnerName = $OwnerName RepositoryName = $RepositoryName - Name = $OriginBranchName + BranchName = $OriginBranchName Whatif = $false Confirm = $false } @@ -304,7 +326,7 @@ function New-GitHubRepositoryBranch $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs" $hashBody = @{ - ref = "refs/heads/$Name" + ref = "refs/heads/$BranchName" sha = $originBranch.commit.sha } @@ -312,17 +334,17 @@ function New-GitHubRepositoryBranch 'UriFragment' = $uriFragment 'Body' = (ConvertTo-Json -InputObject $hashBody) 'Method' = 'Post' - 'Description' = "Creating branch $Name for $RepositoryName" + 'Description' = "Creating branch $BranchName for $RepositoryName" 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } - return Invoke-GHRestMethod @params + return (Invoke-GHRestMethod @params | Add-GitHubBranchAdditionalProperties) } -function Remove-GitHubRepositoryBranch +filter Remove-GitHubRepositoryBranch { <# .SYNOPSIS @@ -363,20 +385,38 @@ function Remove-GitHubRepositoryBranch If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS - None + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Release + GitHub.Repository .OUTPUTS None .EXAMPLE - Remove-GitHubRepositoryBranch -Name develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub + Remove-GitHubRepositoryBranch -BranchName develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub Removes the 'develop' branch from the specified repository. .EXAMPLE - Remove-GitHubRepositoryBranch -Name develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub -Force + Remove-GitHubRepositoryBranch -BranchName develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub -Force Removes the 'develop' branch from the specified repository without prompting for confirmation. + + .EXAMPLE + $branch = Get-GitHubRepositoryBranch -Uri https://github.com/You/YourRepo -BranchName BranchToDelete + $branch | Remove-GitHubRepositoryBranch -Force + + You can also pipe in a repo that was returned from a previous command. #> [CmdletBinding( SupportsShouldProcess, @@ -395,13 +435,16 @@ function Remove-GitHubRepositoryBranch param( [Parameter( Mandatory, + ValueFromPipelineByPropertyName, Position = 1)] - [string] $Name, + [string] $BranchName, [Parameter( Mandatory, + ValueFromPipelineByPropertyName, Position = 2, ParameterSetName = 'Uri')] + [Alias('RepositoryUrl')] [string] $Uri, [Parameter(ParameterSetName = 'Elements')] @@ -426,21 +469,21 @@ function Remove-GitHubRepositoryBranch 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } - $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/heads/$Name" + $uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/heads/$BranchName" if ($Force -and (-not $Confirm)) { $ConfirmPreference = 'None' } - if ($PSCmdlet.ShouldProcess($Name, "Remove Repository Branch")) + if ($PSCmdlet.ShouldProcess($BranchName, "Remove Repository Branch")) { Write-InvocationLog $params = @{ 'UriFragment' = $uriFragment 'Method' = 'Delete' - 'Description' = "Deleting branch $Name from $RepositoryName" + 'Description' = "Deleting branch $BranchName from $RepositoryName" 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties @@ -490,11 +533,28 @@ filter Add-GitHubBranchAdditionalProperties if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport)) { - $elements = Split-GitHubUri -Uri $item.commit.url + if ($null -ne $item.url) + { + $elements = Split-GitHubUri -Uri $item.url + } + else + { + $elements = Split-GitHubUri -Uri $item.commit.url + } $repositoryUrl = Join-GitHubUri @elements + Add-Member -InputObject $item -Name 'RepositoryUrl' -Value $repositoryUrl -MemberType NoteProperty -Force - Add-Member -InputObject $item -Name 'BranchName' -Value $item.name -MemberType NoteProperty -Force + if ($null -ne $item.name) + { + $branchName = $item.name + } + else + { + $branchName = $item.ref -replace ('refs/heads/', '') + } + + Add-Member -InputObject $item -Name 'BranchName' -Value $branchName -MemberType NoteProperty -Force } Write-Output $item diff --git a/Tests/GitHubBranches.tests.ps1 b/Tests/GitHubBranches.tests.ps1 index 8cdc0f1e..f7a6e632 100644 --- a/Tests/GitHubBranches.tests.ps1 +++ b/Tests/GitHubBranches.tests.ps1 @@ -39,7 +39,7 @@ try $branches.name | Should -Contain $branchName } - It 'Should have the exected type and addititional properties' { + It 'Should have the expected type and addititional properties' { $branches[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Branch' $branches[0].RepositoryUrl | Should -Be $repo.RepositoryUrl $branches[0].BranchName | Should -Be $branches[0].name @@ -57,7 +57,7 @@ try $branches.name | Should -Contain $branchName } - It 'Should have the exected type and addititional properties' { + It 'Should have the expected type and addititional properties' { $branches[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Branch' $branches[0].RepositoryUrl | Should -Be $repo.RepositoryUrl $branches[0].BranchName | Should -Be $branches[0].name @@ -71,7 +71,7 @@ try $branch.name | Should -Be $branchName } - It 'Should have the exected type and addititional properties' { + 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 $branch.name @@ -85,7 +85,7 @@ try $branch.name | Should -Be $branchName } - It 'Should have the exected type and addititional properties' { + 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 $branch.name @@ -100,7 +100,7 @@ try $branchAgain.name | Should -Be $branchName } - It 'Should have the exected type and addititional properties' { + It 'Should have the expected type and addititional properties' { $branchAgain.PSObject.TypeNames[0] | Should -Be 'GitHub.Branch' $branchAgain.RepositoryUrl | Should -Be $repo.RepositoryUrl $branchAgain.BranchName | Should -Be $branchAgain.name @@ -110,7 +110,7 @@ try Describe 'GitHubBranches\New-GitHubRepositoryBranch' { Context 'When creating a new GitHub repository branch' { - BeforeAll -Scriptblock { + BeforeAll { $repoName = [Guid]::NewGuid().Guid $originBranchName = 'master' $newBranchName = 'develop' @@ -123,25 +123,33 @@ try $newGitHubRepositoryBranchParms = @{ OwnerName = $script:ownerName RepositoryName = $repoName - Name = $newBranchName + BranchName = $newBranchName OriginBranchName = $originBranchName } $branch = New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } - It 'Should return an object of the correct type' { - $branch | Should -BeOfType PSCustomObject + It 'Should support pipeline input for the uri parameter' { + { $repo | New-GitHubRepositoryBranch -BranchName $newBranchName -WhatIf } | + Should -Not -Throw + } + + It 'Should support pipeline input for the BranchName parameter' { + { $newBranchName | New-GitHubRepositoryBranch -Uri $repo.html_url -WhatIf } | + Should -Not -Throw } - It 'Should return the correct properties' { - $branch.ref | Should -Be "refs/heads/$newBranchName" + 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 } It 'Should have created the branch' { $getGitHubRepositoryBranchParms = @{ OwnerName = $script:ownerName RepositoryName = $repoName - Name = $newBranchName + BranchName = $newBranchName } { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | Should -Not -Throw @@ -158,7 +166,7 @@ try $newGitHubRepositoryBranchParms = @{ OwnerName = $script:ownerName RepositoryName = $repoName - Name = $newBranchName + BranchName = $newBranchName OriginBranchName = $missingOriginBranchName } { New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } | @@ -171,7 +179,7 @@ try $newGitHubRepositoryBranchParms = @{ OwnerName = $script:ownerName RepositoryName = 'test' - Name = 'test' + BranchName = 'test' OriginBranchName = 'test' } { New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } | @@ -202,17 +210,21 @@ try $newGitHubRepositoryBranchParms = @{ OwnerName = $script:ownerName RepositoryName = $repoName - Name = $newBranchName + BranchName = $newBranchName OriginBranchName = $originBranchName } $branch = New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } + It 'Should support pipeline input for the BranchName and Uri parameters' { + { $branch | Remove-GitHubRepositoryBranch -WhatIf } | Should -Not -Throw + } + It 'Should not throw an exception' { $removeGitHubRepositoryBranchParms = @{ OwnerName = $script:ownerName RepositoryName = $repoName - Name = $newBranchName + BranchName = $newBranchName Confirm = $false } { Remove-GitHubRepositoryBranch @removeGitHubRepositoryBranchParms } | @@ -223,7 +235,7 @@ try $getGitHubRepositoryBranchParms = @{ OwnerName = $script:ownerName RepositoryName = $repoName - Name = $newBranchName + BranchName = $newBranchName } { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | Should -Throw From a1d75142f50a8fd999ed7ae8ccb9f8a737c4ca40 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 21 Jun 2020 09:48:12 +0100 Subject: [PATCH 16/21] Update tests for StrictMode v1.0 --- Tests/GitHubBranches.tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/GitHubBranches.tests.ps1 b/Tests/GitHubBranches.tests.ps1 index f7a6e632..f9c8e0ed 100644 --- a/Tests/GitHubBranches.tests.ps1 +++ b/Tests/GitHubBranches.tests.ps1 @@ -188,7 +188,7 @@ try } AfterAll -ScriptBlock { - if ($repo) + if (Get-Variable -Name repo -ErrorAction SilentlyContinue) { Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false } @@ -242,7 +242,7 @@ try } AfterAll -ScriptBlock { - if ($repo) + if (Get-Variable -Name repo -ErrorAction SilentlyContinue) { Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false } From 65edf9f0d101847b319376d01862ea6d5f8aa967 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sat, 27 Jun 2020 18:11:19 +0100 Subject: [PATCH 17/21] Fix review comments --- GitHubBranches.ps1 | 94 ++++++++++++++++++---------------- Tests/GitHubBranches.tests.ps1 | 9 ++++ 2 files changed, 58 insertions(+), 45 deletions(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index b848e013..eaf3c653 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -178,12 +178,12 @@ filter New-GitHubRepositoryBranch The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER Name - Name of the branch to be created. - - .PARAMETER OriginBranchName + .PARAMETER BranchName The name of the origin branch to create the new branch from. + .PARAMETER TargetBranchName + Name of the branch to be created. + .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. @@ -213,18 +213,18 @@ filter New-GitHubRepositoryBranch GitHub.Branch .EXAMPLE - New-GitHubRepositoryBranch -BranchName New-Branch1 -OwnerName Microsoft -RepositoryName PowerShellForGitHub + New-GitHubRepositoryBranch -OwnerName microsoft -RepositoryName PowerShellForGitHub -TargetBranchName new-branch Creates a new branch in the specified repository from the master branch. .EXAMPLE - New-GitHubRepositoryBranch -BranchName New-Branch2 -Uri 'https://github.com/PowerShell/PowerShellForGitHub' -OriginBranchName 'New-Branch1' + New-GitHubRepositoryBranch -Uri 'https://github.com/microsoft/PowerShellForGitHub' -BranchName develop -TargetBranchName new-branch - Creates a new branch in the specified repository from the specified origin branch. + Creates a new branch in the specified repository from the 'develop' origin branch. .EXAMPLE $repo = Get-GithubRepository -Uri https://github.com/You/YourRepo - $repo | New-GitHubRepositoryBranch -BranchName 'NewBranch' + $repo | New-GitHubRepositoryBranch -TargetBranchName new-branch You can also pipe in a repo that was returned from a previous command. #> @@ -234,35 +234,35 @@ filter New-GitHubRepositoryBranch PositionalBinding = $false )] [OutputType({$script:GitHubBranchTypeName})] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", - Justification = "Methods called within here make use of PSShouldProcess, and the switch is - passed on to them inherently.")] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", - Justification = "One or more parameters (like NoStatus) are only referenced by helper - methods which get access to it from the stack via Get-Variable -Scope 1.")] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '', + Justification = 'Methods called within here make use of PSShouldProcess, and the switch is + passed on to them inherently.')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '', + Justification = 'One or more parameters (like NoStatus) are only referenced by helper + methods which get access to it from the stack via Get-Variable -Scope 1.')] [Alias('New-GitHubBranch')] param( - [Parameter( - Mandatory, - ValueFromPipeline, - Position = 1)] - [string] $BranchName, + [Parameter(ParameterSetName = 'Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName = 'Elements')] + [string] $RepositoryName, [Parameter( Mandatory, ValueFromPipelineByPropertyName, - Position = 2, + Position = 1, ParameterSetName = 'Uri')] [Alias('RepositoryUrl')] [string] $Uri, - [Parameter(ParameterSetName = 'Elements')] - [string] $OwnerName, - - [Parameter(ParameterSetName = 'Elements')] - [string] $RepositoryName, + [string] $BranchName = 'master', - [string] $OriginBranchName = 'master', + [Parameter( + Mandatory, + ValueFromPipeline, + Position = 2)] + [string] $TargetBranchName, [string] $AccessToken, @@ -285,7 +285,7 @@ filter New-GitHubRepositoryBranch $getGitHubRepositoryBranchParms = @{ OwnerName = $OwnerName RepositoryName = $RepositoryName - BranchName = $OriginBranchName + BranchName = $BranchName Whatif = $false Confirm = $false } @@ -297,6 +297,9 @@ filter New-GitHubRepositoryBranch { $getGitHubRepositoryBranchParms['NoStatus'] = $NoStatus } + + Write-Log -Level Verbose "Getting $TargetBranchName branch for sha reference" + $originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } catch @@ -309,24 +312,25 @@ filter New-GitHubRepositoryBranch if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException] -and ($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Branch not found') { - $throwObject = "Origin branch $OriginBranchName not found" + $throwObject = "Origin branch $BranchName not found" } } else { if ($_.Exception.Message -like '*Not Found*') { - $throwObject = "Origin branch $OriginBranchName 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/$BranchName" + ref = "refs/heads/$TargetBranchName" sha = $originBranch.commit.sha } @@ -334,7 +338,7 @@ filter New-GitHubRepositoryBranch 'UriFragment' = $uriFragment 'Body' = (ConvertTo-Json -InputObject $hashBody) 'Method' = 'Post' - 'Description' = "Creating branch $BranchName for $RepositoryName" + 'Description' = "Creating branch $TargetBranchName for $RepositoryName" 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties @@ -368,7 +372,7 @@ filter Remove-GitHubRepositoryBranch The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER Name + .PARAMETER BranchName Name of the branch to be removed. .PARAMETER Force @@ -403,12 +407,12 @@ filter Remove-GitHubRepositoryBranch None .EXAMPLE - Remove-GitHubRepositoryBranch -BranchName develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub + Remove-GitHubRepositoryBranch -OwnerName microsoft -RepositoryName PowerShellForGitHub -BranchName develop Removes the 'develop' branch from the specified repository. .EXAMPLE - Remove-GitHubRepositoryBranch -BranchName develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub -Force + Remove-GitHubRepositoryBranch -OwnerName microsoft -RepositoryName PowerShellForGitHub -BranchName develop -Force Removes the 'develop' branch from the specified repository without prompting for confirmation. @@ -433,25 +437,25 @@ filter Remove-GitHubRepositoryBranch [Alias('Delete-GitHubRepositoryBranch')] [Alias('Delete-GitHubBranch')] param( - [Parameter( - Mandatory, - ValueFromPipelineByPropertyName, - Position = 1)] - [string] $BranchName, + [Parameter(ParameterSetName = 'Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName = 'Elements')] + [string] $RepositoryName, [Parameter( Mandatory, ValueFromPipelineByPropertyName, - Position = 2, + Position = 1, ParameterSetName = 'Uri')] [Alias('RepositoryUrl')] [string] $Uri, - [Parameter(ParameterSetName = 'Elements')] - [string] $OwnerName, - - [Parameter(ParameterSetName = 'Elements')] - [string] $RepositoryName, + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + Position = 2)] + [string] $BranchName, [switch] $Force, diff --git a/Tests/GitHubBranches.tests.ps1 b/Tests/GitHubBranches.tests.ps1 index f9c8e0ed..686e8ed5 100644 --- a/Tests/GitHubBranches.tests.ps1 +++ b/Tests/GitHubBranches.tests.ps1 @@ -118,6 +118,7 @@ try RepositoryName = $repoName AutoInit = $true } + $repo = New-GitHubRepository @newGitHubRepositoryParms $newGitHubRepositoryBranchParms = @{ @@ -126,6 +127,7 @@ try BranchName = $newBranchName OriginBranchName = $originBranchName } + $branch = New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } @@ -151,6 +153,7 @@ try RepositoryName = $repoName BranchName = $newBranchName } + { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | Should -Not -Throw } @@ -169,6 +172,7 @@ try BranchName = $newBranchName OriginBranchName = $missingOriginBranchName } + { New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } | Should -Throw $errorMessage } @@ -182,6 +186,7 @@ try BranchName = 'test' OriginBranchName = 'test' } + { New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } | Should -Throw 'Not Found' } @@ -205,6 +210,7 @@ try RepositoryName = $repoName AutoInit = $true } + $repo = New-GitHubRepository @newGitHubRepositoryParms $newGitHubRepositoryBranchParms = @{ @@ -213,6 +219,7 @@ try BranchName = $newBranchName OriginBranchName = $originBranchName } + $branch = New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } @@ -227,6 +234,7 @@ try BranchName = $newBranchName Confirm = $false } + { Remove-GitHubRepositoryBranch @removeGitHubRepositoryBranchParms } | Should -Not -Throw } @@ -237,6 +245,7 @@ try RepositoryName = $repoName BranchName = $newBranchName } + { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | Should -Throw } From 67d8be1f48885d8a67c5a4d8e68a4d64f35fa99e Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sat, 27 Jun 2020 18:13:03 +0100 Subject: [PATCH 18/21] Update Usage.md --- USAGE.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/USAGE.md b/USAGE.md index 25e6fd1b..bdd04488 100644 --- a/USAGE.md +++ b/USAGE.md @@ -441,13 +441,13 @@ Get-GitHubUser #### Adding a new Branch to a Repository ```powershell -New-GitHubRepositoryBranch -Name develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub +New-GitHubRepositoryBranch -OwnerName microsoft -RepositoryName PowerShellForGitHub -Name develop ``` #### Removing a Branch from a Repository ```powershell -Remove-GitHubRepositoryBranch -Name develop -OwnerName Microsoft -RepositoryName PowerShellForGitHub +Remove-GitHubRepositoryBranch -OwnerName microsoft -RepositoryName PowerShellForGitHub -Name develop ``` ---------- From b56f064a739102cc99d8512f6ad78c2c72290ad8 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Mon, 6 Jul 2020 17:49:53 +0100 Subject: [PATCH 19/21] Fixed review comments --- GitHubBranches.ps1 | 5 +- Tests/GitHubBranches.tests.ps1 | 213 ++++++++++++++++++++++----------- 2 files changed, 145 insertions(+), 73 deletions(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index eaf3c653..c115f1eb 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -299,7 +299,6 @@ filter New-GitHubRepositoryBranch } Write-Log -Level Verbose "Getting $TargetBranchName branch for sha reference" - $originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } catch @@ -412,7 +411,7 @@ filter Remove-GitHubRepositoryBranch Removes the 'develop' branch from the specified repository. .EXAMPLE - Remove-GitHubRepositoryBranch -OwnerName microsoft -RepositoryName PowerShellForGitHub -BranchName develop -Force + Remove-GitHubRepositoryBranch -OwnerName microsoft -RepositoryName PowerShellForGitHub -BranchName develop -Force Removes the 'develop' branch from the specified repository without prompting for confirmation. @@ -426,7 +425,7 @@ filter Remove-GitHubRepositoryBranch SupportsShouldProcess, DefaultParameterSetName = 'Elements', PositionalBinding = $false, - ConfirmImpact="High")] + ConfirmImpact = 'High')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] diff --git a/Tests/GitHubBranches.tests.ps1 b/Tests/GitHubBranches.tests.ps1 index 686e8ed5..2d52a19b 100644 --- a/Tests/GitHubBranches.tests.ps1 +++ b/Tests/GitHubBranches.tests.ps1 @@ -109,53 +109,97 @@ try } Describe 'GitHubBranches\New-GitHubRepositoryBranch' { + BeforeAll { + $repoName = [Guid]::NewGuid().Guid + $originBranchName = 'master' + $newGitHubRepositoryParms = @{ + RepositoryName = $repoName + AutoInit = $true + } + + $repo = New-GitHubRepository @newGitHubRepositoryParms + } + Context 'When creating a new GitHub repository branch' { - BeforeAll { - $repoName = [Guid]::NewGuid().Guid - $originBranchName = 'master' - $newBranchName = 'develop' - $newGitHubRepositoryParms = @{ - RepositoryName = $repoName - AutoInit = $true - } + Context 'When using non-pipelined parameters' { + BeforeAll { + $newBranchName = 'develop1' + $newGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + BranchName = $originBranchName + TargetBranchName = $newBranchName + } - $repo = New-GitHubRepository @newGitHubRepositoryParms + $branch = New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms + } - $newGitHubRepositoryBranchParms = @{ - OwnerName = $script:ownerName - RepositoryName = $repoName - BranchName = $newBranchName - OriginBranchName = $originBranchName + 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 = New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms - } + It 'Should have created the branch' { + $getGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + BranchName = $newBranchName + } - It 'Should support pipeline input for the uri parameter' { - { $repo | New-GitHubRepositoryBranch -BranchName $newBranchName -WhatIf } | - Should -Not -Throw + { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | + Should -Not -Throw + } } - It 'Should support pipeline input for the BranchName parameter' { - { $newBranchName | New-GitHubRepositoryBranch -Uri $repo.html_url -WhatIf } | - Should -Not -Throw - } + Context 'When using pipelined parameters' { + Context 'When providing pipeline input for the "Uri" parameter' { + BeforeAll { + $newBranchName = 'develop2' + $branch = $repo | New-GitHubRepositoryBranch -TargetBranchName $newBranchName + } - 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 - } + 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 + } - It 'Should have created the branch' { - $getGitHubRepositoryBranchParms = @{ - OwnerName = $script:ownerName - RepositoryName = $repoName - BranchName = $newBranchName + It 'Should have created the branch' { + $getGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + BranchName = $newBranchName + } + + { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | + Should -Not -Throw + } } - { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | - Should -Not -Throw + Context 'When providing pipeline input for the "TargetBranchName" parameter' { + BeforeAll { + $newBranchName = 'develop3' + $branch = $newBranchName | New-GitHubRepositoryBranch -Uri $repo.html_url + } + + 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 + } + + It 'Should have created the branch' { + $getGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + BranchName = $newBranchName + } + + { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | + Should -Not -Throw + } + } } Context 'When the origin branch cannot be found' { @@ -169,8 +213,8 @@ try $newGitHubRepositoryBranchParms = @{ OwnerName = $script:ownerName RepositoryName = $repoName - BranchName = $newBranchName - OriginBranchName = $missingOriginBranchName + BranchName = $missingOriginBranchName + TargetBranchName = $newBranchName } { New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } | @@ -184,19 +228,19 @@ try OwnerName = $script:ownerName RepositoryName = 'test' BranchName = 'test' - OriginBranchName = 'test' + TargetBranchName = 'test' } { New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } | Should -Throw 'Not Found' } } + } - AfterAll -ScriptBlock { - if (Get-Variable -Name repo -ErrorAction SilentlyContinue) - { - Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false - } + AfterAll -ScriptBlock { + if (Get-Variable -Name repo -ErrorAction SilentlyContinue) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false } } } @@ -205,49 +249,78 @@ try BeforeAll -Scriptblock { $repoName = [Guid]::NewGuid().Guid $originBranchName = 'master' - $newBranchName = 'develop' $newGitHubRepositoryParms = @{ RepositoryName = $repoName AutoInit = $true } $repo = New-GitHubRepository @newGitHubRepositoryParms + } - $newGitHubRepositoryBranchParms = @{ - OwnerName = $script:ownerName - RepositoryName = $repoName - BranchName = $newBranchName - OriginBranchName = $originBranchName - } + Context 'When using non-pipelined parameters' { + BeforeAll { + $newBranchName = 'develop1' + $newGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + BranchName = $originBranchName + TargetBranchName = $newBranchName + } - $branch = New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms - } + $branch = New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms + } - It 'Should support pipeline input for the BranchName and Uri parameters' { - { $branch | Remove-GitHubRepositoryBranch -WhatIf } | Should -Not -Throw - } + It 'Should not throw an exception' { + $removeGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + BranchName = $newBranchName + Confirm = $false + } - It 'Should not throw an exception' { - $removeGitHubRepositoryBranchParms = @{ - OwnerName = $script:ownerName - RepositoryName = $repoName - BranchName = $newBranchName - Confirm = $false + { Remove-GitHubRepositoryBranch @removeGitHubRepositoryBranchParms } | + Should -Not -Throw } - { Remove-GitHubRepositoryBranch @removeGitHubRepositoryBranchParms } | - Should -Not -Throw + It 'Should have removed the branch' { + $getGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + BranchName = $newBranchName + } + + { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | + Should -Throw + } } - It 'Should have removed the branch' { - $getGitHubRepositoryBranchParms = @{ - OwnerName = $script:ownerName - RepositoryName = $repoName - BranchName = $newBranchName + Context 'When using pipelined parameters' { + BeforeAll { + $newBranchName = 'develop2' + $newGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + BranchName = $originBranchName + TargetBranchName = $newBranchName + } + + $branch = New-GitHubRepositoryBranch @newGitHubRepositoryBranchParms } - { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | - Should -Throw + It 'Should not throw an exception' { + { $branch | Remove-GitHubRepositoryBranch -Force } | Should -Not -Throw + } + + It 'Should have removed the branch' { + $getGitHubRepositoryBranchParms = @{ + OwnerName = $script:ownerName + RepositoryName = $repoName + BranchName = $newBranchName + } + + { Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } | + Should -Throw + } } AfterAll -ScriptBlock { From 7df2c4965229b26c130b4fe03d91638b31f44aea Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Wed, 8 Jul 2020 17:32:48 +0100 Subject: [PATCH 20/21] Fix review comments --- GitHubBranches.ps1 | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index c115f1eb..a8b78328 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -280,6 +280,8 @@ filter New-GitHubRepositoryBranch 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } + $originBranch = $null + try { $getGitHubRepositoryBranchParms = @{ @@ -298,7 +300,7 @@ filter New-GitHubRepositoryBranch $getGitHubRepositoryBranchParms['NoStatus'] = $NoStatus } - Write-Log -Level Verbose "Getting $TargetBranchName branch for sha reference" + Write-Log -Level Verbose "Getting $BranchName branch for sha reference" $originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms } catch @@ -536,11 +538,8 @@ filter Add-GitHubBranchAdditionalProperties if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport)) { - if ($null -ne $item.url) - { - $elements = Split-GitHubUri -Uri $item.url - } - else + $elements = Split-GitHubUri -Uri $item.url + if ($null -eq $item.url) { $elements = Split-GitHubUri -Uri $item.commit.url } @@ -548,11 +547,8 @@ filter Add-GitHubBranchAdditionalProperties Add-Member -InputObject $item -Name 'RepositoryUrl' -Value $repositoryUrl -MemberType NoteProperty -Force - if ($null -ne $item.name) - { - $branchName = $item.name - } - else + $branchName = $item.name + if ($null -eq $branchName) { $branchName = $item.ref -replace ('refs/heads/', '') } From 172bde9edfce0b108c4496970177e30f1653c4ba Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Thu, 16 Jul 2020 17:19:37 +0100 Subject: [PATCH 21/21] Fix review comments --- GitHubBranches.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/GitHubBranches.ps1 b/GitHubBranches.ps1 index a8b78328..2e52cf40 100644 --- a/GitHubBranches.ps1 +++ b/GitHubBranches.ps1 @@ -538,8 +538,11 @@ filter Add-GitHubBranchAdditionalProperties if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport)) { - $elements = Split-GitHubUri -Uri $item.url - if ($null -eq $item.url) + if ($null -ne $item.url) + { + $elements = Split-GitHubUri -Uri $item.url + } + else { $elements = Split-GitHubUri -Uri $item.commit.url }