Skip to content

Commit

Permalink
fix: retry and throttle GitHub API requests
Browse files Browse the repository at this point in the history
Closes #35.
  • Loading branch information
dessant committed Jun 12, 2023
1 parent bec0993 commit 1618e91
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 9 deletions.
132 changes: 124 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@octokit/plugin-throttling": "^5.2.3",
"@octokit/plugin-retry": "^4.1.6",
"joi": "^17.9.2"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ const core = require('@actions/core');
const github = require('@actions/github');

const schema = require('./schema');
const {getClient} = require('./utils');

async function run() {
try {
const config = getConfig();
const client = github.getOctokit(config['github-token']);
const client = getClient(config['github-token']);

const app = new App(config, client);
await app.lockThreads();
Expand Down
36 changes: 36 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const core = require('@actions/core');
const github = require('@actions/github');
const {retry} = require('@octokit/plugin-retry');
const {throttling} = require('@octokit/plugin-throttling');

function getClient(token) {
const rateLimitRetries = 3;

const rateLimitCallback = function (
retryAfter,
options,
octokit,
retryCount
) {
core.info(
`Request quota exhausted for request ${options.method} ${options.url}`
);

if (retryCount < rateLimitRetries) {
core.info(`Retrying after ${retryAfter} seconds`);

return true;
}
};

const options = {
throttle: {
onSecondaryRateLimit: rateLimitCallback,
onRateLimit: rateLimitCallback
}
};

return github.getOctokit(token, options, retry, throttling);
}

module.exports = {getClient};

0 comments on commit 1618e91

Please sign in to comment.