Invoke-GitHubRequest

SYNOPSIS

Sends a request to the GitHub REST API

SYNTAX

__AllParameterSets

Invoke-GitHubRequest [[-Method] <string>] [-Path] <string> [[-PathParameters] <hashtable>]
 [[-Query] <hashtable>] [[-CommaSeparatedQuery] <string[]>] [[-Body] <Object>] [[-InFile] <string>]
 [[-ContentType] <string>] [[-Accept] <string>] [[-Headers] <hashtable>]
 [[-PaginationProperty] <string>] [[-OutFile] <string>] [[-OperationId] <string>] [-UseUploadUri]
 [-AllResults] [-StatusCodeAsBoolean] [-Raw]

ALIASES

This cmdlet has the following aliases,

  • Get-GHInvokeRequest

DESCRIPTION

The Invoke-GitHubRequest function is the single request engine behind every Celerium.GitHub command. It can also be used directly to call any endpoint, including ones that are newer than the module

Features

  • Path templates (‘/repos/{owner}/{repo}’) are resolved from -PathParameters with per segment escaping
  • Query strings are built from -Query (arrays, booleans & dates are formatted for GitHub)
  • Request bodies: hashtable / object / array -> JSON, string -> sent as is, byte[] & -InFile -> sent raw
  • Headers: Accept, X-GitHub-Api-Version, User-Agent & Authorization (Bearer) are always handled
  • Authentication token order: key stored with Add-GitHubApiKey, $env:GITHUB_TOKEN, $env:GH_TOKEN, anonymous. The token is only sent to the configured API / upload hosts
  • Redirects (301, 302, 303, 307, 308) are followed (maximum 5)
  • Pagination: -AllResults follows the Link rel=”next” header (same host only)
  • Rate limits: primary (x-ratelimit-remaining = 0) & secondary (retry-after) limits wait & retry when the wait is <= MaxRateLimitWaitSeconds, otherwise a LimitsExceeded error with the reset time is thrown. GET requests are retried on 500, 502, 503, 504 & transport failures with exponential backoff. The number of retries is MaxRetryCount
  • Errors: non-success responses are thrown as terminating errors (ErrorId GitHubApi.) with the GitHub message, documentation url & errors[] details. The error TargetObject has StatusCode, Method, Uri, Message, DocumentationUrl, Errors & RequestId

Output

  • JSON array: every element is written to the pipeline (an empty array writes nothing)
  • JSON object: written once
  • 204 or an empty body: nothing
  • text, diff, patch & sha media types: a string
  • anything else: a [byte[]]
  • -AllResults: items of every page are streamed. Wrapped responses stream the -PaginationProperty items (fallback: the only array property of the response, otherwise the page object)
  • Without -AllResults a wrapped response is returned unchanged

EXAMPLES

EXAMPLE 1

Invoke-GitHubRequest -Path '/repos/{owner}/{repo}' -PathParameters @{ owner = 'octocat'; repo = 'Hello-World' }

Gets one repository

EXAMPLE 2

Invoke-GitHubRequest -Path '/repos/{owner}/{repo}/issues' -PathParameters @{ owner = 'octocat'; repo = 'Hello-World' } -Query @{ state = 'open'; per_page = 100 } -AllResults

Streams every open issue of a repository

EXAMPLE 3

Invoke-GitHubRequest -Method POST -Path '/repos/{owner}/{repo}/issues' -PathParameters @{ owner = 'octocat'; repo = 'Hello-World' } -Body @{ title = 'Bug'; labels = @('bug') }

Creates an issue

EXAMPLE 4

Invoke-GitHubRequest -Path '/user/starred/{owner}/{repo}' -PathParameters @{ owner = 'octocat'; repo = 'Hello-World' } -StatusCodeAsBoolean

Returns $true when the repository is starred & $false when it is not

PARAMETERS

-Accept

Accept header value

Defaults to application/vnd.github+json

Type: System.String
DefaultValue: application/vnd.github+json
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 8
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-AllResults

Follow Link rel=”next” headers & return the results of every page

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Body

Request body. Hashtables, objects & arrays are sent as JSON, a string is sent as is & a [byte[]] is sent raw

Type: System.Object
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 5
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-CommaSeparatedQuery

Names of -Query keys whose array values are sent comma separated (key=a,b) instead of repeated

Type: System.String[]
DefaultValue: '@()'
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 4
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-ContentType

Content-Type of the body

Defaults to application/json; charset=utf-8 (application/octet-stream for -InFile & [byte[]] bodies)

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 7
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Headers

Extra request headers. The Authorization header cannot be overridden

Type: System.Collections.Hashtable
DefaultValue: '@{}'
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 9
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-InFile

Path of a file whose bytes are sent as the body (release asset uploads)

Without -ContentType the content type is application/octet-stream

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 6
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Method

HTTP method

Defaults to GET

Type: System.String
DefaultValue: GET
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 0
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-OperationId

OpenAPI operationId, used in verbose & error text

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 12
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-OutFile

Write the response bytes to this file & return the FileInfo object

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 11
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-PaginationProperty

Name of the array property that holds the items in wrapped list responses (for example ‘workflow_runs’)

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 10
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Path

Path template such as ‘/repos/{owner}/{repo}’, or an absolute https url

Type: System.String
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 1
  IsRequired: true
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-PathParameters

Hashtable of values for the {placeholders} in -Path, keys use the API names (owner, repo, …)

Type: System.Collections.Hashtable
DefaultValue: '@{}'
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 2
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Query

Hashtable of query parameters, keys use the API names (per_page). $null values are skipped

Type: System.Collections.Hashtable
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: 3
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-Raw

Return one [pscustomobject] per page with StatusCode, Headers, Content & Uri instead of the content

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-StatusCodeAsBoolean

Return $true for a 2xx response & $false for a 404 response, other errors are still thrown

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

-UseUploadUri

Send the request to the upload uri (uploads.github.com) instead of the base uri

Type: System.Management.Automation.SwitchParameter
DefaultValue: False
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
  Position: Named
  IsRequired: false
  ValueFromPipeline: false
  ValueFromPipelineByPropertyName: false
  ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

NOTES

Every generated Celerium.GitHub command calls this function

Use Get-GitHubLastResponse to read the status code & rate limit of the last request