mirror of
https://github.com/actions/github-script.git
synced 2025-12-08 16:16:21 +00:00
refactor and add tests for retry options
This commit is contained in:
55
src/main.ts
55
src/main.ts
@@ -5,6 +5,12 @@ import * as glob from '@actions/glob'
|
||||
import * as io from '@actions/io'
|
||||
import {retry} from '@octokit/plugin-retry'
|
||||
import {callAsyncFunction} from './async-function'
|
||||
import {
|
||||
getRetryOptions,
|
||||
parseNumberArray,
|
||||
RequestOptions,
|
||||
RetryOptions
|
||||
} from './retry-options'
|
||||
import {wrapRequire} from './wrap-require'
|
||||
|
||||
process.on('unhandledRejection', handleError)
|
||||
@@ -14,14 +20,8 @@ type Options = {
|
||||
log?: Console
|
||||
userAgent?: string
|
||||
previews?: string[]
|
||||
retry?: {
|
||||
doNotRetry?: number[]
|
||||
enabled?: boolean
|
||||
}
|
||||
request?: {
|
||||
retries: number
|
||||
retryAfter: number
|
||||
}
|
||||
retry?: RetryOptions
|
||||
request?: RequestOptions
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
@@ -32,34 +32,18 @@ async function main(): Promise<void> {
|
||||
const retries = parseInt(core.getInput('retries'))
|
||||
const retryAfter = parseInt(core.getInput('retry-after'))
|
||||
const doNotRetry = parseNumberArray(core.getInput('do-not-retry'))
|
||||
const [retryOpts, requestOpts] = getRetryOptions(
|
||||
retries,
|
||||
retryAfter,
|
||||
doNotRetry
|
||||
)
|
||||
|
||||
const opts: Options = {}
|
||||
if (debug === 'true') opts.log = console
|
||||
if (userAgent != null) opts.userAgent = userAgent
|
||||
if (previews != null) opts.previews = previews.split(',')
|
||||
|
||||
if (retries > 0) {
|
||||
if (doNotRetry.length > 0) {
|
||||
opts.retry = {doNotRetry}
|
||||
}
|
||||
|
||||
opts.request = {
|
||||
retries,
|
||||
retryAfter
|
||||
}
|
||||
|
||||
core.info(
|
||||
`GitHub client configured with: (retries: ${retries}, retryAfter: ${retryAfter}, doNotRetry: ${
|
||||
doNotRetry.length == 0
|
||||
? 'octokit default: [400, 401, 403, 404, 422]'
|
||||
: doNotRetry
|
||||
})`
|
||||
)
|
||||
} else {
|
||||
opts.retry = {
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
if (retryOpts) opts.retry = retryOpts
|
||||
if (requestOpts) opts.request = requestOpts
|
||||
|
||||
const github = getOctokit(token, opts, retry)
|
||||
|
||||
@@ -104,12 +88,3 @@ function handleError(err: any): void {
|
||||
console.error(err)
|
||||
core.setFailed(`Unhandled error: ${err}`)
|
||||
}
|
||||
|
||||
function parseNumberArray(listString: string): number[] {
|
||||
if (!listString) {
|
||||
return []
|
||||
}
|
||||
|
||||
const split = listString.trim().split(',')
|
||||
return split.map(x => parseInt(x))
|
||||
}
|
||||
|
||||
53
src/retry-options.ts
Normal file
53
src/retry-options.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import * as core from '@actions/core'
|
||||
|
||||
export type RetryOptions = {
|
||||
doNotRetry?: number[]
|
||||
enabled?: boolean
|
||||
}
|
||||
|
||||
export type RequestOptions = {
|
||||
retries?: number
|
||||
retryAfter?: number
|
||||
}
|
||||
|
||||
export function getRetryOptions(
|
||||
retries: number,
|
||||
retryAfter: number,
|
||||
doNotRetry: number[]
|
||||
): [RetryOptions, RequestOptions] {
|
||||
if (retries <= 0) {
|
||||
return [{enabled: false}, {}]
|
||||
}
|
||||
|
||||
const retryOptions: RetryOptions = {
|
||||
enabled: true
|
||||
}
|
||||
|
||||
if (doNotRetry.length > 0) {
|
||||
retryOptions.doNotRetry = doNotRetry
|
||||
}
|
||||
|
||||
const requestOptions: RequestOptions = {
|
||||
retries,
|
||||
retryAfter: retryAfter
|
||||
}
|
||||
|
||||
core.info(
|
||||
`GitHub client configured with: (retries: ${
|
||||
requestOptions.retries
|
||||
}, retryAfter: ${requestOptions.retryAfter}, doNotRetry: ${
|
||||
retryOptions?.doNotRetry ?? 'octokit default: [400, 401, 403, 404, 422]'
|
||||
})`
|
||||
)
|
||||
|
||||
return [retryOptions, requestOptions]
|
||||
}
|
||||
|
||||
export function parseNumberArray(listString: string): number[] {
|
||||
if (!listString) {
|
||||
return []
|
||||
}
|
||||
|
||||
const split = listString.trim().split(',')
|
||||
return split.map(x => parseInt(x))
|
||||
}
|
||||
Reference in New Issue
Block a user