Skip to content

Commit 797d330

Browse files
authored
chore: Log GitHub API rate limit (#65)
1 parent b7037c7 commit 797d330

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/ActionWrapper.psm1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ function Invoke-Action {
66
.SYNOPSIS
77
Invoke specific action handler.
88
#>
9+
Write-Log -Summary 'GITHUB API RATE LIMIT' -Message (Get-RateLimit -Core | ConvertTo-Json -compress)
10+
911
switch ($EVENT_TYPE) {
1012
'pull_request' { Initialize-PR }
1113
'pull_request_target' { Initialize-PR }
@@ -16,6 +18,8 @@ function Invoke-Action {
1618
'issues' { Initialize-Issue }
1719
default { Write-Log 'Not supported event type' }
1820
}
21+
22+
Write-Log -Summary 'GITHUB API RATE LIMIT' -Message (Get-RateLimit -Core | ConvertTo-Json -compress)
1923
}
2024

2125
Export-ModuleMember -Function Invoke-Action

src/Github.psm1

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,35 @@ function Remove-Label {
201201
return $responses
202202
}
203203

204+
function Get-RateLimit {
205+
<#
206+
.SYNOPSIS
207+
Retrieves the current GitHub API rate limit status.
208+
Returns the rate limit information as a deserialized JSON object.
209+
.PARAMETER Core
210+
If specified, returns only the core rate limit information.
211+
#>
212+
param(
213+
[Switch] $Core
214+
)
215+
216+
$rateLimit = $null
217+
218+
try {
219+
$response = Invoke-GithubRequest -Query 'rate_limit'
220+
221+
$rateLimit = $response.Content | ConvertFrom-Json
222+
} catch {
223+
Write-Log -Summary "Failed to retrieve rate limit information.`n Exception occurred: $($_.Exception.Message)"
224+
}
225+
226+
if ($Core -and $rateLimit -and $rateLimit.resources -and $rateLimit.resources.core) {
227+
$rateLimit = $rateLimit.resources.core
228+
}
229+
230+
return $rateLimit
231+
}
232+
204233
function Get-JobID {
205234
<#
206235
.SYNOPSIS
@@ -279,10 +308,10 @@ function Get-LogURL {
279308
$logURL += "/job/$job_id"
280309
}
281310

282-
Write-Log "Log URL" $logURL
311+
Write-Log 'Log URL' $logURL
283312

284313
return $logURL
285314
}
286315

287316
Export-ModuleMember -Function Invoke-GithubRequest, Add-Comment, Get-AllChangedFilesInPR, New-Issue, Close-Issue, `
288-
Add-Label, Remove-Label, Get-JobID, Get-LogURL
317+
Add-Label, Remove-Label, Get-RateLimit, Get-JobID, Get-LogURL

0 commit comments

Comments
 (0)