@@ -971,6 +971,67 @@ function Get-GitHubOrganizationRepository
971
971
return $resultToReturn
972
972
}
973
973
974
+ <#
975
+ .SYNOPSIS Function which gets a list of branches for a given repository
976
+ .PARAM
977
+ owner The name of the repository owner
978
+ .PARAM
979
+ repository The name of the repository
980
+ .PARAM
981
+ gitHubAccessToken GitHub API Access Token.
982
+ Get github token from https://github.com/settings/tokens
983
+ If you don't provide it, you can still use this script, but you will be limited to 60 queries per hour.
984
+ . EXAMPLE
985
+ $branches = Get-GitHubRepositoryBranch -owner PowerShell -repository PowerShellForGitHub
986
+ #>
987
+ function Get-GitHubRepositoryBranch
988
+ {
989
+ param
990
+ (
991
+ [Parameter (Mandatory = $true )]
992
+ [ValidateNotNullOrEmpty ()]
993
+ [String ] $owner ,
994
+ [Parameter (Mandatory = $true )]
995
+ [ValidateNotNullOrEmpty ()]
996
+ [String ] $repository ,
997
+ $gitHubAccessToken = $script :gitHubToken
998
+ )
999
+
1000
+ $resultToReturn = @ ()
1001
+
1002
+ $query = " $script :gitHubApiUrl /repos/$owner /$repository /branches?"
1003
+
1004
+ if (! [string ]::IsNullOrEmpty($gitHubAccessToken ))
1005
+ {
1006
+ $query += " &access_token=$gitHubAccessToken "
1007
+ }
1008
+
1009
+ do
1010
+ {
1011
+ try
1012
+ {
1013
+ $jsonResult = Invoke-WebRequest $query
1014
+ $branches = (ConvertFrom-Json - InputObject $jsonResult.content )
1015
+ }
1016
+ catch [System.Net.WebException ] {
1017
+ Write-Error " Failed to execute query with exception: $ ( $_.Exception ) `n HTTP status code: $ ( $_.Exception.Response.StatusCode ) "
1018
+ return $null
1019
+ }
1020
+ catch {
1021
+ Write-Error " Failed to execute query with exception: $ ( $_.Exception ) "
1022
+ return $null
1023
+ }
1024
+
1025
+ foreach ($branch in $branches )
1026
+ {
1027
+ $resultToReturn += $branch
1028
+ }
1029
+ $query = Get-NextResultPage - jsonResult $jsonResult
1030
+ } while ($query -ne $null )
1031
+
1032
+ return $resultToReturn
1033
+ }
1034
+
974
1035
<#
975
1036
.SYNOPSIS Function to get next page with results from query to GitHub API
976
1037
0 commit comments