* Fetch the current GitHub API rate-limit information via the rate-limit API * and write a JSONL entry for each resource category. * * Use this for a point-in-time snapshot at the start or end of a script, * rather than after every individual API call. * * Returns the core rate-limit snapshot
(github, operation = "fetch")
| 132 | * Core rate-limit data, or null if the call fails or the core resource is absent. |
| 133 | */ |
| 134 | async function fetchAndLogRateLimit(github, operation = "fetch") { |
| 135 | try { |
| 136 | const response = await github.rest.rateLimit.get(); |
| 137 | const resources = response?.data?.resources; |
| 138 | if (!resources) return null; |
| 139 | |
| 140 | const timestamp = new Date().toISOString(); |
| 141 | for (const [resource, data] of Object.entries(resources)) { |
| 142 | if (!data || typeof data !== "object") continue; |
| 143 | /** @type {Record<string, unknown>} */ |
| 144 | const entry = { |
| 145 | timestamp, |
| 146 | source: "rate_limit_api", |
| 147 | operation, |
| 148 | resource, |
| 149 | limit: data.limit, |
| 150 | remaining: data.remaining, |
| 151 | used: data.used, |
| 152 | reset: data.reset ? new Date(data.reset * 1000).toISOString() : null, |
| 153 | }; |
| 154 | appendEntry(entry); |
| 155 | } |
| 156 | |
| 157 | const coreData = resources.core; |
| 158 | if (!coreData || typeof coreData !== "object") return null; |
| 159 | const remaining = Number(coreData.remaining); |
| 160 | const limit = Number(coreData.limit); |
| 161 | const used = Number(coreData.used); |
| 162 | const resetSeconds = Number(coreData.reset); |
| 163 | if (!Number.isFinite(remaining) || !Number.isFinite(limit) || !Number.isFinite(used) || !Number.isFinite(resetSeconds)) { |
| 164 | return null; |
| 165 | } |
| 166 | return { |
| 167 | remaining, |
| 168 | limit, |
| 169 | used, |
| 170 | reset: new Date(resetSeconds * 1000).toISOString(), |
| 171 | }; |
| 172 | } catch (err) { |
| 173 | core.warning(`github_rate_limit_logger: fetchAndLogRateLimit failed: ${getErrorMessage(err)}`); |
| 174 | return null; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Log a retry attempt to the JSONL log file, capturing any rate-limit headers |
no test coverage detected