Skip to content

Commit

Permalink
Address comments and add option to get all references
Browse files Browse the repository at this point in the history
  • Loading branch information
S Raghav committed Jul 14, 2019
1 parent 50585d7 commit 4937cdb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 36 deletions.
46 changes: 30 additions & 16 deletions GitHubReferences.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function Get-GitHubReference
them individually.
.PARAMETER Reference
Name of the reference, for example: "heads/<branch name>" for branches and "tags/<tag name>" for tags
Name of the reference, for example: "heads/<branch name>" for branches and "tags/<tag name>" for tags.
Gets all the references if not provided
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -60,7 +62,6 @@ function Get-GitHubReference
ParameterSetName='Uri')]
[string] $Uri,

[Parameter(Mandatory)]
[string] $Reference,

[string] $AccessToken,
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -207,16 +222,15 @@ function New-GitHubReference
$description = "Creating Reference $Reference for $RepositoryName from SHA $Sha"

$hashBody = @{
'ref' = $Reference
'ref' = "refs/" + $Reference
'sha' = $Sha
}

$params = @{
'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
Expand Down
6 changes: 3 additions & 3 deletions PowerShellForGitHub.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
'Get-GitHubPathTraffic',
'Get-GitHubPullRequest',
'Get-GitHubRateLimit',
'Get-GitHubReference',
'Get-GitHubReferrerTraffic',
'Get-GitHubRepository',
'Get-GitHubRepositoryBranch',
Expand All @@ -92,6 +93,7 @@
'New-GitHubIssue',
'New-GitHubLabel',
'New-GitHubMilestone',
'New-GithubReference',
'New-GitHubRepository',
'New-GitHubRepositoryFork',
'Remove-GithubAssignee',
Expand All @@ -116,9 +118,7 @@
'Update-GitHubCurrentUser',
'Update-GitHubIssue',
'Update-GitHubLabel',
'Update-GitHubRepository',
'Get-GitHubReference',
'New-GithubReference'
'Update-GitHubRepository'
)

AliasesToExport = @(
Expand Down
63 changes: 46 additions & 17 deletions Tests/GitHubReferences.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -127,38 +127,67 @@ 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
}
}

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
}
}
Expand Down

0 comments on commit 4937cdb

Please sign in to comment.