forked from microsoft/PowerShellForGitHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitHubAnalytics.ps1
297 lines (245 loc) · 8.31 KB
/
GitHubAnalytics.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
function Group-GitHubIssue
{
<#
.SYNOPSIS
Groups the provided issues based on the specified grouping criteria.
.DESCRIPTION
Groups the provided issues based on the specified grouping criteria.
Currently able to group Issues by week.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Issue
The Issue(s) to be grouped.
.PARAMETER Weeks
The number of weeks to group the Issues by.
.PARAMETER DateType
The date property that should be inspected when determining which week grouping the issue
if part of.
.INPUTS
GitHub.Issue
.OUTPUTS
[PSCustomObject[]]
Collection of issues and counts, by week, along with the total count of issues.
.EXAMPLE
$issues = @()
$issues += Get-GitHubIssue -Uri 'https://github.com/powershell/xpsdesiredstateconfiguration'
$issues += Get-GitHubIssue -Uri 'https://github.com/powershell/xactivedirectory'
$issues | Group-GitHubIssue -Weeks 12 -DateType Closed
#>
[CmdletBinding(DefaultParameterSetName = 'Weekly')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="DateType due to PowerShell/PSScriptAnalyzer#1472")]
param
(
[Parameter(
Mandatory,
ValueFromPipeline)]
[PSCustomObject[]] $Issue,
[Parameter(
Mandatory,
ParameterSetName='Weekly')]
[ValidateRange(0, 10000)]
[int] $Weeks,
[Parameter(ParameterSetName='Weekly')]
[ValidateSet('Created', 'Closed')]
[string] $DateType = 'Created'
)
begin
{
Write-InvocationLog
if ($PSCmdlet.ParameterSetName -eq 'Weekly')
{
$weekDates = Get-WeekDate -Weeks $Weeks
$result = [ordered]@{}
foreach ($week in $weekDates)
{
$result[$week] = ([PSCustomObject]([ordered]@{
'WeekStart' = $week
'Count' = 0
'Issues' = @()
}))
}
$result['total'] = ([PSCustomObject]([ordered]@{
'WeekStart' = 'total'
'Count' = 0
'Issues' = @()
}))
}
}
process
{
if ($PSCmdlet.ParameterSetName -eq 'Weekly')
{
$endOfWeek = Get-Date
foreach ($week in $weekDates)
{
$filteredIssues = @($Issue | Where-Object {
(($DateType -eq 'Created') -and
($_.created_at -ge $week) -and
($_.created_at -le $endOfWeek)) -or
(($DateType -eq 'Closed') -and
($_.closed_at -ge $week) -and
($_.closed_at -le $endOfWeek))
})
$endOfWeek = $week
$result[$week].Issues += $filteredIssues
$result[$week].Count += ($filteredIssues.Count)
$result['total'].Issues += $filteredIssues
$result['total'].Count += ($filteredIssues.Count)
}
}
else
{
Write-Output -InputObject $Issue
}
}
end
{
if ($PSCmdlet.ParameterSetName -eq 'Weekly')
{
foreach ($entry in $result.Values.GetEnumerator())
{
Write-Output -InputObject $entry
}
}
}
}
function Group-GitHubPullRequest
{
<#
.SYNOPSIS
Groups the provided pull requests based on the specified grouping criteria.
.DESCRIPTION
Groups the provided pull requests based on the specified grouping criteria.
Currently able to group Pull Requests by week.
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER PullRequest
The Pull Requests(s) to be grouped.
.PARAMETER Weeks
The number of weeks to group the Pull Requests by.
.PARAMETER DateType
The date property that should be inspected when determining which week grouping the
pull request if part of.
.INPUTS
GitHub.PullRequest
.OUTPUTS
[PSCustomObject[]] Collection of pull requests and counts, by week, along with the
total count of pull requests.
.EXAMPLE
$pullRequests = @()
$pullRequests += Get-GitHubPullRequest -Uri 'https://github.com/powershell/xpsdesiredstateconfiguration'
$pullRequests += Get-GitHubPullRequest -Uri 'https://github.com/powershell/xactivedirectory'
$pullRequests | Group-GitHubPullRequest -Weeks 12 -DateType Closed
#>
[CmdletBinding(DefaultParameterSetName='Weekly')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="DateType due to PowerShell/PSScriptAnalyzer#1472")]
param
(
[Parameter(
Mandatory,
ValueFromPipeline)]
[PSCustomObject[]] $PullRequest,
[Parameter(
Mandatory,
ParameterSetName='Weekly')]
[ValidateRange(0, 10000)]
[int] $Weeks,
[Parameter(ParameterSetName='Weekly')]
[ValidateSet('Created', 'Merged')]
[string] $DateType = 'Created'
)
begin
{
Write-InvocationLog
if ($PSCmdlet.ParameterSetName -eq 'Weekly')
{
$weekDates = Get-WeekDate -Weeks $Weeks
$result = [ordered]@{}
foreach ($week in $weekDates)
{
$result[$week] = ([PSCustomObject]([ordered]@{
'WeekStart' = $week
'Count' = 0
'PullRequests' = @()
}))
}
$result['total'] = ([PSCustomObject]([ordered]@{
'WeekStart' = 'total'
'Count' = 0
'PullRequests' = @()
}))
}
}
process
{
if ($PSCmdlet.ParameterSetName -eq 'Weekly')
{
$endOfWeek = Get-Date
foreach ($week in $weekDates)
{
$filteredPullRequests = @($PullRequest | Where-Object {
(($DateType -eq 'Created') -and
($_.created_at -ge $week) -and
($_.created_at -le $endOfWeek)) -or
(($DateType -eq 'Merged') -and
($_.merged_at -ge $week) -and
($_.merged_at -le $endOfWeek))
})
$endOfWeek = $week
$result[$week].PullRequests += $filteredPullRequests
$result[$week].Count += ($filteredPullRequests.Count)
$result['total'].PullRequests += $filteredPullRequests
$result['total'].Count += ($filteredPullRequests.Count)
}
}
else
{
Write-Output -InputObject $PullRequest
}
}
end
{
if ($PSCmdlet.ParameterSetName -eq 'Weekly')
{
foreach ($entry in $result.Values.GetEnumerator())
{
Write-Output -InputObject $entry
}
}
}
}
function Get-WeekDate
{
<#
.SYNOPSIS
Retrieves an array of dates with starts of $Weeks previous weeks.
Dates are sorted in reverse chronological order
.DESCRIPTION
Retrieves an array of dates with starts of $Weeks previous weeks.
Dates are sorted in reverse chronological order
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.PARAMETER Weeks
The number of weeks prior to today that should be included in the final result.
.OUTPUTS
[DateTime[]] List of DateTimes representing the first day of each requested week.
.EXAMPLE
Get-WeekDate -Weeks 10
#>
[CmdletBinding()]
[OutputType([DateTime[]])]
param(
[ValidateRange(0, 10000)]
[int] $Weeks = 12
)
$dates = @()
$midnightToday = Get-Date -Hour 0 -Minute 0 -Second 0 -Millisecond 0
$startOfWeek = $midnightToday.AddDays(- ($midnightToday.DayOfWeek.value__ - 1))
$i = 0
while ($i -lt $Weeks)
{
$dates += $startOfWeek
$startOfWeek = $startOfWeek.AddDays(-7)
$i++
}
return $dates
}