Skip to content

Commit

Permalink
Update GitHubRepositories Tests and Refactor Get-GitHubRepository Fun…
Browse files Browse the repository at this point in the history
…ction (#233)

Updates the Pester tests for the `GitHubRepositories` module to increase the code coverage.

This also:
  * Refactors `Get-GithubRepository` to remove a code path that could never be taken
  * Adds a `ValidateSet` to the `Affiliation` parameter of the `Get-GitHubRepository` function
  * Fixes the Comment Based Help descriptions for the `Get-GitHubRepositoryCollaborator` and `Move-GitHubRepositoryOwnership`
  • Loading branch information
X-Guardian committed Jun 28, 2020
1 parent 19c8417 commit eedfaa3
Show file tree
Hide file tree
Showing 2 changed files with 456 additions and 166 deletions.
101 changes: 60 additions & 41 deletions GitHubRepositories.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,10 @@ filter Get-GitHubRepository
SupportsShouldProcess,
DefaultParameterSetName='AuthenticatedUser')]
[OutputType({$script:GitHubRepositoryTypeName})]
[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.")]
param(
[Parameter(
ValueFromPipelineByPropertyName,
Expand Down Expand Up @@ -687,6 +689,7 @@ filter Get-GitHubRepository
[string] $Visibility,

[Parameter(ParameterSetName='AuthenticatedUser')]
[ValidateSet('Owner', 'Collaborator', 'OrganizationMember')]
[string[]] $Affiliation,

[Parameter(ParameterSetName='AuthenticatedUser')]
Expand Down Expand Up @@ -736,11 +739,11 @@ filter Get-GitHubRepository
$description = [String]::Empty
switch ($PSCmdlet.ParameterSetName)
{
{ ('ElementsOrUser', 'Uri') -contains $_ } {
'ElementsOrUser' {
# This is a little tricky. Ideally we'd have two separate ParameterSets (Elements, User),
# however PowerShell would be unable to disambiguate between the two, so unfortunately
# we need to do some additional work here. And because fallthru doesn't appear to be
# working right, we're combining both of those, along with Uri.
# working right, we're combining both of those.

if ([String]::IsNullOrWhiteSpace($OwnerName))
{
Expand All @@ -750,37 +753,24 @@ filter Get-GitHubRepository
}
elseif ([String]::IsNullOrWhiteSpace($RepositoryName))
{
if ($PSCmdlet.ParameterSetName -eq 'ElementsOrUser')
{
$telemetryProperties['UsageType'] = 'User'
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
$telemetryProperties['UsageType'] = 'User'
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName

$uriFragment = "users/$OwnerName/repos"
$description = "Getting repos for $OwnerName"
}
else
{
$message = 'RepositoryName could not be determined.'
Write-Log -Message $message -Level Error
throw $message
}
$uriFragment = "users/$OwnerName/repos"
$description = "Getting repos for $OwnerName"
}
else
{
if ($PSCmdlet.ParameterSetName -eq 'ElementsOrUser')
if ($PSBoundParameters.ContainsKey('Type') -or
$PSBoundParameters.ContainsKey('Sort') -or
$PSBoundParameters.ContainsKey('Direction'))
{
$telemetryProperties['UsageType'] = 'Elements'

if ($PSBoundParameters.ContainsKey('Type') -or
$PSBoundParameters.ContainsKey('Sort') -or
$PSBoundParameters.ContainsKey('Direction'))
{
$message = 'Unable to specify -Type, -Sort and/or -Direction when retrieving a specific repository.'
Write-Log -Message $message -Level Error
throw $message
}
$message = 'Unable to specify -Type, -Sort and/or -Direction when retrieving a specific repository.'
Write-Log -Message $message -Level Error
throw $message
}

$telemetryProperties['UsageType'] = 'Elements'
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
$telemetryProperties['RepositoryName'] = Get-PiiSafeString -PlainText $RepositoryName

Expand All @@ -791,20 +781,30 @@ filter Get-GitHubRepository
break
}

'Organization' {
$telemetryProperties['OrganizationName'] = Get-PiiSafeString -PlainText $OrganizationName
'Uri' {
if ($PSBoundParameters.ContainsKey('Type') -or
$PSBoundParameters.ContainsKey('Sort') -or
$PSBoundParameters.ContainsKey('Direction'))
{
$message = 'Unable to specify -Type, -Sort and/or -Direction when retrieving a specific repository.'
Write-Log -Message $message -Level Error
throw $message
}

$uriFragment = "orgs/$OrganizationName/repos"
$description = "Getting repos for $OrganizationName"
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
$telemetryProperties['RepositoryName'] = Get-PiiSafeString -PlainText $RepositoryName

$uriFragment = "repos/$OwnerName/$RepositoryName"
$description = "Getting $OwnerName/$RepositoryName"

break
}

'User' {
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
'Organization' {
$telemetryProperties['OrganizationName'] = Get-PiiSafeString -PlainText $OrganizationName

$uriFragment = "users/$OwnerName/repos"
$description = "Getting repos for $OwnerName"
$uriFragment = "orgs/$OrganizationName/repos"
$description = "Getting repos for $OrganizationName"

break
}
Expand Down Expand Up @@ -857,7 +857,18 @@ filter Get-GitHubRepository
if ($PSBoundParameters.ContainsKey('Direction')) { $getParams += "direction=$($directionConverter[$Direction])" }
if ($PSBoundParameters.ContainsKey('Affiliation') -and $Affiliation.Count -gt 0)
{
$getParams += "affiliation=$($Affiliation -join ',')"
$affiliationMap = @{
Owner = 'owner'
Collaborator = 'collaborator'
OrganizationMember = 'organization_member'
}
$affiliationParam = @()

foreach ($member in $Affiliation)
{
$affiliationParam += $affiliationMap[$member]
}
$getParams += "affiliation=$($affiliationParam -join ',')"
}
if ($PSBoundParameters.ContainsKey('Since')) { $getParams += "since=$Since" }

Expand Down Expand Up @@ -1602,8 +1613,12 @@ filter Get-GitHubRepositoryContributor
.EXAMPLE
Get-GitHubRepositoryContributor -OwnerName microsoft -RepositoryName PowerShellForGitHub
Gets a list of contributors for the PowerShellForGithub repository.
.EXAMPLE
Get-GitHubRepositoryContributor -Uri 'https://github.com/PowerShell/PowerShellForGitHub' -IncludeStatistics
Gets a list of contributors for the PowerShellForGithub repository including statistics.
#>
[CmdletBinding(
SupportsShouldProcess,
Expand Down Expand Up @@ -1691,10 +1706,10 @@ filter Get-GitHubRepositoryCollaborator
{
<#
.SYNOPSIS
Retrieve list of contributors for a given repository.
Retrieve list of collaborators for a given repository.
.DESCRIPTION
Retrieve list of contributors for a given repository.
Retrieve list of collaborators for a given repository.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
Expand Down Expand Up @@ -1749,8 +1764,12 @@ filter Get-GitHubRepositoryCollaborator
.EXAMPLE
Get-GitHubRepositoryCollaborator -OwnerName microsoft -RepositoryName PowerShellForGitHub
Gets a list of collaborators for the PowerShellForGithub repository.
.EXAMPLE
Get-GitHubRepositoryCollaborator -Uri 'https://github.com/PowerShell/PowerShellForGitHub'
Gets a list of collaborators for the PowerShellForGithub repository.
#>
[CmdletBinding(
SupportsShouldProcess,
Expand Down Expand Up @@ -2027,10 +2046,10 @@ filter Move-GitHubRepositoryOwnership
{
<#
.SYNOPSIS
Creates a new repository on GitHub.
Changes the ownership of a repository on GitHub.
.DESCRIPTION
Creates a new repository on GitHub.
Changes the ownership of a repository on GitHub.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
Expand Down
Loading

0 comments on commit eedfaa3

Please sign in to comment.