From 707108960089279e4dd6c404e53cdf065434e2f6 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Fri, 29 May 2020 18:53:20 +0100 Subject: [PATCH 01/18] 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 3504bf7c..a829e5bc 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -115,6 +115,7 @@ 'New-GitHubPullRequest', 'New-GitHubRepository', 'New-GitHubRepositoryFromTemplate', + 'New-GitHubRepositoryBranch', 'New-GitHubRepositoryFork', 'Remove-GitHubAssignee', 'Remove-GitHubIssueComment', @@ -125,6 +126,7 @@ 'Remove-GitHubProjectCard', 'Remove-GitHubProjectColumn', 'Remove-GitHubRepository', + 'Remove-GitHubRepositoryBranch' 'Rename-GitHubRepository', 'Reset-GitHubConfiguration', 'Restore-GitHubConfiguration', From 113bab13e9b6c18c25d702e3a5630022e60f0997 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Tue, 2 Jun 2020 17:54:05 +0100 Subject: [PATCH 02/18] 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 a829e5bc..0f11ca55 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -154,6 +154,7 @@ ) AliasesToExport = @( + 'Delete-GitHubBranch', 'Delete-GitHubComment', 'Delete-GitHubIssueComment', 'Delete-GitHubLabel', @@ -162,9 +163,12 @@ 'Delete-GitHubProjectCard', 'Delete-GitHubProjectColumn' 'Delete-GitHubRepository', + 'Delete-GitHubRepositoryBranch', 'Get-GitHubBranch', 'Get-GitHubComment', + 'New-GitHubBranch', 'New-GitHubComment', + 'Remove-GitHubBranch' 'Remove-GitHubComment', 'Set-GitHubComment', 'Transfer-GitHubRepositoryOwnership' From ee023f838222ee0842d5f5e3af7470a54ad7b0d1 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Fri, 5 Jun 2020 20:16:17 +0100 Subject: [PATCH 03/18] 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 9ddd6ade3da7004651c139f0c8f232fb9abf3f25 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Fri, 5 Jun 2020 20:16:32 +0100 Subject: [PATCH 04/18] 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 5c0c55c86699f4ab18f7a94f2bb846e9e175efcb Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 7 Jun 2020 13:38:19 +0100 Subject: [PATCH 05/18] 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 005903615372058bab721c0a1407e2b6114adcff Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 7 Jun 2020 13:42:44 +0100 Subject: [PATCH 06/18] 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 1fcda4871ff3887ac61e05c6b64b49cb2116a236 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 7 Jun 2020 15:16:07 +0100 Subject: [PATCH 07/18] 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 7abab896943ea2e35234c5f17606239135a51519 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 7 Jun 2020 15:44:08 +0100 Subject: [PATCH 08/18] 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 23a3839851f4156ff8c717c2c6d72d91b75d7523 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 7 Jun 2020 22:33:57 +0100 Subject: [PATCH 09/18] 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 32dec0c0e8a9be40eaf839062558acd3f0fef9b4 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Wed, 10 Jun 2020 21:29:40 +0100 Subject: [PATCH 10/18] Update USAGE.md --- USAGE.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 2b275c61..8dd37c09 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) @@ -429,6 +432,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 @@ -456,7 +474,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 7a4ba220e4f2de370c6c1712407bf5cfd28c8779 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Wed, 10 Jun 2020 21:35:22 +0100 Subject: [PATCH 11/18] 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 8ae5dbf4bea60366edd095206b0875b6af791717 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Wed, 10 Jun 2020 21:36:00 +0100 Subject: [PATCH 12/18] 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 e346fcc17f834ef942312f03756fcb42a306f398 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Wed, 10 Jun 2020 21:36:21 +0100 Subject: [PATCH 13/18] 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 8f5404e3a583da557b9bfc5f05b13695b478136b Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Thu, 18 Jun 2020 07:57:46 +0100 Subject: [PATCH 14/18] 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 b3e50a7dadfd890d99cf5f6c372af9bb323a3285 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Thu, 18 Jun 2020 18:16:37 +0100 Subject: [PATCH 15/18] 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 0b3d1bd40b7fa86c236c4e83f6fe1adfc85281e0 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 21 Jun 2020 09:48:12 +0100 Subject: [PATCH 16/18] 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 e2f0b0e54e9747a61cccdb6da2e4f5e0d35cc3a9 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sat, 27 Jun 2020 18:11:19 +0100 Subject: [PATCH 17/18] 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 d89afdd924ed3d1702ecc01977b52025e3538b8c Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sat, 27 Jun 2020 18:13:03 +0100 Subject: [PATCH 18/18] Update Usage.md --- USAGE.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/USAGE.md b/USAGE.md index 8dd37c09..7b652e33 100644 --- a/USAGE.md +++ b/USAGE.md @@ -438,13 +438,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 ``` ----------