Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 660d907

Browse filesBrowse files
committed
refactor and add tests for retry options
1 parent 355d895 commit 660d907
Copy full SHA for 660d907

File tree

4 files changed

+293
-185
lines changed
Filter options

4 files changed

+293
-185
lines changed

‎__test__/get-retry-options.test.ts

Copy file name to clipboard
+70Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
3+
import {getRetryOptions} from '../src/retry-options'
4+
5+
describe('getRequestOptions', () => {
6+
test('retries disabled if retries == 0', async () => {
7+
const [retryOptions, requestOptions] = getRetryOptions(0, 8, [
8+
400,
9+
500,
10+
502
11+
])
12+
13+
expect(retryOptions.enabled).toBe(false)
14+
expect(retryOptions.doNotRetry).toBeFalsy()
15+
16+
expect(requestOptions.retries).toBeFalsy()
17+
})
18+
19+
test('properties set if retries > 0', async () => {
20+
const [retryOptions, requestOptions] = getRetryOptions(1, 8, [
21+
400,
22+
500,
23+
502
24+
])
25+
26+
expect(retryOptions.enabled).toBe(true)
27+
expect(retryOptions.doNotRetry).toEqual([400, 500, 502])
28+
29+
expect(requestOptions.retries).toEqual(1)
30+
expect(requestOptions.retryAfter).toEqual(8)
31+
})
32+
33+
test('properties set if retries > 0', async () => {
34+
const [retryOptions, requestOptions] = getRetryOptions(1, 8, [
35+
400,
36+
500,
37+
502
38+
])
39+
40+
expect(retryOptions.enabled).toBe(true)
41+
expect(retryOptions.doNotRetry).toEqual([400, 500, 502])
42+
43+
expect(requestOptions.retries).toEqual(1)
44+
expect(requestOptions.retryAfter).toEqual(8)
45+
})
46+
47+
test('retryAfter can be set to zero', async () => {
48+
const [retryOptions, requestOptions] = getRetryOptions(1, 0, [
49+
400,
50+
500,
51+
502
52+
])
53+
54+
expect(retryOptions.enabled).toBe(true)
55+
expect(retryOptions.doNotRetry).toEqual([400, 500, 502])
56+
57+
expect(requestOptions.retries).toEqual(1)
58+
expect(requestOptions.retryAfter).toEqual(0)
59+
})
60+
61+
test('retryOptions.doNotRetry not set if doNotRetry isEmpty', async () => {
62+
const [retryOptions, requestOptions] = getRetryOptions(1, 0, [])
63+
64+
expect(retryOptions.enabled).toBe(true)
65+
expect(retryOptions.doNotRetry).toBeUndefined()
66+
67+
expect(requestOptions.retries).toEqual(1)
68+
expect(requestOptions.retryAfter).toEqual(0)
69+
})
70+
})

‎dist/index.js

Copy file name to clipboardExpand all lines: dist/index.js
+155-145Lines changed: 155 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports =
4040
/******/ // the startup function
4141
/******/ function startup() {
4242
/******/ // Load entry module and return exports
43-
/******/ return __webpack_require__(272);
43+
/******/ return __webpack_require__(858);
4444
/******/ };
4545
/******/ // initialize runtime
4646
/******/ runtime(__webpack_require__);
@@ -7084,150 +7084,6 @@ exports.implementation = class URLImpl {
70847084
};
70857085

70867086

7087-
/***/ }),
7088-
7089-
/***/ 272:
7090-
/***/ (function(__unusedmodule, __webpack_exports__, __webpack_require__) {
7091-
7092-
"use strict";
7093-
__webpack_require__.r(__webpack_exports__);
7094-
7095-
// EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js
7096-
var core = __webpack_require__(186);
7097-
7098-
// EXTERNAL MODULE: ./node_modules/@actions/exec/lib/exec.js
7099-
var exec = __webpack_require__(514);
7100-
7101-
// EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js
7102-
var lib_github = __webpack_require__(438);
7103-
7104-
// EXTERNAL MODULE: ./node_modules/@actions/glob/lib/glob.js
7105-
var glob = __webpack_require__(90);
7106-
7107-
// EXTERNAL MODULE: ./node_modules/@actions/io/lib/io.js
7108-
var io = __webpack_require__(436);
7109-
7110-
// EXTERNAL MODULE: ./node_modules/@octokit/plugin-retry/dist-node/index.js
7111-
var dist_node = __webpack_require__(745);
7112-
7113-
// CONCATENATED MODULE: ./src/async-function.ts
7114-
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor;
7115-
function callAsyncFunction(args, source) {
7116-
const fn = new AsyncFunction(...Object.keys(args), source);
7117-
return fn(...Object.values(args));
7118-
}
7119-
7120-
// EXTERNAL MODULE: external "path"
7121-
var external_path_ = __webpack_require__(622);
7122-
7123-
// CONCATENATED MODULE: ./src/wrap-require.ts
7124-
7125-
const wrapRequire = new Proxy(require, {
7126-
apply: (target, thisArg, [moduleID]) => {
7127-
if (moduleID.startsWith('.')) {
7128-
moduleID = Object(external_path_.resolve)(moduleID);
7129-
return target.apply(thisArg, [moduleID]);
7130-
}
7131-
const modulePath = target.resolve.apply(thisArg, [
7132-
moduleID,
7133-
{
7134-
// Webpack does not have an escape hatch for getting the actual
7135-
// module, other than `eval`.
7136-
paths: [process.cwd()]
7137-
}
7138-
]);
7139-
return target.apply(thisArg, [modulePath]);
7140-
},
7141-
get: (target, prop, receiver) => {
7142-
Reflect.get(target, prop, receiver);
7143-
}
7144-
});
7145-
7146-
// CONCATENATED MODULE: ./src/main.ts
7147-
7148-
7149-
7150-
7151-
7152-
7153-
7154-
7155-
process.on('unhandledRejection', handleError);
7156-
main().catch(handleError);
7157-
async function main() {
7158-
const token = Object(core.getInput)('github-token', { required: true });
7159-
const debug = Object(core.getInput)('debug');
7160-
const userAgent = Object(core.getInput)('user-agent');
7161-
const previews = Object(core.getInput)('previews');
7162-
const retries = parseInt(Object(core.getInput)('retries'));
7163-
const retryAfter = parseInt(Object(core.getInput)('retry-after'));
7164-
const doNotRetry = parseNumberArray(Object(core.getInput)('do-not-retry'));
7165-
const opts = {};
7166-
if (debug === 'true')
7167-
opts.log = console;
7168-
if (userAgent != null)
7169-
opts.userAgent = userAgent;
7170-
if (previews != null)
7171-
opts.previews = previews.split(',');
7172-
if (retries > 0) {
7173-
if (doNotRetry.length > 0) {
7174-
opts.retry = { doNotRetry };
7175-
}
7176-
opts.request = {
7177-
retries,
7178-
retryAfter
7179-
};
7180-
Object(core.info)(`GitHub client configured with: (retries: ${retries}, retryAfter: ${retryAfter}, doNotRetry: ${doNotRetry.length == 0
7181-
? 'octokit default: [400, 401, 403, 404, 422]'
7182-
: doNotRetry})`);
7183-
}
7184-
else {
7185-
opts.retry = {
7186-
enabled: false
7187-
};
7188-
}
7189-
const github = Object(lib_github.getOctokit)(token, opts, dist_node.retry);
7190-
const script = Object(core.getInput)('script', { required: true });
7191-
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation errors.
7192-
const result = await callAsyncFunction({
7193-
require: wrapRequire,
7194-
__original_require__: require,
7195-
github,
7196-
context: lib_github.context,
7197-
core: core,
7198-
exec: exec,
7199-
glob: glob,
7200-
io: io
7201-
}, script);
7202-
let encoding = Object(core.getInput)('result-encoding');
7203-
encoding = encoding ? encoding : 'json';
7204-
let output;
7205-
switch (encoding) {
7206-
case 'json':
7207-
output = JSON.stringify(result);
7208-
break;
7209-
case 'string':
7210-
output = String(result);
7211-
break;
7212-
default:
7213-
throw new Error('"result-encoding" must be either "string" or "json"');
7214-
}
7215-
Object(core.setOutput)('result', output);
7216-
}
7217-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7218-
function handleError(err) {
7219-
console.error(err);
7220-
Object(core.setFailed)(`Unhandled error: ${err}`);
7221-
}
7222-
function parseNumberArray(listString) {
7223-
if (!listString) {
7224-
return [];
7225-
}
7226-
const split = listString.trim().split(',');
7227-
return split.map(x => parseInt(x));
7228-
}
7229-
7230-
72317087
/***/ }),
72327088

72337089
/***/ 274:
@@ -13449,6 +13305,160 @@ function expand(str, isTop) {
1344913305

1345013306

1345113307

13308+
/***/ }),
13309+
13310+
/***/ 858:
13311+
/***/ (function(__unusedmodule, __webpack_exports__, __webpack_require__) {
13312+
13313+
"use strict";
13314+
__webpack_require__.r(__webpack_exports__);
13315+
13316+
// EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js
13317+
var core = __webpack_require__(186);
13318+
13319+
// EXTERNAL MODULE: ./node_modules/@actions/exec/lib/exec.js
13320+
var exec = __webpack_require__(514);
13321+
13322+
// EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js
13323+
var lib_github = __webpack_require__(438);
13324+
13325+
// EXTERNAL MODULE: ./node_modules/@actions/glob/lib/glob.js
13326+
var glob = __webpack_require__(90);
13327+
13328+
// EXTERNAL MODULE: ./node_modules/@actions/io/lib/io.js
13329+
var io = __webpack_require__(436);
13330+
13331+
// EXTERNAL MODULE: ./node_modules/@octokit/plugin-retry/dist-node/index.js
13332+
var dist_node = __webpack_require__(745);
13333+
13334+
// CONCATENATED MODULE: ./src/async-function.ts
13335+
const AsyncFunction = Object.getPrototypeOf(async () => null).constructor;
13336+
function callAsyncFunction(args, source) {
13337+
const fn = new AsyncFunction(...Object.keys(args), source);
13338+
return fn(...Object.values(args));
13339+
}
13340+
13341+
// CONCATENATED MODULE: ./src/retry-options.ts
13342+
13343+
function getRetryOptions(retries, retryAfter, doNotRetry) {
13344+
var _a;
13345+
if (retries <= 0) {
13346+
return [{ enabled: false }, {}];
13347+
}
13348+
const retryOptions = {
13349+
enabled: true
13350+
};
13351+
if (doNotRetry.length > 0) {
13352+
retryOptions.doNotRetry = doNotRetry;
13353+
}
13354+
const requestOptions = {
13355+
retries,
13356+
retryAfter: retryAfter
13357+
};
13358+
Object(core.info)(`GitHub client configured with: (retries: ${requestOptions.retries}, retryAfter: ${requestOptions.retryAfter}, doNotRetry: ${(_a = retryOptions === null || retryOptions === void 0 ? void 0 : retryOptions.doNotRetry) !== null && _a !== void 0 ? _a : 'octokit default: [400, 401, 403, 404, 422]'})`);
13359+
return [retryOptions, requestOptions];
13360+
}
13361+
function parseNumberArray(listString) {
13362+
if (!listString) {
13363+
return [];
13364+
}
13365+
const split = listString.trim().split(',');
13366+
return split.map(x => parseInt(x));
13367+
}
13368+
13369+
// EXTERNAL MODULE: external "path"
13370+
var external_path_ = __webpack_require__(622);
13371+
13372+
// CONCATENATED MODULE: ./src/wrap-require.ts
13373+
13374+
const wrapRequire = new Proxy(require, {
13375+
apply: (target, thisArg, [moduleID]) => {
13376+
if (moduleID.startsWith('.')) {
13377+
moduleID = Object(external_path_.resolve)(moduleID);
13378+
return target.apply(thisArg, [moduleID]);
13379+
}
13380+
const modulePath = target.resolve.apply(thisArg, [
13381+
moduleID,
13382+
{
13383+
// Webpack does not have an escape hatch for getting the actual
13384+
// module, other than `eval`.
13385+
paths: [process.cwd()]
13386+
}
13387+
]);
13388+
return target.apply(thisArg, [modulePath]);
13389+
},
13390+
get: (target, prop, receiver) => {
13391+
Reflect.get(target, prop, receiver);
13392+
}
13393+
});
13394+
13395+
// CONCATENATED MODULE: ./src/main.ts
13396+
13397+
13398+
13399+
13400+
13401+
13402+
13403+
13404+
13405+
process.on('unhandledRejection', handleError);
13406+
main().catch(handleError);
13407+
async function main() {
13408+
const token = Object(core.getInput)('github-token', { required: true });
13409+
const debug = Object(core.getInput)('debug');
13410+
const userAgent = Object(core.getInput)('user-agent');
13411+
const previews = Object(core.getInput)('previews');
13412+
const retries = parseInt(Object(core.getInput)('retries'));
13413+
const retryAfter = parseInt(Object(core.getInput)('retry-after'));
13414+
const doNotRetry = parseNumberArray(Object(core.getInput)('do-not-retry'));
13415+
const [retryOpts, requestOpts] = getRetryOptions(retries, retryAfter, doNotRetry);
13416+
const opts = {};
13417+
if (debug === 'true')
13418+
opts.log = console;
13419+
if (userAgent != null)
13420+
opts.userAgent = userAgent;
13421+
if (previews != null)
13422+
opts.previews = previews.split(',');
13423+
if (retryOpts)
13424+
opts.retry = retryOpts;
13425+
if (requestOpts)
13426+
opts.request = requestOpts;
13427+
const github = Object(lib_github.getOctokit)(token, opts, dist_node.retry);
13428+
const script = Object(core.getInput)('script', { required: true });
13429+
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation errors.
13430+
const result = await callAsyncFunction({
13431+
require: wrapRequire,
13432+
__original_require__: require,
13433+
github,
13434+
context: lib_github.context,
13435+
core: core,
13436+
exec: exec,
13437+
glob: glob,
13438+
io: io
13439+
}, script);
13440+
let encoding = Object(core.getInput)('result-encoding');
13441+
encoding = encoding ? encoding : 'json';
13442+
let output;
13443+
switch (encoding) {
13444+
case 'json':
13445+
output = JSON.stringify(result);
13446+
break;
13447+
case 'string':
13448+
output = String(result);
13449+
break;
13450+
default:
13451+
throw new Error('"result-encoding" must be either "string" or "json"');
13452+
}
13453+
Object(core.setOutput)('result', output);
13454+
}
13455+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13456+
function handleError(err) {
13457+
console.error(err);
13458+
Object(core.setFailed)(`Unhandled error: ${err}`);
13459+
}
13460+
13461+
1345213462
/***/ }),
1345313463

1345413464
/***/ 871:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.