Skip to content

Commit 8d2e76f

Browse files
joseartriveraHowardWolosky
authored andcommitted
Add Support for the Github Traffic APIs (#49)
Adds support for the Repository Traffics API's defined here: https://developer.github.com/v3/repos/traffic/
1 parent c6835f4 commit 8d2e76f

File tree

4 files changed

+529
-0
lines changed

4 files changed

+529
-0
lines changed

GitHubTraffic.ps1

Lines changed: 362 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,362 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
function Get-GitHubReferrerTraffic
5+
{
6+
<#
7+
.SYNOPSIS
8+
Get the top 10 referrers over the last 14 days for a given GitHub repository.
9+
10+
.DESCRIPTION
11+
Get the top 10 referrers over the last 14 days for a given GitHub repository.
12+
13+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
14+
15+
.PARAMETER OwnerName
16+
Owner of the repository.
17+
If not supplied here, the DefaultOwnerName configuration property value will be used.
18+
19+
.PARAMETER RepositoryName
20+
Name of the repository.
21+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
22+
23+
.PARAMETER Uri
24+
Uri for the repository.
25+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
26+
them individually.
27+
28+
.PARAMETER AccessToken
29+
If provided, this will be used as the AccessToken for authentication with the
30+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
31+
32+
.PARAMETER NoStatus
33+
If this switch is specified, long-running commands will run on the main thread
34+
with no commandline status update. When not specified, those commands run in
35+
the background, enabling the command prompt to provide status information.
36+
If not supplied here, the DefaultNoStatus configuration property value will be used.
37+
38+
.EXAMPLE
39+
Get-GitHubReferrerTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub
40+
41+
Get the top 10 referrers over the last 14 days from the PowerShell\PowerShellForGitHub project.
42+
#>
43+
[CmdletBinding(
44+
SupportsShouldProcess,
45+
DefaultParametersetName='Elements')]
46+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
47+
param(
48+
[Parameter(ParameterSetName='Elements')]
49+
[string] $OwnerName,
50+
51+
[Parameter(ParameterSetName='Elements')]
52+
[string] $RepositoryName,
53+
54+
[Parameter(
55+
Mandatory,
56+
ParameterSetName='Uri')]
57+
[string] $Uri,
58+
59+
[string] $AccessToken,
60+
61+
[switch] $NoStatus
62+
)
63+
64+
Write-InvocationLog -Invocation $MyInvocation
65+
66+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters
67+
$OwnerName = $elements.ownerName
68+
$RepositoryName = $elements.repositoryName
69+
70+
$telemetryProperties = @{
71+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
72+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
73+
}
74+
75+
$params = @{
76+
'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/popular/referrers"
77+
'Method' = 'Get'
78+
'Description' = "Getting referrers for $RepositoryName"
79+
'AccessToken' = $AccessToken
80+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
81+
'TelemetryProperties' = $telemetryProperties
82+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
83+
}
84+
85+
return Invoke-GHRestMethod @params
86+
}
87+
88+
function Get-GitHubPathTraffic
89+
{
90+
<#
91+
.SYNOPSIS
92+
Get the top 10 popular contents over the last 14 days for a given Github repository.
93+
94+
.DESCRIPTION
95+
Get the top 10 popular contents over the last 14 days for a given GitHub repository.
96+
97+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
98+
99+
.PARAMETER OwnerName
100+
Owner of the repository.
101+
If not supplied here, the DefaultOwnerName configuration property value will be used.
102+
103+
.PARAMETER RepositoryName
104+
Name of the repository.
105+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
106+
107+
.PARAMETER Uri
108+
Uri for the repository.
109+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
110+
them individually.
111+
112+
.PARAMETER AccessToken
113+
If provided, this will be used as the AccessToken for authentication with the
114+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
115+
116+
.PARAMETER NoStatus
117+
If this switch is specified, long-running commands will run on the main thread
118+
with no commandline status update. When not specified, those commands run in
119+
the background, enabling the command prompt to provide status information.
120+
If not supplied here, the DefaultNoStatus configuration property value will be used.
121+
122+
.EXAMPLE
123+
Get-GitHubPathTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub
124+
125+
Get the top 10 popular contents over the last 14 days from the PowerShell\PowerShellForGitHub project.
126+
#>
127+
[CmdletBinding(
128+
SupportsShouldProcess,
129+
DefaultParametersetName='Elements')]
130+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
131+
param(
132+
[Parameter(ParameterSetName='Elements')]
133+
[string] $OwnerName,
134+
135+
[Parameter(ParameterSetName='Elements')]
136+
[string] $RepositoryName,
137+
138+
[Parameter(
139+
Mandatory,
140+
ParameterSetName='Uri')]
141+
[string] $Uri,
142+
143+
[string] $AccessToken,
144+
145+
[switch] $NoStatus
146+
)
147+
148+
Write-InvocationLog -Invocation $MyInvocation
149+
150+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters
151+
$OwnerName = $elements.ownerName
152+
$RepositoryName = $elements.repositoryName
153+
154+
$telemetryProperties = @{
155+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
156+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
157+
}
158+
159+
$params = @{
160+
'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/popular/paths"
161+
'Method' = 'Get'
162+
'Description' = "Getting popular contents for $RepositoryName"
163+
'AccessToken' = $AccessToken
164+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
165+
'TelemetryProperties' = $telemetryProperties
166+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
167+
}
168+
169+
return Invoke-GHRestMethod @params
170+
}
171+
172+
function Get-GitHubViewTraffic
173+
{
174+
<#
175+
.SYNOPSIS
176+
Get the total number of views and breakdown per day or week for the last 14 days for the given Github Repository.
177+
178+
.DESCRIPTION
179+
Get the total number of views and breakdown per day or week for the last 14 days.
180+
Timestamps are aligned to UTC midnight of the beginning of the day or week. Week begins on Monday.
181+
182+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
183+
184+
.PARAMETER OwnerName
185+
Owner of the repository.
186+
If not supplied here, the DefaultOwnerName configuration property value will be used.
187+
188+
.PARAMETER RepositoryName
189+
Name of the repository.
190+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
191+
192+
.PARAMETER Uri
193+
Uri for the repository.
194+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
195+
them individually.
196+
197+
.PARAMETER Per
198+
The interval at which return to return the view counts.
199+
200+
.PARAMETER AccessToken
201+
If provided, this will be used as the AccessToken for authentication with the
202+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
203+
204+
.PARAMETER NoStatus
205+
If this switch is specified, long-running commands will run on the main thread
206+
with no commandline status update. When not specified, those commands run in
207+
the background, enabling the command prompt to provide status information.
208+
If not supplied here, the DefaultNoStatus configuration property value will be used.
209+
210+
.EXAMPLE
211+
Get-GitHubViewTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub
212+
213+
Get the total number of views and breakdown per day or week for the last 14 days from the PowerShell\PowerShellForGitHub project.
214+
#>
215+
[CmdletBinding(
216+
SupportsShouldProcess,
217+
DefaultParametersetName='Elements')]
218+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
219+
param(
220+
[Parameter(ParameterSetName='Elements')]
221+
[string] $OwnerName,
222+
223+
[Parameter(ParameterSetName='Elements')]
224+
[string] $RepositoryName,
225+
226+
[Parameter(
227+
Mandatory,
228+
ParameterSetName='Uri')]
229+
[string] $Uri,
230+
231+
[ValidateSet('day', 'week')]
232+
[string] $Per = 'day',
233+
234+
[string] $AccessToken,
235+
236+
[switch] $NoStatus
237+
)
238+
239+
Write-InvocationLog -Invocation $MyInvocation
240+
241+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters
242+
$OwnerName = $elements.ownerName
243+
$RepositoryName = $elements.repositoryName
244+
245+
$telemetryProperties = @{
246+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
247+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
248+
'Per' = $Per
249+
}
250+
251+
$getParams = @(
252+
"per=$Per"
253+
)
254+
255+
$params = @{
256+
'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/views`?" + ($getParams -join '&')
257+
'Method' = 'Get'
258+
'Description' = "Getting views for $RepositoryName"
259+
'AccessToken' = $AccessToken
260+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
261+
'TelemetryProperties' = $telemetryProperties
262+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
263+
}
264+
265+
return Invoke-GHRestMethod @params
266+
}
267+
268+
function Get-GitHubCloneTraffic
269+
{
270+
<#
271+
.SYNOPSIS
272+
Get the total number of clones and breakdown per day or week for the last 14 days for the given Github Repository.
273+
274+
.DESCRIPTION
275+
Get the total number of clones and breakdown per day or week for the last 14 days.
276+
Timestamps are aligned to UTC midnight of the beginning of the day or week. Week begins on Monday.
277+
278+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
279+
280+
.PARAMETER OwnerName
281+
Owner of the repository.
282+
If not supplied here, the DefaultOwnerName configuration property value will be used.
283+
284+
.PARAMETER RepositoryName
285+
Name of the repository.
286+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
287+
288+
.PARAMETER Uri
289+
Uri for the repository.
290+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
291+
them individually.
292+
293+
.PARAMETER Per
294+
The interval at which return to return the view counts.
295+
296+
.PARAMETER AccessToken
297+
If provided, this will be used as the AccessToken for authentication with the
298+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
299+
300+
.PARAMETER NoStatus
301+
If this switch is specified, long-running commands will run on the main thread
302+
with no commandline status update. When not specified, those commands run in
303+
the background, enabling the command prompt to provide status information.
304+
If not supplied here, the DefaultNoStatus configuration property value will be used.
305+
306+
.EXAMPLE
307+
Get-GitHubCloneTraffic -OwnerName Powershell -RepositoryName PowerShellForGitHub
308+
309+
Get the total number of clones and breakdown per day or week for the last 14 days from the PowerShell\PowerShellForGitHub project.
310+
#>
311+
[CmdletBinding(
312+
SupportsShouldProcess,
313+
DefaultParametersetName='Elements')]
314+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
315+
param(
316+
[Parameter(ParameterSetName='Elements')]
317+
[string] $OwnerName,
318+
319+
[Parameter(ParameterSetName='Elements')]
320+
[string] $RepositoryName,
321+
322+
[Parameter(
323+
Mandatory,
324+
ParameterSetName='Uri')]
325+
[string] $Uri,
326+
327+
[ValidateSet('day', 'week')]
328+
[string] $Per = 'day',
329+
330+
[string] $AccessToken,
331+
332+
[switch] $NoStatus
333+
)
334+
335+
Write-InvocationLog -Invocation $MyInvocation
336+
337+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters
338+
$OwnerName = $elements.ownerName
339+
$RepositoryName = $elements.repositoryName
340+
341+
$telemetryProperties = @{
342+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
343+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
344+
'Per' = $Per
345+
}
346+
347+
$getParams = @(
348+
"per=$Per"
349+
)
350+
351+
$params = @{
352+
'UriFragment' = "repos/$OwnerName/$RepositoryName/traffic/clones`?" + ($getParams -join '&')
353+
'Method' = 'Get'
354+
'Description' = "Getting number of clones for $RepositoryName"
355+
'AccessToken' = $AccessToken
356+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
357+
'TelemetryProperties' = $telemetryProperties
358+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
359+
}
360+
361+
return Invoke-GHRestMethod @params
362+
}

PowerShellForGitHub.psd1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
'GitHubRepositories.ps1',
3131
'GitHubRepositoryForks.ps1',
3232
'GitHubTeams.ps1',
33+
'GitHubTraffic.ps1',
3334
'GitHubUsers.ps1',
3435
'NugetTools.ps1',
3536
'Telemetry.ps1')
@@ -42,6 +43,7 @@
4243
'Backup-GitHubConfiguration',
4344
'Clear-GitHubAuthentication',
4445
'ConvertFrom-Markdown',
46+
'Get-GitHubCloneTraffic',
4547
'Get-GitHubCodeOfConduct',
4648
'Get-GitHubConfiguration',
4749
'Get-GitHubEmoji',
@@ -51,8 +53,10 @@
5153
'Get-GitHubLabel',
5254
'Get-GitHubLicense',
5355
'Get-GitHubOrganizationMember',
56+
'Get-GitHubPathTraffic',
5457
'Get-GitHubPullRequest',
5558
'Get-GitHubRateLimit',
59+
'Get-GitHubReferrerTraffic',
5660
'Get-GitHubRepository',
5761
'Get-GitHubRepositoryBranch',
5862
'Get-GitHubRepositoryCollaborator',
@@ -66,6 +70,7 @@
6670
'Get-GitHubTeamMember',
6771
'Get-GitHubUser',
6872
'Get-GitHubUserContextualInformation',
73+
'Get-GitHubViewTraffic',
6974
'Group-GitHubIssue',
7075
'Invoke-GHRestMethod',
7176
'Invoke-GHRestMethodMultipleResult',

0 commit comments

Comments
 (0)