Skip to content

Commit

Permalink
fix: retry and throttle GitHub API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Nov 10, 2023
1 parent 5db6818 commit 0178594
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
38 changes: 38 additions & 0 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 @@ -23,6 +23,8 @@
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0",
"@octokit/plugin-throttling": "^8.1.2",
"@octokit/plugin-retry": "^6.0.1",
"joi": "^17.11.0",
"js-yaml": "^4.1.0"
},
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import core from '@actions/core';
import github from '@actions/github';
import yaml from 'js-yaml';

import {getClient} from './utils.js';
import {configSchema, actionSchema} from './schema.js';
import {
addDiscussionCommentQuery,
Expand All @@ -19,7 +20,7 @@ import {
async function run() {
try {
const config = getConfig();
const client = github.getOctokit(config['github-token']);
const client = getClient(config['github-token']);

const actions = await getActionConfig(client, config['config-path']);

Expand Down
37 changes: 37 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import core from '@actions/core';
import github from '@actions/github';
import {retry} from '@octokit/plugin-retry';
import {throttling} from '@octokit/plugin-throttling';

function getClient(token) {
const requestRetries = 3;

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

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

return true;
}
};

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

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

export {getClient};

0 comments on commit 0178594

Please sign in to comment.