diff --git a/package-lock.json b/package-lock.json index 2a53e6e..ec61c15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,8 @@ "dependencies": { "@actions/core": "^1.10.1", "@actions/github": "^6.0.0", + "@octokit/plugin-retry": "^6.0.1", + "@octokit/plugin-throttling": "^8.1.2", "joi": "^17.11.0" }, "devDependencies": { @@ -480,6 +482,37 @@ "@octokit/core": ">=5" } }, + "node_modules/@octokit/plugin-retry": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@octokit/plugin-retry/-/plugin-retry-6.0.1.tgz", + "integrity": "sha512-SKs+Tz9oj0g4p28qkZwl/topGcb0k0qPNX/i7vBKmDsjoeqnVfFUquqrE/O9oJY7+oLzdCtkiWSXLpLjvl6uog==", + "dependencies": { + "@octokit/request-error": "^5.0.0", + "@octokit/types": "^12.0.0", + "bottleneck": "^2.15.3" + }, + "engines": { + "node": ">= 18" + }, + "peerDependencies": { + "@octokit/core": ">=5" + } + }, + "node_modules/@octokit/plugin-throttling": { + "version": "8.1.2", + "resolved": "https://registry.npmjs.org/@octokit/plugin-throttling/-/plugin-throttling-8.1.2.tgz", + "integrity": "sha512-oFba+ioR6HGb0fgqxMta7Kpk/MdffUTuUxNY856l1nXPvh7Qggp8w4AksRx1SDA8SGd+4cbrpkY4k1J/Xz8nZQ==", + "dependencies": { + "@octokit/types": "^12.0.0", + "bottleneck": "^2.15.3" + }, + "engines": { + "node": ">= 18" + }, + "peerDependencies": { + "@octokit/core": "^5.0.0" + } + }, "node_modules/@octokit/request": { "version": "8.1.5", "resolved": "https://registry.npmjs.org/@octokit/request/-/request-8.1.5.tgz", @@ -881,6 +914,11 @@ "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz", "integrity": "sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==" }, + "node_modules/bottleneck": { + "version": "2.19.5", + "resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz", + "integrity": "sha512-VHiNCbI1lKdl44tGrhNfU3lup0Tj/ZBMJB5/2ZbNXRCPuRCO7ed2mgcK4r17y+KB2EfuYuRaVlwNbAeaWGSpbw==" + }, "node_modules/boxen": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/boxen/-/boxen-7.1.1.tgz", diff --git a/package.json b/package.json index e98824b..548b67c 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.js b/src/index.js index fd06f68..6633c00 100644 --- a/src/index.js +++ b/src/index.js @@ -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') { diff --git a/src/utils.js b/src/utils.js index 39d965c..9d40057 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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'; @@ -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};