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 19, 2023
1 parent c40827f commit 5c35d10
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
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 @@ -24,6 +24,8 @@
"@actions/artifact": "^1.1.2",
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0",
"@octokit/plugin-throttling": "^8.1.3",
"@octokit/plugin-retry": "^6.0.1",
"adm-zip": "^0.5.10",
"dedent": "^1.5.1",
"fs-extra": "^11.1.1",
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {writeJson, remove} from 'fs-extra/esm';
import dedent from 'dedent';
import zip from 'adm-zip';

import {getConfig} from './utils.js';
import {getConfig, getClient} from './utils.js';
import {reactionRx} from './data.js';

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);

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

import {schema} from './schema.js';

Expand All @@ -15,4 +18,35 @@ function getConfig() {
return value;
}

export {getConfig};
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 {getConfig, getClient};

0 comments on commit 5c35d10

Please sign in to comment.