Skip to content

Commit 67fbf82

Browse files
committed
Close issues when inactive for long period of time
1 parent 635399d commit 67fbf82

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/github.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type CloseIssueParams = {
6060
};
6161
exports.closeIssue = ({ id, owner, repo }: CloseIssueParams) => {
6262
return got.patch(`${BASE_URI}/repos/${owner}/${repo}/issues/${id}`, {
63+
headers,
6364
body: JSON.stringify({ state: 'closed' })
6465
});
6566
};
@@ -91,3 +92,14 @@ exports.createIssueReaction = ({ content, number, owner, repo }: IssueParams) =>
9192
body: JSON.stringify({ content })
9293
}).then(({ body }) => body);
9394
}
95+
96+
type LabeledIssuesParams = {
97+
labels: string;
98+
owner: string;
99+
repo: string;
100+
}
101+
102+
exports.getLabeledIssues = ({ labels, owner, repo }: LabeledIssuesParams) => {
103+
return get(`${BASE_URI}/repos/${owner}/${repo}/issues?labels=${labels}`, { json: true })
104+
.then(({ body }) => body);
105+
}

src/handlers/issues/closing.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// @flow
2+
3+
import github from '../../github';
4+
import Logger from '../../logger';
5+
6+
const { log } = new Logger('issues/closing.js');
7+
8+
type ClosingIssuesPayload = {
9+
repository: {
10+
owner: { login: string; };
11+
name: string;
12+
}
13+
}
14+
15+
const closingIssueMessage = "Closing this issue because it has been inactive for a long period of time.";
16+
17+
const isInactive = updatedAt => {
18+
const idleTime = 30 * 24 * 60 * 60 * 1000;
19+
const currentDate = new Date();
20+
const updateDate = new Date(updatedAt);
21+
return currentDate - updateDate > idleTime;
22+
}
23+
24+
export default function({ repository }: ClosingIssuesPayload) {
25+
const { owner: { login: owner }, name: repo } = repository;
26+
27+
github.getLabeledIssues({
28+
labels: encodeURIComponent("Needs Info"),
29+
owner: owner,
30+
repo: repo
31+
}).then(issues => {
32+
issues.forEach(({ pull_request, number, updated_at }) => {
33+
if (!pull_request && isInactive(updated_at)) {
34+
log(`Adding comment to issue #${number}`, 'verbose');
35+
github.addIssueComment(
36+
number,
37+
owner,
38+
repo,
39+
closingIssueMessage
40+
).then(res => {
41+
log(`Closing issue #${number}`, 'verbose')
42+
return github.closeIssue({
43+
id: number,
44+
owner: owner,
45+
repo: repo
46+
});
47+
}).catch(err => {
48+
log(`Failed closing issue ${number}. Details: ${err.message}`);
49+
});
50+
}
51+
})
52+
});
53+
}

0 commit comments

Comments
 (0)