Skip to content
This repository was archived by the owner on May 20, 2023. It is now read-only.

Commit

Permalink
Double-check issue state is not closed before acting
Browse files Browse the repository at this point in the history
Fixes #263
  • Loading branch information
JamesMGreene committed Mar 29, 2020
1 parent cd1e762 commit a11311e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/stale.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ module.exports = class Stale {

const staleItems = (await this.getStale(type)).data.items

await Promise.all(staleItems.filter(issue => !issue.locked).map(issue => {
return this.markIssue(type, issue)
}))
await Promise.all(
staleItems
.filter(issue => !issue.locked && issue.state !== 'closed')
.map(issue => this.markIssue(type, issue))
)
}

async sweep (type) {
Expand All @@ -54,9 +56,11 @@ module.exports = class Stale {
this.logger.trace({ owner, repo }, 'Configured to close stale issues')
const closableItems = (await this.getClosable(type)).data.items

await Promise.all(closableItems.filter(issue => !issue.locked).map(issue => {
this.close(type, issue)
}))
await Promise.all(
closableItems
.filter(issue => !issue.locked && issue.state !== 'closed')
.map(issue => this.close(type, issue))
)
} else {
this.logger.trace({ owner, repo }, 'Configured to leave stale issues open')
}
Expand Down
88 changes: 88 additions & 0 deletions test/stale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,92 @@ describe('stale', () => {
expect(stale.getClosable).not.toHaveBeenCalled()
}
)

describe('mark', () => {
test(
'should not mark issue if it is already closed',
async () => {
let stale = new Stale(github, { perform: true, owner: 'probot', repo: 'stale', logger: app.log })
stale.getStale = jest.fn().mockImplementation(() => {
return Promise.resolve({
data: {
items: [
{ number: 1, state: 'closed' }
]
}
})
})
stale.markIssue = jest.fn()

await stale.mark('issues')
expect(stale.markIssue).not.toHaveBeenCalled()
}
)

test(
'should not mark issue if it is already closed',
async () => {
let stale = new Stale(github, { perform: true, owner: 'probot', repo: 'stale', logger: app.log })
stale.getStale = jest.fn().mockImplementation(() => {
return Promise.resolve({
data: {
items: [
{ number: 1, state: 'open' }
]
}
})
})
stale.markIssue = jest.fn()

await stale.mark('issues')
expect(stale.markIssue).toHaveBeenCalled()
}
)
})

describe('sweep', () => {
test(
'should not close issue if it is already closed',
async () => {
const staleLabel = 'stale'
let stale = new Stale(github, { perform: true, owner: 'probot', repo: 'stale', logger: app.log })
stale.config.daysUntilClose = 1
stale.getClosable = jest.fn().mockImplementation(() => {
return Promise.resolve({
data: {
items: [
{ number: 1, labels: [{ name: staleLabel }], state: 'closed' }
]
}
})
})
stale.close = jest.fn()

await stale.sweep('issues')
expect(stale.close).not.toHaveBeenCalled()
}
)

test(
'should close issue if it is open',
async () => {
const staleLabel = 'stale'
let stale = new Stale(github, { perform: true, owner: 'probot', repo: 'stale', logger: app.log })
stale.config.daysUntilClose = 1
stale.getClosable = jest.fn().mockImplementation(() => {
return Promise.resolve({
data: {
items: [
{ number: 1, labels: [{ name: staleLabel }], state: 'open' }
]
}
})
})
stale.close = jest.fn()

await stale.sweep('issues')
expect(stale.close).toHaveBeenCalled()
}
)
})
})

0 comments on commit a11311e

Please sign in to comment.