generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
65 lines (56 loc) · 2.27 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
import * as core from '@actions/core'
import { act } from './action'
import { validateResource } from './validateResource'
import { processThreshold } from './processThreshold'
import { fetchRateLimit } from './limitFetcher'
/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
*/
export async function run(): Promise<void> {
try {
// Get and process the threshold input
const thresholdAsString: string = core.getInput('threshold') || '10%'
const { thresholdAsAbsolute, thresholdAsFraction } =
processThreshold(thresholdAsString)
// Get the action input
const actionToTake: string = core.getInput('actionToTake') || 'sweep'
//Get and validate the alarm and delay inputs
const alarm: number = parseInt(core.getInput('alarm')) || -1
const delay: number = parseInt(core.getInput('delay')) || -1
if (alarm <= 0 && actionToTake === 'sleep') {
throw new Error('Alarm must be a positive number')
}
if (delay < 0 && actionToTake === 'sleep') {
throw new Error('Delay must be a positive number')
}
// Get and validate resource input
const resource: string = core.getInput('resource') || 'core'
validateResource(resource)
// Get the token input
const token: string =
core.getInput('token') || String(process.env.GITHUB_TOKEN)
// Fetch the remaining requests, the limit as well as time of the next reset.
const { limit, remaining, reset } = await fetchRateLimit(token, resource)
// set the cutoff, either from the absolute or relative threshold
const cutoff: number =
thresholdAsAbsolute ||
(thresholdAsFraction !== undefined
? Math.ceil(thresholdAsFraction * limit)
: 50)
// corroborate that enough requests remain, otherwise act
if (remaining > cutoff) {
core.info(
`The API quota is plentiful: ${remaining} of ${limit} requests on ${resource} remain.`
)
} else {
core.info(
`The API quota is below the threshold: ${remaining} of ${limit} requests on ${resource} remain.`
)
await act(actionToTake, limit, reset, alarm, delay, resource)
}
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
}
}