generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.ts
55 lines (51 loc) · 1.52 KB
/
action.ts
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
import * as core from '@actions/core'
import { sleep } from './sleep'
export async function act(
actionToTake: string,
limit: number,
reset: number,
alarm: number,
delay: number,
resource: string
): Promise<void> {
// calculate the number of seconds until the rate limit resets
// (getTime() returns milliseconds, the API UTC epoch seconds)
const seconds_to_reset = reset - Math.round(new Date().getTime() / 1000)
const minutes_to_reset = Math.floor(seconds_to_reset / 60)
switch (actionToTake) {
case 'sleep': {
const minutes_alarm = Math.floor(alarm / 60)
core.info(
`The API quota will reset in ${minutes_to_reset} minutes and ${
seconds_to_reset % 60
} seconds.`
)
if (seconds_to_reset < alarm) {
// sleep n milliseconds + delay past the reset time to ensure the limit has been reset
await sleep((seconds_to_reset + delay) * 1000)
core.info(
`The API quota has been reset to ${limit} requests. Farewell!`
)
} else {
core.setFailed(
`Your alarm set to ${minutes_alarm} minutes and ${
alarm % 60
} seconds went off: Sleep is overrated.`
)
}
break
}
case 'peep': {
core.info(
`The API quota will reset in ${minutes_to_reset} minutes and ${
seconds_to_reset % 60
} seconds.`
)
break
}
default:
core.setFailed(
`Workflow run was cancelled because of a low ${resource} API quota.`
)
}
}