Skip to content

Commit e611bf9

Browse files
authored
Close stale issues even if they were just marked stale (#64)
1 parent 1e900bc commit e611bf9

3 files changed

Lines changed: 52 additions & 13 deletions

File tree

__tests__/main.test.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const DefaultProcessorOptions: IssueProcessorOptions = {
3636
staleIssueMessage: 'This issue is stale',
3737
stalePrMessage: 'This PR is stale',
3838
daysBeforeStale: 1,
39-
daysBeforeClose: 1,
39+
daysBeforeClose: 30,
4040
staleIssueLabel: 'Stale',
4141
exemptIssueLabels: '',
4242
stalePrLabel: 'Stale',
@@ -62,7 +62,7 @@ test('empty issue list results in 1 operation', async () => {
6262
expect(operationsLeft).toEqual(99);
6363
});
6464

65-
test('processing an issue with no label will make it stale', async () => {
65+
test('processing an issue with no label will make it stale and close it, if it is old enough', async () => {
6666
const TestIssueList: Issue[] = [
6767
generateIssue(1, 'An issue with no label', '2020-01-01T17:00:00Z')
6868
];
@@ -77,6 +77,30 @@ test('processing an issue with no label will make it stale', async () => {
7777
// process our fake issue list
7878
await processor.processIssues(1);
7979

80+
expect(processor.staleIssues.length).toEqual(1);
81+
expect(processor.closedIssues.length).toEqual(1);
82+
});
83+
84+
test('processing an issue with no label will make it stale but not close it', async () => {
85+
// issue should be from 2 days ago so it will be
86+
// stale but not close-able, based on default settings
87+
let issueDate = new Date();
88+
issueDate.setDate(issueDate.getDate() - 2);
89+
90+
const TestIssueList: Issue[] = [
91+
generateIssue(1, 'An issue with no label', issueDate.toDateString())
92+
];
93+
94+
const processor = new IssueProcessor(
95+
DefaultProcessorOptions,
96+
async p => (p == 1 ? TestIssueList : []),
97+
async (num, dt) => [],
98+
async (issue, label) => new Date().toDateString()
99+
);
100+
101+
// process our fake issue list
102+
await processor.processIssues(1);
103+
80104
expect(processor.staleIssues.length).toEqual(1);
81105
expect(processor.closedIssues.length).toEqual(0);
82106
});

dist/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8510,14 +8510,20 @@ class IssueProcessor {
85108510
core.debug(`Skipping ${issueType} because it has an exempt label`);
85118511
continue; // don't process exempt issues
85128512
}
8513-
if (IssueProcessor.isLabeled(issue, staleLabel)) {
8514-
core.debug(`Found a stale ${issueType}`);
8515-
yield this.processStaleIssue(issue, issueType, staleLabel);
8516-
}
8517-
else if (!IssueProcessor.updatedSince(issue.updated_at, this.options.daysBeforeStale)) {
8518-
core.debug(`Marking ${issueType} stale because it was last updated on ${issue.updated_at}`);
8513+
// does this issue have a stale label?
8514+
let isStale = IssueProcessor.isLabeled(issue, staleLabel);
8515+
// determine if this issue needs to be marked stale first
8516+
if (!isStale &&
8517+
!IssueProcessor.updatedSince(issue.updated_at, this.options.daysBeforeStale)) {
8518+
core.debug(`Marking ${issueType} stale because it was last updated on ${issue.updated_at} and it does not have a stale label`);
85198519
yield this.markStale(issue, staleMessage, staleLabel);
85208520
this.operationsLeft -= 2;
8521+
isStale = true; // this issue is now considered stale
8522+
}
8523+
// process any issues marked stale (including the issue above, if it was marked)
8524+
if (isStale) {
8525+
core.debug(`Found a stale ${issueType}`);
8526+
yield this.processStaleIssue(issue, issueType, staleLabel);
85218527
}
85228528
}
85238529
// do the next batch

src/IssueProcessor.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,29 @@ export class IssueProcessor {
150150
continue; // don't process exempt issues
151151
}
152152

153-
if (IssueProcessor.isLabeled(issue, staleLabel)) {
154-
core.debug(`Found a stale ${issueType}`);
155-
await this.processStaleIssue(issue, issueType, staleLabel);
156-
} else if (
153+
// does this issue have a stale label?
154+
let isStale = IssueProcessor.isLabeled(issue, staleLabel);
155+
156+
// determine if this issue needs to be marked stale first
157+
if (
158+
!isStale &&
157159
!IssueProcessor.updatedSince(
158160
issue.updated_at,
159161
this.options.daysBeforeStale
160162
)
161163
) {
162164
core.debug(
163-
`Marking ${issueType} stale because it was last updated on ${issue.updated_at}`
165+
`Marking ${issueType} stale because it was last updated on ${issue.updated_at} and it does not have a stale label`
164166
);
165167
await this.markStale(issue, staleMessage, staleLabel);
166168
this.operationsLeft -= 2;
169+
isStale = true; // this issue is now considered stale
170+
}
171+
172+
// process any issues marked stale (including the issue above, if it was marked)
173+
if (isStale) {
174+
core.debug(`Found a stale ${issueType}`);
175+
await this.processStaleIssue(issue, issueType, staleLabel);
167176
}
168177
}
169178

0 commit comments

Comments
 (0)