Skip to content

Instantly share code, notes, and snippets.

@bielawb
Created March 13, 2017 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bielawb/406e4d73fbb7c898e5f9bd2823e26574 to your computer and use it in GitHub Desktop.
Save bielawb/406e4d73fbb7c898e5f9bd2823e26574 to your computer and use it in GitHub Desktop.
GitHubPS - simple function to get Pull Requests using GitHub REST API
function Get-GitHubPullRequest {
<#
.Synopsis
Function to read pull requests from GitHub
.Description
Function that uses REST endpoint to read information about Pull Requests available on GitHub.
It is required to provide it with:
- owner
- repository id
#>
[CmdletBinding()]
[OutputType('GitHub.PullRequest')]
param (
# Name of the owner of the repo (user or organization)
[Parameter(
Mandatory
)]
[String]$Owner,
# Name of the repository
[Parameter(
Mandatory
)]
[String]$Repository
)
$uri = "https://api.github.com/repos/$Owner/$Repository/pulls"
try {
$ProgressPreference = 'SilentlyContinue'
$pullRequests = Invoke-WebRequest -UseBasicParsing -Uri $uri -ErrorAction Stop
($pullRequests.Content | ConvertFrom-Json) | ForEach-Object {
$closedAt = try { [datetime]$_.closed_at } catch { '' }
[PSCustomObject]@{
PSTypeName = 'GitHub.PullRequest'
Title = $_.title
Body = $_.body
User = $_.user.login
CreatedAt = [dateTime]$_.created_at
ClosedAt = $closedAt
State = $_.state
}
}
} catch {
throw "Failed to read PRs from $Repository owned by $Owner - $_"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment