(token, repo, log)
| 51 | }; |
| 52 | |
| 53 | const octokit = (token, repo, log) => { |
| 54 | if (!token) throw new Error('token not found'); |
| 55 | |
| 56 | const throttleHandler = (reason, offset) => async (retryAfter, options) => { |
| 57 | if (options.request.retryCount <= 5) { |
| 58 | logger.info( |
| 59 | `Retrying because of ${reason} in ${retryAfter + offset} seconds` |
| 60 | ); |
| 61 | await new Promise((resolve) => setTimeout(resolve, offset * 1000)); |
| 62 | return true; |
| 63 | } |
| 64 | }; |
| 65 | const octokitOptions = { |
| 66 | request: { agent: new ProxyAgent() }, |
| 67 | auth: token, |
| 68 | log, |
| 69 | throttle: { |
| 70 | onAbuseLimit: throttleHandler('abuse limit', 120), // deprecated, see onSecondaryRateLimit |
| 71 | onRateLimit: throttleHandler('rate limit', 30), |
| 72 | onSecondaryRateLimit: throttleHandler('secondary rate limit', 30) |
| 73 | } |
| 74 | }; |
| 75 | const { host, hostname } = new url.URL(repo); |
| 76 | if (hostname !== 'github.com') { |
| 77 | // GitHub Enterprise, use the: repo URL host + '/api/v3' - as baseURL |
| 78 | // as per: https://developer.github.com/enterprise/v3/enterprise-admin/#endpoint-urls |
| 79 | octokitOptions.baseUrl = `https://${host}/api/v3`; |
| 80 | } |
| 81 | |
| 82 | const MyOctokit = Octokit.plugin(throttling); |
| 83 | return new MyOctokit(octokitOptions); |
| 84 | }; |
| 85 | |
| 86 | class Github { |
| 87 | constructor(opts = {}) { |
no test coverage detected