diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index f5c0d8f4..7a2c2e04 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -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, @@ -687,6 +689,7 @@ filter Get-GitHubRepository [string] $Visibility, [Parameter(ParameterSetName='AuthenticatedUser')] + [ValidateSet('Owner', 'Collaborator', 'OrganizationMember')] [string[]] $Affiliation, [Parameter(ParameterSetName='AuthenticatedUser')] @@ -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)) { @@ -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 @@ -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 } @@ -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" } @@ -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, @@ -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 @@ -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, @@ -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 diff --git a/Tests/GitHubRepositories.tests.ps1 b/Tests/GitHubRepositories.tests.ps1 index 96e90cff..37bd6983 100644 --- a/Tests/GitHubRepositories.tests.ps1 +++ b/Tests/GitHubRepositories.tests.ps1 @@ -368,99 +368,259 @@ try } } - Describe 'Getting repositories' { - Context 'For authenticated user' { - BeforeAll -Scriptblock { - $publicRepo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit - $privateRepo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit -Private + Describe 'GitHubRepositories\Get-GitHubRepository' { + Context 'When getting a repository for the authenticated user' { + BeforeAll { + $publicRepo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) + $privateRepo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -Private + } + + Context 'When specify the visibility parameter' { + BeforeAll { + $publicRepos = Get-GitHubRepository -Visibility Public + $privateRepos = Get-GitHubRepository -Visibility Private + } + + It 'Should return objects of the correct type' { + $publicRepos[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' + $privateRepos[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' + } + + It "Should return the correct membership" { + $publicRepo.name | Should -BeIn $publicRepos.name + $publicRepo.name | Should -Not -BeIn $privateRepos.name + $privateRepo.name | Should -BeIn $privateRepos.name + $privateRepo.name | Should -Not -BeIn $publicRepos.name + } + } + + Context 'When specifying the Type parameter' { + BeforeAll { + $publicRepos = Get-GitHubRepository -Type Public + $privateRepos = Get-GitHubRepository -Type Private + $ownerRepos = Get-GitHubRepository -Type Owner + $allRepos = Get-GitHubRepository -Type All + } + + It 'Should return objects of the correct type' { + $publicRepos[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' + $publicRepos[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' + $ownerRepos[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' + } + + It "Should return the correct membership" { + $publicRepo.name | Should -BeIn $publicRepos.name + $publicRepo.name | Should -Not -BeIn $privateRepos.name + $privateRepo.name | Should -BeIn $privateRepos.name + $privateRepo.name | Should -Not -BeIn $publicRepos.name + $publicRepo.name | Should -BeIn $ownerRepos.name + $privateRepo.name | Should -BeIn $ownerRepos.name + $publicRepo.name | Should -BeIn $allRepos.name + $privateRepo.name | Should -BeIn $allRepos.name + } } - It "Should have the public repo" { - $publicRepos = @(Get-GitHubRepository -Visibility Public) - $privateRepos = @(Get-GitHubRepository -Visibility Private) - $publicRepo.name | Should -BeIn $publicRepos.name - $publicRepo.name | Should -Not -BeIn $privateRepos.name + Context 'When specifying the Affiliation parameter' { + BeforeAll { + $ownerRepos = Get-GitHubRepository -Affiliation Owner, Collaborator + } + + It 'Should return objects of the correct type' { + $ownerRepos[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' + } + + It "Should return the correct membership" { + $publicRepo.name | Should -BeIn $ownerRepos.name + $privateRepo.name | Should -BeIn $ownerRepos.name + } } - It "Should have the private repo" { - $publicRepos = @(Get-GitHubRepository -Visibility Public) - $privateRepos = @(Get-GitHubRepository -Visibility Private) - $privateRepo.name | Should -BeIn $privateRepos.name - $privateRepo.name | Should -Not -BeIn $publicRepos.name + Context 'When specifying the Sort and Direction parameters' { + BeforeAll { + $sortedRepos = Get-GitHubRepository -Sort 'FullName' + $sortedDescendingRepos = Get-GitHubRepository -Sort FullName -Direction Descending + + $sortedRepoFullNames = [System.Collections.ArrayList]$sortedRepos.full_Name + $sortedRepoFullNames.Sort([System.StringComparer]::OrdinalIgnoreCase) + $sortedDescendingRepoFullNames = [System.Collections.ArrayList]$sortedDescendingRepos.full_Name + $sortedDescendingRepoFullNames.Sort([System.StringComparer]::OrdinalIgnoreCase) + $sortedDescendingRepoFullNames.Reverse() + } + + It 'Should return objects of the correct type' { + $sortedRepos[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' + $sortedDescendingRepos[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' + } + + It "Should return the correct membership order" { + for ($i = 1; $i -le $sortedRepos.count; $i++) { + $sortedRepos[$i].full_name | Should -Be $sortedRepoFullNames[$i] + $sortedDescendingRepos[$i].full_name | Should -Be $sortedDescendingRepoFullNames[$i] + } + } } - It 'Should not permit bad combination of parameters' { - { Get-GitHubRepository -Type All -Visibility All } | Should -Throw - { Get-GitHubRepository -Type All -Affiliation Owner } | Should -Throw + Context 'When Specifying an invalid Visibility parameter set' { + It 'Should throw the correct exception' { + $errorMessage = 'Unable to specify -Type when using -Visibility and/or -Affiliation.' + { Get-GitHubRepository -Type All -Visibility All } | Should -Throw $errorMessage + } } - AfterAll -ScriptBlock { - Remove-GitHubRepository -Uri $publicRepo.svn_url -Confirm:$false - Remove-GitHubRepository -Uri $privateRepo.svn_url -Confirm:$false + Context 'When Specifying an invalid Affiliation parameter set' { + It 'Should throw the correct exception' { + $errorMessage = 'Unable to specify -Type when using -Visibility and/or -Affiliation.' + { Get-GitHubRepository -Type All -Visibility All } | Should -Throw $errorMessage + } + } + + AfterAll { + Remove-GitHubRepository -Uri $publicRepo.svn_url -Force + Remove-GitHubRepository -Uri $privateRepo.svn_url -Force } } - Context 'For any user' { - It "Should have results for The Octocat" { - $repos = @(Get-GitHubRepository -OwnerName 'octocat' -Type Public) - $repos.Count | Should -BeGreaterThan 0 - $repos[0].owner.login | Should -Be 'octocat' + Context 'When getting a repository for a specified owner' { + BeforeAll { + $ownerName = 'octocat' + $repos = Get-GitHubRepository -OwnerName $ownerName + } + + It 'Should return objects of the correct type' { + $repos | Should -BeOfType PSCustomObject + } + + It "Should return one or more results" { + $repos.Count | Should -BeGreaterOrEqual 1 + } + + It 'Should return the correct properties' { + foreach ($repo in $repos) { + $repo.owner.login | Should -Be $ownerName + } } } - Context 'For organizations' { - BeforeAll -Scriptblock { - $repo = New-GitHubRepository -OrganizationName $script:organizationName -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + Context 'When getting a repository for a specified organization' { + BeforeAll { + $repo = New-GitHubRepository -OrganizationName $script:organizationName -RepositoryName ([Guid]::NewGuid().Guid) } It "Should have results for the organization" { - $repos = @(Get-GitHubRepository -OrganizationName $script:organizationName -Type All) + $repos = Get-GitHubRepository -OrganizationName $script:organizationName -Type All $repo.name | Should -BeIn $repos.name } - AfterAll -ScriptBlock { + AfterAll { Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false } } - Context 'For public repos' { - # Skipping these tests for now, as it would run for a _very_ long time. - # No obviously good way to verify this. - } + Context 'When getting all public repositories' { + BeforeAll { + $repo1 = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) + $repo2 = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) - Context 'For a specific repo' { - BeforeAll -ScriptBlock { - $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + $repos = Get-GitHubRepository -GetAllPublicRepositories -Since $repo1.id } - It "Should be a single result using Uri ParameterSet" { - $result = Get-GitHubRepository -Uri $repo.svn_url - $result | Should -BeOfType PSCustomObject + It 'Should return an object of the correct type' { + $repos[0].PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' } - It "Should be a single result using Elements ParameterSet" { - $result = Get-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name - $result | Should -BeOfType PSCustomObject + It 'Should return at least one result' { + $repos.count | Should -BeGreaterOrEqual 1 } - It 'Should not permit additional parameters' { - { Get-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name -Type All } | Should -Throw + It "Should return the correct membership" { + $repo2.name | Should -BeIn $repos.name } - It 'Should require both OwnerName and RepositoryName' { - { Get-GitHubRepository -RepositoryName $repo.name } | Should -Throw - { Get-GitHubRepository -Uri "https://github.com/$script:ownerName" } | Should -Throw + AfterAll { + Remove-GitHubRepository -Uri $repo1.svn_url -Force + Remove-GitHubRepository -Uri $repo2.svn_url -Force } + } - AfterAll -ScriptBlock { - Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + Context 'When getting a specific repository' { + BeforeAll { + $repoName = [Guid]::NewGuid().Guid + $newGitHubRepositoryParms = @{ + RepositoryName = $repoName + Description = $defaultRepoDesc + HomePage = $defaultRepoHomePage + } + + $repo = New-GitHubRepository @newGitHubRepositoryParms + } + + Context 'When specifiying the Uri parameter' { + BeforeAll { + $uriRepo = Get-GitHubRepository -Uri $repo.svn_url + } + + It 'Should return an object of the correct type' { + $uriRepo.PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' + } + + It 'Should return a single result' { + $uriRepo | Should -HaveCount 1 + } + + It 'Should return the correct properties' { + $uriRepo.name | Should -Be $repoName + $uriRepo.description | Should -Be $defaultRepoDesc + $uriRepo.homepage | Should -Be $defaultRepoHomePage + } + } + + Context 'When specifying the Owner and RepositoryName parameters' { + BeforeAll { + $elementsRepo = Get-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name + } + + It 'Should return an object of the correct type' { + $uriRepo.PSObject.TypeNames[0] | Should -Be 'GitHub.Repository' + } + + It 'Should return a single result' { + $uriRepo | Should -HaveCount 1 + } + + It 'Should return the correct properties' { + $uriRepo.name | Should -Be $repoName + $uriRepo.description | Should -Be $defaultRepoDesc + $uriRepo.homepage | Should -Be $defaultRepoHomePage + } + + Context 'When specifying additional invalid parameters' { + It 'Should throw the correct exception' { + $errorMessage = 'Unable to specify -Type, -Sort and/or -Direction when retrieving a specific repository.' + { Get-GitHubRepository -OwnerName $repo.owner.login -RepositoryName $repo.name -Type All } | + Should -Throw $errorMessage + } + } + } + + Context 'When specifying only the Repository parameter' { + It 'Should throw the correct exception' { + $errorMessage = 'OwnerName could not be determined.' + { Get-GitHubRepository -RepositoryName $repo.name } | Should -Throw $errorMessage + } + } + + AfterAll { + if (Get-Variable -Name repo -ErrorAction SilentlyContinue) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } } } } - Describe 'Deleting repositories' { + Describe 'GitHubRepositories\Delete-GitHubRepository' { - Context -Name 'For deleting a repository' -Fixture { + Context -Name 'When deleting a repository' -Fixture { BeforeEach -ScriptBlock { $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -Description $defaultRepoDesc -AutoInit } @@ -477,9 +637,9 @@ try } } - Describe 'Renaming repositories' { + Describe 'GitHubRepositories\Rename-GitHubRepository' { - Context -Name 'For renaming a repository' -Fixture { + Context -Name 'When renaming a repository' -Fixture { BeforeEach -Scriptblock { $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit $suffixToAddToRepo = "_renamed" @@ -607,7 +767,7 @@ try } AfterAll -ScriptBlock { - if ($repo) + if (Get-Variable -Name repo -ErrorAction SilentlyContinue) { Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false } @@ -713,11 +873,14 @@ try } } - Describe 'Get/set repository topic' { + Describe 'GitHubRepositories\Get-GitHubRepositoryTopic' { - Context -Name 'For creating and getting a repository topic' -Fixture { - BeforeAll -ScriptBlock { - $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + Context -Name 'When getting a repository topic' { + BeforeAll { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) + Set-GitHubRepositoryTopic -OwnerName $repo.owner.login -RepositoryName $repo.name -Name $defaultRepoTopic | + Out-Null + $topic = Get-GitHubRepositoryTopic -OwnerName $repo.owner.login -RepositoryName $repo.name } It 'Should have the expected topic' { @@ -766,128 +929,236 @@ try } } - AfterAll -ScriptBlock { + AfterAll { Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false } } } - Describe 'Get repository languages' { + Describe 'GitHubRepositories\Set-GitHubRepositoryTopic' { + BeforeAll { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) + $topic = Set-GitHubRepositoryTopic -OwnerName $repo.owner.login -RepositoryName $repo.name -Name $defaultRepoTopic + } - Context -Name 'For getting repository languages' -Fixture { - BeforeAll -ScriptBlock { - $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + Context -Name 'When setting a repository topic' { + It 'Should return an object of the correct type' { + $topic | Should -BeOfType PSCustomObject } - It 'Should be empty' { - $languages = Get-GitHubRepositoryLanguage -OwnerName $repo.owner.login -RepositoryName $repo.name - $languages | Should -BeNullOrEmpty + It 'Should return the correct properties' { + $defaultRepoTopic | Should -BeIn $topic.names } + } - It 'Should contain PowerShell' { - $languages = Get-GitHubRepositoryLanguage -OwnerName "microsoft" -RepositoryName "PowerShellForGitHub" - $languages.PowerShell | Should -Not -BeNullOrEmpty - $languages.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryLanguage' + Context -Name 'When clearing all repository topics' { + BeforeAll { + $topic = Set-GitHubRepositoryTopic -OwnerName $repo.owner.login -RepositoryName $repo.name -Clear } - It 'Should contain PowerShell (via pipeline)' { - $psfg = Get-GitHubRepository -OwnerName "microsoft" -RepositoryName "PowerShellForGitHub" - $languages = $psfg | Get-GitHubRepositoryLanguage - $languages.PowerShell | Should -Not -BeNullOrEmpty - $languages.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryLanguage' + It 'Should return an object of the correct type' { + $topic.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryTopic' } - AfterAll -ScriptBlock { - Remove-GitHubRepository -Uri $repo.svn_url -Force + It 'Should return the correct properties' { + $topic.names | Should -BeNullOrEmpty } } + + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } } - Describe 'Get repository tags' { + Describe 'GitHubRepositories\Get-GitHubRepositoryContributor' { + BeforeAll { + $repoName = [Guid]::NewGuid().Guid + $repo = New-GitHubRepository -RepositoryName $repoName -AutoInit + } - Context -Name 'For getting repository tags' -Fixture { - BeforeAll -ScriptBlock { - $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + Context 'When getting GitHub Repository Contributors' { + BeforeAll { + $getGitHubRepositoryContributorParms = @{ + OwnerName = $repo.owner.login + RepositoryName = $repoName + } + + $contributors = @(Get-GitHubRepositoryContributor @getGitHubRepositoryContributorParms) } - It 'Should be empty' { - $tags = Get-GitHubRepositoryTag -OwnerName $repo.owner.login -RepositoryName $repo.name - $tags | Should -BeNullOrEmpty + It 'Should return objects of the correct type' { + $contributors[0].PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryContributor' } - It 'Should be empty (via pipeline)' { - $tags = $repo | Get-GitHubRepositoryTag - $tags | Should -BeNullOrEmpty + It 'Should return expected number of contributors' { + $contributors.Count | Should -Be 1 } - AfterAll -ScriptBlock { - Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + It "Should return the correct membership" { + $repo.owner.login | Should -BeIn $contributors.login } } - } - Describe 'Contributors for a repository' { - BeforeAll { - $repo = New-GitHubRepository -RepositoryName ([guid]::NewGuid().Guid) -AutoInit - } + Context 'When getting Github Repository Contributors with Statistics' { + BeforeAll { + $getGitHubRepositoryContributorParms = @{ + OwnerName = $repo.owner.login + RepositoryName = $repoName + IncludeStatistics = $true + } - AfterAll { - $null = Remove-GitHubRepository -Uri $repo.RepositoryUrl -Confirm:$false - } + $contributors = @(Get-GitHubRepositoryContributor @getGitHubRepositoryContributorParms) + } - Context -Name 'Obtaining contributors for repository' -Fixture { - $contributors = @(Get-GitHubRepositoryContributor -Uri $repo.RepositoryUrl) + It 'Should return objects of the correct type' { + $contributors[0].PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryContributorStatistics' + $contributors[0].author.PSObject.TypeNames[0] | Should -Be 'GitHub.User' + } It 'Should return expected number of contributors' { $contributors.Count | Should -Be 1 - $contributors[0].PSObject.TypeNames[0] = 'GitHub.RepositoryContributor' } - } - Context -Name 'Obtaining contributors for repository (via pipeline)' -Fixture { - $contributors = @($repo | Get-GitHubRepositoryContributor -IncludeStatistics) + It 'Should return the correct membership' { + $repo.owner.login | Should -BeIn $contributors.author.login + } - It 'Should return expected number of contributors' { - $contributors.Count | Should -Be 1 - $contributors[0].PSObject.TypeNames[0] = 'GitHub.RepositoryContributor' + It 'Should return the correct properties' { + $contributors.weeks | Should -Not -BeNullOrEmpty } } - Context -Name 'Obtaining contributor statistics for repository' -Fixture { - $stats = @(Get-GitHubRepositoryContributor -Uri $repo.RepositoryUrl -IncludeStatistics) + Context 'When getting Github Repository Contributors including Anonymous' { + BeforeAll { + $getGitHubRepositoryContributorParms = @{ + OwnerName = $repo.owner.login + RepositoryName = $repoName + IncludeAnonymousContributors = $true + } + + $contributors = @(Get-GitHubRepositoryContributor @getGitHubRepositoryContributorParms) + } - It 'Should return expected number of contributors' { - $stats.Count | Should -Be 1 - $stats[0].PSObject.TypeNames[0] = 'GitHub.RepositoryContributorStatistics' - $stats[0].author.PSObject.TypeNames[0] = 'GitHub.User' + It 'Should return objects of the correct type' { + $contributors[0].PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryContributor' + } + + It 'Should return at least one result' { + $contributors.count | Should -BeGreaterOrEqual 1 + } + + It 'Should return the correct membership' { + $repo.owner.login | Should -BeIn $contributors.login } } + + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } } - Describe 'Collaborators for a repository' { + Describe 'GitHubRepositories\Get-GitHubRepositoryCollaborator' { BeforeAll { - $repo = New-GitHubRepository -RepositoryName ([guid]::NewGuid().Guid) -AutoInit + $repoName = [Guid]::NewGuid().Guid + $repo = New-GitHubRepository -RepositoryName $repoName -AutoInit } - AfterAll { - $null = Remove-GitHubRepository -Uri $repo.RepositoryUrl -Confirm:$false - } + Context 'When getting GitHub Repository Collaborators' { + BeforeAll { + $getGitHubRepositoryCollaboratorParms = @{ + OwnerName = $repo.owner.login + RepositoryName = $repoName + } - Context -Name 'Obtaining collaborators for repository' -Fixture { - $collaborators = @(Get-GitHubRepositoryCollaborator -Uri $repo.RepositoryUrl) + $collaborators = @(Get-GitHubRepositoryCollaborator @getGitHubRepositoryCollaboratorParms) + } + + It 'Should return objects of the correct type' { + $collaborators[0].PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryCollaborator' + } It 'Should return expected number of collaborators' { $collaborators.Count | Should -Be 1 - $collaborators[0].PSObject.TypeNames[0] = 'GitHub.RepositoryCollaborator' + } + + It "Should return the correct membership" { + $repo.owner.login | Should -BeIn $collaborators.login } } - Context -Name 'Obtaining collaborators for repository (via pipeline)' -Fixture { - $collaborators = @($repo | Get-GitHubRepositoryCollaborator) + Context 'When getting GitHub Repository Collaborators (via pipeline)' { + BeforeAll { + $collaborators = @($repo | Get-GitHubRepositoryCollaborator) + } + + It 'Should return objects of the correct type' { + $collaborators[0].PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryCollaborator' + } It 'Should return expected number of collaborators' { $collaborators.Count | Should -Be 1 - $collaborators[0].PSObject.TypeNames[0] = 'GitHub.RepositoryCollaborator' + } + + It "Should return the correct membership" { + $repo.owner.login | Should -BeIn $collaborators.login + } + } + + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } + + Describe 'GitHubRepositories\Get-GitHubRepositoryLanguage' { + + Context -Name 'When getting repository languages' { + BeforeAll { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + } + + It 'Should be empty' { + $languages = Get-GitHubRepositoryLanguage -OwnerName $repo.owner.login -RepositoryName $repo.name + $languages | Should -BeNullOrEmpty + } + + It 'Should contain PowerShell' { + $languages = Get-GitHubRepositoryLanguage -OwnerName "microsoft" -RepositoryName "PowerShellForGitHub" + $languages.PowerShell | Should -Not -BeNullOrEmpty + $languages.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryLanguage' + } + + It 'Should contain PowerShell (via pipeline)' { + $psfg = Get-GitHubRepository -OwnerName "microsoft" -RepositoryName "PowerShellForGitHub" + $languages = $psfg | Get-GitHubRepositoryLanguage + $languages.PowerShell | Should -Not -BeNullOrEmpty + $languages.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryLanguage' + } + + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Force + } + } + } + + Describe 'GitHubRepositories\Get-GitHubRepositoryTag' { + + Context -Name 'When getting repository tags' { + BeforeAll { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + } + + It 'Should be empty' { + $tags = Get-GitHubRepositoryTag -OwnerName $repo.owner.login -RepositoryName $repo.name + $tags | Should -BeNullOrEmpty + } + + It 'Should be empty (via pipeline)' { + $tags = $repo | Get-GitHubRepositoryTag + $tags | Should -BeNullOrEmpty + } + + AfterAll { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false } } }