Skip to content

Commit 7529d7f

Browse files
committed
Fixed review comments
1 parent 554d42a commit 7529d7f

File tree

3 files changed

+22
-102
lines changed

3 files changed

+22
-102
lines changed

GitHubReferences.ps1

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ function Get-GitHubReference
2626
2727
.PARAMETER Reference
2828
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
29+
Gets all the references (everything in the namespace, not only heads and tags) if not provided
3030
3131
.PARAMETER AccessToken
3232
If provided, this will be used as the AccessToken for authentication with the
33-
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
33+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
3434
3535
.PARAMETER NoStatus
3636
If this switch is specified, long-running commands will run on the main thread
@@ -48,8 +48,7 @@ function Get-GitHubReference
4848
[CmdletBinding(
4949
SupportsShouldProcess,
5050
DefaultParametersetName='Elements')]
51-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "",
52-
Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
51+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
5352
param(
5453
[Parameter(ParameterSetName='Elements')]
5554
[string] $OwnerName,
@@ -90,13 +89,18 @@ function Get-GitHubReference
9089

9190
if (-not [String]::IsNullOrEmpty($RepositoryName))
9291
{
93-
$uriFragment = "/repos/$OwnerName/$RepositoryName/git/refs"
92+
$uriFragment = "repos/$OwnerName/$RepositoryName/git/matching-refs"
9493
$description = "Getting all references for $RepositoryName"
9594
if ($PSBoundParameters.ContainsKey('Reference'))
9695
{
9796
$uriFragment = $uriFragment + "/$Reference"
9897
$description = "Getting Reference $Reference for $RepositoryName"
9998
}
99+
else
100+
{
101+
# Add a slash at the end. Calling it with only matching-refs without the slash causes a 404
102+
$uriFragment = $uriFragment + "//"
103+
}
100104
}
101105

102106
$params = @{
@@ -108,23 +112,7 @@ function Get-GitHubReference
108112
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
109113
}
110114

111-
try
112-
{
113-
return Invoke-GHRestMethodMultipleResult @params
114-
}
115-
catch
116-
{
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"
125-
return $null
126-
}
127-
115+
return Invoke-GHRestMethodMultipleResult @params
128116
}
129117

130118
function New-GitHubReference
@@ -176,8 +164,7 @@ function New-GitHubReference
176164
[CmdletBinding(
177165
SupportsShouldProcess,
178166
DefaultParametersetName='Elements')]
179-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "",
180-
Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
167+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
181168
param(
182169
[Parameter(ParameterSetName='Elements')]
183170
[string] $OwnerName,
@@ -219,7 +206,7 @@ function New-GitHubReference
219206
throw $message
220207
}
221208

222-
$uriFragment = "/repos/$OwnerName/$RepositoryName/git/refs"
209+
$uriFragment = "repos/$OwnerName/$RepositoryName/git/refs"
223210
$description = "Creating Reference $Reference for $RepositoryName from SHA $Sha"
224211

225212
$hashBody = @{

PowerShellForGitHub.psd1

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,8 @@
9595
'New-GitHubIssue',
9696
'New-GitHubLabel',
9797
'New-GitHubMilestone',
98-
<<<<<<< HEAD
9998
'New-GitHubPullRequest',
100-
=======
10199
'New-GithubReference',
102-
>>>>>>> Address comments and add option to get all references
103100
'New-GitHubRepository',
104101
'New-GitHubRepositoryFork',
105102
'Remove-GithubAssignee',

Tests/GitHubReferences.Tests.ps1

Lines changed: 10 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,82 +6,14 @@
66
Tests for GitHubReferences.ps1 module
77
#>
88

9-
[String] $root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
10-
. (Join-Path -Path $root -ChildPath 'Tests\Config\Settings.ps1')
11-
Import-Module -Name $root -Force
12-
13-
function Initialize-AppVeyor
14-
{
15-
<#
16-
.SYNOPSIS
17-
Configures the tests to run with the authentication information stored in AppVeyor
18-
(if that information exists in the environment).
19-
20-
.DESCRIPTION
21-
Configures the tests to run with the authentication information stored in AppVeyor
22-
(if that information exists in the environment).
23-
24-
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
25-
26-
.NOTES
27-
Internal-only helper method.
28-
29-
The only reason this exists is so that we can leverage CodeAnalysis.SuppressMessageAttribute,
30-
which can only be applied to functions.
31-
32-
We call this immediately after the declaration so that AppVeyor initialization can heppen
33-
(if applicable).
34-
35-
#>
36-
[CmdletBinding()]
37-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "",
38-
Justification="Needed to configure with the stored, encrypted string value in AppVeyor.")]
39-
param()
40-
41-
if ($env:AppVeyor)
42-
{
43-
$secureString = $env:avAccessToken | ConvertTo-SecureString -AsPlainText -Force
44-
$cred = New-Object System.Management.Automation.PSCredential "<username is ignored>", $secureString
45-
Set-GitHubAuthentication -Credential $cred
46-
47-
$script:ownerName = $env:avOwnerName
48-
$script:organizationName = $env:avOrganizationName
49-
50-
$message = @(
51-
'This run is executed in the AppVeyor environment.',
52-
'The GitHub Api Token won''t be decrypted in PR runs causing some tests to fail.',
53-
'403 errors possible due to GitHub hourly limit for unauthenticated queries.',
54-
'Use Set-GitHubAuthentication manually. modify the values in Tests\Config\Settings.ps1,',
55-
'and run tests on your machine first.')
56-
Write-Warning -Message ($message -join [Environment]::NewLine)
57-
}
58-
}
59-
60-
Initialize-AppVeyor
61-
62-
$accessTokenConfigured = Test-GitHubAuthenticationConfigured
63-
if (-not $accessTokenConfigured)
64-
{
65-
$message = @(
66-
'GitHub API Token not defined, some of the tests will be skipped.',
67-
'403 errors possible due to GitHub hourly limit for unauthenticated queries.')
68-
Write-Warning -Message ($message -join [Environment]::NewLine)
69-
}
70-
71-
# Backup the user's configuration before we begin, and ensure we're at a pure state before running
72-
# the tests. We'll restore it at the end.
73-
$configFile = New-TemporaryFile
9+
# This is common test code setup logic for all Pester test files
10+
$moduleRootPath = Split-Path -Path $PSScriptRoot -Parent
11+
. (Join-Path -Path $moduleRootPath -ChildPath 'Tests\Common.ps1')
7412

7513
try
7614
{
77-
Backup-GitHubConfiguration -Path $configFile
78-
Reset-GitHubConfiguration
79-
Set-GitHubConfiguration -DisableTelemetry # We don't want UT's to impact telemetry
80-
Set-GitHubConfiguration -LogRequestBody # Make it easier to debug UT failures
81-
8215
if ($accessTokenConfigured)
8316
{
84-
8517
Describe 'Create a new reference(branch) in repository' {
8618
$repositoryName = [Guid]::NewGuid()
8719
$repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
@@ -126,7 +58,7 @@ try
12658
$repositoryName = [Guid]::NewGuid()
12759
$repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
12860
$masterRefName = "heads/master"
129-
$randomRefName = "heads/someRandomRef"
61+
$randomRefName = "heads/$([Guid]::NewGuid())"
13062

13163
Context 'On getting a valid reference from a new repository' {
13264
$reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName
@@ -195,7 +127,11 @@ try
195127
}
196128
catch
197129
{
198-
# Restore the user's configuration to its pre-test state
199-
Restore-GitHubConfiguration -Path $configFile
130+
if (Test-Path -Path $script:originalConfigFile -PathType Leaf)
131+
{
132+
# Restore the user's configuration to its pre-test state
133+
Restore-GitHubConfiguration -Path $script:originalConfigFile
134+
$script:originalConfigFile = $null
135+
}
200136
}
201137

0 commit comments

Comments
 (0)