diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 1940809f..1ea8766a 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -36,7 +36,7 @@ const DefaultProcessorOptions: IssueProcessorOptions = { staleIssueMessage: 'This issue is stale', stalePrMessage: 'This PR is stale', daysBeforeStale: 1, - daysBeforeClose: 1, + daysBeforeClose: 30, staleIssueLabel: 'Stale', exemptIssueLabels: '', stalePrLabel: 'Stale', @@ -62,7 +62,7 @@ test('empty issue list results in 1 operation', async () => { expect(operationsLeft).toEqual(99); }); -test('processing an issue with no label will make it stale', async () => { +test('processing an issue with no label will make it stale and close it, if it is old enough', async () => { const TestIssueList: Issue[] = [ generateIssue(1, 'An issue with no label', '2020-01-01T17:00:00Z') ]; @@ -77,6 +77,30 @@ test('processing an issue with no label will make it stale', async () => { // process our fake issue list await processor.processIssues(1); + expect(processor.staleIssues.length).toEqual(1); + expect(processor.closedIssues.length).toEqual(1); +}); + +test('processing an issue with no label will make it stale but not close it', async () => { + // issue should be from 2 days ago so it will be + // stale but not close-able, based on default settings + let issueDate = new Date(); + issueDate.setDate(issueDate.getDate() - 2); + + const TestIssueList: Issue[] = [ + generateIssue(1, 'An issue with no label', issueDate.toDateString()) + ]; + + const processor = new IssueProcessor( + DefaultProcessorOptions, + async p => (p == 1 ? TestIssueList : []), + async (num, dt) => [], + async (issue, label) => new Date().toDateString() + ); + + // process our fake issue list + await processor.processIssues(1); + expect(processor.staleIssues.length).toEqual(1); expect(processor.closedIssues.length).toEqual(0); }); diff --git a/dist/index.js b/dist/index.js index 33f2755a..e1937e41 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8510,14 +8510,20 @@ class IssueProcessor { core.debug(`Skipping ${issueType} because it has an exempt label`); continue; // don't process exempt issues } - if (IssueProcessor.isLabeled(issue, staleLabel)) { - core.debug(`Found a stale ${issueType}`); - yield this.processStaleIssue(issue, issueType, staleLabel); - } - else if (!IssueProcessor.updatedSince(issue.updated_at, this.options.daysBeforeStale)) { - core.debug(`Marking ${issueType} stale because it was last updated on ${issue.updated_at}`); + // does this issue have a stale label? + let isStale = IssueProcessor.isLabeled(issue, staleLabel); + // determine if this issue needs to be marked stale first + if (!isStale && + !IssueProcessor.updatedSince(issue.updated_at, this.options.daysBeforeStale)) { + core.debug(`Marking ${issueType} stale because it was last updated on ${issue.updated_at} and it does not have a stale label`); yield this.markStale(issue, staleMessage, staleLabel); this.operationsLeft -= 2; + isStale = true; // this issue is now considered stale + } + // process any issues marked stale (including the issue above, if it was marked) + if (isStale) { + core.debug(`Found a stale ${issueType}`); + yield this.processStaleIssue(issue, issueType, staleLabel); } } // do the next batch diff --git a/src/IssueProcessor.ts b/src/IssueProcessor.ts index 7188835f..21d5eabe 100644 --- a/src/IssueProcessor.ts +++ b/src/IssueProcessor.ts @@ -150,20 +150,29 @@ export class IssueProcessor { continue; // don't process exempt issues } - if (IssueProcessor.isLabeled(issue, staleLabel)) { - core.debug(`Found a stale ${issueType}`); - await this.processStaleIssue(issue, issueType, staleLabel); - } else if ( + // does this issue have a stale label? + let isStale = IssueProcessor.isLabeled(issue, staleLabel); + + // determine if this issue needs to be marked stale first + if ( + !isStale && !IssueProcessor.updatedSince( issue.updated_at, this.options.daysBeforeStale ) ) { core.debug( - `Marking ${issueType} stale because it was last updated on ${issue.updated_at}` + `Marking ${issueType} stale because it was last updated on ${issue.updated_at} and it does not have a stale label` ); await this.markStale(issue, staleMessage, staleLabel); this.operationsLeft -= 2; + isStale = true; // this issue is now considered stale + } + + // process any issues marked stale (including the issue above, if it was marked) + if (isStale) { + core.debug(`Found a stale ${issueType}`); + await this.processStaleIssue(issue, issueType, staleLabel); } }