diff --git a/package-lock.json b/package-lock.json index 0c6b3d2..41608da 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", "js-yaml": "^4.1.0" }, @@ -482,6 +484,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", @@ -882,6 +915,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 5e8ad5c..fc23429 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", "js-yaml": "^4.1.0" }, diff --git a/src/index.js b/src/index.js index 8c2579e..4b1b7ba 100644 --- a/src/index.js +++ b/src/index.js @@ -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, @@ -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']); diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..cf21b95 --- /dev/null +++ b/src/utils.js @@ -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};