-
-
Save bielawb/406e4d73fbb7c898e5f9bd2823e26574 to your computer and use it in GitHub Desktop.
GitHubPS - simple function to get Pull Requests using GitHub REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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