-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoctokit.js
55 lines (45 loc) · 1.68 KB
/
octokit.js
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
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
const core = require('@actions/core')
const { GitHub, getOctokitOptions } = require('@actions/github/lib/utils')
const { Octokit } = require('@octokit/rest')
const { retry } = require('@octokit/plugin-retry')
const { throttling } = require('@octokit/plugin-throttling')
const abuseLimitRetries = 5
const rateLimitRetries = 5
const baseOptions = {
log: {
debug: core.debug,
info: core.info,
warning: core.warning,
error: core.error
},
throttle: {
onAbuseLimit (retryAfter, options) {
core.info(`Abuse limit triggered for request ${options.method} ${options.url} (attempt ${options.request.retryCount}/${abuseLimitRetries})`)
if (options.request.retryCount < abuseLimitRetries) {
core.info(`Retrying after ${retryAfter} seconds`)
return true
}
core.warning(`Exhausted abuse limit retry count (${abuseLimitRetries}) for ${options.method} ${options.url}`)
},
onRateLimit (retryAfter, options) {
core.info(`Rate limit triggered for request ${options.method} ${options.url} (attempt ${options.request.retryCount}/${rateLimitRetries})`)
if (options.request.retryCount < rateLimitRetries) {
core.info(`Retrying after ${retryAfter} seconds`)
return true
}
core.warning(`Exhausted rate limit retry count (${rateLimitRetries}) for ${options.method} ${options.url}`)
}
}
}
module.exports = function client (token) {
if (!token) {
return new Octokit(baseOptions)
}
const OctokitInstance = GitHub.plugin(throttling, retry)
const options = getOctokitOptions(token, baseOptions)
return new OctokitInstance(options)
}