mirror of
https://github.com/actions/stale.git
synced 2025-12-10 20:21:20 +00:00
Add RateLimit
This commit is contained in:
@@ -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<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,
|
||||
|
||||
15
src/classes/rate-limit.ts
Normal file
15
src/classes/rate-limit.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
10
src/interfaces/rate-limit.ts
Normal file
10
src/interfaces/rate-limit.ts
Normal 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'];
|
||||
27
src/main.ts
27
src/main.ts
@@ -13,8 +13,35 @@ async function _run(): Promise<void> {
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user