fix(operations): fail fast the current batch to respect the operations limit (#474)

* fix(operations): fail fast the current batch to respect the operations limit

Instead of processing an entire batch of 100 issues before checking the operations left, simply do it before processing an issue so that we respect as expected the limitation of the operations per run
Fixes #466

* test(debug): disable the dry-run for the test by default

we will be able to test the operations per run and have more complete logs that could help us debug the workflow

* chore(logs): also display the stats when the operations per run stopped the workflow

* chore(stats): fix a bad stats related to the consumed operations

* test(operations-per-run): add coverage

* chore: update index
This commit is contained in:
Geoffrey Testelin
2021-06-07 21:20:11 +02:00
committed by GitHub
parent 8deaf75055
commit 5f6f311ca6
5 changed files with 267 additions and 24 deletions

View File

@@ -66,8 +66,8 @@ export class IssuesProcessor {
}
private readonly _logger: Logger = new Logger();
private readonly _operations: StaleOperations;
private readonly _statistics: Statistics | undefined;
readonly operations: StaleOperations;
readonly client: InstanceType<typeof GitHub>;
readonly options: IIssuesProcessorOptions;
readonly staleIssues: Issue[] = [];
@@ -78,7 +78,7 @@ export class IssuesProcessor {
constructor(options: IIssuesProcessorOptions) {
this.options = options;
this.client = getOctokit(this.options.repoToken);
this._operations = new StaleOperations(this.options);
this.operations = new StaleOperations(this.options);
this._logger.info(
LoggerService.yellow(`Starting the stale action process...`)
@@ -110,10 +110,10 @@ export class IssuesProcessor {
LoggerService.green(`No more issues found to process. Exiting...`)
);
this._statistics
?.setRemainingOperations(this._operations.getRemainingOperationsCount())
?.setOperationsCount(this.operations.getConsumedOperationsCount())
.logStats();
return this._operations.getRemainingOperationsCount();
return this.operations.getRemainingOperationsCount();
} else {
this._logger.info(
`${LoggerService.yellow(
@@ -127,6 +127,11 @@ export class IssuesProcessor {
}
for (const issue of issues.values()) {
// Stop the processing if no more operations remains
if (!this.operations.hasRemainingOperations()) {
break;
}
const issueLogger: IssueLogger = new IssueLogger(issue);
this._statistics?.incrementProcessedItemsCount(issue);
@@ -405,7 +410,7 @@ export class IssuesProcessor {
IssuesProcessor._endIssueProcessing(issue);
}
if (!this._operations.hasRemainingOperations()) {
if (!this.operations.hasRemainingOperations()) {
this._logger.warning(
LoggerService.yellowBright(`No more operations left! Exiting...`)
);
@@ -418,6 +423,9 @@ export class IssuesProcessor {
'option which is currently set to'
)} ${LoggerService.cyan(this.options.operationsPerRun)}`
);
this._statistics
?.setOperationsCount(this.operations.getConsumedOperationsCount())
.logStats();
return 0;
}
@@ -439,7 +447,7 @@ export class IssuesProcessor {
): Promise<IComment[]> {
// Find any comments since date on the given issue
try {
this._operations.consumeOperation();
this.operations.consumeOperation();
this._statistics?.incrementFetchedItemsCommentsCount();
const comments = await this.client.issues.listComments({
owner: context.repo.owner,
@@ -459,7 +467,7 @@ export class IssuesProcessor {
let actor;
try {
this._operations.consumeOperation();
this.operations.consumeOperation();
actor = await this.client.users.getAuthenticated();
} catch (error) {
return context.actor;
@@ -475,7 +483,7 @@ export class IssuesProcessor {
type OctoKitIssueList = GetResponseTypeFromEndpointMethod<typeof endpoint>;
try {
this._operations.consumeOperation();
this.operations.consumeOperation();
const issueResult: OctoKitIssueList = await this.client.issues.listForRepo(
{
owner: context.repo.owner,
@@ -1004,7 +1012,7 @@ export class IssuesProcessor {
}
private _consumeIssueOperation(issue: Readonly<Issue>): void {
this._operations.consumeOperation();
this.operations.consumeOperation();
issue.operations.consumeOperation();
}