Skip to content

Commit 3faaff9

Browse files
committed
PR feedback
1 parent 3cca041 commit 3faaff9

File tree

6 files changed

+68
-61
lines changed

6 files changed

+68
-61
lines changed

README.md

+46-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
This action makes it easy to quickly write a script in your workflow that
88
uses the GitHub API and the workflow run context.
99

10-
To use this action, provide an input named `script` that contains the body of an asynchronous function call.
10+
To use this action, provide an input named `script` that contains the body of an asynchronous function call.
1111
The following arguments will be provided:
1212

1313
- `github` A pre-authenticated
@@ -83,6 +83,47 @@ output of a github-script step. For some workflows, string encoding is preferred
8383
script: return "I will be string (not JSON) encoded!"
8484
```
8585

86+
## Retries
87+
88+
By default, requests made with the `github` instance will not be retried. You can configure this with the `retries` option:
89+
90+
```yaml
91+
- uses: actions/github-script@v6
92+
id: my-script
93+
with:
94+
result-encoding: string
95+
retries: 3
96+
script: |
97+
github.rest.issues.get({
98+
issue_number: context.issue.number,
99+
owner: context.repo.owner,
100+
repo: context.repo.repo,
101+
})
102+
```
103+
104+
In this example, request failures from `github.rest.issues.get()` will be retried up to 3 times.
105+
106+
You can also configure which status codes should be exempt from retries via the `retry-exempt-status-codes` option:
107+
108+
```yaml
109+
- uses: actions/github-script@v6
110+
id: my-script
111+
with:
112+
result-encoding: string
113+
retries: 3
114+
retry-exempt-status-codes: 400,401
115+
script: |
116+
github.rest.issues.get({
117+
issue_number: context.issue.number,
118+
owner: context.repo.owner,
119+
repo: context.repo.repo,
120+
})
121+
```
122+
123+
By default, the following status codes will not be retried: `400, 401, 403, 404, 422` [(source)](https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/index.ts#L14).
124+
125+
These retries are implemented using the [octokit/plugin-retry.js](https://github.com/octokit/plugin-retry.js) plugin. The retries use [exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff) to space out retries. ([source](https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/error-request.ts#L13))
126+
86127
## Examples
87128

88129
Note that `github-token` is optional in this action, and the input is there
@@ -354,8 +395,11 @@ jobs:
354395
To import an ESM file, you'll need to reference your script by an absolute path and ensure you have a `package.json` file with `"type": "module"` specified.
355396
356397
For a script in your repository `src/print-stuff.js`:
398+
357399
```js
358-
export default function printStuff() { console.log('stuff') }
400+
export default function printStuff() {
401+
console.log('stuff')
402+
}
359403
```
360404

361405
```yaml

__test__/get-retry-options.test.ts

+5-34
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import {getRetryOptions} from '../src/retry-options'
44

55
describe('getRequestOptions', () => {
66
test('retries disabled if retries == 0', async () => {
7-
const [retryOptions, requestOptions] = getRetryOptions(0, 8, [
8-
400,
9-
500,
10-
502
11-
])
7+
const [retryOptions, requestOptions] = getRetryOptions(0, [400, 500, 502])
128

139
expect(retryOptions.enabled).toBe(false)
1410
expect(retryOptions.doNotRetry).toBeFalsy()
@@ -17,54 +13,29 @@ describe('getRequestOptions', () => {
1713
})
1814

1915
test('properties set if retries > 0', async () => {
20-
const [retryOptions, requestOptions] = getRetryOptions(1, 8, [
21-
400,
22-
500,
23-
502
24-
])
16+
const [retryOptions, requestOptions] = getRetryOptions(1, [400, 500, 502])
2517

2618
expect(retryOptions.enabled).toBe(true)
2719
expect(retryOptions.doNotRetry).toEqual([400, 500, 502])
2820

2921
expect(requestOptions.retries).toEqual(1)
30-
expect(requestOptions.retryAfter).toEqual(8)
3122
})
3223

3324
test('properties set if retries > 0', async () => {
34-
const [retryOptions, requestOptions] = getRetryOptions(1, 8, [
35-
400,
36-
500,
37-
502
38-
])
25+
const [retryOptions, requestOptions] = getRetryOptions(1, [400, 500, 502])
3926

4027
expect(retryOptions.enabled).toBe(true)
4128
expect(retryOptions.doNotRetry).toEqual([400, 500, 502])
4229

4330
expect(requestOptions.retries).toEqual(1)
44-
expect(requestOptions.retryAfter).toEqual(8)
4531
})
4632

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, [])
33+
test('retryOptions.doNotRetry not set if exemptStatusCodes isEmpty', async () => {
34+
const [retryOptions, requestOptions] = getRetryOptions(1, [])
6335

6436
expect(retryOptions.enabled).toBe(true)
6537
expect(retryOptions.doNotRetry).toBeUndefined()
6638

6739
expect(requestOptions.retries).toEqual(1)
68-
expect(requestOptions.retryAfter).toEqual(0)
6940
})
7041
})

action.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ inputs:
2929
retry-after:
3030
description: "The number of seconds between retries. No effect unless `retries` is set."
3131
default: "0"
32-
do-not-retry:
32+
retry-exempt-status-codes:
3333
description: "A comma separated list of status codes that will NOT be retried. Example: '400,500'. No effect unless `retries` is set."
34-
default: ""
34+
default: "400, 401, 403, 404, 422" # from https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/index.ts#L14
3535
outputs:
3636
result:
3737
description: The return value of the script, stringified with `JSON.stringify`

dist/index.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -13340,22 +13340,21 @@ function callAsyncFunction(args, source) {
1334013340

1334113341
// CONCATENATED MODULE: ./src/retry-options.ts
1334213342

13343-
function getRetryOptions(retries, retryAfter, doNotRetry) {
13343+
function getRetryOptions(retries, exemptStatusCodes) {
1334413344
var _a;
1334513345
if (retries <= 0) {
1334613346
return [{ enabled: false }, {}];
1334713347
}
1334813348
const retryOptions = {
1334913349
enabled: true
1335013350
};
13351-
if (doNotRetry.length > 0) {
13352-
retryOptions.doNotRetry = doNotRetry;
13351+
if (exemptStatusCodes.length > 0) {
13352+
retryOptions.doNotRetry = exemptStatusCodes;
1335313353
}
1335413354
const requestOptions = {
13355-
retries,
13356-
retryAfter: retryAfter
13355+
retries
1335713356
};
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]'})`);
13357+
Object(core.info)(`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]'})`);
1335913358
return [retryOptions, requestOptions];
1336013359
}
1336113360
function parseNumberArray(listString) {
@@ -13410,9 +13409,8 @@ async function main() {
1341013409
const userAgent = Object(core.getInput)('user-agent');
1341113410
const previews = Object(core.getInput)('previews');
1341213411
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);
13412+
const exemptStatusCodes = parseNumberArray(Object(core.getInput)('retry-exempt-status-codes'));
13413+
const [retryOpts, requestOpts] = getRetryOptions(retries, exemptStatusCodes);
1341613414
const opts = {};
1341713415
if (debug === 'true')
1341813416
opts.log = console;

src/main.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ async function main(): Promise<void> {
3030
const userAgent = core.getInput('user-agent')
3131
const previews = core.getInput('previews')
3232
const retries = parseInt(core.getInput('retries'))
33-
const retryAfter = parseInt(core.getInput('retry-after'))
34-
const doNotRetry = parseNumberArray(core.getInput('do-not-retry'))
35-
const [retryOpts, requestOpts] = getRetryOptions(
36-
retries,
37-
retryAfter,
38-
doNotRetry
33+
const exemptStatusCodes = parseNumberArray(
34+
core.getInput('retry-exempt-status-codes')
3935
)
36+
const [retryOpts, requestOpts] = getRetryOptions(retries, exemptStatusCodes)
4037

4138
const opts: Options = {}
4239
if (debug === 'true') opts.log = console

src/retry-options.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ export type RetryOptions = {
77

88
export type RequestOptions = {
99
retries?: number
10-
retryAfter?: number
1110
}
1211

1312
export function getRetryOptions(
1413
retries: number,
15-
retryAfter: number,
16-
doNotRetry: number[]
14+
exemptStatusCodes: number[]
1715
): [RetryOptions, RequestOptions] {
1816
if (retries <= 0) {
1917
return [{enabled: false}, {}]
@@ -23,19 +21,18 @@ export function getRetryOptions(
2321
enabled: true
2422
}
2523

26-
if (doNotRetry.length > 0) {
27-
retryOptions.doNotRetry = doNotRetry
24+
if (exemptStatusCodes.length > 0) {
25+
retryOptions.doNotRetry = exemptStatusCodes
2826
}
2927

3028
const requestOptions: RequestOptions = {
31-
retries,
32-
retryAfter: retryAfter
29+
retries
3330
}
3431

3532
core.info(
3633
`GitHub client configured with: (retries: ${
3734
requestOptions.retries
38-
}, retryAfter: ${requestOptions.retryAfter}, doNotRetry: ${
35+
}, retry-exempt-status-code: ${
3936
retryOptions?.doNotRetry ?? 'octokit default: [400, 401, 403, 404, 422]'
4037
})`
4138
)

0 commit comments

Comments
 (0)