Skip to content

Commit

Permalink
Add RateLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
dsame committed Jun 13, 2023
1 parent 02989dc commit 9d01691
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
44 changes: 44 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,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.
*/
Expand Down Expand Up @@ -721,6 +722,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* () {
Expand Down Expand Up @@ -1469,6 +1482,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:
Expand Down Expand Up @@ -2186,7 +2219,18 @@ function _run() {
try {
const args = _getAndValidateArgs();
const issueProcessor = new issues_processor_1.IssuesProcessor(args);
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 processOutput(issueProcessor.staleIssues, issueProcessor.closedIssues);
}
catch (error) {
Expand Down
12 changes: 12 additions & 0 deletions src/classes/issues-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {Statistics} from './statistics';
import {LoggerService} from '../services/logger.service';
import {OctokitIssue} from '../interfaces/issue';
import {retry} from '@octokit/plugin-retry';
import {IRateLimit} from '../interfaces/rate-limit';
import {RateLimit} from './rate-limit';

/***
* Handle processing of issues for staleness/closure.
Expand Down Expand Up @@ -623,6 +625,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
private async _processStaleIssue(
issue: Issue,
Expand Down
15 changes: 15 additions & 0 deletions src/classes/rate-limit.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
10 changes: 10 additions & 0 deletions src/interfaces/rate-limit.ts
Original file line number Diff line number Diff line change
@@ -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'];
27 changes: 27 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,35 @@ async function _run(): Promise<void> {
const args = _getAndValidateArgs();

const issueProcessor: IssuesProcessor = new IssuesProcessor(args);

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 processOutput(
issueProcessor.staleIssues,
issueProcessor.closedIssues
Expand Down

0 comments on commit 9d01691

Please sign in to comment.