-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove-stale-issues.js
181 lines (143 loc) · 5.38 KB
/
move-stale-issues.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const github = require('@actions/github');
const util = require('util');
const isTTYout = Boolean(process.stdout.isTTY);
const labelInfo = isTTYout ? '\x1b[32m{info}\x1b[0m' : '';
const ts = () => `${(new Date()).toISOString()} ~ `;
const logger = {
info(msg, context) {
const params = [ labelInfo + ts() + msg ];
if (context) {
params.push(util.inspect(context));
}
params.push('');
console.log(params.join(''));
}
};
function getDaysDiff(startDate, endDate) {
const dayInMs = 1000 * 60 * 60 * 24;
const diffInMs = startDate.getTime() - endDate.getTime();
const diffInDays = Math.round(diffInMs / dayInMs);
return diffInDays;
}
async function runAction(owner, repo, config) {
const {
token,
fromMilestone,
targetMilestone,
daysBeforeStale = 30,
exemptAllAssignees = false,
closeIssuesInTargetIfStaleForDays = false
} = config;
logger.info('Starting action with params:');
logger.info(`\tfromMilestone: ${fromMilestone}`);
logger.info(`\ttargetMilestone: ${targetMilestone}`);
logger.info(`\tdaysBeforeStale: ${daysBeforeStale}`);
logger.info(`\texemptAllAssignees: ${exemptAllAssignees}`);
logger.info('\tcloseIssuesInTargetIfStaleForDays: ' +
closeIssuesInTargetIfStaleForDays);
logger.info(`\trepo owner: ${owner}`);
logger.info(`\trepo: ${repo}`);
const octokit = github.getOctokit(token);
logger.info('Trying to get milestones...');
const { data: milestones } = await octokit.rest.issues.listMilestones({
owner,
repo,
state: 'open'
});
const fromMilestoneIds = milestones.filter((m) =>
fromMilestone.includes(m.title)
).map((m) => m.number);
const targetMilestoneId = (milestones
.find((m) => m.title === targetMilestone) || {}).number;
if (!targetMilestoneId) {
throw new Error(`Could not find target milestone: ${targetMilestone}`);
}
if (!fromMilestoneIds.length) {
throw new Error('Could not find from milestone: ' +
fromMilestone.join(' or '));
}
logger.info(`\tFrom milestone ids: #${fromMilestoneIds.join(', ')}`);
logger.info(`\tTarget milestone id: #${targetMilestoneId}`);
logger.info('Trying to get issues...');
const opts = {
owner,
repo,
state: 'open',
per_page: 100,
milestone: fromMilestoneIds.join(',')
};
if (exemptAllAssignees) { opts.assignee = 'none'; }
const now = new Date();
let totalMoved = 0;
const fn = octokit.rest.issues.listForRepo;
const issuesToMove = [];
for await (const response of octokit.paginate.iterator(fn, opts)) {
const issues = response.data;
for (const issue of issues) {
const { number, updated_at, pull_request } = issue;
if (pull_request) { continue; }
const issueDate = new Date(updated_at);
const diff = getDaysDiff(now, issueDate);
logger.info(`\tLast update on issue #${number} was ${diff} days ago.`);
if (daysBeforeStale === 0 || diff >= daysBeforeStale) {
logger.info(`\t[stale] Issue #${number} is stale.`);
issuesToMove.push(issue);
} else {
logger.info(`\t[not stale] Issue #${number} is NOT stale.`);
}
}
}
for (const issue of issuesToMove) {
const { number } = issue;
await octokit.rest.issues.update({
owner,
repo,
issue_number: number,
milestone: targetMilestoneId
});
logger.info(`\t[stale] Issue #${number} moved to: ${targetMilestone}`);
totalMoved++;
}
let totalClosed = 0;
if (closeIssuesInTargetIfStaleForDays !== false) {
const fn = octokit.rest.issues.listForRepo;
const issuesToClose = [];
for await (const response of octokit.paginate.iterator(fn, {
owner,
repo,
state: 'open',
per_page: 100,
milestone: targetMilestoneId
})) {
const issues = response.data;
for (const issue of issues) {
const { number, updated_at, pull_request } = issue;
if (pull_request) { continue; }
const issueDate = new Date(updated_at);
const diff = getDaysDiff(now, issueDate);
logger.info(`\tLast update on issue #${number} was ${diff} days ago.`);
if (diff >= closeIssuesInTargetIfStaleForDays) {
logger.info(`\t[stale] {close} Issue #${number} is stale.`);
issuesToClose.push(issue);
} else {
logger.info(`\t[not stale] Issue #${number} is NOT stale.`);
}
}
for (const issue of issuesToClose) {
const { number } = issue;
await octokit.rest.issues.update({
owner,
repo,
issue_number: number,
state: 'closed'
});
logger.info(`\t[stale] Issue #${number} closed!`);
totalClosed++;
}
}
}
logger.info('Done!');
logger.info(`Total of issues moved: ${totalMoved}, ` +
`total closed: ${totalClosed}.`);
}
module.exports = { logger, getDaysDiff, runAction };