This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
235 lines (216 loc) · 6.6 KB
/
index.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const ghGot = require('gh-got');
module.exports = function(opts) {
if (opts.org === undefined) {
return Promise.reject(new Error('opts.org must be set'));
}
if (opts.team === undefined) {
return Promise.reject(new Error('opts.team must be set'));
}
if (opts.token === undefined) {
return Promise.reject(new Error('opts.token must be set'));
}
if (typeof opts['max-issues'] !== 'number') {
return Promise.reject(
new Error('opts.max-issues must be a number, use 0 for all')
);
}
var teamInfo = null;
var teamMembers = null;
var teamIssues = null;
return checkOrg(opts)
.then(() =>
getTeamId(opts).then(ti => {
teamInfo = ti;
logger('Team: ' + ti.name);
})
)
.then(() =>
getTeamMembers(opts, teamInfo).then(mem => {
teamMembers = mem.filter(mem => opts.nonmembers.indexOf(mem) === -1);
logger('Num Members: ' + mem.length);
})
)
.then(() =>
findIssues(opts).then(issues => {
teamIssues = issues.filter(
iss => opts['ignore-repos'].indexOf(iss.repo) === -1
);
logger('Num Issues: ' + issues.length);
})
)
.then(() => fillInComments(opts, teamIssues)) // mutates teamIssues
.then(() => (teamIssues = filterIssuesByComments(teamIssues, teamMembers)))
.then(() => fillInReactions(opts, teamIssues)) // mutates teamIssues
.then(
() =>
(teamIssues = filterIssuesByReactions(opts, teamIssues, teamMembers))
);
};
function filterIssuesByReactions(opts, issues, members) {
return issues.filter(iss => {
// only use +1 reactions
var hasMemberReaction = iss.reactions
.filter(rec => rec.content === '+1')
.some(rec => {
return members.indexOf(rec.user) > -1;
});
// we want to keep issues that members haven't reacted too
return hasMemberReaction === false;
});
}
function fillInReactions(opts, issues) {
return Promise.all(
issues.map(iss => {
var mentionedInComments = iss.comments.some(c => c.mentionsTeam);
if (mentionedInComments === false) return addReactions(opts, iss);
iss.reactions = []; // just so all issues look the same
return iss;
})
);
}
function addReactions(opts, iss) {
return safeGh(`repos/${opts.org}/${iss.repo}/issues/${iss.id}/reactions`, {
headers: {
accept: 'application/vnd.github.squirrel-girl-preview'
},
token: opts.token
}).then(resp => {
iss.reactions = resp.body.map(rec => {
return {
user: rec.user.login,
content: rec.content
};
});
return iss;
});
}
function logger(msg) {
if (process.env.DEBUG === 'missed-issues') {
console.log('[missed-issues] ' + msg); // eslint-disable-line no-console
}
}
function filterIssuesByComments(issues, members) {
return issues.filter(t => {
var lastMention = t.comments.reduce((m, c, i) => {
if (c.mentionsTeam) return i;
return m;
}, -1);
// -2 means no member has ever been on this ticket
// -1 means the creator of the ticket was a member
// 0 and up means a member has commented
var lastReply = t.comments.reduce((m, c, i) => {
if (members.indexOf(c.user) === -1) return m;
return i;
}, members.indexOf(t.user) === -1 ? -2 : -1);
return lastMention > lastReply;
});
}
function fillInComments(opts, issues) {
var count = 0;
return Promise.all(
issues.map(t => {
return getComments(opts, t).then(c => {
t.comments = c;
count += c.length;
return t;
});
})
).then(() => logger('Num Comments: ' + count));
}
function getComments(opts, ticket, page) {
// TODO: use `rel="last"` to do reverse paging and be able to quickly exit
page = page || 1;
var url = `repos/${opts.org}/${ticket.repo}/issues/${
ticket.id
}/comments?page=${page}`;
return safeGh(url, { token: opts.token }).then(resp => {
var link = resp.headers.link || '';
var hasNext = link.indexOf('rel="next"') !== -1;
var comments = resp.body.map(c => {
return {
user: c.user.login,
mentionsTeam: c.body.indexOf(`@${opts.org}/${opts.team}`) !== -1
};
});
if (hasNext) {
return getComments(opts, ticket, page + 1, comments.length).then(c =>
comments.concat(c)
);
}
return comments;
});
}
function findIssues(opts, page, numBefore) {
page = page || 1;
numBefore = numBefore || 0;
var q = `team:${opts.org}/${opts.team} state:open updated:>=${opts.from}`;
return safeGh(`search/issues?q=${q}&page=${page}&sort=updated&order=desc`, {
token: opts.token
}).then(resp => {
var link = resp.headers.link || '';
var hasNext = link.indexOf('rel="next"') !== -1;
var issues = resp.body.items.map(i => {
var repoParts = i.repository_url.split('/');
var urlParts = i.html_url.split('/');
return {
id: i.number,
repo: repoParts[repoParts.length - 1],
url: i.html_url,
title: i.title,
user: i.user.login,
type: urlParts[urlParts.length - 2],
created: i.created_at,
updated: i.updated_at
};
});
var numNow = numBefore + issues.length;
if (hasNext && numNow < opts['max-issues']) {
return findIssues(opts, page + 1, numNow).then(next =>
issues.concat(next).slice(0, opts['max-issues'])
);
}
return issues.slice(0, opts['max-issues']);
});
}
function safeGh(url, opts) {
return ghGot(url, opts).catch(resp => {
if (
resp.headers.status === '403 Forbidden' &&
resp.headers['retry-after']
) {
console.error('[missed-issues] warning: you are being ratelimited'); // eslint-disable-line no-console
return wait(resp.headers['retry-after']).then(() => safeGh(url, opts));
}
throw resp;
});
}
function wait(seconds) {
return new Promise(function(resolve) {
setTimeout(resolve, seconds * 1000);
});
}
function checkOrg(opts) {
return safeGh(`orgs/${opts.org}`, { token: opts.token }).then(() => {
return null;
});
}
function getTeamMembers(opts, teamInfo) {
return safeGh(`teams/${teamInfo.id}/members`, { token: opts.token }).then(
resp => {
return resp.body.map(u => u.login);
}
);
}
function getTeamId(opts, page) {
page = page || 1;
return safeGh(`orgs/${opts.org}/teams?page=${page}`, {
token: opts.token
}).then(resp => {
var link = resp.headers.link || '';
var hasNext = link.indexOf('rel="next"') !== -1;
var matches = resp.body.filter(t => t.slug === opts.team);
if (matches.length) return matches[0];
if (hasNext) return getTeamId(opts, page + 1);
throw new Error('Cannot find team in the org');
});
}