generated from Stravaig-Projects/stravaig-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list-contributors.ps1
392 lines (347 loc) · 14.2 KB
/
list-contributors.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# This script is licenced under an MIT licence. Licence details can be found at
# https://github.com/Stravaig-Projects/Repo-Contributor-List/blob/master/LICENSE
# Please keep this licence notice link intact.
#
# Full details of this script can be found at:
# https://github.com/Stravaig-Projects/Repo-Contributor-List
param
(
[parameter(Mandatory=$false)]
[string]$OutputFile = "contributors.md",
[parameter(Mandatory=$false)]
[string]$DateTimeFormat = "dddd, d MMMM, yyyy @ HH:mm:ss zzz",
[parameter(Mandatory=$false)]
[string]$TimeFormat = "HH:mm:ss zzz",
[parameter(Mandatory=$false)]
[ValidateSet("Name", "FirstCommit", "LastCommit", "CommitCount")]
[string]$SortOrder = "Name",
[parameter(Mandatory=$false)]
[ValidateSet("Ascending", "Descending")]
[string]$SortDirection = "Ascending",
[parameter(Mandatory=$false)]
[System.IO.FileInfo]$AkaFilePath,
[parameter(Mandatory=$false)]
[System.IO.FileInfo]$IgnoredNamesPath,
[parameter(Mandatory=$false)]
[System.IO.FileInfo]$IgnoredEmailsPath,
[Switch]
$HideAKAs
)
function Test-StringEquality($A, $B)
{
return [string]::Compare($A, $B, $true) -eq 0
}
function Test-Name($contributor, $committerName)
{
return $null -ne $contributor.Names.Where({ Test-StringEquality $_ $committerName }, "First", 1)[0];
}
function Test-Email($contributor, $committerEmail)
{
return $null -ne $contributor.Emails.Where({ Test-StringEquality $_ $committerEmail }, "First", 1)[0];
}
function Test-IgnoredItem($Item, $IgnoredItemsList)
{
return $null -ne $IgnoredItemsList.Where({ Test-StringEquality $_ $Item }, "First", 1)[0];
}
function Test-Contributor($contributor, $commit)
{
$nameMatch = Test-Name -Contributor $contributor -Commit $commit.Name;
if ($nameMatch) {
return $true;
}
$emailMatch = Test-Email -Contributor $contributor -Commit $commit.Email;
return $emailMatch;
}
function Get-ConfigFilePath($configFilePath, $defaultConfigFilePath, $friendlyName)
{
$configFileMustExist = $true;
if ($null -eq $configFilePath)
{
$configFileMustExist = $false;
$configFilePath = $defaultConfigFilePath;
}
$pathExists = Test-Path -Path $configFilePath;
if (-not $pathExists)
{
if ($configFileMustExist)
{
throw "The `"$friendlyName`" file at $configFilePath does not exist.";
}
else
{
return $null;
}
}
return $configFilePath;
}
function Get-IgnoredNamesList($IgnoredNamesPath, $AkaContributors)
{
$result = @();
foreach($contributor in $AkaContributors)
{
foreach($name in $contributor.names)
{
$result += $name;
Write-Verbose "Ignoring name `"$name`"";
}
}
$IgnoredNamesPath = Get-ConfigFilePath -configFilePath $IgnoredNamesPath -defaultConfigFilePath "$PSScriptRoot/.stravaig/list-contributor-ignore-names.txt" -friendlyName "list of ignored names";
if ($null -ne $IgnoredNamesPath)
{
$ignoredNames = Get-Content -Path $IgnoredNamesPath;
foreach($name in $ignoredNames)
{
$result += $name;
Write-Verbose "Ignoring name `"$name`"";
}
}
$result = $result | Select-Object -Unique;
return (,$result);
}
function Get-IgnoredEmailsList($IgnoredEmailsPath, $AkaContributors)
{
$result = @();
foreach($contributor in $AkaContributors)
{
foreach($email in $contributor.emails)
{
$result += $email;
Write-Verbose "Ignoring email `"$email`"";
}
}
$IgnoredEmailsPath = Get-ConfigFilePath -configFilePath $IgnoredEmailsPath -defaultConfigFilePath "$PSScriptRoot/.stravaig/list-contributor-ignore-emails.txt" -friendlyName "list of ignored email addresses";
if ($null -ne $IgnoredEmailsPath)
{
$ignoredEmails = Get-Content -Path $IgnoredEmailsPath;
foreach($email in $ignoredEmails)
{
$result += $email;
Write-Verbose "Ignoring email `"$email`"";
}
}
$result = $result | Select-Object -Unique;
return (,$result);
}
function Get-InitialContributors($AkaFilePath)
{
$result = @();
$akaFilePath = Get-ConfigFilePath -configFilePath $akaFilePath -defaultConfigFilePath "$PSScriptRoot/.stravaig/list-contributor-akas.json" -friendlyName "AKA (Also Known As)";
if ($null -eq $akaFilePath)
{
return (,$result);
}
try
{
$akaDetails = Get-Content -Raw -Path $akaFilePath | ConvertFrom-Json -ErrorAction Stop
}
catch
{
# For some reason it isn't stopping the script when erroring with
# -ErrorAction Stop in the ConvertFrom-Json
throw $_
}
foreach($person in $akaDetails)
{
if ($null -eq $person.primaryName)
{
$fragment = $person | ConvertTo-Json;
throw "Each entry must have a `"primaryPerson`" element.`n$fragment";
}
if ($null -eq $person.emails)
{
$fragment = $person | ConvertTo-Json;
throw "Each entry must have a `"emails`" element. If there are no emails supply an empty array.`n$fragment";
}
if ($null -eq $person.akas)
{
$fragment = $person | ConvertTo-Json;
throw "Each entry must have a `"akas`" element. If there are no AKAs supply an empty array.`n$fragment";
}
$contributor = New-Object -TypeName PSObject -Property @{
Names=@($person.primaryName);
PrimaryName = $person.primaryName;
Emails=@();
FirstCommit=[DateTime]::MaxValue;
LastCommit=[DateTime]::MinValue;
CommitCount=0
};
Write-Verbose "Adding name `"$($contributor.PrimaryName)`" as primary name from $akaFilePath.";
foreach($email in $person.emails)
{
if (-not (Test-Email -Contributor $contributor -CommitterEmail $email))
{
$contributor.Emails += $email;
Write-Verbose "Adding email `"$email`" to $($contributor.PrimaryName) from $akaFilePath."
}
}
foreach($name in $person.akas)
{
if (-not (Test-Name -Contributor $contributor -CommitterName $name))
{
$contributor.Names += $name;
Write-Verbose "Adding name `"$name`" to $($contributor.PrimaryName) from $akaFilePath."
}
}
$result += $contributor;
}
return (,$result);
}
$contributors = Get-InitialContributors -AkaFilePath $AkaFilePath;
$ignoredNamesList = Get-IgnoredNamesList -IgnoredNamesPath $IgnoredNamesPath -AkaContributors $contributors
$ignoredEmailsList = Get-IgnoredEmailsList -IgnoredEmailsPath $IgnoredEmailsPath -AkaContributors $contributors
& git log --format="\`"%ai\`",\`"%an\`",\`"%ae\`",\`"%H\`"" > raw-contributors.csv
$commits = Import-Csv raw-contributors.csv -Header Time,Name,Email,Hash | Sort-Object Time;
Remove-Item .\raw-contributors.csv
$commitsPerPercentPoint = [Math]::Ceiling($commits.Length / 100)
$totalCommits = $commits.Length;
for($i = 0; $i -lt $totalCommits; $i++)
{
$nextCommit = $commits[$i];
$commitTime = [DateTime]::ParseExact($nextCommit.Time, "yyyy-MM-dd HH:mm:ss zzz", [CultureInfo]::InvariantCulture);
if ($i % $commitsPerPercentPoint -eq 0)
{
$percent = [Math]::Floor($i / $totalCommits * 100);
Write-Progress -Activity "Processing" -CurrentOperation "Commit $i of $totalCommits" -PercentComplete $percent
}
$contributor = $contributors.Where({Test-Contributor -Contributor $_ -Commit $nextCommit}, "First", 1)[0]
if ($null -eq $contributor)
{
$contributor = New-Object -TypeName PSObject -Property @{
Names=@($nextCommit.Name);
PrimaryName = $nextCommit.Name;
Emails=@($nextCommit.Email);
FirstCommit=$commitTime;
LastCommit=$commitTime;
CommitCount=1
};
$contributors += $contributor;
Write-Verbose "Adding name `"$($nextCommit.Name)`" as primary name from commit $($nextCommit.Hash).";
Write-Verbose "Adding email `"$($nextCommit.Email)`" to $($contributor.PrimaryName) from commit $($nextCommit.Hash)."
}
else
{
if ((-not(Test-IgnoredItem -Item $nextCommit.Email -IgnoredItemsList $ignoredEmailsList)) -and
(-not (Test-Email -Contributor $contributor -CommitterEmail $nextCommit.Email)))
{
Write-Verbose "Adding email `"$($nextCommit.Email)`" to $($contributor.PrimaryName) from commit $($nextCommit.Hash)."
$contributor.Emails += $nextCommit.Email;
}
if ((-not(Test-IgnoredItem -Item $nextCommit.Name -IgnoredItemsList $ignoredNamesList)) -and
(-not (Test-Name -Contributor $contributor -CommitterName $nextCommit.Name)))
{
Write-Verbose "Adding name `"$($nextCommit.Name)`" to $($contributor.PrimaryName) from commit $($nextCommit.Hash)."
$contributor.Names += $nextCommit.Name;
}
if ($commitTime -lt $contributor.FirstCommit)
{
$contributor.FirstCommit = $commitTime;
}
if ($commitTime -gt $contributor.LastCommit)
{
$contributor.LastCommit = $commitTime
}
$contributor.CommitCount += 1;
}
}
Write-Progress -Activity "Processing" -PercentComplete 100 -Completed
$contributors = $contributors | Where-Object CommitCount -gt 0
$isDescending = $SortDirection -eq "Descending";
Switch($SortOrder)
{
"Name" {
$contributors = $contributors | Sort-Object PrimaryName -Descending:$isDescending;
$textOrderBy = "contributor name";
}
"FirstCommit" {
$contributors = $contributors | Sort-Object FirstCommit -Descending:$isDescending;
$textOrderBy = "first commit date";
}
"LastCommit" {
$contributors = $contributors | Sort-Object LastCommit -Descending:$isDescending;
$textOrderBy = "last commit date";
}
"CommitCount" {
$contributors = $contributors | Sort-Object CommitCount -Descending:$isDescending;
$textOrderBy = "number of commits";
}
}
"# Contributors" | Out-File $OutputFile -Encoding utf8
"" | Out-File $OutputFile -Append -Encoding utf8
$line = "This is a list of all the contributors to this repository in "
$line += $SortDirection.ToLower();
"$line order by the $textOrderBy." | Out-File $OutputFile -Append -Encoding utf8
"" | Out-File $OutputFile -Append -Encoding utf8
foreach($contributor in $contributors)
{
$name = $contributor.PrimaryName;
$aka = ""
if ($contributor.Names.Length -gt 1 -and $HideAKAs -eq $false)
{
$aka = " (AKA ";
$isFirst = $true;
for($i = 1; $i -lt $contributor.Names.Length; $i++)
{
if (-not $isFirst) { $aka += ", " }
$aka += "*"+$contributor.Names[$i]+"*";
$isFirst = $false
}
$aka += ")"
}
$numCommits = $contributor.CommitCount
$commitMsg = "commit"; if ($numCommits -gt 1) { $commitMsg += "s" }
$start = $contributor.FirstCommit.ToString($DateTimeFormat);
if ($contributor.FirstCommit.Date -eq $contributor.LastCommit.Date)
{
$end = $contributor.LastCommit.ToString($TimeFormat);
}
else {
$end = $contributor.LastCommit.ToString($DateTimeFormat);
}
"**$name**$aka contributed $numCommits $commitMsg" | Out-File $OutputFile -Append -Encoding utf8 -NoNewline
if ($numCommits -eq 1)
{
" on $start." | Out-File $OutputFile -Append -Encoding utf8
}
else
{
" from $start to $end." | Out-File $OutputFile -Append -Encoding utf8
}
"" | Out-File $OutputFile -Append -Encoding utf8
}
"## Summary" | Out-File $OutputFile -Append -Encoding utf8
"" | Out-File $OutputFile -Append -Encoding utf8
$firstCommit = $commits[0];
$firstCommitMsg = [DateTime]::ParseExact($firstCommit.Time, "yyyy-MM-dd HH:mm:ss zzz", [CultureInfo]::InvariantCulture).ToString($DateTimeFormat);
$lastCommit = $commits[$totalCommits - 1];
$lastCommitMsg = [DateTime]::ParseExact($lastCommit.Time, "yyyy-MM-dd HH:mm:ss zzz", [CultureInfo]::InvariantCulture).ToString($DateTimeFormat);
":octocat: $totalCommits commits in total." | Out-File $OutputFile -Append -Encoding utf8
"" | Out-File $OutputFile -Append -Encoding utf8
":date: From $firstCommitMsg." | Out-File $OutputFile -Append -Encoding utf8
"" | Out-File $OutputFile -Append -Encoding utf8
":date: Until $lastCommitMsg." | Out-File $OutputFile -Append -Encoding utf8
"" | Out-File $OutputFile -Append -Encoding utf8
$topCommitters = ($contributors | Sort-Object CommitCount -Descending);
$topCommitter = $topCommitters[0];
$name = $topCommitter.PrimaryName;
$commitCount = $topCommitter.CommitCount;
$percentage = $topCommitter.CommitCount / $totalCommits;
":1st_place_medal: Gold medal to $name with $commitCount commits which represents "+("{0:P2}" -f $percentage)+" of all commits." | Out-File $OutputFile -Append -Encoding utf8
"" | Out-File $OutputFile -Append -Encoding utf8
$topCommitter = $topCommitters[1];
if ($null -ne $topCommitter)
{
$name = $topCommitter.PrimaryName;
$commitCount = $topCommitter.CommitCount;
$percentage = $topCommitter.CommitCount / $totalCommits;
":2nd_place_medal: Silver medal to $name with $commitCount commits which represents "+("{0:P2}" -f $percentage)+" of all commits." | Out-File $OutputFile -Append -Encoding utf8
"" | Out-File $OutputFile -Append -Encoding utf8
}
$topCommitter = $topCommitters[2];
if ($null -ne $topCommitter)
{
$name = $topCommitter.PrimaryName;
$commitCount = $topCommitter.CommitCount;
$percentage = $topCommitter.CommitCount / $totalCommits;
":3rd_place_medal: Bronze medal to $name with $commitCount commits which represents "+("{0:P2}" -f $percentage)+" of all commits." | Out-File $OutputFile -Append -Encoding utf8
"" | Out-File $OutputFile -Append -Encoding utf8
}