Skip to content

Commit

Permalink
Show human readable reset datetime (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaasnicolaas authored Jul 1, 2024
1 parent 9881b43 commit 25ca011
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
9 changes: 9 additions & 0 deletions __tests__/ratelimit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ describe('GitHub Action - Rate Limit Handling', () => {
expect(rateLimitStatus.resetTime).toEqual(
expectedReset - Math.floor(Date.now() / 1000),
)
expect(rateLimitStatus.resetTimeHumanReadable).toEqual(
new Date(expectedReset * 1000).toLocaleString(),
)
expect(core.info).toHaveBeenCalledWith(
`Rate limit remaining: ${expectedRemaining}`,
)
expect(core.info).toHaveBeenCalledWith(
`Rate limit resets at: ${new Date(expectedReset * 1000).toLocaleString()}`,
)
})

it('should warn and stop processing if initial rate limit is exceeded', async () => {
Expand Down
7 changes: 4 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,11 @@ async function checkRateLimit(octokit) {
const remaining = rateLimit.data.resources.core.remaining;
const reset = rateLimit.data.resources.core.reset;
const now = Math.floor(Date.now() / 1000);
const resetTime = reset - now;
const resetTimeInSeconds = reset - now;
const resetTimeHumanReadable = new Date(reset * 1000).toLocaleString();
core.info(`Rate limit remaining: ${remaining}`);
core.info(`Rate limit resets in: ${resetTime} seconds`);
return { remaining, resetTime };
core.info(`Rate limit resets at: ${resetTimeHumanReadable}`);
return { remaining, resetTime: resetTimeInSeconds, resetTimeHumanReadable };
}
run();

Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,15 @@ export async function checkRateLimit(octokit: ReturnType<typeof getOctokit>) {
const rateLimit = await octokit.rest.rateLimit.get()
const remaining = rateLimit.data.resources.core.remaining
const reset = rateLimit.data.resources.core.reset

const now = Math.floor(Date.now() / 1000)
const resetTime = reset - now
const resetTimeInSeconds = reset - now
const resetTimeHumanReadable = new Date(reset * 1000).toLocaleString()

core.info(`Rate limit remaining: ${remaining}`)
core.info(`Rate limit resets in: ${resetTime} seconds`)
core.info(`Rate limit resets at: ${resetTimeHumanReadable}`)

return { remaining, resetTime }
return { remaining, resetTime: resetTimeInSeconds, resetTimeHumanReadable }
}

run()

0 comments on commit 25ca011

Please sign in to comment.