Merge pull request #1032 from akv-platform/ratelimit

Log github api rate info
This commit is contained in:
Sergey Dolin 2023-07-14 09:57:07 +02:00 committed by GitHub
commit 47ab9e7777
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 108 additions and 0 deletions

44
dist/index.js vendored
View File

@ -381,6 +381,7 @@ const stale_operations_1 = __nccwpck_require__(5080);
const statistics_1 = __nccwpck_require__(3334); const statistics_1 = __nccwpck_require__(3334);
const logger_service_1 = __nccwpck_require__(1973); const logger_service_1 = __nccwpck_require__(1973);
const plugin_retry_1 = __nccwpck_require__(6298); const plugin_retry_1 = __nccwpck_require__(6298);
const rate_limit_1 = __nccwpck_require__(7069);
/*** /***
* Handle processing of issues for staleness/closure. * 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 // handle all of the stale issue logic when we find a stale issue
_processStaleIssue(issue, staleLabel, staleMessage, labelsToAddWhenUnstale, labelsToRemoveWhenUnstale, labelsToRemoveWhenStale, closeMessage, closeLabel) { _processStaleIssue(issue, staleLabel, staleMessage, labelsToAddWhenUnstale, labelsToRemoveWhenUnstale, labelsToRemoveWhenStale, closeMessage, closeLabel) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
@ -1486,6 +1499,26 @@ class Operations {
exports.Operations = 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: /***/ 5080:
@ -2435,7 +2468,18 @@ function _run() {
const state = (0, state_service_1.getStateInstance)(args); const state = (0, state_service_1.getStateInstance)(args);
yield state.restore(); yield state.restore();
const issueProcessor = new issues_processor_1.IssuesProcessor(args, state); 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(); 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 state.persist();
yield processOutput(issueProcessor.staleIssues, issueProcessor.closedIssues); yield processOutput(issueProcessor.staleIssues, issueProcessor.closedIssues);
} }

View File

@ -27,6 +27,8 @@ import {LoggerService} from '../services/logger.service';
import {OctokitIssue} from '../interfaces/issue'; import {OctokitIssue} from '../interfaces/issue';
import {retry} from '@octokit/plugin-retry'; import {retry} from '@octokit/plugin-retry';
import {IState} from '../interfaces/state/state'; import {IState} from '../interfaces/state/state';
import {IRateLimit} from '../interfaces/rate-limit';
import {RateLimit} from './rate-limit';
/*** /***
* Handle processing of issues for staleness/closure. * Handle processing of issues for staleness/closure.
@ -636,6 +638,16 @@ export class IssuesProcessor {
} }
} }
async getRateLimit(): Promise<IRateLimit | undefined> {
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 // handle all of the stale issue logic when we find a stale issue
private async _processStaleIssue( private async _processStaleIssue(
issue: Issue, issue: Issue,

15
src/classes/rate-limit.ts Normal file
View File

@ -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<OctokitRateLimit>) {
this.limit = rateLimit.limit;
this.remaining = rateLimit.remaining;
this.used = rateLimit.used;
this.reset = new Date(rateLimit.reset * 1000);
}
}

View File

@ -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'];

View File

@ -13,8 +13,35 @@ async function _run(): Promise<void> {
await state.restore(); await state.restore();
const issueProcessor: IssuesProcessor = new IssuesProcessor(args, state); 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(); 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 state.persist();
await processOutput( await processOutput(