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 14, 2023
1 parent 1f28647 commit 2755608
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 @@ -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"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import core from '@actions/core';
import github from '@actions/github';

import {getConfig} from './utils.js';
import {getConfig, getClient} from './utils.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);
if (github.context.payload.action === 'labeled') {
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 2755608

Please sign in to comment.