Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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