This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbugz.js
339 lines (282 loc) · 7.47 KB
/
bugz.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var bugz = (function() {
class Bug {
constructor(data) {
this._data = data;
}
get id() {
return this._data.id;
}
get isAssigned() {
return this._data.assignee !== null;
}
get assignee() {
return this._data.assignee;
}
get title() {
return this._data.title;
}
get labels() {
return [];
}
get whiteboard() {
return "";
}
get url() {
return this._data.url;
}
get hasPriority() {
return this._data.priority !== null;
}
get priority() {
return this._data.priority;
}
get points() {
return this._data.points;
}
get project() {
return "";
}
get isPullRequest() {
return false;
}
get mentors() {
return [];
}
get resolution() {
return "";
}
}
class GithubIssue extends Bug {
get whiteboard() {
return this._data.labels
.filter(l => !l.match(/^priority:[0-9]$/))
.map(l => "[" + l + "]")
.join(" ");
}
get project() {
return this._data.project;
}
get isPullRequest() {
return this._data.isPullRequest;
}
}
class BugzillaBug extends Bug {
get whiteboard() {
return this._data.whiteboard;
}
get isAssigned() {
return this._data.assignee !== "[email protected]";
}
get project() {
return this._data.component;
}
get mentors() {
return this._data.mentors;
}
get resolution() {
return this._data.resolution;
}
}
async function loadIssuesFromGithubRepo(searchParams) {
let {search, filters} = searchParams;
let projectIssues = gh.getIssues(search.user, search.project);
let queryParams = {
state: (filters && filters.open) ? "open" : "closed",
};
let response = await projectIssues.listIssues(queryParams);
let mapped = response.data.map(is => {
let data = {
id: "gh:" + is.id,
assignee: null,
points: null,
title: is.title,
lastChangeDate: is.updated_at,
url: is.html_url,
whiteboard: null,
priority: null,
labels: null,
project: search.project,
isPullRequest: ("pull_request" in is),
};
if (is.assignee) {
data.assignee = is.assignee.login;
} else if (data.isPullRequest) {
data.assignee = is.user.login;
}
let labelNames = is.labels.map(l => l.name);
data.labels = labelNames;
if (data.isPullRequest) {
data.labels.push("pr");
}
let priorityLabel = labelNames.find(l => l.match(/^priority:[0-9]$/));
if (priorityLabel) {
data.priority = priorityLabel.split(":")[1];
}
return new GithubIssue(data);
});
return mapped;
}
async function loadBugsFromBugzilla(searchParams) {
let {search, filters} = searchParams;
let queryParams = {};
// Set up basic search type.
switch (search.type) {
case "bugzillaComponent":
queryParams.product = search.product;
queryParams.component = search.component;
//queryParams.quicksearch = `product:"${search.product}" component:"${search.component}"`;
break;
case "bugzillaAssignees":
queryParams.quicksearch = `assigned_to:${search.assignees.join(',')}`;
break;
case "bugzillaMentors":
//queryParams.quicksearch = `mentor:"${search.mentors.join(',')}"`;
queryParams.emailtype1 = "regexp";
queryParams.email1 = teamEmails.join("|");
queryParams.emailbug_mentor1 = "1";
break;
case "bugzillaWhiteboard":
queryParams.quicksearch = `whiteboard:"${search.whiteboardContent}"`;
break;
default:
throw new Error("Oops... unsupported query type.");
}
// Add query-time filters.
if (filters) {
if ("priority" in filters) {
queryParams.priority = "P" + filters.priority;
}
if ("open" in filters) {
if (filters.open) {
queryParams.resolution = "---";
} else {
queryParams.resolution = ["FIXED",
"INVALID",
"WONTFIX",
"DUPLICATE",
"WORKSFORME",
"INCOMPLETE"];
}
}
if ("isAssigned" in filters) {
queryParams.emailtype2 = filters.isAssigned ? "notequals" : "equals";
queryParams.email2 = "[email protected]";
queryParams.emailassigned_to2 = "1";
}
if ("whiteboard" in filters) {
queryParams.whiteboard = filters.whiteboard;
}
if ("lastChangeTime" in filters) {
queryParams.last_change_time = filters.lastChangeTime.toISOString();
}
}
// We don't want _all_ the fields.
const include_fields = [
"id",
"summary",
"whiteboard",
"product",
"component",
"assigned_to",
"cf_fx_points",
"priority",
"mentors",
"resolution",
].join(",");
queryParams.include_fields = include_fields;
let bugs = await new Promise((resolve, reject) => {
bugzilla.searchBugs(queryParams, (error, bugs) => {
if (error) {
reject(error);
return;
}
resolve(bugs);
});
});
let mapped = bugs.map(b => {
let data = {
id: "bz:" + b.id,
assignee: null,
points: null,
title: b.summary,
lastChangeDate: null,
url: "https://bugzilla.mozilla.org/show_bug.cgi?id=" + b.id,
whiteboard: b.whiteboard,
priority: null,
labels: null,
product: b.product,
component: b.component,
mentors: b.mentors,
resolution: b.resolution,
};
if (b.assigned_to !== "[email protected]") {
data.assignee = b.assigned_to;
}
if (b.cf_fx_points !== "---") {
data.points = parseInt(b.cf_fx_points, 10);
}
if (b.priority !== "--") {
data.priority = parseInt(b.priority.substring(1), 10);
}
return new BugzillaBug(data);
});
return mapped;
}
function findBugs(searchParams) {
let queryWords = new Map([
["githubRepo", loadIssuesFromGithubRepo],
["bugzillaComponent", loadBugsFromBugzilla],
["bugzillaAssignees", loadBugsFromBugzilla],
["bugzillaMentors", loadBugsFromBugzilla],
["bugzillaWhiteboard", loadBugsFromBugzilla],
]);
let {search} = searchParams;
if (!search || !(queryWords.has(search.type))) {
throw new Error("Oops ... unsupported bug search type.");
}
return queryWords.get(search.type)(searchParams);
}
function filterBugs(bugs, searchParams) {
let {filters} = searchParams;
if (!filters) {
return bugs;
}
if ("unprioritized" in filters) {
bugs = bugs.filter(b => b.priority === null);
}
if ("priority" in filters) {
bugs = bugs.filter(b => String(b.priority) === String(filters.priority));
}
if ("customFilter" in filters) {
bugs = bugs.filter(b => filters.customFilter(b));
}
if ("assignees" in filters) {
bugs = bugs.filter(b => filters.assignees.includes(b.assignee));
}
if ("isPullRequest" in filters) {
bugs = bugs.filter(b => b.isPullRequest == filters.isPullRequest);
}
return bugs;
}
const bugCache = new Map();
this.findBugs = async function(searchList) {
let buglists = [];
for (let search of searchList) {
// TODO: non-hackish way to cache this.
let cacheKey = JSON.stringify(search);
let cached = bugCache.get(cacheKey);
if (cached === undefined) {
let bugs = await findBugs(search);
let filtered = filterBugs(bugs, search);
bugCache.set(cacheKey, filtered);
}
buglists.push(bugCache.get(cacheKey));
}
let bugMaps = buglists.map(bl => new Map(bl.map(b => [b.id, b])));
let uniques = new Map();
bugMaps.forEach(bm => uniques = new Map([...uniques, ...bm]));
let joined = [...uniques.values()];
return joined;
}
return this;
})();