forked from microsoft/PowerShellForGitHub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GitHubAnalytics.psm1
1200 lines (1038 loc) · 38.5 KB
/
GitHubAnalytics.psm1
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS PowerShell module for GitHub analytics
#>
# Import module which defines $global:gitHubApiToken with GitHub API access token. Create this file it if it doesn't exist.
$apiTokensFilePath = "$PSScriptRoot\ApiTokens.psm1"
if (Test-Path $apiTokensFilePath)
{
Write-Host "Importing $apiTokensFilePath"
Import-Module -force $apiTokensFilePath
}
else
{
Write-Host "$apiTokensFilePath does not exist, skipping import"
Write-Host @'
This module should define $global:gitHubApiToken with your GitHub API access token in ApiTokens.psm1. Create this file if it doesn't exist.
You can simply rename ApiTokensTemplate.psm1 to ApiTokens.psm1 and update value of $global:gitHubApiToken.
You can get GitHub token from https://github.com/settings/tokens
If you don't provide it, you can still use this module, but you will be limited to 60 queries per hour.
'@
}
$script:gitHubToken = $global:gitHubApiToken
$script:gitHubApiUrl = "https://api.github.com"
$script:gitHubApiReposUrl = "https://api.github.com/repos"
$script:gitHubApiOrgsUrl = "https://api.github.com/orgs"
<#
.SYNOPSIS Function which gets list of issues for given repository
.PARAM
repositoryUrl Array of repository urls which we want to get issues from
.PARAM
state Whether we want to get open, closed or all issues
.PARAM
createdOnOrAfter Filter to only get issues created on or after specific date
.PARAM
createdOnOrBefore Filter to only get issues created on or before specific date
.PARAM
closedOnOrAfter Filter to only get issues closed on or after specific date
.PARAM
ClosedOnOrBefore Filter to only get issues closed on or before specific date
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
$issues = Get-GitHubIssueForRepository -repositoryUrl @('https://github.com/PowerShell/xPSDesiredStateConfiguration')
.EXAMPLE
$issues = Get-GitHubIssueForRepository `
-repositoryUrl @('https://github.com/PowerShell/xPSDesiredStateConfiguration', "https://github.com/PowerShell/xWindowsUpdate" ) `
-createdOnOrAfter '2015-04-20'
#>
function Get-GitHubIssueForRepository
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String[]] $repositoryUrl,
[ValidateSet("open", "closed", "all")]
[String] $state = "open",
[DateTime] $createdOnOrAfter,
[DateTime] $createdOnOrBefore,
[DateTime] $closedOnOrAfter,
[DateTime] $closedOnOrBefore,
$gitHubAccessToken = $script:gitHubToken
)
$resultToReturn = @()
$index = 0
foreach ($repository in $repositoryUrl)
{
Write-Host "Getting issues for repository $repository" -ForegroundColor Yellow
$repositoryName = Get-GitHubRepositoryNameFromUrl -repositoryUrl $repository
$repositoryOwner = Get-GitHubRepositoryOwnerFromUrl -repositoryUrl $repository
# Create query for issues
$query = "$script:gitHubApiReposUrl/$repositoryOwner/$repositoryName/issues?state=$state"
if (![string]::IsNullOrEmpty($gitHubAccessToken))
{
$query += "&access_token=$gitHubAccessToken"
}
# Obtain issues
do
{
try
{
$jsonResult = Invoke-WebRequest $query
$issues = ConvertFrom-Json -InputObject $jsonResult.content
}
catch [System.Net.WebException] {
Write-Error "Failed to execute query with exception: $($_.Exception)`nHTTP status code: $($_.Exception.Response.StatusCode)"
return $null
}
catch {
Write-Error "Failed to execute query with exception: $($_.Exception)"
return $null
}
foreach ($issue in $issues)
{
# GitHub considers pull request to be an issue, so let's skip pull requests.
if ($issue.pull_request -ne $null)
{
continue
}
# Filter according to createdOnOrAfter
$createdDate = Get-Date -Date $issue.created_at
if (($createdOnOrAfter -ne $null) -and ($createdDate -lt $createdOnOrAfter))
{
continue
}
# Filter according to createdOnOrBefore
if (($createdOnOrBefore -ne $null) -and ($createdDate -gt $createdOnOrBefore))
{
continue
}
if ($issue.closed_at -ne $null)
{
# Filter according to closedOnOrAfter
$closedDate = Get-Date -Date $issue.closed_at
if (($closedOnOrAfter -ne $null) -and ($closedDate -lt $closedOnOrAfter))
{
continue
}
# Filter according to closedOnOrBefore
if (($closedOnOrBefore -ne $null) -and ($closedDate -gt $closedOnOrBefore))
{
continue
}
}
else
{
# If issue isn't closed, but we specified filtering on closedOn, skip it
if (($closedOnOrAfter -ne $null) -or ($closedOnOrBefore -ne $null))
{
continue
}
}
Write-Verbose "$index. $($issue.html_url) ## Created: $($issue.created_at) ## Closed: $($issue.closed_at)"
$index++
$resultToReturn += $issue
}
$query = Get-NextResultPage -jsonResult $jsonResult
} while ($query -ne $null)
}
return $resultToReturn
}
<#
.SYNOPSIS Function which returns number of issues created/merged in every week in specific repositories
.PARAM
repositoryUrl Array of repository urls which we want to get pull requests from
.PARAM
numberOfWeeks How many weeks we want to obtain data for
.PARAM
dataType Whether we want to get information about created or merged issues in specific weeks
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
Get-GitHubWeeklyIssueForRepository -repositoryUrl @('https://github.com/powershell/xpsdesiredstateconfiguration', 'https://github.com/powershell/xactivedirectory') -datatype closed
#>
function Get-GitHubWeeklyIssueForRepository
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String[]] $repositoryUrl,
[int] $numberOfWeeks = 12,
[Parameter(Mandatory=$true)]
[ValidateSet("created","closed")]
[string] $dataType,
$gitHubAccessToken = $script:gitHubToken
)
$weekDates = Get-WeekDate -numberOfWeeks $numberOfWeeks
$endOfWeek = Get-Date
$results = @()
$totalIssues = 0
foreach ($week in $weekDates)
{
Write-Host "Getting issues from week of $week"
$issues = $null
if ($dataType -eq "closed")
{
$issues = Get-GitHubIssueForRepository `
-repositoryUrl $repositoryUrl -state 'all' -closedOnOrAfter $week -closedOnOrBefore $endOfWeek
}
elseif ($dataType -eq "created")
{
$issues = Get-GitHubIssueForRepository `
-repositoryUrl $repositoryUrl -state 'all' -createdOnOrAfter $week -createdOnOrBefore $endOfWeek
}
$endOfWeek = $week
if (($issues -ne $null) -and ($issues.Count -eq $null))
{
$count = 1
}
else
{
$count = $issues.Count
}
$totalIssues += $count
$results += @{"BeginningOfWeek"=$week; "Issues"=$count}
}
$results += @{"BeginningOfWeek"="total"; "Issues"=$totalIssues}
return $results
}
<#
.SYNOPSIS Function which returns repositories with biggest number of issues meeting specified criteria
.PARAM
repositoryUrl Array of repository urls which we want to get issues from
.PARAM
state Whether we want to get information about open issues, closed or both
.PARAM
createdOnOrAfter Get information about issues created after specific date
.PARAM
closedOnOrAfter Get information about issues closed after specific date
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
Get-GitHubTopIssueRepository -repositoryUrl @('https://github.com/powershell/xsharepoint', 'https://github.com/powershell/xCertificate', 'https://github.com/powershell/xwebadministration') -state open
#>
function Get-GitHubTopIssueRepository
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String[]] $repositoryUrl,
[ValidateSet("open", "closed", "all")]
[String] $state = "open",
[DateTime] $createdOnOrAfter,
[DateTime] $closedOnOrAfter,
$gitHubAccessToken = $script:gitHubToken
)
if (($state -eq "open") -and ($closedOnOrAfter -ne $null))
{
Throw "closedOnOrAfter cannot be specified if state is open"
}
$repositoryIssues = @{}
foreach ($repository in $repositoryUrl)
{
if (($closedOnOrAfter -ne $null) -and ($createdOnOrAfter -ne $null))
{
$issues = Get-GitHubIssueForRepository `
-repositoryUrl $repository `
-state $state -closedOnOrAfter $closedOnOrAfter -createdOnOrAfter $createdOnOrAfter
}
elseif (($closedOnOrAfter -ne $null) -and ($createdOnOrAfter -eq $null))
{
$issues = Get-GitHubIssueForRepository `
-repositoryUrl $repository `
-state $state -closedOnOrAfter $closedOnOrAfter
}
elseif (($closedOnOrAfter -eq $null) -and ($createdOnOrAfter -ne $null))
{
$issues = Get-GitHubIssueForRepository `
-repositoryUrl $repository `
-state $state -createdOnOrAfter $createdOnOrAfter
}
elseif (($closedOnOrAfter -eq $null) -and ($createdOnOrAfter -eq $null))
{
$issues = Get-GitHubIssueForRepository `
-repositoryUrl $repository `
-state $state
}
if (($issues -ne $null) -and ($issues.Count -eq $null))
{
$count = 1
}
else
{
$count = $issues.Count
}
$repositoryName = Get-GitHubRepositoryNameFromUrl -repositoryUrl $repository
$repositoryIssues.Add($repositoryName, $count)
}
$repositoryIssues = $repositoryIssues.GetEnumerator() | Sort-Object Value -Descending
return $repositoryIssues
}
<#
.SYNOPSIS Function which gets list of pull requests for given repository
.PARAM
repositoryUrl Array of repository urls which we want to get pull requests from
.PARAM
state Whether we want to get open, closed or all pull requests
.PARAM
createdOnOrAfter Filter to only get pull requests created on or after specific date
.PARAM
createdOnOrBefore Filter to only get pull requests created on or before specific date
.PARAM
mergedOnOrAfter Filter to only get issues merged on or after specific date
.PARAM
mergedOnOrBefore Filter to only get issues merged on or before specific date
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
$pullRequests = Get-GitHubPullRequestForRepository -repositoryUrl @('https://github.com/PowerShell/xPSDesiredStateConfiguration')
.EXAMPLE
$pullRequests = Get-GitHubPullRequestForRepository `
-repositoryUrl @('https://github.com/PowerShell/xPSDesiredStateConfiguration', 'https://github.com/PowerShell/xWebAdministration') `
-state closed -mergedOnOrAfter 2015-02-13 -mergedOnOrBefore 2015-06-17
#>
function Get-GitHubPullRequestForRepository
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String[]] $repositoryUrl,
[ValidateSet("open", "closed", "all")]
[String] $state = "open",
[DateTime] $createdOnOrAfter,
[DateTime] $createdOnOrBefore,
[DateTime] $mergedOnOrAfter,
[DateTime] $mergedOnOrBefore,
$gitHubAccessToken = $script:gitHubToken
)
$resultToReturn = @()
$index = 0
foreach ($repository in $repositoryUrl)
{
Write-Host "Getting pull requests for repository $repository" -ForegroundColor Yellow
$repositoryName = Get-GitHubRepositoryNameFromUrl -repositoryUrl $repository
$repositoryOwner = Get-GitHubRepositoryOwnerFromUrl -repositoryUrl $repository
# Create query for pull requests
$query = "$script:gitHubApiReposUrl/$repositoryOwner/$repositoryName/pulls?state=$state"
if (![string]::IsNullOrEmpty($gitHubAccessToken))
{
$query += "&access_token=$gitHubAccessToken"
}
# Obtain pull requests
do
{
try
{
$jsonResult = Invoke-WebRequest $query
$pullRequests = ConvertFrom-Json -InputObject $jsonResult.content
}
catch [System.Net.WebException] {
Write-Error "Failed to execute query with exception: $($_.Exception)`nHTTP status code: $($_.Exception.Response.StatusCode)"
return $null
}
catch {
Write-Error "Failed to execute query with exception: $($_.Exception)"
return $null
}
foreach ($pullRequest in $pullRequests)
{
# Filter according to createdOnOrAfter
$createdDate = Get-Date -Date $pullRequest.created_at
if (($createdOnOrAfter -ne $null) -and ($createdDate -lt $createdOnOrAfter))
{
continue
}
# Filter according to createdOnOrBefore
if (($createdOnOrBefore -ne $null) -and ($createdDate -gt $createdOnOrBefore))
{
continue
}
if ($pullRequest.merged_at -ne $null)
{
# Filter according to mergedOnOrAfter
$mergedDate = Get-Date -Date $pullRequest.merged_at
if (($mergedOnOrAfter -ne $null) -and ($mergedDate -lt $mergedOnOrAfter))
{
continue
}
# Filter according to mergedOnOrBefore
if (($mergedOnOrBefore -ne $null) -and ($mergedDate -gt $mergedOnOrBefore))
{
continue
}
}
else
{
# If issue isn't merged, but we specified filtering on mergedOn, skip it
if (($mergedOnOrAfter -ne $null) -or ($mergedOnOrBefore -ne $null))
{
continue
}
}
Write-Verbose "$index. $($pullRequest.html_url) ## Created: $($pullRequest.created_at) ## Merged: $($pullRequest.merged_at)"
$index++
$resultToReturn += $pullRequest
}
$query = Get-NextResultPage -jsonResult $jsonResult
} while ($query -ne $null)
}
return $resultToReturn
}
<#
.SYNOPSIS Function which returns number of pull requests created/merged in every week in specific repositories
.PARAM
repositoryUrl Array of repository urls which we want to get pull requests from
.PARAM
numberOfWeeks How many weeks we want to obtain data for
.PARAM
dataType Whether we want to get information about created or merged pull requests in specific weeks
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
Get-GitHubWeeklyPullRequestForRepository -repositoryUrl @('https://github.com/powershell/xpsdesiredstateconfiguration', 'https://github.com/powershell/xwebadministration') -datatype merged
#>
function Get-GitHubWeeklyPullRequestForRepository
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String[]] $repositoryUrl,
[int] $numberOfWeeks = 12,
[Parameter(Mandatory=$true)]
[ValidateSet("created","merged")]
[string] $dataType,
$gitHubAccessToken = $script:gitHubToken
)
$weekDates = Get-WeekDate -numberOfWeeks $numberOfWeeks
$endOfWeek = Get-Date
$results = @()
$totalPullRequests = 0
foreach ($week in $weekDates)
{
Write-Host "Getting Pull Requests from week of $week"
$pullRequests = $null
if ($dataType -eq "merged")
{
$pullRequests = Get-GitHubPullRequestForRepository `
-repositoryUrl $repositoryUrl `
-state 'all' -mergedOnOrAfter $week -mergedOnOrBefore $endOfWeek
}
elseif ($dataType -eq "created")
{
$pullRequests = Get-GitHubPullRequestForRepository `
-repositoryUrl $repositoryUrl `
-state 'all' -createdOnOrAfter $week -createdOnOrBefore $endOfWeek
}
$endOfWeek = $week
if (($pullRequests -ne $null) -and ($pullRequests.Count -eq $null))
{
$count = 1
}
else
{
$count = $pullRequests.Count
}
$totalPullRequests += $count
$results += @{"BeginningOfWeek"=$week; "PullRequests"=$count}
}
$results += @{"BeginningOfWeek"="total"; "PullRequests"=$totalPullRequests}
return $results
}
<#
.SYNOPSIS Function which returns repositories with biggest number of pull requests meeting specified criteria
.PARAM
repositoryUrl Array of repository urls which we want to get pull requests from
.PARAM
state Whether we want to get information about open pull requests, closed or both
.PARAM
createdOnOrAfter Get information about pull requests created after specific date
.PARAM
mergedOnOrAfter Get information about pull requests merged after specific date
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
Get-GitHubTopPullRequestRepository -repositoryUrl @('https://github.com/powershell/xsharepoint', 'https://github.com/powershell/xwebadministration') -state closed -mergedOnOrAfter 2015-04-20
#>
function Get-GitHubTopPullRequestRepository
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String[]] $repositoryUrl,
[ValidateSet("open", "closed", "all")]
[String] $state = "open",
[DateTime] $createdOnOrAfter,
[DateTime] $mergedOnOrAfter,
$gitHubAccessToken = $script:gitHubToken
)
if (($state -eq "open") -and ($mergedOnOrAfter -ne $null))
{
Throw "mergedOnOrAfter cannot be specified if state is open"
}
$repositoryPullRequests = @{}
foreach ($repository in $repositoryUrl)
{
if (($mergedOnOrAfter -ne $null) -and ($createdOnOrAfter -ne $null))
{
$pullRequests = Get-GitHubPullRequestForRepository `
-repositoryUrl $repository `
-state $state -mergedOnOrAfter $mergedOnOrAfter -createdOnOrAfter $createdOnOrAfter
}
elseif (($mergedOnOrAfter -ne $null) -and ($createdOnOrAfter -eq $null))
{
$pullRequests = Get-GitHubPullRequestForRepository `
-repositoryUrl $repository `
-state $state -mergedOnOrAfter $mergedOnOrAfter
}
elseif (($mergedOnOrAfter -eq $null) -and ($createdOnOrAfter -ne $null))
{
$pullRequests = Get-GitHubPullRequestForRepository `
-repositoryUrl $repository `
-state $state -createdOnOrAfter $createdOnOrAfter
}
elseif (($mergedOnOrAfter -eq $null) -and ($createdOnOrAfter -eq $null))
{
$pullRequests = Get-GitHubPullRequestForRepository `
-repositoryUrl $repository `
-state $state
}
if (($pullRequests -ne $null) -and ($pullRequests.Count -eq $null))
{
$count = 1
}
else
{
$count = $pullRequests.Count
}
$repositoryName = Get-GitHubRepositoryNameFromUrl -repositoryUrl $repository
$repositoryPullRequests.Add($repositoryName, $count)
}
$repositoryPullRequests = $repositoryPullRequests.GetEnumerator() | Sort-Object Value -Descending
return $repositoryPullRequests
}
<#
.SYNOPSIS Obtain repository collaborators
.EXAMPLE $collaborators = Get-GitHubRepositoryCollaborator -repositoryUrl @('https://github.com/PowerShell/DscResources')
#>
function Get-GitHubRepositoryCollaborator
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String[]] $repositoryUrl,
$gitHubAccessToken = $script:gitHubToken
)
$resultToReturn = @()
foreach ($repository in $repositoryUrl)
{
$index = 0
Write-Host "Getting repository collaborators for repository $repository" -ForegroundColor Yellow
$repositoryName = Get-GitHubRepositoryNameFromUrl -repositoryUrl $repository
$repositoryOwner = Get-GitHubRepositoryOwnerFromUrl -repositoryUrl $repository
$query = "$script:gitHubApiReposUrl/$repositoryOwner/$repositoryName/collaborators"
if (![string]::IsNullOrEmpty($gitHubAccessToken))
{
$query += "?access_token=$gitHubAccessToken"
}
# Obtain all collaborators
do
{
try
{
$jsonResult = Invoke-WebRequest $query
$collaborators = ConvertFrom-Json -InputObject $jsonResult.content
}
catch [System.Net.WebException] {
Write-Error "Failed to execute query with exception: $($_.Exception)`nHTTP status code: $($_.Exception.Response.StatusCode)"
return $null
}
catch {
Write-Error "Failed to execute query with exception: $($_.Exception)"
return $null
}
foreach ($collaborator in $collaborators)
{
Write-Verbose "$index. $($collaborator.login)"
$index++
$resultToReturn += $collaborator
}
$query = Get-NextResultPage -jsonResult $jsonResult
} while ($query -ne $null)
}
return $resultToReturn
}
<#
.SYNOPSIS Obtain repository contributors
.EXAMPLE $contributors = Get-GitHubRepositoryContributor -repositoryUrl @('https://github.com/PowerShell/DscResources', 'https://github.com/PowerShell/xWebAdministration')
#>
function Get-GitHubRepositoryContributor
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String[]] $repositoryUrl,
$gitHubAccessToken = $script:gitHubToken
)
$resultToReturn = @()
foreach ($repository in $repositoryUrl)
{
$index = 0
Write-Host "Getting repository contributors for repository $repository" -ForegroundColor Yellow
$repositoryName = Get-GitHubRepositoryNameFromUrl -repositoryUrl $repository
$repositoryOwner = Get-GitHubRepositoryOwnerFromUrl -repositoryUrl $repository
$query = "$script:gitHubApiReposUrl/$repositoryOwner/$repositoryName/stats/contributors"
if (![string]::IsNullOrEmpty($gitHubAccessToken))
{
$query += "?access_token=$gitHubAccessToken"
}
# Obtain all contributors
do
{
try
{
$jsonResult = Invoke-WebRequest $query
$contributors = ConvertFrom-Json -InputObject $jsonResult.content
}
catch [System.Net.WebException] {
Write-Error "Failed to execute query with exception: $($_.Exception)`nHTTP status code: $($_.Exception.Response.StatusCode)"
return $null
}
catch {
Write-Error "Failed to execute query with exception: $($_.Exception)"
return $null
}
foreach ($contributor in $contributors)
{
Write-Verbose "$index. $($contributor.author.login). Commits: $($contributor.total)"
$index++
$resultToReturn += $contributor
}
$query = Get-NextResultPage -jsonResult $jsonResult
} while ($query -ne $null)
}
return $resultToReturn
}
<#
.SYNOPSIS Obtain organization members list
.PARAM
organizationName name of the organization
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE $members = Get-GitHubOrganizationMember -organizationName PowerShell
#>
function Get-GitHubOrganizationMember
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String] $organizationName,
$gitHubAccessToken = $script:gitHubToken
)
$resultToReturn = @()
$index = 0
$query = "$script:gitHubApiOrgsUrl/$organizationName/members"
if (![string]::IsNullOrEmpty($gitHubAccessToken))
{
$query += "?access_token=$gitHubAccessToken"
}
do
{
try
{
$jsonResult = Invoke-WebRequest $query
$members = ConvertFrom-Json -InputObject $jsonResult.content
}
catch [System.Net.WebException] {
Write-Error "Failed to execute query with exception: $($_.Exception)`nHTTP status code: $($_.Exception.Response.StatusCode)"
return $null
}
catch {
Write-Error "Failed to execute query with exception: $($_.Exception)"
return $null
}
foreach ($member in $members)
{
Write-Verbose "$index. $(($member).login)"
$index++
$resultToReturn += $member
}
$query = Get-NextResultPage -jsonResult $jsonResult
} while ($query -ne $null)
return $resultToReturn
}
<#
.SYNOPSIS Obtain organization teams list
.PARAM
organizationName name of the organization
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE Get-GitHubTeam -organizationName PowerShell
#>
function Get-GitHubTeam
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String] $organizationName,
$gitHubAccessToken = $script:gitHubToken
)
$resultToReturn = @()
$index = 0
$query = "$script:gitHubApiUrl/orgs/$organizationName/teams"
if (![string]::IsNullOrEmpty($gitHubAccessToken))
{
$query += "?access_token=$gitHubAccessToken"
}
do
{
try
{
$jsonResult = Invoke-WebRequest $query
$teams = ConvertFrom-Json -InputObject $jsonResult.content
}
catch [System.Net.WebException] {
Write-Error "Failed to execute query with exception: $($_.Exception)`nHTTP status code: $($_.Exception.Response.StatusCode)"
return $null
}
catch {
Write-Error "Failed to execute query with exception: $($_.Exception)"
return $null
}
foreach ($team in $teams)
{
Write-Verbose "$index. $(($team).name)"
$index++
$resultToReturn += $team
}
$query = Get-NextResultPage -jsonResult $jsonResult
} while ($query -ne $null)
return $resultToReturn
}
<#
.SYNOPSIS Obtain organization team members list
.PARAM
organizationName name of the organization
.PARAM
teamName name of the team in the organization
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE $members = Get-GitHubTeamMember -organizationName PowerShell -teamName Everybody
#>
function Get-GitHubTeamMember
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String] $organizationName,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String] $teamName,
$gitHubAccessToken = $script:gitHubToken
)
$resultToReturn = @()
$index = 0
$teams = Get-GitHubTeam -organizationName $organizationName
$team = $teams | ? {$_.name -eq $teamName}
if ($team) {
Write-Host "Found team $teamName with id $($team.id)"
} else {
Write-Host "Cannot find team $teamName"
return
}
$query = "$script:gitHubApiUrl/teams/$($team.id)/members"
if (![string]::IsNullOrEmpty($gitHubAccessToken))
{
$query += "?access_token=$gitHubAccessToken"
}
do
{
try
{
$jsonResult = Invoke-WebRequest $query
$members = ConvertFrom-Json -InputObject $jsonResult.content
}
catch [System.Net.WebException] {
Write-Error "Failed to execute query with exception: $($_.Exception)`nHTTP status code: $($_.Exception.Response.StatusCode)"
return $null
}
catch {
Write-Error "Failed to execute query with exception: $($_.Exception)"
return $null
}
foreach ($member in $members)
{
Write-Verbose "$index. $($member.login)"
$index++
$resultToReturn += $member
}
$query = Get-NextResultPage -jsonResult $jsonResult
} while ($query -ne $null)
return $resultToReturn
}
<#
.SYNOPSIS Function which gets list of repositories for a given organization
.PARAM
organization The name of the organization
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
$repositories = Get-GitHubOrganizationRepository -organization 'PowerShell'
#>
function Get-GitHubOrganizationRepository
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String] $organization,
$gitHubAccessToken = $script:gitHubToken
)
$resultToReturn = @()
$query = "$script:gitHubApiUrl/orgs/$organization/repos?"
if (![string]::IsNullOrEmpty($gitHubAccessToken))
{
$query += "&access_token=$gitHubAccessToken"
}
do
{
try
{
$jsonResult = Invoke-WebRequest $query
$repositories = (ConvertFrom-Json -InputObject $jsonResult.content)
}
catch [System.Net.WebException] {
Write-Error "Failed to execute query with exception: $($_.Exception)`nHTTP status code: $($_.Exception.Response.StatusCode)"
return $null
}
catch {
Write-Error "Failed to execute query with exception: $($_.Exception)"
return $null
}
foreach($repository in $repositories)
{
$resultToReturn += $repository
}
$query = Get-NextResultPage -jsonResult $jsonResult
} while ($query -ne $null)
return $resultToReturn
}
<#
.SYNOPSIS Function which gets a list of branches for a given repository
.PARAM
owner The name of the repository owner
.PARAM
repository The name of the repository
.PARAM
gitHubAccessToken GitHub API Access Token.
Get github token from https://github.com/settings/tokens
If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
.EXAMPLE
$branches = Get-GitHubRepositoryBranch -owner PowerShell -repository PowerShellForGitHub
#>
function Get-GitHubRepositoryBranch
{
param
(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String] $owner,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String] $repository,
$gitHubAccessToken = $script:gitHubToken
)
$resultToReturn = @()