diff --git a/__tests__/remove-stale-when-updated.spec.ts b/__tests__/remove-stale-when-updated.spec.ts index 05f7b3041..daea41b80 100644 --- a/__tests__/remove-stale-when-updated.spec.ts +++ b/__tests__/remove-stale-when-updated.spec.ts @@ -7,6 +7,28 @@ import {DefaultProcessorOptions} from './constants/default-processor-options'; import {generateIssue} from './functions/generate-issue'; import {alwaysFalseStateMock} from './classes/state-mock'; +jest.mock('@actions/github', () => { + const actual = jest.requireActual('@actions/github'); + return { + ...actual, + context: { + ...actual.context, + repo: { + owner: 'owner-mock', + repo: 'repo-mock' + } + }, + getOctokit: (...args: unknown[]) => { + const originalCall = actual.getOctokit; + const client = originalCall(...args); + client.rest.issues.removeLabel = () => { + throw new Error('Something went wrong'); + }; + return client; + } + }; +}); + let issuesProcessorBuilder: IssuesProcessorBuilder; let issuesProcessor: IssuesProcessorMock; @@ -65,6 +87,18 @@ describe('remove-stale-when-updated option', (): void => { expect(issuesProcessor.removedLabelIssues).toHaveLength(1); }); + + test('should not count stale label removal when the operation is unsuccessful', async (): Promise => { + expect.assertions(1); + issuesProcessor = issuesProcessorBuilder + .staleIssues([{}]) + .unsetDebugMode() + .build(); + + await issuesProcessor.processIssues(); + + expect(issuesProcessor.removedLabelIssues).toHaveLength(0); + }); }); }); @@ -541,6 +575,11 @@ class IssuesProcessorBuilder { return this; } + unsetDebugMode() { + this._options.debugOnly = false; + return this; + } + build(): IssuesProcessorMock { return new IssuesProcessorMock( this._options, diff --git a/src/classes/issues-processor.ts b/src/classes/issues-processor.ts index 486c6a78a..453da75dd 100644 --- a/src/classes/issues-processor.ts +++ b/src/classes/issues-processor.ts @@ -1016,11 +1016,9 @@ export class IssuesProcessor { isSubStep ? LoggerService.white('├── ') : '' }Removing the label "${LoggerService.cyan(label)}" from this $$type...` ); - this.removedLabelIssues.push(issue); try { this._consumeIssueOperation(issue); - this.statistics?.incrementDeletedItemsLabelsCount(issue); if (!this.options.debugOnly) { await this.client.rest.issues.removeLabel({ @@ -1036,6 +1034,9 @@ export class IssuesProcessor { isSubStep ? LoggerService.white('└── ') : '' }The label "${LoggerService.cyan(label)}" was removed` ); + + this.statistics?.incrementDeletedItemsLabelsCount(issue); + this.removedLabelIssues.push(issue); } catch (error) { issueLogger.error( `${