add throttling

This commit is contained in:
Peter Evans 2024-08-14 21:09:51 +01:00
parent 3cc54e847d
commit 13fa7b0c66
5 changed files with 5460 additions and 6792 deletions

View file

@ -1,7 +1,7 @@
import * as core from '@actions/core'
import {Inputs} from './create-pull-request'
import {Commit} from './git-command-manager'
import {Octokit, OctokitOptions} from './octokit-client'
import {Octokit, OctokitOptions, throttleOptions} from './octokit-client'
import pLimit from 'p-limit'
import * as utils from './utils'
@ -41,6 +41,7 @@ export class GitHubHelper {
} else {
options.baseUrl = 'https://api.github.com'
}
options.throttle = throttleOptions
this.octokit = new Octokit(options)
}

View file

@ -1,17 +1,37 @@
import {Octokit as Core} from '@octokit/core'
import * as core from '@actions/core'
import {Octokit as OctokitCore} from '@octokit/core'
import {paginateRest} from '@octokit/plugin-paginate-rest'
import {restEndpointMethods} from '@octokit/plugin-rest-endpoint-methods'
import {throttling} from '@octokit/plugin-throttling'
import {getProxyForUrl} from 'proxy-from-env'
import {ProxyAgent, fetch as undiciFetch} from 'undici'
export {RestEndpointMethodTypes} from '@octokit/plugin-rest-endpoint-methods'
// eslint-disable-next-line import/no-unresolved
export {OctokitOptions} from '@octokit/core/dist-types/types'
export const Octokit = Core.plugin(
export const Octokit = OctokitCore.plugin(
paginateRest,
restEndpointMethods,
throttling,
autoProxyAgent
)
export const throttleOptions = {
onRateLimit: (retryAfter, options, _, retryCount) => {
core.debug(`Hit rate limit for request ${options.method} ${options.url}`)
// Retries twice for a total of three attempts
if (retryCount < 2) {
core.debug(`Retrying after ${retryAfter} seconds!`)
return true
}
},
onSecondaryRateLimit: (_, options) => {
core.warning(
`Hit secondary rate limit for request ${options.method} ${options.url}`
)
}
}
const proxyFetch =
(proxyUrl: string): typeof undiciFetch =>
(url, opts) => {
@ -24,7 +44,7 @@ const proxyFetch =
}
// Octokit plugin to support the standard environment variables http_proxy, https_proxy and no_proxy
function autoProxyAgent(octokit: Core) {
function autoProxyAgent(octokit: OctokitCore) {
octokit.hook.before('request', options => {
const proxy = getProxyForUrl(options.baseUrl)
if (proxy) {