-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-GitHubRateLimit.ps1
More file actions
465 lines (376 loc) · 11.5 KB
/
Copy pathGet-GitHubRateLimit.ps1
File metadata and controls
465 lines (376 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#Requires -Version 7.0
<#
.SYNOPSIS
Displays GitHub API rate-limit buckets using GitHub CLI.
.DESCRIPTION
Queries GitHub's /rate_limit endpoint through GitHub CLI and renders the
response as either formatted JSON or a terminal-friendly table sorted by the
most constrained buckets first.
This PowerShell version only requires GitHub CLI. Unlike the Bash version, it
does not require jq because JSON parsing is handled natively by PowerShell.
Requirements:
- GitHub CLI: gh
- An authenticated gh session
Setup:
Install GitHub CLI, then authenticate once:
gh auth login
Examples:
macOS: brew install gh
Windows: winget install --id GitHub.cli
Linux: https://cli.github.com/ for distro-specific packages
This script polls GitHub's /rate_limit endpoint, which is exempt from rate
limiting, so watch mode is safe to use.
.PARAMETER Watch
Refresh continuously until interrupted.
.PARAMETER Interval
Refresh interval in seconds for watch mode. Default is 10.
.PARAMETER Json
Print the raw API response as formatted JSON.
.PARAMETER Quiet
Only show buckets whose remaining value is below the limit and whose limit is
greater than zero.
.PARAMETER Help
Show usage information.
.INPUTS
None. This script does not accept pipeline input.
.OUTPUTS
System.String. Renders the rate-limit table, formatted JSON, or usage text.
.EXAMPLE
./Get-GitHubRateLimit.ps1
Print a one-off snapshot of all rate-limit buckets.
.EXAMPLE
./Get-GitHubRateLimit.ps1 -Watch -Interval 60
Refresh the table every 60 seconds until interrupted.
.EXAMPLE
./Get-GitHubRateLimit.ps1 -Quiet
Show only buckets with limit greater than zero and remaining below limit.
.EXAMPLE
./Get-GitHubRateLimit.ps1 -Json
Print the raw API response as formatted JSON.
.NOTES
Author: Sebastian Gräf
Repo: https://github.com/segraef/Scripts
Version history is tracked in git, not in this header.
#>
[CmdletBinding()]
param
(
[Alias('w')]
[switch]$Watch,
[Alias('i')]
[ValidateRange(1, 86400)]
[int]$Interval = 10,
[Alias('j')]
[switch]$Json,
[Alias('q')]
[switch]$Quiet,
[Alias('h')]
[switch]$Help
)
#region Initialisation
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
Import-Module "$PSScriptRoot/Write-Log.psm1" -Force
#endregion
#region Functions
function Show-Usage {
<#
.SYNOPSIS
Print command-line usage information for this script.
.DESCRIPTION
Emits a human-readable summary of switches, short flags, requirements and
examples to the output stream.
.EXAMPLE
Show-Usage
Print the usage banner.
#>
[CmdletBinding()]
param ()
Write-Output @'
Get-GitHubRateLimit - GitHub API rate-limit monitor
Usage:
Get-GitHubRateLimit.ps1 # snapshot
Get-GitHubRateLimit.ps1 -Watch # watch mode (refresh every 10s)
Get-GitHubRateLimit.ps1 -Watch -Interval 5
Get-GitHubRateLimit.ps1 -Json # raw JSON
Get-GitHubRateLimit.ps1 -Quiet # only buckets with limit > 0 and remaining < limit
Get-GitHubRateLimit.ps1 -Help
Short flags:
-w -i 5 -j -q -h
Requirements:
- gh (GitHub CLI)
- an authenticated gh session
Setup:
1. Install GitHub CLI.
macOS: brew install gh
Windows: winget install --id GitHub.cli
Linux: https://cli.github.com/
2. Authenticate once:
gh auth login
Notes:
- This PowerShell version does not require jq.
- It uses gh for API calls and auth context.
- /rate_limit itself is exempt from rate limiting, so polling it is safe.
Examples:
Get-GitHubRateLimit.ps1
Get-GitHubRateLimit.ps1 -Quiet
Get-GitHubRateLimit.ps1 -Watch -Interval 60
Get-GitHubRateLimit.ps1 -Json
'@
}
function Test-CommandExists {
<#
.SYNOPSIS
Test whether a command is available on PATH.
.DESCRIPTION
Returns $true when Get-Command can resolve the supplied command name,
otherwise $false.
.PARAMETER Name
The command name to resolve (for example 'gh').
.EXAMPLE
Test-CommandExists -Name 'gh'
Returns $true when GitHub CLI is installed.
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseSingularNouns', '',
Justification = 'Exists is a state predicate, not a plural noun.')]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Name
)
return $null -ne (Get-Command -Name $Name -ErrorAction SilentlyContinue)
}
function Get-Style {
<#
.SYNOPSIS
Build an ANSI escape sequence for a terminal style code.
.DESCRIPTION
Returns an ANSI escape sequence for the supplied SGR code, or an empty
string when output is redirected or the terminal is 'dumb'.
.PARAMETER Code
The SGR (Select Graphic Rendition) code, for example '31' for red.
.EXAMPLE
Get-Style -Code '1'
Returns the bold escape sequence when the terminal supports colour.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Code
)
if ([Console]::IsOutputRedirected -or $env:TERM -eq 'dumb') {
return ''
}
return [char]27 + '[' + $Code + 'm'
}
function Get-Bar {
<#
.SYNOPSIS
Render a coloured usage bar for a rate-limit bucket.
.DESCRIPTION
Produces a fixed-width bar whose filled portion is proportional to the
remaining quota, coloured green, yellow or red by percentage remaining.
.PARAMETER Remaining
The remaining requests in the bucket.
.PARAMETER Limit
The total request limit for the bucket.
.PARAMETER Width
The bar width in characters. Default is 24.
.EXAMPLE
Get-Bar -Remaining 50 -Limit 100
Returns a half-filled bar.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[int]$Remaining,
[Parameter(Mandatory)]
[int]$Limit,
[Parameter()]
[int]$Width = 24
)
if ($Limit -le 0) {
return ' ' * $Width
}
$filled = [math]::Floor(($Remaining * $Width) / $Limit)
if ($filled -lt 0) {
$filled = 0
}
if ($filled -gt $Width) {
$filled = $Width
}
$empty = $Width - $filled
$pct = [math]::Floor(($Remaining * 100) / $Limit)
$colour = $script:C_GRN
if ($pct -lt 10) {
$colour = $script:C_RED
}
elseif ($pct -lt 33) {
$colour = $script:C_YEL
}
$filledText = if ($filled -gt 0) { '█' * $filled } else { '' }
$emptyText = if ($empty -gt 0) { '░' * $empty } else { '' }
return "$colour$filledText$($script:C_DIM)$emptyText$($script:C_RESET)"
}
function Get-HumanReset {
<#
.SYNOPSIS
Format a Unix reset timestamp as a human-readable countdown.
.DESCRIPTION
Converts an absolute Unix epoch reset time into a relative string such as
'in 4m05s', 'in 30s', or 'now' when the reset is in the past.
.PARAMETER Target
The reset time as Unix epoch seconds.
.EXAMPLE
Get-HumanReset -Target 1717000000
Returns a relative countdown string for that reset time.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[long]$Target
)
$now = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
$diff = $Target - $now
if ($diff -le 0) {
return 'now'
}
$minutes = [math]::Floor($diff / 60)
$seconds = $diff % 60
if ($minutes -gt 0) {
return ('in {0}m{1:00}s' -f $minutes, $seconds)
}
return ('in {0}s' -f $seconds)
}
function Get-HostName {
<#
.SYNOPSIS
Resolve the GitHub host the current gh session is logged in to.
.DESCRIPTION
Parses 'gh auth status' to extract the host name, falling back to
'github.com' when it cannot be determined.
.EXAMPLE
Get-HostName
Returns 'github.com' for a standard authenticated session.
#>
[CmdletBinding()]
param ()
try {
$status = gh auth status 2>&1 | Out-String
$match = [regex]::Match($status, 'Logged in to\s+([^\s]+)')
if ($match.Success) {
return $match.Groups[1].Value
}
}
catch {
Write-Log -Message 'Could not determine gh host; falling back to github.com.' -ErrorRecord $_
}
return 'github.com'
}
function Invoke-Render {
<#
.SYNOPSIS
Fetch and render the current GitHub rate-limit state.
.DESCRIPTION
Calls 'gh api rate_limit' and renders the response either as formatted
JSON (when -Json is set) or as a coloured table sorted by the most
constrained buckets first. Honours the script-level -Quiet switch.
.EXAMPLE
Invoke-Render
Render a single snapshot of the rate-limit buckets.
#>
[CmdletBinding()]
param ()
$payloadText = gh api rate_limit 2>$null
if (-not $payloadText) {
throw 'gh api failed - are you authenticated? (gh auth status)'
}
if ($Json) {
Write-Output ($payloadText | ConvertFrom-Json | ConvertTo-Json -Depth 8)
return
}
$payload = $payloadText | ConvertFrom-Json
$user = '?'
try {
$user = (gh api user --jq .login 2>$null).Trim()
if (-not $user) {
$user = '?'
}
}
catch {
Write-Log -Message 'Could not resolve gh user login.' -ErrorRecord $_
}
$hostName = Get-HostName
$now = Get-Date -Format 'HH:mm:ss'
Write-Output ("{0}{1}GitHub rate limits{2} user={3}{4}{5} host={6} {7}{8}{2}" -f $script:C_BOLD, $script:C_CYN, $script:C_RESET, $script:C_BOLD, $user, $script:C_RESET, $hostName, $script:C_DIM, $now)
Write-Output ''
$entries = foreach ($property in $payload.resources.PSObject.Properties) {
$value = $property.Value
$pct = if ($value.limit -gt 0) { [math]::Floor(($value.remaining * 100) / $value.limit) } else { 100 }
[pscustomobject]@{
Key = $property.Name
Remaining = [int]$value.remaining
Limit = [int]$value.limit
Used = [int]$value.used
Reset = [long]$value.reset
Pct = [int]$pct
}
}
foreach ($entry in ($entries | Sort-Object Pct, Key)) {
if ($Quiet -and (($entry.Remaining -eq $entry.Limit) -or ($entry.Limit -eq 0))) {
continue
}
$line = "{0}{1,-26}{2} {3,5}/{4,-5} {5} {6,3}% resets {7}" -f `
$script:C_BOLD, $entry.Key, $script:C_RESET, $entry.Remaining, $entry.Limit, (Get-Bar -Remaining $entry.Remaining -Limit $entry.Limit -Width 24), $entry.Pct, (Get-HumanReset -Target $entry.Reset)
Write-Output $line
}
}
#endregion
#region Execution
if ($Help) {
Show-Usage
return
}
$script:C_RESET = Get-Style '0'
$script:C_DIM = Get-Style '2'
$script:C_BOLD = Get-Style '1'
$script:C_RED = Get-Style '31'
$script:C_YEL = Get-Style '33'
$script:C_GRN = Get-Style '32'
$script:C_CYN = Get-Style '36'
if (-not (Test-CommandExists -Name 'gh')) {
Write-Log -Message 'gh not installed.' -Level Error
exit 1
}
try {
gh auth status *> $null
}
catch {
Write-Log -Message 'gh is installed but not authenticated. Run: gh auth login' -ErrorRecord $_
exit 1
}
if ($Watch) {
while ($true) {
Clear-Host
try {
Invoke-Render
}
catch {
Write-Log -Message 'Failed to render rate-limit snapshot.' -ErrorRecord $_
}
Write-Output ''
Write-Output ("{0}refresh every {1}s - ctrl-c to exit{2}" -f $script:C_DIM, $Interval, $script:C_RESET)
Start-Sleep -Seconds $Interval
}
}
else {
Invoke-Render
}
#endregion