From 4937cdb1dfa08f852bea0ac24199f17af4222228 Mon Sep 17 00:00:00 2001 From: S Raghav Date: Sun, 14 Jul 2019 00:01:14 +0530 Subject: [PATCH] Address comments and add option to get all references --- GitHubReferences.ps1 | 46 +++++++++++++++-------- PowerShellForGitHub.psd1 | 6 +-- Tests/GitHubReferences.Tests.ps1 | 63 +++++++++++++++++++++++--------- 3 files changed, 79 insertions(+), 36 deletions(-) diff --git a/GitHubReferences.ps1 b/GitHubReferences.ps1 index 84e618b0..36c79fff 100644 --- a/GitHubReferences.ps1 +++ b/GitHubReferences.ps1 @@ -25,7 +25,8 @@ function Get-GitHubReference them individually. .PARAMETER Reference - Name of the reference, for example: "heads/" for branches and "tags/" for tags + Name of the reference, for example: "heads/" for branches and "tags/" for tags. + Gets all the references if not provided .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the @@ -36,7 +37,7 @@ function Get-GitHubReference 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 git reference in the given repository @@ -47,7 +48,8 @@ function Get-GitHubReference [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("PSShouldProcess", "", + Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] param( [Parameter(ParameterSetName='Elements')] [string] $OwnerName, @@ -60,7 +62,6 @@ function Get-GitHubReference ParameterSetName='Uri')] [string] $Uri, - [Parameter(Mandatory)] [string] $Reference, [string] $AccessToken, @@ -77,6 +78,7 @@ function Get-GitHubReference $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'ProvidedReference' = $PSBoundParameters.ContainsKey('Reference') } if ($OwnerName -xor $RepositoryName) @@ -85,14 +87,21 @@ function Get-GitHubReference Write-Log -Message $message -Level Error throw $message } - $uriFragment = "/repos/$OwnerName/$RepositoryName/git/refs/$Reference" - $description = "Getting Reference $Reference for $RepositoryName" + + if (-not [String]::IsNullOrEmpty($RepositoryName)) + { + $uriFragment = "/repos/$OwnerName/$RepositoryName/git/refs" + $description = "Getting all references for $RepositoryName" + if ($PSBoundParameters.ContainsKey('Reference')) + { + $uriFragment = $uriFragment + "/$Reference" + $description = "Getting Reference $Reference for $RepositoryName" + } + } $params = @{ 'UriFragment' = $uriFragment - 'Method' = 'Get' 'Description' = $description - 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties @@ -101,12 +110,18 @@ function Get-GitHubReference try { - return Invoke-GHRestMethod @params + return Invoke-GHRestMethodMultipleResult @params } catch { - Write-InteractiveHost "No reference named $Reference exists in repository $RepositoryName" -NoNewline -f Red - Write-Log -Level Error "No reference named $Reference exists in repository $RepositoryName" + if ($PSBoundParameters.ContainsKey('Reference')) + { + Write-InteractiveHost "No reference named $Reference exists in repository $RepositoryName" -NoNewline -f Red + Write-Log -Level Error "No reference named $Reference exists in repository $RepositoryName" + return $null + } + Write-InteractiveHost "No references exist in repository $RepositoryName" -NoNewline -f Red + Write-Log -Level Error "No references exist in repository $RepositoryName" return $null } @@ -136,7 +151,7 @@ function New-GitHubReference them individually. .PARAMETER Reference - The name of the fully qualified reference to be created (eg: refs/heads/master) + The name of the fully qualified reference to be created (eg: heads/master) .PARAMETER Sha The SHA1 value for the reference to be created @@ -156,7 +171,7 @@ function New-GitHubReference Details of the git reference created. Throws an Exception if the reference already exists .EXAMPLE - -GitHubReference -OwnerName Powershell -RepositoryName PowerShellForGitHub -Reference refs/heads/master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd + New-GitHubReference -OwnerName Powershell -RepositoryName PowerShellForGitHub -Reference heads/master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd #> [CmdletBinding( SupportsShouldProcess, @@ -207,7 +222,7 @@ function New-GitHubReference $description = "Creating Reference $Reference for $RepositoryName from SHA $Sha" $hashBody = @{ - 'ref' = $Reference + 'ref' = "refs/" + $Reference 'sha' = $Sha } @@ -215,8 +230,7 @@ function New-GitHubReference 'UriFragment' = $uriFragment 'Method' = 'Post' 'Body' = (ConvertTo-Json -InputObject $hashBody) - 'Description' = $description - 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'Description' = $description 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 0a26438d..f1bf21d2 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -67,6 +67,7 @@ 'Get-GitHubPathTraffic', 'Get-GitHubPullRequest', 'Get-GitHubRateLimit', + 'Get-GitHubReference', 'Get-GitHubReferrerTraffic', 'Get-GitHubRepository', 'Get-GitHubRepositoryBranch', @@ -92,6 +93,7 @@ 'New-GitHubIssue', 'New-GitHubLabel', 'New-GitHubMilestone', + 'New-GithubReference', 'New-GitHubRepository', 'New-GitHubRepositoryFork', 'Remove-GithubAssignee', @@ -116,9 +118,7 @@ 'Update-GitHubCurrentUser', 'Update-GitHubIssue', 'Update-GitHubLabel', - 'Update-GitHubRepository', - 'Get-GitHubReference', - 'New-GithubReference' + 'Update-GitHubRepository' ) AliasesToExport = @( diff --git a/Tests/GitHubReferences.Tests.ps1 b/Tests/GitHubReferences.Tests.ps1 index 1b0dfd76..f5ade1a0 100644 --- a/Tests/GitHubReferences.Tests.ps1 +++ b/Tests/GitHubReferences.Tests.ps1 @@ -84,40 +84,40 @@ try Describe 'Create a new reference(branch) in repository' { $repositoryName = [Guid]::NewGuid() $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit - $existingref = @(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master") + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master" $sha = $existingref.object.sha Context 'On creating a valid reference in a new repository from a given SHA' { - $refName = "refs/heads/" + [Guid]::NewGuid().ToString() - $result = @(New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refName -Sha $sha) + $refName = "heads/" + [Guid]::NewGuid().ToString() + $result = New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refName -Sha $sha It 'Should successfully create the reference' { - $result.ref | Should Be $refName + $result.ref | Should Be "refs/$refName" } } Context 'On creating a valid reference in a new repository (specified by Uri) from a given SHA' { - $refName = "refs/heads/" + [Guid]::NewGuid().ToString() - $result = @(New-GitHubReference -Uri $repo.svn_url -Reference $refName -Sha $sha) + $refName = "heads/" + [Guid]::NewGuid().ToString() + $result = New-GitHubReference -Uri $repo.svn_url -Reference $refName -Sha $sha It 'Should successfully create the reference' { - $result.ref | Should Be $refName + $result.ref | Should Be "refs/$refName" } } Context 'On creating an existing reference in a new repository from a given SHA' { - $refName = "refs/heads/master" + $refName = "heads/master" It 'Should throw an Exception' { - { @(New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refName -Sha $sha) } | Should Throw + { New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refName -Sha $sha } | Should Throw } } Context 'On creating an existing reference in a new repository (specified by Uri) from a given SHA' { - $refName = "refs/heads/master" + $refName = "heads/master" It 'Should throw an exception' { - { @(New-GitHubReference -Uri $repo.svn_url -Reference $refName -Sha $sha) } | Should Throw + { New-GitHubReference -Uri $repo.svn_url -Reference $refName -Sha $sha } | Should Throw } } @@ -127,17 +127,18 @@ try Describe 'Getting a reference(branch) from repository' { $repositoryName = [Guid]::NewGuid() $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $refName = "refs/heads/master" Context 'On getting a valid reference from a new repository' { - $reference = @(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master") + $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master" It 'Should return details of the reference' { - $reference.ref | Should be "refs/heads/master" + $reference.ref | Should be $refName } } Context 'On getting an invalid reference from a new repository' { - $reference = @(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/someRandomRef") + $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/someRandomRef" It 'Should not return any details' { $reference | Should be $null @@ -145,20 +146,48 @@ try } Context 'On getting a valid reference using Uri from a new repository' { - $reference = @(Get-GitHubReference -Uri $repo.svn_url -Reference "heads/master") + $reference = Get-GitHubReference -Uri $repo.svn_url -Reference "heads/master" It 'Should return details of the reference' { - $reference.ref | Should be "refs/heads/master" + $reference.ref | Should be $refName } } Context 'On getting an invalid reference using Uri from a new repository' { - $reference = @(Get-GitHubReference -Uri $repo.svn_url -Reference "heads/someRandomRef") + $reference = Get-GitHubReference -Uri $repo.svn_url -Reference "heads/someRandomRef" It 'Should not return any details' { $reference | Should be $null } } + + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName + } + + Describe 'Getting all references from repository' { + $repositoryName = [Guid]::NewGuid() + $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit + $existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master" + $sha = $existingref.object.sha + New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/branch1" -Sha $sha + $refNames = @("refs/heads/master", "refs/heads/branch1") + + Context 'On getting all references from a new repository' { + $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName + + It 'Should return all references' { + ($reference.ref | Where-Object {$refNames -Contains $_}).Count | Should be $refNames.Count + } + } + + Context 'On getting all references using Uri from a new repository' { + $reference = Get-GitHubReference -Uri $repo.svn_url + + It 'Should return all references' { + ($reference.ref | Where-Object {$refNames -Contains $_}).Count | Should be $refNames.Count + } + } + $null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName } }