Skip to content

Commit 7be9ef8

Browse files
Merge pull request #20 from jrolstad/develop
#19 Adding method to get branches for repository
2 parents 341ed64 + e0eee1c commit 7be9ef8

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

GitHubAnalytics.psm1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,67 @@ function Get-GitHubOrganizationRepository
971971
return $resultToReturn
972972
}
973973

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)`nHTTP 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+
9741035
<#
9751036
.SYNOPSIS Function to get next page with results from query to GitHub API
9761037

Tests/GitHubAnalytics.tests.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,14 @@ Describe 'Getting repository owner from url' {
218218
$owner | Should be "KarolKaczmarek"
219219
}
220220
}
221+
222+
Describe 'Getting branches for repository' {
223+
$branches = Get-GitHubRepositoryBranch -owner $script:organizationName -repository $script:repositoryName
224+
225+
It 'Should return expected number of repository branches' {
226+
@($branches).Count | Should be 1
227+
}
228+
It 'Should return the name of the branches' {
229+
@($branches[0].name) | Should be "master"
230+
}
231+
}

0 commit comments

Comments
 (0)