From efe535e5ae2a9db6bdda9a9a68633885ca29e131 Mon Sep 17 00:00:00 2001 From: GalAbra Date: Wed, 25 Sep 2024 13:58:27 +0300 Subject: [PATCH 1/4] Consider label removed only if the operation was successful --- src/classes/issues-processor.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/classes/issues-processor.ts b/src/classes/issues-processor.ts index 486c6a78a..4eb1f4100 100644 --- a/src/classes/issues-processor.ts +++ b/src/classes/issues-processor.ts @@ -1016,7 +1016,6 @@ export class IssuesProcessor { isSubStep ? LoggerService.white('├── ') : '' }Removing the label "${LoggerService.cyan(label)}" from this $$type...` ); - this.removedLabelIssues.push(issue); try { this._consumeIssueOperation(issue); @@ -1036,6 +1035,8 @@ export class IssuesProcessor { isSubStep ? LoggerService.white('└── ') : '' }The label "${LoggerService.cyan(label)}" was removed` ); + + this.removedLabelIssues.push(issue); } catch (error) { issueLogger.error( `${ From 59ce61f00c151947bbfa247ea137ce2a608cd3e0 Mon Sep 17 00:00:00 2001 From: GalAbra Date: Wed, 25 Sep 2024 14:17:06 +0300 Subject: [PATCH 2/4] Added test --- __tests__/remove-stale-when-updated.spec.ts | 39 +++++++++++++++++++++ 1 file changed, 39 insertions(+) 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, From 451a0cf74733243aec7ace69b7c8540f708ebb18 Mon Sep 17 00:00:00 2001 From: GalAbra Date: Wed, 25 Sep 2024 14:45:46 +0300 Subject: [PATCH 3/4] Also moved consumed issues counter --- src/classes/issues-processor.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/classes/issues-processor.ts b/src/classes/issues-processor.ts index 4eb1f4100..c3f7d5b9f 100644 --- a/src/classes/issues-processor.ts +++ b/src/classes/issues-processor.ts @@ -1018,9 +1018,6 @@ export class IssuesProcessor { ); try { - this._consumeIssueOperation(issue); - this.statistics?.incrementDeletedItemsLabelsCount(issue); - if (!this.options.debugOnly) { await this.client.rest.issues.removeLabel({ owner: context.repo.owner, @@ -1036,6 +1033,8 @@ export class IssuesProcessor { }The label "${LoggerService.cyan(label)}" was removed` ); + this._consumeIssueOperation(issue); + this.statistics?.incrementDeletedItemsLabelsCount(issue); this.removedLabelIssues.push(issue); } catch (error) { issueLogger.error( From e6d0d7b525161c7c3869059b11ea2c7ebc65b7a1 Mon Sep 17 00:00:00 2001 From: GalAbra Date: Wed, 25 Sep 2024 14:54:53 +0300 Subject: [PATCH 4/4] Do consume operation even if query was unsuccessful --- src/classes/issues-processor.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/classes/issues-processor.ts b/src/classes/issues-processor.ts index c3f7d5b9f..453da75dd 100644 --- a/src/classes/issues-processor.ts +++ b/src/classes/issues-processor.ts @@ -1018,6 +1018,8 @@ export class IssuesProcessor { ); try { + this._consumeIssueOperation(issue); + if (!this.options.debugOnly) { await this.client.rest.issues.removeLabel({ owner: context.repo.owner, @@ -1033,7 +1035,6 @@ export class IssuesProcessor { }The label "${LoggerService.cyan(label)}" was removed` ); - this._consumeIssueOperation(issue); this.statistics?.incrementDeletedItemsLabelsCount(issue); this.removedLabelIssues.push(issue); } catch (error) {