From b0083fa32b26aac009859038f57afbac0423b7a1 Mon Sep 17 00:00:00 2001 From: TESTELIN Geoffrey Date: Sat, 16 Jan 2021 15:50:38 +0100 Subject: [PATCH] feat(label): remove the stale label when labeled with an exempt one closes #136 --- __tests__/main.test.ts | 33 +++++++++++++++++++++++++++++++-- src/IssueProcessor.ts | 27 +++++++++++++++------------ 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 3de60e560..bff4ccf4b 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -797,6 +797,7 @@ test('stale locked prs will not be closed', async () => { }); test('exempt issue labels will not be marked stale', async () => { + expect.assertions(3); const TestIssueList: Issue[] = [ generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, [ 'Exempt' @@ -819,6 +820,34 @@ test('exempt issue labels will not be marked stale', async () => { expect(processor.staleIssues.length).toEqual(0); expect(processor.closedIssues.length).toEqual(0); + expect(processor.removedLabelIssues.length).toEqual(0); +}); + +test('exempt issue labels will not be marked stale and will remove the existing stale label', async () => { + expect.assertions(3); + const opts = {...DefaultProcessorOptions}; + opts.exemptIssueLabels = 'Exempt'; + + const TestIssueList: Issue[] = [ + generateIssue(1, 'My first issue', '2020-01-01T17:00:00Z', false, [ + opts.exemptIssueLabels, opts.staleIssueLabel + ]) + ]; + + const processor = new IssueProcessor( + opts, + async () => 'abot', + async p => (p == 1 ? TestIssueList : []), + async (num: number, dt: string) => [], + async (issue: Issue, label: string) => new Date().toDateString() + ); + + // process our fake issue list + await processor.processIssues(1); + + expect(processor.staleIssues.length).toEqual(0); + expect(processor.closedIssues.length).toEqual(0); + expect(processor.removedLabelIssues.length).toEqual(1); }); test('exempt issue labels will not be marked stale (multi issue label with spaces)', async () => { @@ -1150,7 +1179,7 @@ test('skips stale message on issues when skip-stale-issue-message is set', async ); // for sake of testing, mocking private function - const markSpy = jest.spyOn(processor as any, 'markStale'); + const markSpy = jest.spyOn(processor as any, '_markStale'); await processor.processIssues(1); @@ -1195,7 +1224,7 @@ test('skips stale message on prs when skip-stale-pr-message is set', async () => ); // for sake of testing, mocking private function - const markSpy = jest.spyOn(processor as any, 'markStale'); + const markSpy = jest.spyOn(processor as any, '_markStale'); await processor.processIssues(1); diff --git a/src/IssueProcessor.ts b/src/IssueProcessor.ts index bba3dc762..c59fd0f15 100644 --- a/src/IssueProcessor.ts +++ b/src/IssueProcessor.ts @@ -204,18 +204,7 @@ export class IssueProcessor { continue; // don't process locked issues } - if ( - exemptLabels.some((exemptLabel: string) => - isLabeled(issue, exemptLabel) - ) - ) { - issueLogger.info( - `Skipping ${issueType} because it has an exempt label` - ); - continue; // don't process exempt issues - } - - // does this issue have a stale label? + // Does this issue have a stale label? let isStale: boolean = isLabeled(issue, staleLabel); if (isStale) { @@ -224,6 +213,20 @@ export class IssueProcessor { issueLogger.info(`This issue hasn't a stale label`); } + if ( + exemptLabels.some((exemptLabel: Readonly): boolean => + isLabeled(issue, exemptLabel) + ) + ) { + if (isStale) { + core.info(`An exempt label was added after the stale label.`); + await this._removeStaleLabel(issue, staleLabel); + } + + core.info(`Skipping ${issueType} because it has an exempt label`); + continue; // don't process exempt issues + } + // should this issue be marked stale? const shouldBeStale = !IssueProcessor._updatedSince( issue.updated_at,