From 41de3adb29ed583f775ce30e52c3d6ed8ade35ff Mon Sep 17 00:00:00 2001 From: Simon Heather <32168619+X-Guardian@users.noreply.github.com> Date: Sun, 28 Jun 2020 00:23:00 +0100 Subject: [PATCH] GitHubRepositories: Add Output Type Views (#205) * Adds formatting for all of the types exposed in GitHubRepositories.ps1. * Fixes the `OutputType` for RepositoryCollaborator and RepositoryContributor. --- Formatters/GitHubRepositories.Format.ps1xml | 181 ++++++++++++++++++ GitHubRepositories.ps1 | 197 +++++++++++++++++++- PowerShellForGitHub.psd1 | 5 + Tests/GitHubRepositories.tests.ps1 | 8 +- 4 files changed, 383 insertions(+), 8 deletions(-) create mode 100644 Formatters/GitHubRepositories.Format.ps1xml diff --git a/Formatters/GitHubRepositories.Format.ps1xml b/Formatters/GitHubRepositories.Format.ps1xml new file mode 100644 index 00000000..5325fd19 --- /dev/null +++ b/Formatters/GitHubRepositories.Format.ps1xml @@ -0,0 +1,181 @@ + + + + + + GitHub.Repository + + GitHub.Repository + + + + + + + full_name + + + visibility + + + description + + + + + + + + + GitHub.RepositoryTopic + + GitHub.RepositoryTopic + + + + + + + names + + + RepositoryUrl + + + + + + + + + GitHub.RepositoryContributor + + GitHub.RepositoryContributor + + + + + + + UserName + + + type + + + contributions + + + + + + + + + GitHub.RepositoryContributorStatistics + + GitHub.RepositoryContributorStatistics + + + + + + + + + + + + + + + $_.author.UserName + + + + total + + + weeks + + + + + + + + + GitHub.RepositoryCollaborator + + GitHub.RepositoryCollaborator + + + + + + + + + + + + + + + + + + + UserName + + + + $_.permissions.admin + + + + + $_.permissions.push + + + + + $_.permissions.pull + + + + + + + + + + GitHub.RepositoryTag + + GitHub.RepositoryTag + + + + + + + + + + + + + name + + + + $_.commit.sha + + + + + + + + + diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index 50849b7a..f5c0d8f4 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -4,6 +4,8 @@ @{ GitHubRepositoryTypeName = 'GitHub.Repository' GitHubRepositoryTopicTypeName = 'GitHub.RepositoryTopic' + GitHubRepositoryContributorTypeName = 'GitHub.RepositoryContributor' + GitHubRepositoryCollaboratorTypeName = 'GitHub.RepositoryCollaborator' GitHubRepositoryContributorStatisticsTypeName = 'GitHub.RepositoryContributorStatistics' GitHubRepositoryLanguageTypeName = 'GitHub.RepositoryLanguage' GitHubRepositoryTagTypeName = 'GitHub.RepositoryTag' @@ -1606,7 +1608,7 @@ filter Get-GitHubRepositoryContributor [CmdletBinding( SupportsShouldProcess, DefaultParameterSetName='Elements')] - [OutputType({$script:GitHubUserTypeName})] + [OutputType({$script:GitHubRepositoryContributorTypeName})] [OutputType({$script:GitHubRepositoryContributorStatisticsTypeName})] [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.")] @@ -1679,7 +1681,7 @@ filter Get-GitHubRepositoryContributor } else { - $results = $results | Add-GitHubUserAdditionalProperties + $results = $results | Add-GitHubRepositoryContributorAdditionalProperties } return $results @@ -1753,7 +1755,7 @@ filter Get-GitHubRepositoryCollaborator [CmdletBinding( SupportsShouldProcess, DefaultParameterSetName='Elements')] - [OutputType({$script:GitHubUserTypeName})] + [OutputType({$script:GitHubRepositoryCollaboratorTypeName})] [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( @@ -1802,7 +1804,8 @@ filter Get-GitHubRepositoryCollaborator 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) } - return (Invoke-GHRestMethodMultipleResult @params | Add-GitHubUserAdditionalProperties) + return (Invoke-GHRestMethodMultipleResult @params | + Add-GitHubRepositoryCollaboratorAdditionalProperties) } filter Get-GitHubRepositoryLanguage @@ -2850,3 +2853,189 @@ filter Add-GitHubRepositoryAdditionalProperties Write-Output $item } } + +filter Add-GitHubRepositoryContributorAdditionalProperties +{ +<# + .SYNOPSIS + Adds type name and additional properties to ease pipelining to GitHub Contributor objects. + + .PARAMETER InputObject + The GitHub object to add additional properties to. + + .PARAMETER TypeName + The type that should be assigned to the object. + + .PARAMETER Name + The name of the Contributor. This information might be obtainable from InputObject, so this + is optional based on what InputObject contains. + + .PARAMETER Id + The ID of the Contributor. This information might be obtainable from InputObject, so this + is optional based on what InputObject contains. + + .INPUTS + PSCustomObject + + .OUTPUTS + GitHub.RepositoryContributor +#> + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', + Justification='Internal helper that is definitely adding more than one property.')] + param( + [Parameter( + Mandatory, + ValueFromPipeline)] + [AllowNull()] + [AllowEmptyCollection()] + [PSCustomObject[]] $InputObject, + + [ValidateNotNullOrEmpty()] + [string] $TypeName = $script:GitHubRepositoryContributorTypeName, + + [string] $Name, + + [int64] $Id + ) + + foreach ($item in $InputObject) + { + $item.PSObject.TypeNames.Insert(0, $TypeName) + if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport)) + { + $UserName = $item.login + if ([String]::IsNullOrEmpty($UserName) -and $PSBoundParameters.ContainsKey('Name')) + { + $UserName = $Name + } + + if (-not [String]::IsNullOrEmpty($UserName)) + { + $addMemberParms = @{ + InputObject = $item + Name = 'UserName' + Value = $UserName + MemberType = 'NoteProperty' + Force = $true + } + Add-Member @addMemberParms + } + + $UserId = $item.id + if (($UserId -eq 0) -and $PSBoundParameters.ContainsKey('Id')) + { + $UserId = $Id + } + + if ($UserId -ne 0) + { + $addMemberParms = @{ + InputObject = $item + Name = 'UserId' + Value = $UserId + MemberType = 'NoteProperty' + Force = $true + } + + Add-Member @addMemberParms + } + } + + Write-Output $item + } +} + +filter Add-GitHubRepositoryCollaboratorAdditionalProperties +{ +<# + .SYNOPSIS + Adds type name and additional properties to ease pipelining to GitHub Collaborator objects. + + .PARAMETER InputObject + The GitHub object to add additional properties to. + + .PARAMETER TypeName + The type that should be assigned to the object. + + .PARAMETER Name + The name of the Collaborator. This information might be obtainable from InputObject, so this + is optional based on what InputObject contains. + + .PARAMETER Id + The ID of the Collaborator. This information might be obtainable from InputObject, so this + is optional based on what InputObject contains. + + .INPUTS + PSCustomObject + + .OUTPUTS + GitHub.RepositoryCollaborator +#> + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '', + Justification='Internal helper that is definitely adding more than one property.')] + param( + [Parameter( + Mandatory, + ValueFromPipeline)] + [AllowNull()] + [AllowEmptyCollection()] + [PSCustomObject[]] $InputObject, + + [ValidateNotNullOrEmpty()] + [string] $TypeName = $script:GitHubRepositoryCollaboratorTypeName, + + [string] $Name, + + [int64] $Id + ) + + foreach ($item in $InputObject) + { + $item.PSObject.TypeNames.Insert(0, $TypeName) + + if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport)) + { + $userName = $item.login + if ([String]::IsNullOrEmpty($userName) -and $PSBoundParameters.ContainsKey('Name')) + { + $userName = $Name + } + + if (-not [String]::IsNullOrEmpty($userName)) + { + $addMemberParms = @{ + InputObject = $item + Name = 'UserName' + Value = $userName + MemberType = 'NoteProperty' + Force = $true + } + + Add-Member @addMemberParms + } + + $userId = $item.id + if (($userId -eq 0) -and $PSBoundParameters.ContainsKey('Id')) + { + $userId = $Id + } + + if ($userId -ne 0) + { + $addMemberParms = @{ + InputObject = $item + Name = 'UserId' + Value = $userId + MemberType = 'NoteProperty' + Force = $true + } + + Add-Member @addMemberParms + } + } + + Write-Output $item + } +} diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 3504bf7c..b49ff1f0 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -13,6 +13,11 @@ # Script module or binary module file associated with this manifest. RootModule = 'PowerShellForGitHub.psm1' + # Format files (.ps1xml) to be loaded when importing this module + FormatsToProcess = @( + 'Formatters/GitHubRepositories.Format.ps1xml' + ) + # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess NestedModules = @( # Ideally this list would be kept completely alphabetical, but other scripts (like diff --git a/Tests/GitHubRepositories.tests.ps1 b/Tests/GitHubRepositories.tests.ps1 index 6575f249..96e90cff 100644 --- a/Tests/GitHubRepositories.tests.ps1 +++ b/Tests/GitHubRepositories.tests.ps1 @@ -840,7 +840,7 @@ try It 'Should return expected number of contributors' { $contributors.Count | Should -Be 1 - $contributors[0].PSObject.TypeNames[0] = 'GitHub.User' + $contributors[0].PSObject.TypeNames[0] = 'GitHub.RepositoryContributor' } } @@ -849,7 +849,7 @@ try It 'Should return expected number of contributors' { $contributors.Count | Should -Be 1 - $contributors[0].PSObject.TypeNames[0] = 'GitHub.User' + $contributors[0].PSObject.TypeNames[0] = 'GitHub.RepositoryContributor' } } @@ -878,7 +878,7 @@ try It 'Should return expected number of collaborators' { $collaborators.Count | Should -Be 1 - $collaborators[0].PSObject.TypeNames[0] = 'GitHub.User' + $collaborators[0].PSObject.TypeNames[0] = 'GitHub.RepositoryCollaborator' } } @@ -887,7 +887,7 @@ try It 'Should return expected number of collaborators' { $collaborators.Count | Should -Be 1 - $collaborators[0].PSObject.TypeNames[0] = 'GitHub.User' + $collaborators[0].PSObject.TypeNames[0] = 'GitHub.RepositoryCollaborator' } } }