Fix overriding request options from @actions/github

This commit is contained in:
Luke Tomlinson
2022-09-30 10:30:00 -04:00
parent d4560e1570
commit 8445ca871a
7 changed files with 82 additions and 42 deletions

View File

@@ -1,6 +1,6 @@
--- ---
name: "@actions/github" name: "@actions/github"
version: 5.1.0 version: 5.1.1
type: npm type: npm
summary: Actions github lib summary: Actions github lib
homepage: https://github.com/actions/toolkit/tree/main/packages/github homepage: https://github.com/actions/toolkit/tree/main/packages/github

View File

@@ -4,38 +4,66 @@ import {getRetryOptions} from '../src/retry-options'
describe('getRequestOptions', () => { describe('getRequestOptions', () => {
test('retries disabled if retries == 0', async () => { test('retries disabled if retries == 0', async () => {
const [retryOptions, requestOptions] = getRetryOptions(0, [400, 500, 502]) const [retryOptions, requestOptions] = getRetryOptions(
0,
[400, 500, 502],
[]
)
expect(retryOptions.enabled).toBe(false) expect(retryOptions.enabled).toBe(false)
expect(retryOptions.doNotRetry).toBeFalsy() expect(retryOptions.doNotRetry).toBeFalsy()
expect(requestOptions.retries).toBeFalsy() expect(requestOptions?.retries).toBeFalsy()
}) })
test('properties set if retries > 0', async () => { test('properties set if retries > 0', async () => {
const [retryOptions, requestOptions] = getRetryOptions(1, [400, 500, 502]) const [retryOptions, requestOptions] = getRetryOptions(
1,
[400, 500, 502],
[]
)
expect(retryOptions.enabled).toBe(true) expect(retryOptions.enabled).toBe(true)
expect(retryOptions.doNotRetry).toEqual([400, 500, 502]) expect(retryOptions.doNotRetry).toEqual([400, 500, 502])
expect(requestOptions.retries).toEqual(1) expect(requestOptions?.retries).toEqual(1)
}) })
test('properties set if retries > 0', async () => { test('properties set if retries > 0', async () => {
const [retryOptions, requestOptions] = getRetryOptions(1, [400, 500, 502]) const [retryOptions, requestOptions] = getRetryOptions(
1,
[400, 500, 502],
[]
)
expect(retryOptions.enabled).toBe(true) expect(retryOptions.enabled).toBe(true)
expect(retryOptions.doNotRetry).toEqual([400, 500, 502]) expect(retryOptions.doNotRetry).toEqual([400, 500, 502])
expect(requestOptions.retries).toEqual(1) expect(requestOptions?.retries).toEqual(1)
}) })
test('retryOptions.doNotRetry not set if exemptStatusCodes isEmpty', async () => { test('retryOptions.doNotRetry not set if exemptStatusCodes isEmpty', async () => {
const [retryOptions, requestOptions] = getRetryOptions(1, []) const [retryOptions, requestOptions] = getRetryOptions(1, [], [])
expect(retryOptions.enabled).toBe(true) expect(retryOptions.enabled).toBe(true)
expect(retryOptions.doNotRetry).toBeUndefined() expect(retryOptions.doNotRetry).toBeUndefined()
expect(requestOptions.retries).toEqual(1) expect(requestOptions?.retries).toEqual(1)
})
test('requestOptions does not override defaults from @actions/github', async () => {
const [retryOptions, requestOptions] = getRetryOptions(1, [], {
request: {
agent: 'default-user-agent'
},
foo: 'bar'
})
expect(retryOptions.enabled).toBe(true)
expect(retryOptions.doNotRetry).toBeUndefined()
expect(requestOptions?.retries).toEqual(1)
expect(requestOptions?.agent).toEqual('default-user-agent')
expect(requestOptions?.foo).toBeUndefined() // this should not be in the `options.request` object, but at the same level as `request`
}) })
}) })

20
dist/index.js vendored
View File

@@ -246,7 +246,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result; return result;
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getOctokitOptions = exports.GitHub = exports.context = void 0; exports.getOctokitOptions = exports.GitHub = exports.defaults = exports.context = void 0;
const Context = __importStar(__webpack_require__(53)); const Context = __importStar(__webpack_require__(53));
const Utils = __importStar(__webpack_require__(914)); const Utils = __importStar(__webpack_require__(914));
// octokit + plugins // octokit + plugins
@@ -255,13 +255,13 @@ const plugin_rest_endpoint_methods_1 = __webpack_require__(45);
const plugin_paginate_rest_1 = __webpack_require__(193); const plugin_paginate_rest_1 = __webpack_require__(193);
exports.context = new Context.Context(); exports.context = new Context.Context();
const baseUrl = Utils.getApiBaseUrl(); const baseUrl = Utils.getApiBaseUrl();
const defaults = { exports.defaults = {
baseUrl, baseUrl,
request: { request: {
agent: Utils.getProxyAgent(baseUrl) agent: Utils.getProxyAgent(baseUrl)
} }
}; };
exports.GitHub = core_1.Octokit.plugin(plugin_rest_endpoint_methods_1.restEndpointMethods, plugin_paginate_rest_1.paginateRest).defaults(defaults); exports.GitHub = core_1.Octokit.plugin(plugin_rest_endpoint_methods_1.restEndpointMethods, plugin_paginate_rest_1.paginateRest).defaults(exports.defaults);
/** /**
* Convience function to correctly format Octokit Options to pass into the constructor. * Convience function to correctly format Octokit Options to pass into the constructor.
* *
@@ -13322,6 +13322,9 @@ var exec = __webpack_require__(514);
// EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js // EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js
var lib_github = __webpack_require__(438); var lib_github = __webpack_require__(438);
// EXTERNAL MODULE: ./node_modules/@actions/github/lib/utils.js
var utils = __webpack_require__(30);
// EXTERNAL MODULE: ./node_modules/@actions/glob/lib/glob.js // EXTERNAL MODULE: ./node_modules/@actions/glob/lib/glob.js
var glob = __webpack_require__(90); var glob = __webpack_require__(90);
@@ -13340,10 +13343,10 @@ function callAsyncFunction(args, source) {
// CONCATENATED MODULE: ./src/retry-options.ts // CONCATENATED MODULE: ./src/retry-options.ts
function getRetryOptions(retries, exemptStatusCodes) { function getRetryOptions(retries, exemptStatusCodes, defaultOptions) {
var _a; var _a;
if (retries <= 0) { if (retries <= 0) {
return [{ enabled: false }, {}]; return [{ enabled: false }, defaultOptions.request];
} }
const retryOptions = { const retryOptions = {
enabled: true enabled: true
@@ -13351,7 +13354,11 @@ function getRetryOptions(retries, exemptStatusCodes) {
if (exemptStatusCodes.length > 0) { if (exemptStatusCodes.length > 0) {
retryOptions.doNotRetry = exemptStatusCodes; retryOptions.doNotRetry = exemptStatusCodes;
} }
// The GitHub type has some defaults for `options.request`
// see: https://github.com/actions/toolkit/blob/4fbc5c941a57249b19562015edbd72add14be93d/packages/github/src/utils.ts#L15
// We pass these in here so they are not overidden.
const requestOptions = { const requestOptions = {
...defaultOptions.request,
retries retries
}; };
Object(core.debug)(`GitHub client configured with: (retries: ${requestOptions.retries}, retry-exempt-status-code: ${(_a = retryOptions === null || retryOptions === void 0 ? void 0 : retryOptions.doNotRetry) !== null && _a !== void 0 ? _a : 'octokit default: [400, 401, 403, 404, 422]'})`); Object(core.debug)(`GitHub client configured with: (retries: ${requestOptions.retries}, retry-exempt-status-code: ${(_a = retryOptions === null || retryOptions === void 0 ? void 0 : retryOptions.doNotRetry) !== null && _a !== void 0 ? _a : 'octokit default: [400, 401, 403, 404, 422]'})`);
@@ -13401,6 +13408,7 @@ const wrapRequire = new Proxy(require, {
process.on('unhandledRejection', handleError); process.on('unhandledRejection', handleError);
main().catch(handleError); main().catch(handleError);
async function main() { async function main() {
@@ -13410,7 +13418,7 @@ async function main() {
const previews = Object(core.getInput)('previews'); const previews = Object(core.getInput)('previews');
const retries = parseInt(Object(core.getInput)('retries')); const retries = parseInt(Object(core.getInput)('retries'));
const exemptStatusCodes = parseNumberArray(Object(core.getInput)('retry-exempt-status-codes')); const exemptStatusCodes = parseNumberArray(Object(core.getInput)('retry-exempt-status-codes'));
const [retryOpts, requestOpts] = getRetryOptions(retries, exemptStatusCodes); const [retryOpts, requestOpts] = getRetryOptions(retries, exemptStatusCodes, utils.defaults);
const opts = {}; const opts = {};
if (debug === 'true') if (debug === 'true')
opts.log = console; opts.log = console;

18
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "github-script", "name": "github-script",
"version": "6.3.0", "version": "6.3.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "github-script", "name": "github-script",
"version": "6.3.0", "version": "6.3.1",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@actions/core": "^1.9.1", "@actions/core": "^1.9.1",
@@ -52,9 +52,9 @@
} }
}, },
"node_modules/@actions/github": { "node_modules/@actions/github": {
"version": "5.1.0", "version": "5.1.1",
"resolved": "https://registry.npmjs.org/@actions/github/-/github-5.1.0.tgz", "resolved": "https://registry.npmjs.org/@actions/github/-/github-5.1.1.tgz",
"integrity": "sha512-tuI80F7JQIhg77ZTTgUAPpVD7ZnP9oHSPN8xw7LOwtA4vEMbAjWJNbmLBfV7xua7r016GyjzWLuec5cs8f/a8A==", "integrity": "sha512-Nk59rMDoJaV+mHCOJPXuvB1zIbomlKS0dmSIqPGxd0enAXBnOfn4VWF+CGtRCwXZG9Epa54tZA7VIRlJDS8A6g==",
"dependencies": { "dependencies": {
"@actions/http-client": "^2.0.1", "@actions/http-client": "^2.0.1",
"@octokit/core": "^3.6.0", "@octokit/core": "^3.6.0",
@@ -6242,9 +6242,9 @@
} }
}, },
"@actions/github": { "@actions/github": {
"version": "5.1.0", "version": "5.1.1",
"resolved": "https://registry.npmjs.org/@actions/github/-/github-5.1.0.tgz", "resolved": "https://registry.npmjs.org/@actions/github/-/github-5.1.1.tgz",
"integrity": "sha512-tuI80F7JQIhg77ZTTgUAPpVD7ZnP9oHSPN8xw7LOwtA4vEMbAjWJNbmLBfV7xua7r016GyjzWLuec5cs8f/a8A==", "integrity": "sha512-Nk59rMDoJaV+mHCOJPXuvB1zIbomlKS0dmSIqPGxd0enAXBnOfn4VWF+CGtRCwXZG9Epa54tZA7VIRlJDS8A6g==",
"requires": { "requires": {
"@actions/http-client": "^2.0.1", "@actions/http-client": "^2.0.1",
"@octokit/core": "^3.6.0", "@octokit/core": "^3.6.0",
@@ -10972,4 +10972,4 @@
"dev": true "dev": true
} }
} }
} }

View File

@@ -1,7 +1,7 @@
{ {
"name": "github-script", "name": "github-script",
"description": "A GitHub action for executing a simple script", "description": "A GitHub action for executing a simple script",
"version": "6.3.0", "version": "6.3.1",
"author": "GitHub", "author": "GitHub",
"license": "MIT", "license": "MIT",
"main": "dist/index.js", "main": "dist/index.js",
@@ -55,4 +55,4 @@
"ts-jest": "^27.0.5", "ts-jest": "^27.0.5",
"typescript": "^4.3.5" "typescript": "^4.3.5"
} }
} }

View File

@@ -1,16 +1,13 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as exec from '@actions/exec' import * as exec from '@actions/exec'
import {context, getOctokit} from '@actions/github' import {context, getOctokit} from '@actions/github'
import {defaults as defaultGitHubOptions} from '@actions/github/lib/utils'
import * as glob from '@actions/glob' import * as glob from '@actions/glob'
import * as io from '@actions/io' import * as io from '@actions/io'
import {retry} from '@octokit/plugin-retry' import {retry} from '@octokit/plugin-retry'
import {RequestRequestOptions} from '@octokit/types'
import {callAsyncFunction} from './async-function' import {callAsyncFunction} from './async-function'
import { import {getRetryOptions, parseNumberArray, RetryOptions} from './retry-options'
getRetryOptions,
parseNumberArray,
RequestOptions,
RetryOptions
} from './retry-options'
import {wrapRequire} from './wrap-require' import {wrapRequire} from './wrap-require'
process.on('unhandledRejection', handleError) process.on('unhandledRejection', handleError)
@@ -21,7 +18,7 @@ type Options = {
userAgent?: string userAgent?: string
previews?: string[] previews?: string[]
retry?: RetryOptions retry?: RetryOptions
request?: RequestOptions request?: RequestRequestOptions
} }
async function main(): Promise<void> { async function main(): Promise<void> {
@@ -33,7 +30,11 @@ async function main(): Promise<void> {
const exemptStatusCodes = parseNumberArray( const exemptStatusCodes = parseNumberArray(
core.getInput('retry-exempt-status-codes') core.getInput('retry-exempt-status-codes')
) )
const [retryOpts, requestOpts] = getRetryOptions(retries, exemptStatusCodes) const [retryOpts, requestOpts] = getRetryOptions(
retries,
exemptStatusCodes,
defaultGitHubOptions
)
const opts: Options = {} const opts: Options = {}
if (debug === 'true') opts.log = console if (debug === 'true') opts.log = console

View File

@@ -1,20 +1,19 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import {OctokitOptions} from '@octokit/core/dist-types/types'
import {RequestRequestOptions} from '@octokit/types'
export type RetryOptions = { export type RetryOptions = {
doNotRetry?: number[] doNotRetry?: number[]
enabled?: boolean enabled?: boolean
} }
export type RequestOptions = {
retries?: number
}
export function getRetryOptions( export function getRetryOptions(
retries: number, retries: number,
exemptStatusCodes: number[] exemptStatusCodes: number[],
): [RetryOptions, RequestOptions] { defaultOptions: OctokitOptions
): [RetryOptions, RequestRequestOptions | undefined] {
if (retries <= 0) { if (retries <= 0) {
return [{enabled: false}, {}] return [{enabled: false}, defaultOptions.request]
} }
const retryOptions: RetryOptions = { const retryOptions: RetryOptions = {
@@ -25,7 +24,11 @@ export function getRetryOptions(
retryOptions.doNotRetry = exemptStatusCodes retryOptions.doNotRetry = exemptStatusCodes
} }
const requestOptions: RequestOptions = { // The GitHub type has some defaults for `options.request`
// see: https://github.com/actions/toolkit/blob/4fbc5c941a57249b19562015edbd72add14be93d/packages/github/src/utils.ts#L15
// We pass these in here so they are not overidden.
const requestOptions: RequestRequestOptions = {
...defaultOptions.request,
retries retries
} }