|
1 | 1 | function Invoke-GitHubAPI { |
2 | 2 | <# |
3 | | - .SYNOPSIS |
4 | | - Calls the GitHub API using the provided parameters. |
| 3 | + .SYNOPSIS |
| 4 | + Calls the GitHub API using the provided parameters. |
5 | 5 |
|
6 | | - .DESCRIPTION |
7 | | - This function is a wrapper around Invoke-RestMethod tailored for calling GitHub's API. |
8 | | - It automatically handles the endpoint URI construction, headers, and token authentication. |
| 6 | + .DESCRIPTION |
| 7 | + This function is a wrapper around Invoke-RestMethod tailored for calling GitHub's API. |
| 8 | + It automatically handles the endpoint URI construction, headers, and token authentication. |
9 | 9 |
|
10 | | - .EXAMPLE |
11 | | - Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET |
| 10 | + .EXAMPLE |
| 11 | + Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET |
12 | 12 |
|
13 | | - Gets all open pull requests for the specified repository. |
| 13 | + Gets all open pull requests for the specified repository. |
14 | 14 |
|
15 | | - .EXAMPLE |
16 | | - Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET -Body @{ state = 'open' } |
| 15 | + .EXAMPLE |
| 16 | + Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET -Body @{ state = 'open' } |
17 | 17 |
|
18 | | - Gets all open pull requests for the specified repository, filtered by the 'state' parameter. |
| 18 | + Gets all open pull requests for the specified repository, filtered by the 'state' parameter. |
19 | 19 |
|
20 | | - .EXAMPLE |
21 | | - Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET -Body @{ state = 'open' } -Accept 'application/vnd.github.v3+json' |
| 20 | + .EXAMPLE |
| 21 | + Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET -Body @{ state = 'open' } -Accept 'application/vnd.github.v3+json' |
22 | 22 |
|
23 | | - Gets all open pull requests for the specified repository, filtered by the 'state' parameter, and using the specified 'Accept' header. |
24 | | -#> |
| 23 | + Gets all open pull requests for the specified repository, filtered by the 'state' parameter, and using the specified 'Accept' header. |
| 24 | + #> |
25 | 25 | [CmdletBinding()] |
26 | 26 | param ( |
27 | 27 | # The HTTP method to be used for the API request. It can be one of the following: GET, POST, PUT, DELETE, or PATCH. |
|
42 | 42 |
|
43 | 43 | # The 'Accept' header for the API request. If not provided, the default will be used by GitHub's API. |
44 | 44 | [Parameter()] |
45 | | - [string] $Accept, |
| 45 | + [string] $Accept = 'application/vnd.github+json', |
46 | 46 |
|
47 | 47 | # Specifies the HTTP version used for the request. |
48 | 48 | [Parameter()] |
49 | | - $HttpVersion = '2.0', |
| 49 | + [version] $HttpVersion = '2.0', |
50 | 50 |
|
51 | 51 | # Support Pagination Relation Links per RFC5988. |
52 | 52 | [Parameter()] |
53 | | - $FollowRelLink = $true, |
| 53 | + [bool] $FollowRelLink = $true, |
54 | 54 |
|
55 | 55 | # The secure token used for authentication in the GitHub API. It should be stored as a SecureString to ensure it's kept safe in memory. |
56 | 56 | [Parameter()] |
|
76 | 76 |
|
77 | 77 | $URI = ("$ApiBaseUri/" -replace '/$', '') + ("/$ApiEndpoint" -replace '^/', '') |
78 | 78 |
|
| 79 | + # $AccessTokenAsPlainText = ConvertFrom-SecureString $AccessToken -AsPlainText |
| 80 | + # # Swap out this by using the -Authentication Bearer -Token $AccessToken |
| 81 | + # switch -Regex ($AccessTokenAsPlainText) { |
| 82 | + # '^ghp_|^github_pat_' { |
| 83 | + # $headers.authorization = "token $AccessTokenAsPlainText" |
| 84 | + # } |
| 85 | + # '^ghu_|^gho_' { |
| 86 | + # $headers.authorization = "Bearer $AccessTokenAsPlainText" |
| 87 | + # } |
| 88 | + # default { |
| 89 | + # $tokenPrefix = $AccessTokenAsPlainText -replace '_.*$', '_*' |
| 90 | + # $errorMessage = "Unexpected AccessToken format: $tokenPrefix" |
| 91 | + # Write-Error $errorMessage |
| 92 | + # throw $errorMessage |
| 93 | + # } |
| 94 | + # } |
| 95 | + |
79 | 96 | $APICall = @{ |
80 | 97 | Uri = $URI |
81 | 98 | Method = $Method |
|
88 | 105 | StatusCodeVariable = 'StatusCode' |
89 | 106 | ResponseHeadersVariable = 'ResponseHeaders' |
90 | 107 | } |
91 | | - Remove-HashTableEntries -Hashtable $APICall -NullOrEmptyValues |
| 108 | + $APICall | Remove-HashTableEntries -NullOrEmptyValues |
92 | 109 |
|
93 | 110 | if ($Body) { |
| 111 | + $Body | Remove-HashTableEntries -NullOrEmptyValues |
| 112 | + |
| 113 | + # Use body to create the query string for GET requests |
| 114 | + if ($Method -eq 'GET') { |
| 115 | + $queryParams = ($Body.GetEnumerator() | |
| 116 | + ForEach-Object { "$([System.Web.HttpUtility]::UrlEncode($_.Key))=$([System.Web.HttpUtility]::UrlEncode($_.Value))" }) -join '&' |
| 117 | + if ($queryParams) { |
| 118 | + $APICall.Uri = $APICall.Uri + '?' + $queryParams |
| 119 | + } |
| 120 | + } |
94 | 121 | if ($Body -is [string]) { |
95 | 122 | $APICall.Body = $Body |
96 | 123 | } else { |
|
101 | 128 | try { |
102 | 129 | Invoke-RestMethod @APICall | Write-Output |
103 | 130 | Write-Verbose ($StatusCode | ConvertTo-Json -Depth 100) |
104 | | - Write-Verbose ($ResponseHeaders | ConvertTo-Json -Depth 100) |
105 | | - } catch [System.Net.WebException] { |
106 | | - Write-Error "[$functionName] - WebException - $($_.Exception.Message)" |
107 | | - throw $_ |
| 131 | + Write-Verbose ($responseHeaders | ConvertTo-Json -Depth 100) |
108 | 132 | } catch { |
109 | | - Write-Error "[$functionName] - GeneralException - $($_.Exception.Message)" |
110 | | - throw $_ |
| 133 | + Write-Error "[$functionName] - Status code - [$StatusCode]" |
| 134 | + $err = $_ | ConvertFrom-Json -Depth 10 |
| 135 | + Write-Error "[$functionName] - $($err.Message)" |
| 136 | + Write-Error "[$functionName] - For more info please see: [$($err.documentation_url)]" |
111 | 137 | } |
112 | 138 | } |
0 commit comments