PowerShellForGitHub/GitHubRepositoryTraffic.ps1

447 строки
14 KiB
PowerShell

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
@{
GitHubReferrerTrafficTypeName = 'GitHub.ReferrerTraffic'
GitHubPathTrafficTypeName = 'GitHub.PathTraffic'
GitHubViewTrafficTypeName = 'GitHub.ViewTraffic'
GitHubCloneTrafficTypeName = 'GitHub.CloneTraffic'
}.GetEnumerator() | ForEach-Object {
Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value
}
filter 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.
.INPUTS
GitHub.Branch
GitHub.Content
GitHub.Event
GitHub.Issue
GitHub.IssueComment
GitHub.Label
GitHub.Milestone
GitHub.PullRequest
GitHub.Project
GitHub.ProjectCard
GitHub.ProjectColumn
GitHub.Reaction
GitHub.Release
GitHub.ReleaseAsset
GitHub.Repository
.OUTPUTS
GitHub.ReferrerTraffic
.EXAMPLE
Get-GitHubReferrerTraffic -OwnerName microsoft -RepositoryName PowerShellForGitHub
Get the top 10 referrers over the last 14 days from the microsoft\PowerShellForGitHub project.
#>
[CmdletBinding(DefaultParameterSetName = 'Elements')]
[OutputType({$script:GitHubReferrerTrafficTypeName})]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="The Uri parameter is only referenced by Resolve-RepositoryElements which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[Parameter(ParameterSetName='Elements')]
[string] $OwnerName,
[Parameter(ParameterSetName='Elements')]
[string] $RepositoryName,
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='Uri')]
[Alias('RepositoryUrl')]
[string] $Uri,
[string] $AccessToken
)
Write-InvocationLog
$elements = Resolve-RepositoryElements
$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"
'Method' = 'Get'
'Description' = "Getting referrers for $RepositoryName"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
$result = Invoke-GHRestMethod @params
if ($null -ne $result)
{
$result.PSObject.TypeNames.Insert(0, $script:GitHubReferrerTrafficTypeName)
}
return $result
}
filter 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.
.INPUTS
GitHub.Branch
GitHub.Content
GitHub.Event
GitHub.Issue
GitHub.IssueComment
GitHub.Label
GitHub.Milestone
GitHub.PullRequest
GitHub.Project
GitHub.ProjectCard
GitHub.ProjectColumn
GitHub.Reaction
GitHub.Release
GitHub.ReleaseAsset
GitHub.Repository
.OUTPUTS
GitHub.PathTraffic
.EXAMPLE
Get-GitHubPathTraffic -OwnerName microsoft -RepositoryName PowerShellForGitHub
Get the top 10 popular contents over the last 14 days
from the microsoft\PowerShellForGitHub project.
#>
[CmdletBinding(DefaultParameterSetName = 'Elements')]
[OutputType({$script:GitHubPathTrafficTypeName})]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="The Uri parameter is only referenced by Resolve-RepositoryElements which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[Parameter(ParameterSetName='Elements')]
[string] $OwnerName,
[Parameter(ParameterSetName='Elements')]
[string] $RepositoryName,
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='Uri')]
[Alias('RepositoryUrl')]
[string] $Uri,
[string] $AccessToken
)
Write-InvocationLog
$elements = Resolve-RepositoryElements
$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"
'Method' = 'Get'
'Description' = "Getting popular contents for $RepositoryName"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
$result = Invoke-GHRestMethod @params
if ($null -ne $result)
{
$result.PSObject.TypeNames.Insert(0, $script:GitHubPathTrafficTypeName)
}
return $result
}
filter 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 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.
.INPUTS
GitHub.Branch
GitHub.Content
GitHub.Event
GitHub.Issue
GitHub.IssueComment
GitHub.Label
GitHub.Milestone
GitHub.PullRequest
GitHub.Project
GitHub.ProjectCard
GitHub.ProjectColumn
GitHub.Reaction
GitHub.Release
GitHub.ReleaseAsset
GitHub.Repository
.OUTPUTS
GitHub.ViewTraffic
.EXAMPLE
Get-GitHubViewTraffic -OwnerName microsoft -RepositoryName PowerShellForGitHub
Get the total number of views and breakdown per day or week for the last 14 days from
the microsoft\PowerShellForGitHub project.
#>
[CmdletBinding(DefaultParameterSetName = 'Elements')]
[OutputType({$script:GitHubViewTrafficTypeName})]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="The Uri parameter is only referenced by Resolve-RepositoryElements which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[Parameter(ParameterSetName='Elements')]
[string] $OwnerName,
[Parameter(ParameterSetName='Elements')]
[string] $RepositoryName,
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='Uri')]
[Alias('RepositoryUrl')]
[string] $Uri,
[ValidateSet('Day', 'Week')]
[string] $Per = 'Day',
[string] $AccessToken
)
Write-InvocationLog
$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties = @{
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
'Per' = $Per
}
$params = @{
'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/views`?per=$($Per.ToLower())"
'Method' = 'Get'
'Description' = "Getting views for $RepositoryName"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
$result = Invoke-GHRestMethod @params
if ($null -ne $result)
{
$result.PSObject.TypeNames.Insert(0, $script:GitHubViewTrafficTypeName)
}
return $result
}
filter 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 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.
.INPUTS
GitHub.Branch
GitHub.Content
GitHub.Event
GitHub.Issue
GitHub.IssueComment
GitHub.Label
GitHub.Milestone
GitHub.PullRequest
GitHub.Project
GitHub.ProjectCard
GitHub.ProjectColumn
GitHub.Reaction
GitHub.Release
GitHub.ReleaseAsset
GitHub.Repository
.OUTPUTS
GitHub.CloneTraffic
.EXAMPLE
Get-GitHubCloneTraffic -OwnerName microsoft -RepositoryName PowerShellForGitHub
Get the total number of clones and breakdown per day or week for the last 14 days
from the microsoft\PowerShellForGitHub project.
#>
[CmdletBinding(DefaultParameterSetName = 'Elements')]
[OutputType({$script:GitHubCloneTrafficTypeName})]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="The Uri parameter is only referenced by Resolve-RepositoryElements which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[Parameter(ParameterSetName='Elements')]
[string] $OwnerName,
[Parameter(ParameterSetName='Elements')]
[string] $RepositoryName,
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='Uri')]
[Alias('RepositoryUrl')]
[string] $Uri,
[ValidateSet('Day', 'Week')]
[string] $Per = 'Day',
[string] $AccessToken
)
Write-InvocationLog
$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
$telemetryProperties = @{
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
'Per' = $Per
}
$params = @{
'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/clones`?per=$($Per.ToLower())"
'Method' = 'Get'
'Description' = "Getting number of clones for $RepositoryName"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
}
$result = Invoke-GHRestMethod @params
if ($null -ne $result)
{
$result.PSObject.TypeNames.Insert(0, $script:GitHubCloneTrafficTypeName)
}
return $result
}