From 390f28fa3719c2d196b4c0b262d0fa4502b9709d Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Wed, 14 Nov 2018 14:05:27 -0800 Subject: [PATCH 1/8] Add traffic support --- GitHubTrafficSupport.ps1 | 364 +++++++++++++++++++++++++++ PowerShellForGitHub.psd1 | 5 + Tests/GitHubTrafficSupport.tests.ps1 | 127 ++++++++++ 3 files changed, 496 insertions(+) create mode 100644 GitHubTrafficSupport.ps1 create mode 100644 Tests/GitHubTrafficSupport.tests.ps1 diff --git a/GitHubTrafficSupport.ps1 b/GitHubTrafficSupport.ps1 new file mode 100644 index 00000000..3d3a6eec --- /dev/null +++ b/GitHubTrafficSupport.ps1 @@ -0,0 +1,364 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +function Get-GitHubReferrerTraffic +{ +<# + .SYNOPSIS + Get the top 10 referrers over the last 14 days for a given GitHub repository. + + .DESCRIPTION + Get the top 10 referrers over the last 14 days for a given GitHub repository. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Get-GitHubPathTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + + Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub project. + + .EXAMPLE + Get-GitHubLabel -OwnerName Powershell -RepositoryName PowerShellForGitHub -LabelName TestLabel + + Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub + project. +#> + [CmdletBinding( + SupportsShouldProcess, + DefaultParametersetName='Elements')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + param( + [Parameter(ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ParameterSetName='Uri')] + [string] $Uri, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/popular/referrers" + 'Description' = "Get top 10 referrers for $RepositoryName" + 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethodMultipleResult @params +} + +function Get-GitHubPathTraffic +{ +<# + .SYNOPSIS + Get the top 10 popular contents over the last 14 days for a given Github repository. + + .DESCRIPTION + Get the top 10 popular contents over the last 14 days for a given GitHub repository. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Get-GitHubPathTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + + Get the top 10 popular contents over the last 14 days from the PowerShell\PowerShellForGitHub project. + + .EXAMPLE + Get-GitHubLabel -OwnerName Powershell -RepositoryName PowerShellForGitHub -LabelName TestLabel + + Get the top 10 popular contents over the last 14 days from the PowerShell\PowerShellForGitHub + project. +#> + [CmdletBinding( + SupportsShouldProcess, + DefaultParametersetName='Elements')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + param( + [Parameter(ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ParameterSetName='Uri')] + [string] $Uri, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/popular/paths" + 'Description' = "Get top 10 popular contents for $RepositoryName" + 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethodMultipleResult @params +} + +function Get-GitHubViewTraffic +{ +<# + .SYNOPSIS + Get the total number of views and breakdown per day or week for the last 14 days for the given Github Repository. + + .DESCRIPTION + Get the total number of views and breakdown per day or week for the last 14 days. + Timestamps are aligned to UTC midnight of the beginning of the day or week. Week begins on Monday. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Get-GitHubPathTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + + Get the total number of views and breakdown per day or week for the last 14 days from the PowerShell\PowerShellForGitHub project. + + .EXAMPLE + Get-GitHubLabel -OwnerName Powershell -RepositoryName PowerShellForGitHub -LabelName TestLabel + + Get the total number of views and breakdown per day or week for the last 14 days from the PowerShell\PowerShellForGitHub + project. +#> + [CmdletBinding( + SupportsShouldProcess, + DefaultParametersetName='Elements')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + param( + [Parameter(ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ParameterSetName='Uri')] + [string] $Uri, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/views" + 'Description' = "Get the total number of views and breakdown per day or week for the last 14 days for $RepositoryName" + 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethodMultipleResult @params +} + +function Get-GitHubCloneTraffic +{ +<# + .SYNOPSIS + Get the total number of clones and breakdown per day or week for the last 14 days for the given Github Repository. + + .DESCRIPTION + Get the total number of clones and breakdown per day or week for the last 14 days. + Timestamps are aligned to UTC midnight of the beginning of the day or week. Week begins on Monday. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Get-GitHubCloneTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + + Get the total number of clones and breakdown per day or week for the last 14 days from the PowerShell\PowerShellForGitHub project. + + .EXAMPLE + Get-GitHubLabel -OwnerName Powershell -RepositoryName PowerShellForGitHub -LabelName TestLabel + + Get the total number of clones and breakdown per day or week for the last 14 days from the PowerShell\PowerShellForGitHub + project. +#> + [CmdletBinding( + SupportsShouldProcess, + DefaultParametersetName='Elements')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] + param( + [Parameter(ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ParameterSetName='Uri')] + [string] $Uri, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/clones" + 'Description' = "Get the total number of clones and breakdown per day or week for the last 14 days for $RepositoryName" + 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + return Invoke-GHRestMethodMultipleResult @params +} \ No newline at end of file diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index e32bc165..5b909596 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -30,6 +30,7 @@ 'GitHubRepositories.ps1', 'GitHubRepositoryForks.ps1', 'GitHubTeams.ps1', + 'GitHubTrafficSupport.ps1', 'GitHubUsers.ps1', 'NugetTools.ps1', 'Telemetry.ps1') @@ -42,6 +43,7 @@ 'Backup-GitHubConfiguration', 'Clear-GitHubAuthentication', 'ConvertFrom-Markdown', + 'Get-GitHubCloneTraffic', 'Get-GitHubCodeOfConduct', 'Get-GitHubConfiguration', 'Get-GitHubEmoji', @@ -51,8 +53,10 @@ 'Get-GitHubLabel', 'Get-GitHubLicense', 'Get-GitHubOrganizationMember', + 'Get-GitHubPathTraffic', 'Get-GitHubPullRequest', 'Get-GitHubRateLimit', + 'Get-GitHubReferrerTraffic', 'Get-GitHubRepository', 'Get-GitHubRepositoryBranch', 'Get-GitHubRepositoryCollaborator', @@ -66,6 +70,7 @@ 'Get-GitHubTeamMember', 'Get-GitHubUser', 'Get-GitHubUserContextualInformation', + 'Get-GitHubViewTraffic', 'Group-GitHubIssue', 'Invoke-GHRestMethod', 'Invoke-GHRestMethodMultipleResult', diff --git a/Tests/GitHubTrafficSupport.tests.ps1 b/Tests/GitHubTrafficSupport.tests.ps1 new file mode 100644 index 00000000..5c338663 --- /dev/null +++ b/Tests/GitHubTrafficSupport.tests.ps1 @@ -0,0 +1,127 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +<# +.Synopsis + Tests for GitHubRepositoryForks.ps1 module +#> + +[String] $root = Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path) +. (Join-Path -Path $root -ChildPath 'Tests\Config\Settings.ps1') +Import-Module -Name $root -Force + +function Initialize-AppVeyor +{ +<# + .SYNOPSIS + Configures the tests to run with the authentication information stored in AppVeyor + (if that information exists in the environment). + + .DESCRIPTION + Configures the tests to run with the authentication information stored in AppVeyor + (if that information exists in the environment). + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .NOTES + Internal-only helper method. + + The only reason this exists is so that we can leverage CodeAnalysis.SuppressMessageAttribute, + which can only be applied to functions. + + We call this immediately after the declaration so that AppVeyor initialization can heppen + (if applicable). + +#> + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification="Needed to configure with the stored, encrypted string value in AppVeyor.")] + param() + + if ($env:AppVeyor) + { + $secureString = $env:avAccessToken | ConvertTo-SecureString -AsPlainText -Force + $cred = New-Object System.Management.Automation.PSCredential "", $secureString + Set-GitHubAuthentication -Credential $cred + + $script:ownerName = $env:avOwnerName + $script:organizationName = $env:avOrganizationName + + $message = @( + 'This run is executed in the AppVeyor environment.', + 'The GitHub Api Token won''t be decrypted in PR runs causing some tests to fail.', + '403 errors possible due to GitHub hourly limit for unauthenticated queries.', + 'Use Set-GitHubAuthentication manually. modify the values in Tests\Config\Settings.ps1,', + 'and run tests on your machine first.') + Write-Warning -Message ($message -join [Environment]::NewLine) + } +} + +Initialize-AppVeyor + +$script:accessTokenConfigured = Test-GitHubAuthenticationConfigured +if (-not $script:accessTokenConfigured) +{ + $message = @( + 'GitHub API Token not defined, some of the tests will be skipped.', + '403 errors possible due to GitHub hourly limit for unauthenticated queries.') + Write-Warning -Message ($message -join [Environment]::NewLine) +} + +# Backup the user's configuration before we begin, and ensure we're at a pure state before running +# the tests. We'll restore it at the end. +$configFile = New-TemporaryFile +Backup-GitHubConfiguration -Path $configFile +Reset-GitHubConfiguration + +Describe 'Getting the referrer list' { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + + Context 'When initially created, there are no referrers' { + $referrerList = Get-GitHubReferrerTraffic -Uri $repo.svn_url + + It 'Should return expected number of referrers' { + @($referrerList).Count | Should be 0 + } + + Remove-GitHubRepository -Uri $repo.svn_url + } +} + +Describe 'Getting the popular content over the last 14 days' { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + + Context 'When initially created, there are is no popular content' { + $pathList = Get-GitHubPathTraffic -Uri $repo.svn_url + + It 'Should return expected number of popular content' { + @($pathList).Count | Should be 0 + } + } +} + +Describe 'Getting the views over the last 14 days' { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + + Context 'When initially created, there are no views' { + $viewList = Get-GitHubViewTraffic -Uri $repo.svn_url + + It 'Should return 0 in the count property' { + $viewList.Count | Should be 0 + } + } +} + +Describe 'Getting the clones over the last 14 days' { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + + Context 'When initially created, there is 0 clones' { + $cloneList = Get-GitHubCloneTraffic -Uri $repo.svn_url + + It 'Should return expected number of clones' { + $cloneList.Count | Should be 0 + } + } +} + +# Restore the user's configuration to its pre-test state +Restore-GitHubConfiguration -Path $configFile From 16b617a4da782ead0dc65c3d099986194ce1ebb9 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Wed, 14 Nov 2018 14:55:08 -0800 Subject: [PATCH 2/8] Use regular get --- GitHubTrafficSupport.ps1 | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/GitHubTrafficSupport.ps1 b/GitHubTrafficSupport.ps1 index 3d3a6eec..81ae3fd5 100644 --- a/GitHubTrafficSupport.ps1 +++ b/GitHubTrafficSupport.ps1 @@ -80,6 +80,7 @@ function Get-GitHubReferrerTraffic $params = @{ 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/popular/referrers" + 'Method' = 'Get' 'Description' = "Get top 10 referrers for $RepositoryName" 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' 'AccessToken' = $AccessToken @@ -88,7 +89,7 @@ function Get-GitHubReferrerTraffic 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) } - return Invoke-GHRestMethodMultipleResult @params + return Invoke-GHRestMethod @params } function Get-GitHubPathTraffic @@ -170,6 +171,7 @@ function Get-GitHubPathTraffic $params = @{ 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/popular/paths" + 'Method' = 'Get' 'Description' = "Get top 10 popular contents for $RepositoryName" 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' 'AccessToken' = $AccessToken @@ -178,7 +180,7 @@ function Get-GitHubPathTraffic 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) } - return Invoke-GHRestMethodMultipleResult @params + return Invoke-GHRestMethod @params } function Get-GitHubViewTraffic @@ -261,6 +263,7 @@ function Get-GitHubViewTraffic $params = @{ 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/views" + 'Method' = 'Get' 'Description' = "Get the total number of views and breakdown per day or week for the last 14 days for $RepositoryName" 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' 'AccessToken' = $AccessToken @@ -269,7 +272,7 @@ function Get-GitHubViewTraffic 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) } - return Invoke-GHRestMethodMultipleResult @params + return Invoke-GHRestMethod @params } function Get-GitHubCloneTraffic @@ -352,6 +355,7 @@ function Get-GitHubCloneTraffic $params = @{ 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/clones" + 'Method' = 'Get' 'Description' = "Get the total number of clones and breakdown per day or week for the last 14 days for $RepositoryName" 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' 'AccessToken' = $AccessToken @@ -360,5 +364,5 @@ function Get-GitHubCloneTraffic 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus) } - return Invoke-GHRestMethodMultipleResult @params + return Invoke-GHRestMethod @params } \ No newline at end of file From 8f71142e82ddf5c387aeb445001eff921d7a5eb0 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Wed, 14 Nov 2018 15:06:58 -0800 Subject: [PATCH 3/8] Fix duplicate comment --- GitHubTrafficSupport.ps1 | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/GitHubTrafficSupport.ps1 b/GitHubTrafficSupport.ps1 index 81ae3fd5..4f636913 100644 --- a/GitHubTrafficSupport.ps1 +++ b/GitHubTrafficSupport.ps1 @@ -36,15 +36,9 @@ function Get-GitHubReferrerTraffic If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Get-GitHubPathTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + Get-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub project. - - .EXAMPLE - Get-GitHubLabel -OwnerName Powershell -RepositoryName PowerShellForGitHub -LabelName TestLabel - - Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub - project. #> [CmdletBinding( SupportsShouldProcess, @@ -130,12 +124,6 @@ function Get-GitHubPathTraffic Get-GitHubPathTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub Get the top 10 popular contents over the last 14 days from the PowerShell\PowerShellForGitHub project. - - .EXAMPLE - Get-GitHubLabel -OwnerName Powershell -RepositoryName PowerShellForGitHub -LabelName TestLabel - - Get the top 10 popular contents over the last 14 days from the PowerShell\PowerShellForGitHub - project. #> [CmdletBinding( SupportsShouldProcess, @@ -219,15 +207,9 @@ function Get-GitHubViewTraffic If not supplied here, the DefaultNoStatus configuration property value will be used. .EXAMPLE - Get-GitHubPathTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub + Get-GitHubViewTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub Get the total number of views and breakdown per day or week for the last 14 days from the PowerShell\PowerShellForGitHub project. - - .EXAMPLE - Get-GitHubLabel -OwnerName Powershell -RepositoryName PowerShellForGitHub -LabelName TestLabel - - Get the total number of views and breakdown per day or week for the last 14 days from the PowerShell\PowerShellForGitHub - project. #> [CmdletBinding( SupportsShouldProcess, @@ -314,12 +296,6 @@ function Get-GitHubCloneTraffic Get-GitHubCloneTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub Get the total number of clones and breakdown per day or week for the last 14 days from the PowerShell\PowerShellForGitHub project. - - .EXAMPLE - Get-GitHubLabel -OwnerName Powershell -RepositoryName PowerShellForGitHub -LabelName TestLabel - - Get the total number of clones and breakdown per day or week for the last 14 days from the PowerShell\PowerShellForGitHub - project. #> [CmdletBinding( SupportsShouldProcess, From 56984356acd818fb8574a124e660dc27452892b6 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Wed, 14 Nov 2018 15:11:55 -0800 Subject: [PATCH 4/8] rename files --- GitHubTrafficSupport.ps1 => GitHubTraffic.ps1 | 0 PowerShellForGitHub.psd1 | 2 +- .../{GitHubTrafficSupport.tests.ps1 => GitHubTraffic.tests.ps1} | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename GitHubTrafficSupport.ps1 => GitHubTraffic.ps1 (100%) rename Tests/{GitHubTrafficSupport.tests.ps1 => GitHubTraffic.tests.ps1} (100%) diff --git a/GitHubTrafficSupport.ps1 b/GitHubTraffic.ps1 similarity index 100% rename from GitHubTrafficSupport.ps1 rename to GitHubTraffic.ps1 diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 5b909596..f1bb1ec7 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -30,7 +30,7 @@ 'GitHubRepositories.ps1', 'GitHubRepositoryForks.ps1', 'GitHubTeams.ps1', - 'GitHubTrafficSupport.ps1', + 'GitHubTraffic.ps1', 'GitHubUsers.ps1', 'NugetTools.ps1', 'Telemetry.ps1') diff --git a/Tests/GitHubTrafficSupport.tests.ps1 b/Tests/GitHubTraffic.tests.ps1 similarity index 100% rename from Tests/GitHubTrafficSupport.tests.ps1 rename to Tests/GitHubTraffic.tests.ps1 From d383c18758bb79a31063204913c15f6d2bbc1c03 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Wed, 14 Nov 2018 15:30:27 -0800 Subject: [PATCH 5/8] Delete repros --- Tests/GitHubTrafficSupport.tests.ps1 | 133 +++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 Tests/GitHubTrafficSupport.tests.ps1 diff --git a/Tests/GitHubTrafficSupport.tests.ps1 b/Tests/GitHubTrafficSupport.tests.ps1 new file mode 100644 index 00000000..752ddf84 --- /dev/null +++ b/Tests/GitHubTrafficSupport.tests.ps1 @@ -0,0 +1,133 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +<# +.Synopsis + Tests for GitHubRepositoryForks.ps1 module +#> + +[String] $root = Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path) +. (Join-Path -Path $root -ChildPath 'Tests\Config\Settings.ps1') +Import-Module -Name $root -Force + +function Initialize-AppVeyor +{ +<# + .SYNOPSIS + Configures the tests to run with the authentication information stored in AppVeyor + (if that information exists in the environment). + + .DESCRIPTION + Configures the tests to run with the authentication information stored in AppVeyor + (if that information exists in the environment). + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .NOTES + Internal-only helper method. + + The only reason this exists is so that we can leverage CodeAnalysis.SuppressMessageAttribute, + which can only be applied to functions. + + We call this immediately after the declaration so that AppVeyor initialization can heppen + (if applicable). + +#> + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification="Needed to configure with the stored, encrypted string value in AppVeyor.")] + param() + + if ($env:AppVeyor) + { + $secureString = $env:avAccessToken | ConvertTo-SecureString -AsPlainText -Force + $cred = New-Object System.Management.Automation.PSCredential "", $secureString + Set-GitHubAuthentication -Credential $cred + + $script:ownerName = $env:avOwnerName + $script:organizationName = $env:avOrganizationName + + $message = @( + 'This run is executed in the AppVeyor environment.', + 'The GitHub Api Token won''t be decrypted in PR runs causing some tests to fail.', + '403 errors possible due to GitHub hourly limit for unauthenticated queries.', + 'Use Set-GitHubAuthentication manually. modify the values in Tests\Config\Settings.ps1,', + 'and run tests on your machine first.') + Write-Warning -Message ($message -join [Environment]::NewLine) + } +} + +Initialize-AppVeyor + +$script:accessTokenConfigured = Test-GitHubAuthenticationConfigured +if (-not $script:accessTokenConfigured) +{ + $message = @( + 'GitHub API Token not defined, some of the tests will be skipped.', + '403 errors possible due to GitHub hourly limit for unauthenticated queries.') + Write-Warning -Message ($message -join [Environment]::NewLine) +} + +# Backup the user's configuration before we begin, and ensure we're at a pure state before running +# the tests. We'll restore it at the end. +$configFile = New-TemporaryFile +Backup-GitHubConfiguration -Path $configFile +Reset-GitHubConfiguration + +Describe 'Getting the referrer list' { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + + Context 'When initially created, there are no referrers' { + $referrerList = Get-GitHubReferrerTraffic -Uri $repo.svn_url + + It 'Should return expected number of referrers' { + @($referrerList).Count | Should be 0 + } + + Remove-GitHubRepository -Uri $repo.svn_url + } +} + +Describe 'Getting the popular content over the last 14 days' { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + + Context 'When initially created, there are is no popular content' { + $pathList = Get-GitHubPathTraffic -Uri $repo.svn_url + + It 'Should return expected number of popular content' { + @($pathList).Count | Should be 0 + } + + Remove-GitHubRepository -Uri $repo.svn_url + } +} + +Describe 'Getting the views over the last 14 days' { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + + Context 'When initially created, there are no views' { + $viewList = Get-GitHubViewTraffic -Uri $repo.svn_url + + It 'Should return 0 in the count property' { + $viewList.Count | Should be 0 + } + } + + Remove-GitHubRepository -Uri $repo.svn_url +} + +Describe 'Getting the clones over the last 14 days' { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit + + Context 'When initially created, there is 0 clones' { + $cloneList = Get-GitHubCloneTraffic -Uri $repo.svn_url + + It 'Should return expected number of clones' { + $cloneList.Count | Should be 0 + } + } + + Remove-GitHubRepository -Uri $repo.svn_url +} + +# Restore the user's configuration to its pre-test state +Restore-GitHubConfiguration -Path $configFile From 9ab5b62feedd7329d37f209e896b55a94302e5c5 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Wed, 14 Nov 2018 15:32:54 -0800 Subject: [PATCH 6/8] Move deletion into curly brace --- Tests/GitHubTrafficSupport.tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/GitHubTrafficSupport.tests.ps1 b/Tests/GitHubTrafficSupport.tests.ps1 index 752ddf84..eee59303 100644 --- a/Tests/GitHubTrafficSupport.tests.ps1 +++ b/Tests/GitHubTrafficSupport.tests.ps1 @@ -110,9 +110,9 @@ Describe 'Getting the views over the last 14 days' { It 'Should return 0 in the count property' { $viewList.Count | Should be 0 } - } - Remove-GitHubRepository -Uri $repo.svn_url + Remove-GitHubRepository -Uri $repo.svn_url + } } Describe 'Getting the clones over the last 14 days' { @@ -124,9 +124,9 @@ Describe 'Getting the clones over the last 14 days' { It 'Should return expected number of clones' { $cloneList.Count | Should be 0 } - } - Remove-GitHubRepository -Uri $repo.svn_url + Remove-GitHubRepository -Uri $repo.svn_url + } } # Restore the user's configuration to its pre-test state From 9af7dc28403091fc536947732bf5fb4e618624e4 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Wed, 14 Nov 2018 15:52:22 -0800 Subject: [PATCH 7/8] Delete renamed file that was added --- Tests/GitHubTraffic.tests.ps1 | 6 ++ Tests/GitHubTrafficSupport.tests.ps1 | 133 --------------------------- 2 files changed, 6 insertions(+), 133 deletions(-) delete mode 100644 Tests/GitHubTrafficSupport.tests.ps1 diff --git a/Tests/GitHubTraffic.tests.ps1 b/Tests/GitHubTraffic.tests.ps1 index 5c338663..eee59303 100644 --- a/Tests/GitHubTraffic.tests.ps1 +++ b/Tests/GitHubTraffic.tests.ps1 @@ -96,6 +96,8 @@ Describe 'Getting the popular content over the last 14 days' { It 'Should return expected number of popular content' { @($pathList).Count | Should be 0 } + + Remove-GitHubRepository -Uri $repo.svn_url } } @@ -108,6 +110,8 @@ Describe 'Getting the views over the last 14 days' { It 'Should return 0 in the count property' { $viewList.Count | Should be 0 } + + Remove-GitHubRepository -Uri $repo.svn_url } } @@ -120,6 +124,8 @@ Describe 'Getting the clones over the last 14 days' { It 'Should return expected number of clones' { $cloneList.Count | Should be 0 } + + Remove-GitHubRepository -Uri $repo.svn_url } } diff --git a/Tests/GitHubTrafficSupport.tests.ps1 b/Tests/GitHubTrafficSupport.tests.ps1 deleted file mode 100644 index eee59303..00000000 --- a/Tests/GitHubTrafficSupport.tests.ps1 +++ /dev/null @@ -1,133 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -<# -.Synopsis - Tests for GitHubRepositoryForks.ps1 module -#> - -[String] $root = Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path) -. (Join-Path -Path $root -ChildPath 'Tests\Config\Settings.ps1') -Import-Module -Name $root -Force - -function Initialize-AppVeyor -{ -<# - .SYNOPSIS - Configures the tests to run with the authentication information stored in AppVeyor - (if that information exists in the environment). - - .DESCRIPTION - Configures the tests to run with the authentication information stored in AppVeyor - (if that information exists in the environment). - - The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub - - .NOTES - Internal-only helper method. - - The only reason this exists is so that we can leverage CodeAnalysis.SuppressMessageAttribute, - which can only be applied to functions. - - We call this immediately after the declaration so that AppVeyor initialization can heppen - (if applicable). - -#> - [CmdletBinding()] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification="Needed to configure with the stored, encrypted string value in AppVeyor.")] - param() - - if ($env:AppVeyor) - { - $secureString = $env:avAccessToken | ConvertTo-SecureString -AsPlainText -Force - $cred = New-Object System.Management.Automation.PSCredential "", $secureString - Set-GitHubAuthentication -Credential $cred - - $script:ownerName = $env:avOwnerName - $script:organizationName = $env:avOrganizationName - - $message = @( - 'This run is executed in the AppVeyor environment.', - 'The GitHub Api Token won''t be decrypted in PR runs causing some tests to fail.', - '403 errors possible due to GitHub hourly limit for unauthenticated queries.', - 'Use Set-GitHubAuthentication manually. modify the values in Tests\Config\Settings.ps1,', - 'and run tests on your machine first.') - Write-Warning -Message ($message -join [Environment]::NewLine) - } -} - -Initialize-AppVeyor - -$script:accessTokenConfigured = Test-GitHubAuthenticationConfigured -if (-not $script:accessTokenConfigured) -{ - $message = @( - 'GitHub API Token not defined, some of the tests will be skipped.', - '403 errors possible due to GitHub hourly limit for unauthenticated queries.') - Write-Warning -Message ($message -join [Environment]::NewLine) -} - -# Backup the user's configuration before we begin, and ensure we're at a pure state before running -# the tests. We'll restore it at the end. -$configFile = New-TemporaryFile -Backup-GitHubConfiguration -Path $configFile -Reset-GitHubConfiguration - -Describe 'Getting the referrer list' { - $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit - - Context 'When initially created, there are no referrers' { - $referrerList = Get-GitHubReferrerTraffic -Uri $repo.svn_url - - It 'Should return expected number of referrers' { - @($referrerList).Count | Should be 0 - } - - Remove-GitHubRepository -Uri $repo.svn_url - } -} - -Describe 'Getting the popular content over the last 14 days' { - $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit - - Context 'When initially created, there are is no popular content' { - $pathList = Get-GitHubPathTraffic -Uri $repo.svn_url - - It 'Should return expected number of popular content' { - @($pathList).Count | Should be 0 - } - - Remove-GitHubRepository -Uri $repo.svn_url - } -} - -Describe 'Getting the views over the last 14 days' { - $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit - - Context 'When initially created, there are no views' { - $viewList = Get-GitHubViewTraffic -Uri $repo.svn_url - - It 'Should return 0 in the count property' { - $viewList.Count | Should be 0 - } - - Remove-GitHubRepository -Uri $repo.svn_url - } -} - -Describe 'Getting the clones over the last 14 days' { - $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit - - Context 'When initially created, there is 0 clones' { - $cloneList = Get-GitHubCloneTraffic -Uri $repo.svn_url - - It 'Should return expected number of clones' { - $cloneList.Count | Should be 0 - } - - Remove-GitHubRepository -Uri $repo.svn_url - } -} - -# Restore the user's configuration to its pre-test state -Restore-GitHubConfiguration -Path $configFile From c283c9ce5c5bc8b52bdad91728629b8d1039ded0 Mon Sep 17 00:00:00 2001 From: Pepe Rivera Date: Fri, 16 Nov 2018 14:35:35 -0800 Subject: [PATCH 8/8] Add per param --- GitHubTraffic.ps1 | 38 ++++++++++++++++++++++++++++---------- USAGE.md | 29 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/GitHubTraffic.ps1 b/GitHubTraffic.ps1 index 4f636913..f8db7d7b 100644 --- a/GitHubTraffic.ps1 +++ b/GitHubTraffic.ps1 @@ -75,8 +75,7 @@ function Get-GitHubReferrerTraffic $params = @{ 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/popular/referrers" 'Method' = 'Get' - 'Description' = "Get top 10 referrers for $RepositoryName" - 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'Description' = "Getting referrers for $RepositoryName" 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties @@ -160,8 +159,7 @@ function Get-GitHubPathTraffic $params = @{ 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/popular/paths" 'Method' = 'Get' - 'Description' = "Get top 10 popular contents for $RepositoryName" - 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'Description' = "Getting popular contents for $RepositoryName" 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties @@ -196,6 +194,9 @@ function Get-GitHubViewTraffic The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. + .PARAMETER Per + The interval at which return to return the view counts. + .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. @@ -227,6 +228,9 @@ function Get-GitHubViewTraffic ParameterSetName='Uri')] [string] $Uri, + [ValidateSet('day', 'week')] + [string] $Per = 'day', + [string] $AccessToken, [switch] $NoStatus @@ -241,13 +245,17 @@ function Get-GitHubViewTraffic $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'Per' = $Per } + $getParams = @( + "per=$Per" + ) + $params = @{ - 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/views" + 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/views`?" + ($getParams -join '&') 'Method' = 'Get' - 'Description' = "Get the total number of views and breakdown per day or week for the last 14 days for $RepositoryName" - 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'Description' = "Getting views for $RepositoryName" 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties @@ -282,6 +290,9 @@ function Get-GitHubCloneTraffic The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. + .PARAMETER Per + The interval at which return to return the view counts. + .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. @@ -313,6 +324,9 @@ function Get-GitHubCloneTraffic ParameterSetName='Uri')] [string] $Uri, + [ValidateSet('day', 'week')] + [string] $Per = 'day', + [string] $AccessToken, [switch] $NoStatus @@ -327,13 +341,17 @@ function Get-GitHubCloneTraffic $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'Per' = $Per } + $getParams = @( + "per=$Per" + ) + $params = @{ - 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/clones" + 'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/clones`?" + ($getParams -join '&') 'Method' = 'Get' - 'Description' = "Get the total number of clones and breakdown per day or week for the last 14 days for $RepositoryName" - 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' + 'Description' = "Getting number of clones for $RepositoryName" 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name 'TelemetryProperties' = $telemetryProperties diff --git a/USAGE.md b/USAGE.md index 064868c6..da6af709 100644 --- a/USAGE.md +++ b/USAGE.md @@ -25,6 +25,11 @@ * [Forks](#forks) * [Get all the forks for a repository](#get-all-the-forks-for-a-repository) * [Create a new fork](#create-a-new-fork) + * [Traffic](#traffic) + * [Get the referrer traffic for a repository](#get-the-referrer-traffic-for-a-repository) + * [Get the popular content for a repository](#get-the-popular-content-for-a-repository) + * [Get the number of views for a repository](#get-the-number-of-views-for-a-repository) + * [Get the number of clones for a repository](#get-the-number-of-clones-for-a-repository) ---------- @@ -313,3 +318,27 @@ Get-GitHubRepositoryFork -OwnerName PowerShell -RepositoryName PowerShellForGitH ```powershell New-GitHubRepositoryForm -OwnerName PowerShell -RepositoryName PowerShellForGitHub ``` + +---------- + +### Traffic + +#### Get the referrer traffic for a repository +```powershell +Get-GitHubReferrerTraffic -OwnerName PowerShell -RepositoryName PowerShellForGitHub +``` + +#### Get the popular content for a repository +```powershell +Get-GitHubPathTraffic -OwnerName PowerShell -RepositoryName PowerShellForGitHub +``` + +#### Get the number of views for a repository +```powershell +Get-GitHubViewTraffic -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Per 'week' +``` + +#### Get the number of clones for a repository +```powershell +Get-GitHubCloneTraffic -OwnerName PowerShell -RepositoryName PowerShellForGitHub -Per 'day' +```