Skip to content

Commit 1618e91

Browse files
committed
fix: retry and throttle GitHub API requests
Closes #35.
1 parent bec0993 commit 1618e91

File tree

4 files changed

+164
-9
lines changed

4 files changed

+164
-9
lines changed

package-lock.json

+124-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
"dependencies": {
2323
"@actions/core": "^1.10.0",
2424
"@actions/github": "^5.1.1",
25+
"@octokit/plugin-throttling": "^5.2.3",
26+
"@octokit/plugin-retry": "^4.1.6",
2527
"joi": "^17.9.2"
2628
},
2729
"devDependencies": {

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ const core = require('@actions/core');
22
const github = require('@actions/github');
33

44
const schema = require('./schema');
5+
const {getClient} = require('./utils');
56

67
async function run() {
78
try {
89
const config = getConfig();
9-
const client = github.getOctokit(config['github-token']);
10+
const client = getClient(config['github-token']);
1011

1112
const app = new App(config, client);
1213
await app.lockThreads();

src/utils.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const core = require('@actions/core');
2+
const github = require('@actions/github');
3+
const {retry} = require('@octokit/plugin-retry');
4+
const {throttling} = require('@octokit/plugin-throttling');
5+
6+
function getClient(token) {
7+
const rateLimitRetries = 3;
8+
9+
const rateLimitCallback = function (
10+
retryAfter,
11+
options,
12+
octokit,
13+
retryCount
14+
) {
15+
core.info(
16+
`Request quota exhausted for request ${options.method} ${options.url}`
17+
);
18+
19+
if (retryCount < rateLimitRetries) {
20+
core.info(`Retrying after ${retryAfter} seconds`);
21+
22+
return true;
23+
}
24+
};
25+
26+
const options = {
27+
throttle: {
28+
onSecondaryRateLimit: rateLimitCallback,
29+
onRateLimit: rateLimitCallback
30+
}
31+
};
32+
33+
return github.getOctokit(token, options, retry, throttling);
34+
}
35+
36+
module.exports = {getClient};

0 commit comments

Comments
 (0)