From 4b8e745923d700ae96ab3cf0285759ad20c611b5 Mon Sep 17 00:00:00 2001 From: Sergey Dolin Date: Tue, 13 Jun 2023 13:21:22 +0200 Subject: [PATCH] Add RateLimit --- dist/index.js | 44 +++++++++++++++++++++++++++++++++ src/classes/issues-processor.ts | 12 +++++++++ src/classes/rate-limit.ts | 15 +++++++++++ src/interfaces/rate-limit.ts | 10 ++++++++ src/main.ts | 27 ++++++++++++++++++++ 5 files changed, 108 insertions(+) create mode 100644 src/classes/rate-limit.ts create mode 100644 src/interfaces/rate-limit.ts diff --git a/dist/index.js b/dist/index.js index f964b453..0c7b9f79 100644 --- a/dist/index.js +++ b/dist/index.js @@ -381,6 +381,7 @@ const stale_operations_1 = __nccwpck_require__(5080); const statistics_1 = __nccwpck_require__(3334); const logger_service_1 = __nccwpck_require__(1973); const plugin_retry_1 = __nccwpck_require__(6298); +const rate_limit_1 = __nccwpck_require__(7069); /*** * Handle processing of issues for staleness/closure. */ @@ -738,6 +739,18 @@ class IssuesProcessor { } }); } + getRateLimit() { + return __awaiter(this, void 0, void 0, function* () { + const logger = new logger_1.Logger(); + try { + const rateLimitResult = yield this.client.rest.rateLimit.get(); + return new rate_limit_1.RateLimit(rateLimitResult.data.rate); + } + catch (error) { + logger.error(`Error when getting rateLimit: ${error.message}`); + } + }); + } // handle all of the stale issue logic when we find a stale issue _processStaleIssue(issue, staleLabel, staleMessage, labelsToAddWhenUnstale, labelsToRemoveWhenUnstale, labelsToRemoveWhenStale, closeMessage, closeLabel) { return __awaiter(this, void 0, void 0, function* () { @@ -1486,6 +1499,26 @@ class Operations { exports.Operations = Operations; +/***/ }), + +/***/ 7069: +/***/ ((__unused_webpack_module, exports) => { + +"use strict"; + +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.RateLimit = void 0; +class RateLimit { + constructor(rateLimit) { + this.limit = rateLimit.limit; + this.remaining = rateLimit.remaining; + this.used = rateLimit.used; + this.reset = new Date(rateLimit.reset * 1000); + } +} +exports.RateLimit = RateLimit; + + /***/ }), /***/ 5080: @@ -2435,7 +2468,18 @@ function _run() { const state = (0, state_service_1.getStateInstance)(args); yield state.restore(); const issueProcessor = new issues_processor_1.IssuesProcessor(args, state); + const rateLimitAtStart = yield issueProcessor.getRateLimit(); + if (rateLimitAtStart) { + core.debug(`Github API rate status: limit=${rateLimitAtStart.limit}, used=${rateLimitAtStart.used}, remaining=${rateLimitAtStart.remaining}`); + } yield issueProcessor.processIssues(); + const rateLimitAtEnd = yield issueProcessor.getRateLimit(); + if (rateLimitAtEnd) { + core.debug(`Github API rate status: limit=${rateLimitAtEnd.limit}, used=${rateLimitAtEnd.used}, remaining=${rateLimitAtEnd.remaining}`); + if (rateLimitAtStart) + core.info(`Github API rate used: ${rateLimitAtStart.remaining - rateLimitAtEnd.remaining}`); + core.info(`Github API rate remaining: ${rateLimitAtEnd.remaining}; reset at: ${rateLimitAtEnd.reset}`); + } yield state.persist(); yield processOutput(issueProcessor.staleIssues, issueProcessor.closedIssues); } diff --git a/src/classes/issues-processor.ts b/src/classes/issues-processor.ts index 821c83e0..31bbb99d 100644 --- a/src/classes/issues-processor.ts +++ b/src/classes/issues-processor.ts @@ -27,6 +27,8 @@ import {LoggerService} from '../services/logger.service'; import {OctokitIssue} from '../interfaces/issue'; import {retry} from '@octokit/plugin-retry'; import {IState} from '../interfaces/state/state'; +import {IRateLimit} from '../interfaces/rate-limit'; +import {RateLimit} from './rate-limit'; /*** * Handle processing of issues for staleness/closure. @@ -636,6 +638,16 @@ export class IssuesProcessor { } } + async getRateLimit(): Promise { + const logger: Logger = new Logger(); + try { + const rateLimitResult = await this.client.rest.rateLimit.get(); + return new RateLimit(rateLimitResult.data.rate); + } catch (error) { + logger.error(`Error when getting rateLimit: ${error.message}`); + } + } + // handle all of the stale issue logic when we find a stale issue private async _processStaleIssue( issue: Issue, diff --git a/src/classes/rate-limit.ts b/src/classes/rate-limit.ts new file mode 100644 index 00000000..a17fe4ea --- /dev/null +++ b/src/classes/rate-limit.ts @@ -0,0 +1,15 @@ +import {IRateLimit, OctokitRateLimit} from '../interfaces/rate-limit'; + +export class RateLimit implements IRateLimit { + readonly limit: number; + readonly remaining: number; + readonly reset: Date; + readonly used: number; + + constructor(rateLimit: Readonly) { + this.limit = rateLimit.limit; + this.remaining = rateLimit.remaining; + this.used = rateLimit.used; + this.reset = new Date(rateLimit.reset * 1000); + } +} diff --git a/src/interfaces/rate-limit.ts b/src/interfaces/rate-limit.ts new file mode 100644 index 00000000..0496c835 --- /dev/null +++ b/src/interfaces/rate-limit.ts @@ -0,0 +1,10 @@ +import {components} from '@octokit/openapi-types'; + +export interface IRateLimit { + limit: number; + used: number; + remaining: number; + reset: Date; +} + +export type OctokitRateLimit = components['schemas']['rate-limit']; diff --git a/src/main.ts b/src/main.ts index 76870041..a7836c16 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,8 +13,35 @@ async function _run(): Promise { await state.restore(); const issueProcessor: IssuesProcessor = new IssuesProcessor(args, state); + + const rateLimitAtStart = await issueProcessor.getRateLimit(); + if (rateLimitAtStart) { + core.debug( + `Github API rate status: limit=${rateLimitAtStart.limit}, used=${rateLimitAtStart.used}, remaining=${rateLimitAtStart.remaining}` + ); + } + await issueProcessor.processIssues(); + const rateLimitAtEnd = await issueProcessor.getRateLimit(); + + if (rateLimitAtEnd) { + core.debug( + `Github API rate status: limit=${rateLimitAtEnd.limit}, used=${rateLimitAtEnd.used}, remaining=${rateLimitAtEnd.remaining}` + ); + + if (rateLimitAtStart) + core.info( + `Github API rate used: ${ + rateLimitAtStart.remaining - rateLimitAtEnd.remaining + }` + ); + + core.info( + `Github API rate remaining: ${rateLimitAtEnd.remaining}; reset at: ${rateLimitAtEnd.reset}` + ); + } + await state.persist(); await processOutput(