Skip to content

Commit

Permalink
Merge pull request #20 from jrolstad/develop
Browse files Browse the repository at this point in the history
#19 Adding method to get branches for repository
  • Loading branch information
KarolKaczmarek committed Aug 19, 2016
2 parents 341ed64 + e0eee1c commit 7be9ef8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
61 changes: 61 additions & 0 deletions GitHubAnalytics.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,67 @@ function Get-GitHubOrganizationRepository
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 = @()

$query = "$script:gitHubApiUrl/repos/$owner/$repository/branches?"

if (![string]::IsNullOrEmpty($gitHubAccessToken))
{
$query += "&access_token=$gitHubAccessToken"
}

do
{
try
{
$jsonResult = Invoke-WebRequest $query
$branches = (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($branch in $branches)
{
$resultToReturn += $branch
}
$query = Get-NextResultPage -jsonResult $jsonResult
} while ($query -ne $null)

return $resultToReturn
}

<#
.SYNOPSIS Function to get next page with results from query to GitHub API
Expand Down
11 changes: 11 additions & 0 deletions Tests/GitHubAnalytics.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,14 @@ Describe 'Getting repository owner from url' {
$owner | Should be "KarolKaczmarek"
}
}

Describe 'Getting branches for repository' {
$branches = Get-GitHubRepositoryBranch -owner $script:organizationName -repository $script:repositoryName

It 'Should return expected number of repository branches' {
@($branches).Count | Should be 1
}
It 'Should return the name of the branches' {
@($branches[0].name) | Should be "master"
}
}

0 comments on commit 7be9ef8

Please sign in to comment.