Skip to content

Commit 3a359af

Browse files
author
S Raghav
committed
Address comments and add option to get all references
1 parent 50585d7 commit 3a359af

File tree

3 files changed

+81
-37
lines changed

3 files changed

+81
-37
lines changed

GitHubReferences.ps1

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ function Get-GitHubReference
2525
them individually.
2626
2727
.PARAMETER Reference
28-
Name of the reference, for example: "heads/<branch name>" for branches and "tags/<tag name>" for tags
28+
Name of the reference, for example: "heads/<branch name>" for branches and "tags/<tag name>" for tags.
29+
Gets all the references if not provided
2930
3031
.PARAMETER AccessToken
3132
If provided, this will be used as the AccessToken for authentication with the
@@ -36,7 +37,7 @@ function Get-GitHubReference
3637
with no commandline status update. When not specified, those commands run in
3738
the background, enabling the command prompt to provide status information.
3839
If not supplied here, the DefaultNoStatus configuration property value will be used.
39-
40+
4041
.OUTPUTS
4142
[PSCustomObject]
4243
Details of the git reference in the given repository
@@ -47,7 +48,8 @@ function Get-GitHubReference
4748
[CmdletBinding(
4849
SupportsShouldProcess,
4950
DefaultParametersetName='Elements')]
50-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
51+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "",
52+
Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
5153
param(
5254
[Parameter(ParameterSetName='Elements')]
5355
[string] $OwnerName,
@@ -60,7 +62,6 @@ function Get-GitHubReference
6062
ParameterSetName='Uri')]
6163
[string] $Uri,
6264

63-
[Parameter(Mandatory)]
6465
[string] $Reference,
6566

6667
[string] $AccessToken,
@@ -77,6 +78,7 @@ function Get-GitHubReference
7778
$telemetryProperties = @{
7879
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
7980
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
81+
'ProvidedReference' = $PSBoundParameters.ContainsKey('Reference')
8082
}
8183

8284
if ($OwnerName -xor $RepositoryName)
@@ -85,14 +87,21 @@ function Get-GitHubReference
8587
Write-Log -Message $message -Level Error
8688
throw $message
8789
}
88-
$uriFragment = "/repos/$OwnerName/$RepositoryName/git/refs/$Reference"
89-
$description = "Getting Reference $Reference for $RepositoryName"
90+
91+
if (-not [String]::IsNullOrEmpty($RepositoryName))
92+
{
93+
$uriFragment = "/repos/$OwnerName/$RepositoryName/git/refs"
94+
$description = "Getting all references for $RepositoryName"
95+
if ($PSBoundParameters.ContainsKey('Reference'))
96+
{
97+
$uriFragment = $uriFragment + "/$Reference"
98+
$description = "Getting Reference $Reference for $RepositoryName"
99+
}
100+
}
90101

91102
$params = @{
92103
'UriFragment' = $uriFragment
93-
'Method' = 'Get'
94104
'Description' = $description
95-
'AcceptHeader' = 'application/vnd.github.symmetra-preview+json'
96105
'AccessToken' = $AccessToken
97106
'TelemetryEventName' = $MyInvocation.MyCommand.Name
98107
'TelemetryProperties' = $telemetryProperties
@@ -101,12 +110,18 @@ function Get-GitHubReference
101110

102111
try
103112
{
104-
return Invoke-GHRestMethod @params
113+
return Invoke-GHRestMethodMultipleResult @params
105114
}
106115
catch
107116
{
108-
Write-InteractiveHost "No reference named $Reference exists in repository $RepositoryName" -NoNewline -f Red
109-
Write-Log -Level Error "No reference named $Reference exists in repository $RepositoryName"
117+
if ($PSBoundParameters.ContainsKey('Reference'))
118+
{
119+
Write-InteractiveHost "No reference named $Reference exists in repository $RepositoryName" -NoNewline -f Red
120+
Write-Log -Level Error "No reference named $Reference exists in repository $RepositoryName"
121+
return $null
122+
}
123+
Write-InteractiveHost "No references exist in repository $RepositoryName" -NoNewline -f Red
124+
Write-Log -Level Error "No references exist in repository $RepositoryName"
110125
return $null
111126
}
112127

@@ -136,7 +151,7 @@ function New-GitHubReference
136151
them individually.
137152
138153
.PARAMETER Reference
139-
The name of the fully qualified reference to be created (eg: refs/heads/master)
154+
The name of the fully qualified reference to be created (eg: heads/master)
140155
141156
.PARAMETER Sha
142157
The SHA1 value for the reference to be created
@@ -156,12 +171,13 @@ function New-GitHubReference
156171
Details of the git reference created. Throws an Exception if the reference already exists
157172
158173
.EXAMPLE
159-
-GitHubReference -OwnerName Powershell -RepositoryName PowerShellForGitHub -Reference refs/heads/master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd
174+
New-GitHubReference -OwnerName Powershell -RepositoryName PowerShellForGitHub -Reference heads/master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd
160175
#>
161176
[CmdletBinding(
162177
SupportsShouldProcess,
163178
DefaultParametersetName='Elements')]
164-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
179+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "",
180+
Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
165181
param(
166182
[Parameter(ParameterSetName='Elements')]
167183
[string] $OwnerName,
@@ -207,16 +223,15 @@ function New-GitHubReference
207223
$description = "Creating Reference $Reference for $RepositoryName from SHA $Sha"
208224

209225
$hashBody = @{
210-
'ref' = $Reference
226+
'ref' = "refs/" + $Reference
211227
'sha' = $Sha
212228
}
213229

214230
$params = @{
215231
'UriFragment' = $uriFragment
216232
'Method' = 'Post'
217233
'Body' = (ConvertTo-Json -InputObject $hashBody)
218-
'Description' = $description
219-
'AcceptHeader' = 'application/vnd.github.symmetra-preview+json'
234+
'Description' = $description
220235
'AccessToken' = $AccessToken
221236
'TelemetryEventName' = $MyInvocation.MyCommand.Name
222237
'TelemetryProperties' = $telemetryProperties

PowerShellForGitHub.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
'Get-GitHubPathTraffic',
6868
'Get-GitHubPullRequest',
6969
'Get-GitHubRateLimit',
70+
'Get-GitHubReference',
7071
'Get-GitHubReferrerTraffic',
7172
'Get-GitHubRepository',
7273
'Get-GitHubRepositoryBranch',
@@ -92,6 +93,7 @@
9293
'New-GitHubIssue',
9394
'New-GitHubLabel',
9495
'New-GitHubMilestone',
96+
'New-GithubReference',
9597
'New-GitHubRepository',
9698
'New-GitHubRepositoryFork',
9799
'Remove-GithubAssignee',
@@ -116,9 +118,7 @@
116118
'Update-GitHubCurrentUser',
117119
'Update-GitHubIssue',
118120
'Update-GitHubLabel',
119-
'Update-GitHubRepository',
120-
'Get-GitHubReference',
121-
'New-GithubReference'
121+
'Update-GitHubRepository'
122122
)
123123

124124
AliasesToExport = @(

Tests/GitHubReferences.Tests.ps1

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,40 +84,40 @@ try
8484
Describe 'Create a new reference(branch) in repository' {
8585
$repositoryName = [Guid]::NewGuid()
8686
$repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
87-
$existingref = @(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master")
87+
$existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master"
8888
$sha = $existingref.object.sha
8989

9090
Context 'On creating a valid reference in a new repository from a given SHA' {
91-
$refName = "refs/heads/" + [Guid]::NewGuid().ToString()
92-
$result = @(New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refName -Sha $sha)
91+
$refName = "heads/" + [Guid]::NewGuid().ToString()
92+
$result = New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refName -Sha $sha
9393

9494
It 'Should successfully create the reference' {
95-
$result.ref | Should Be $refName
95+
$result.ref | Should Be "refs/$refName"
9696
}
9797
}
9898

9999
Context 'On creating a valid reference in a new repository (specified by Uri) from a given SHA' {
100-
$refName = "refs/heads/" + [Guid]::NewGuid().ToString()
101-
$result = @(New-GitHubReference -Uri $repo.svn_url -Reference $refName -Sha $sha)
100+
$refName = "heads/" + [Guid]::NewGuid().ToString()
101+
$result = New-GitHubReference -Uri $repo.svn_url -Reference $refName -Sha $sha
102102

103103
It 'Should successfully create the reference' {
104-
$result.ref | Should Be $refName
104+
$result.ref | Should Be "refs/$refName"
105105
}
106106
}
107107

108108
Context 'On creating an existing reference in a new repository from a given SHA' {
109-
$refName = "refs/heads/master"
109+
$refName = "heads/master"
110110

111111
It 'Should throw an Exception' {
112-
{ @(New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refName -Sha $sha) } | Should Throw
112+
{ New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refName -Sha $sha } | Should Throw
113113
}
114114
}
115115

116116
Context 'On creating an existing reference in a new repository (specified by Uri) from a given SHA' {
117-
$refName = "refs/heads/master"
117+
$refName = "heads/master"
118118

119119
It 'Should throw an exception' {
120-
{ @(New-GitHubReference -Uri $repo.svn_url -Reference $refName -Sha $sha) } | Should Throw
120+
{ New-GitHubReference -Uri $repo.svn_url -Reference $refName -Sha $sha } | Should Throw
121121
}
122122
}
123123

@@ -127,38 +127,67 @@ try
127127
Describe 'Getting a reference(branch) from repository' {
128128
$repositoryName = [Guid]::NewGuid()
129129
$repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
130+
$refName = "refs/heads/master"
130131

131132
Context 'On getting a valid reference from a new repository' {
132-
$reference = @(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master")
133+
$reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master"
133134

134135
It 'Should return details of the reference' {
135-
$reference.ref | Should be "refs/heads/master"
136+
$reference.ref | Should be $refName
136137
}
137138
}
138139

139140
Context 'On getting an invalid reference from a new repository' {
140-
$reference = @(Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/someRandomRef")
141+
$reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/someRandomRef"
141142

142143
It 'Should not return any details' {
143144
$reference | Should be $null
144145
}
145146
}
146147

147148
Context 'On getting a valid reference using Uri from a new repository' {
148-
$reference = @(Get-GitHubReference -Uri $repo.svn_url -Reference "heads/master")
149+
$reference = Get-GitHubReference -Uri $repo.svn_url -Reference "heads/master"
149150

150151
It 'Should return details of the reference' {
151-
$reference.ref | Should be "refs/heads/master"
152+
$reference.ref | Should be $refName
152153
}
153154
}
154155

155156
Context 'On getting an invalid reference using Uri from a new repository' {
156-
$reference = @(Get-GitHubReference -Uri $repo.svn_url -Reference "heads/someRandomRef")
157+
$reference = Get-GitHubReference -Uri $repo.svn_url -Reference "heads/someRandomRef"
157158

158159
It 'Should not return any details' {
159160
$reference | Should be $null
160161
}
161162
}
163+
164+
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
165+
}
166+
167+
Describe 'Getting all references from repository' {
168+
$repositoryName = [Guid]::NewGuid()
169+
$repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
170+
$existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master"
171+
$sha = $existingref.object.sha
172+
New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/branch1" -Sha $sha
173+
$refNames = @("refs/heads/master", "refs/heads/branch1")
174+
175+
Context 'On getting all references from a new repository' {
176+
$reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName
177+
178+
It 'Should return all references' {
179+
($reference.ref | Where-Object {$refNames -Contains $_}).Count | Should be $refNames.Count
180+
}
181+
}
182+
183+
Context 'On getting all references using Uri from a new repository' {
184+
$reference = Get-GitHubReference -Uri $repo.svn_url
185+
186+
It 'Should return all references' {
187+
($reference.ref | Where-Object {$refNames -Contains $_}).Count | Should be $refNames.Count
188+
}
189+
}
190+
162191
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
163192
}
164193
}

0 commit comments

Comments
 (0)