-
Notifications
You must be signed in to change notification settings - Fork 6
/
bug-assign.user.js
693 lines (618 loc) · 24 KB
/
bug-assign.user.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
// ==UserScript==
// @name Gentoo Bugzilla bug assignment helper
// @namespace https://github.com/mgorny/bug-assign-user-js
// @version 6
// @description Helper to suggest proper assignees for a bug
// @author Michał Górny, Mart Raudsepp
// @match https://bugs.gentoo.org/*
// ==/UserScript==
(function()
{
'use strict';
// current package list
var packageList = [];
// object where keys are packages and values are maintainer lists from json
var maintainers = {};
// object where keys are maintainer emails and values are objects with maint boxes
var maintElems = {};
// object where keys are packages and values are maintainer groups
var maintGroups = {};
// number of requests in progress
var reqInProgress = 0;
// create the outer box used to contain bug assignment data
function createBox()
{
var top = document.querySelector('#bz_show_bug_column_2 tbody');
if (top !== null)
{
// bug edit form
var tr = document.createElement('tr');
tr.innerHTML += ('<th class="field_label">Maintainers:</th><td>' +
'<form action="#"><table id="bug-assign-table">' +
'</table></form></td>');
top.appendChild(tr);
}
else
{
// bug file form
var over = document.querySelector('#field_container_bug_status + td + td');
var newRowSpan = over.rowSpan - 1;
over.rowSpan = 1;
var left = document.querySelector('#field_label_assigned_to + td');
left.colSpan = 1;
var th = document.createElement('th');
th.class = 'field_label';
th.textContent = 'Maintainers:';
left.parentNode.appendChild(th);
var td = document.createElement('td');
td.rowSpan = newRowSpan;
td.innerHTML = ('<form action="#"><table id="bug-assign-table">' +
'</table></form>');
left.parentNode.appendChild(td);
}
}
// add triggers for auto-updates where appropriate
function addTriggers()
{
// summary edits
var summaryInput = document.getElementById('short_desc');
summaryInput.addEventListener('keyup', function(ev) {
if ('bugARefreshTimeout' in ev.target)
window.clearTimeout(ev.target.bugARefreshTimeout);
ev.target.bugARefreshTimeout = window.setTimeout(updatePackages, 2000);
});
summaryInput.addEventListener('change', function(ev) {
if ('bugARefreshTimeout' in ev.target)
window.clearTimeout(ev.target.bugARefreshTimeout);
updatePackages();
});
// assignee edits
var assignInput = document.getElementById('assigned_to');
assignInput.addEventListener('keyup', function(ev) {
if ('bugARefreshTimeout' in ev.target)
window.clearTimeout(ev.target.bugARefreshTimeout);
ev.target.bugARefreshTimeout = window.setTimeout(syncMaintainerStatesToBug, 2000);
});
assignInput.addEventListener('change', function(ev) {
if ('bugARefreshTimeout' in ev.target)
window.clearTimeout(ev.target.bugARefreshTimeout);
syncMaintainerStatesToBug();
});
// assignee reset
var assignReset = document.getElementById('set_default_assignee');
if (assignReset !== null)
assignReset.addEventListener('change', syncMaintainerStatesToBug);
// 'add cc' edits
var newCCInput = document.getElementById('newcc');
if (newCCInput === null)
newCCInput = document.getElementById('cc');
newCCInput.addEventListener('keyup', function(ev) {
if ('bugARefreshTimeout' in ev.target)
window.clearTimeout(ev.target.bugARefreshTimeout);
ev.target.bugARefreshTimeout = window.setTimeout(syncMaintainerStatesToBug, 2000);
});
newCCInput.addEventListener('change', function(ev) {
if ('bugARefreshTimeout' in ev.target)
window.clearTimeout(ev.target.bugARefreshTimeout);
syncMaintainerStatesToBug();
});
// cc removal list
var removeCCBox = document.getElementById('removecc');
if (removeCCBox !== null)
{
removeCCBox.addEventListener('change', syncMaintainerStatesToBug);
var removeCCSelect = document.getElementById('cc');
removeCCSelect.addEventListener('change', syncMaintainerStatesToBug);
}
}
// perform brace expansion
function performBraceExpansion(s)
{
var words = s.split(/[\s]/);
var braceExpansion = true;
while (braceExpansion)
{
var expanded = [];
braceExpansion = false;
for (var i = 0; i < words.length; ++i)
{
var m = words[i].match(/^(.*){([^}]*,[^}]*)}(.*)$/);
if (m)
{
var spl = m[2].split(',');
for (var j = 0; j < spl.length; ++j)
expanded.push(m[1] + spl[j] + m[3]);
braceExpansion = true;
}
else
expanded.push(words[i]);
}
words = expanded;
}
return words.join(' ');
}
// find package names in summary and return them as array
// TODO: support brace expansion
function getPackageNames()
{
var summ = performBraceExpansion(document.getElementById('short_desc').value);
var words = summ.split(/[\s,;\[\]:]/);
var pnames = [];
for (var i = 0; i < words.length; ++i)
{
// strip leading and trailing symbols
var cpv = words[i].replace(/^\W*/, '').replace(/[^\w+]*$/, '');
// category/package(-version)?
if (cpv.match(/^\w[\w+.-]*\/\w[\w+-]*(-\d+(\.\d+)*[a-z]?((_alpha|_beta|_pre|_rc|_p)\d*)*(-r\d+)?)?$/))
{
// strip the version if any
var cp = cpv.replace(/-\d+(\.\d+)*[a-z]?((_alpha|_beta|_pre|_rc|_p)\d*)*(-r\d+)?$/, '');
if (!pnames.includes(cp))
{
pnames.push(cp);
}
}
}
return pnames;
}
// fill in the maintainer table using data from 'maintainers' object
function updateMaintainerTable()
{
var topbox = document.getElementById('bug-assign-table');
topbox.innerHTML = '';
var a, tr, td, input;
// header row
tr = document.createElement('tr');
topbox.appendChild(tr);
td = document.createElement('th');
tr.appendChild(td);
td = document.createElement('th');
td.textContent = 'A';
td.style = 'text-align: center';
tr.appendChild(td);
td = document.createElement('th');
td.textContent = 'CC';
td.style = 'text-align: center';
tr.appendChild(td);
// reset
maintElems = {};
// use the original package list to preserve order
for (var i = 0; i < packageList.length; ++i)
{
var pkg = packageList[i];
if (!(pkg in maintainers))
continue;
tr = document.createElement('tr');
topbox.appendChild(tr);
td = document.createElement('th');
if (pkg != '_special') {
a = document.createElement('a');
a.href = 'https://packages.gentoo.org/packages/' + pkg;
a.textContent = pkg;
td.appendChild(a);
}
td.style = 'text-align: left; border-top: 1px dashed black';
tr.appendChild(td);
td = document.createElement('td');
td.style = 'text-align: center; border-top: 1px dashed black';
var groupA = document.createElement('input');
groupA.type = 'checkbox';
if (pkg == '_special')
groupA.style = 'display: none';
groupA.maintainers = [];
groupA.pkg = pkg;
groupA.addEventListener('change', groupAssignExplicitChange);
td.appendChild(groupA);
tr.appendChild(td);
td = document.createElement('td');
td.style = 'text-align: center; border-top: 1px dashed black';
var groupCC = document.createElement('input');
if (pkg == '_special')
groupCC.style = 'display: none';
groupCC.type = 'checkbox';
groupCC.maintainers = [];
groupCC.pkg = pkg;
groupCC.addEventListener('change', groupCCExplicitChange);
td.appendChild(groupCC);
tr.appendChild(td);
maintGroups[pkg] = {
'a': groupA,
'cc': groupCC
};
// check for maintainer-needed packages
if (maintainers[pkg].length === 0)
{
maintainers[pkg] = [
{
'email': 'maintainer-needed@gentoo.org'
}
];
}
for (var m in maintainers[pkg])
{
var mdata = maintainers[pkg][m];
tr = document.createElement('tr');
topbox.appendChild(tr);
if (!(mdata.email in maintElems))
{
maintElems[mdata.email] = {
'a': [],
'cc': []
};
}
var full_name = '';
if (mdata.name !== null)
full_name = mdata.name + ' ';
full_name += '<' + mdata.email + '>';
td = document.createElement('td');
var maintText = document.createElement('span');
maintText.style = 'padding-left: 1em;';
maintText.textContent = mdata.email;
maintText.title = full_name;
td.appendChild(maintText);
var small = document.createElement('small');
small.style = 'padding-left: .5em';
var a = document.createElement('a');
a.href = 'https://bugs.gentoo.org/custom_userhistory.cgi?matchstr=' + mdata.email;
a.textContent = '[h]';
small.appendChild(a);
a = document.createElement('a');
a.href = 'https://bugs.gentoo.org/buglist.cgi?quicksearch=ALL%20assignee%3A' + mdata.email;
a.textContent = '[a]';
small.appendChild(a);
a = document.createElement('a');
a.href = 'https://bugs.gentoo.org/buglist.cgi?quicksearch=ALL%20cc%3A' + mdata.email;
a.textContent = '[cc]';
small.appendChild(a);
td.appendChild(small);
tr.appendChild(td);
td = document.createElement('td');
td.style = 'text-align: center';
input = document.createElement('input');
input.type = 'checkbox';
input.email = mdata.email;
input.groupA = groupA;
input.groupCC = groupCC;
maintElems[mdata.email].a.push(input);
input.addEventListener('change', maintainerAssignExplicitChange);
td.appendChild(input);
tr.appendChild(td);
td = document.createElement('td');
td.style = 'text-align: center';
input = document.createElement('input');
input.type = 'checkbox';
input.email = mdata.email;
input.groupA = groupA;
input.groupCC = groupCC;
maintElems[mdata.email].cc.push(input);
input.addEventListener('change', maintainerCCExplicitChange);
td.appendChild(input);
tr.appendChild(td);
groupA.maintainers.push(mdata.email);
groupCC.maintainers.push(mdata.email);
if (mdata.description !== null)
{
tr = document.createElement('tr');
topbox.appendChild(tr);
td = document.createElement('td');
td.style = 'padding-left: 10px; padding-bottom: 5px; font-size: 60%;';
td.textContent = mdata.description;
tr.appendChild(td);
}
}
}
syncMaintainerStatesToBug();
}
// Set all maintainer checkboxes to a specific state
// @email: maintainer's e-mail
// @which: 'a' or 'cc'
// @state: true or false
function setMaintainerCheckBox(email, which, state)
{
for (var i in maintElems[email][which])
{
maintElems[email][which][i].checked = state;
}
}
// Set all maintainer 'a' or 'cc' checkboxes to a specific state,
// then unset the second checkboxes if necessary.
// @email: maintainer's email
// @which: 'a' or 'cc', the one causing the change
// @state: true or false
function setBothMaintainerCheckBoxes(email, which, state)
{
setMaintainerCheckBox(email, which, state);
if (state)
setMaintainerCheckBox(email, which == 'a' ? 'cc' : 'a', !state);
}
// unset A states on all assignees except for the excluded one
// but CC them if they were assignees before
function clearAllOtherAssignees(exclude)
{
for (var m in maintElems)
{
if (m == exclude)
continue;
if (maintElems[m].a[0].checked)
{
setMaintainerCheckBox(m, 'cc', true);
alterCCList(m, true);
}
setMaintainerCheckBox(m, 'a', false);
}
}
// Alter the CC list on the bug to include/exclude specified email
function alterCCList(email, state)
{
var unCCBox = document.getElementById('removecc');
var foundInCC = false;
if (unCCBox !== null)
{
var unCCList = document.querySelectorAll('#cc option');
for (i = 0; i < unCCList.length; ++i)
{
if (unCCList[i].value == email)
{
unCCList[i].selected = !state;
// if we want him removed, ensure the checkbox is checked
if (!state)
unCCBox.checked = true;
foundInCC = true;
}
}
}
var newCCInput = document.getElementById('newcc');
if (newCCInput === null)
newCCInput = document.getElementById('cc');
var newCC = newCCInput.value.split(',');
// first, clean up the list from any occurences of email and empty items
newCC = newCC.filter(function (val) { return val && val.trim() != email; });
// then, add just one if requested and not in CC already
if (state && !foundInCC)
newCC.push(email);
// trim extra whitespace
for (var i = 0; i < newCC.length; ++i)
newCC[i] = newCC[i].trim();
newCCInput.value = newCC.join(', ');
}
// Set new assignee for the bug
function alterAssignee(value)
{
var assignInput = document.getElementById('assigned_to');
assignInput.value = value;
// expand the edit
var editButton = document.getElementById('bz_assignee_edit_action');
if (editButton !== null)
editButton.click();
var resetAssign = document.getElementById('set_default_assignee');
if (resetAssign !== null)
resetAssign.checked = false;
}
// Handle an explicit change of 'a' input
function maintainerAssignExplicitChange(ev)
{
// update checkboxes!
setBothMaintainerCheckBoxes(ev.target.email, 'a', ev.target.checked);
clearAllOtherAssignees(ev.target.email);
// set new assignee
alterAssignee(ev.target.checked ? ev.target.email : '');
// remove new assignee from CC
if (ev.target.checked)
alterCCList(ev.target.email, false);
// always uncheck group controls
ev.target.groupA.checked = false;
ev.target.groupCC.checked = false;
}
// Handle an explicit change of 'cc' input
function maintainerCCExplicitChange(ev)
{
// un-assign if he was an assignee
if (maintElems[ev.target.email].a[0].checked)
alterAssignee('');
// update checkboxes!
setBothMaintainerCheckBoxes(ev.target.email, 'cc', ev.target.checked);
// update the CC list
alterCCList(ev.target.email, ev.target.checked);
// always uncheck group controls
ev.target.groupA.checked = false;
ev.target.groupCC.checked = false;
}
// Handle an explicit change of 'a' group
function groupAssignExplicitChange(ev)
{
// store checked state as it will be cleaned
var groupChecked = ev.target.checked;
// mark all maintainers appropriately
for (var i = 0; i < ev.target.maintainers.length; ++i)
{
var m = maintElems[ev.target.maintainers[i]];
var mty;
if (groupChecked)
mty = i === 0 ? 'a' : 'cc';
else
{
if (m.a[0].checked)
mty = 'a';
else if (m.cc[0].checked)
mty = 'cc';
}
if (mty !== undefined)
{
m[mty][0].checked = groupChecked;
if (mty == 'a')
maintainerAssignExplicitChange({'target': m[mty][0]});
else
maintainerCCExplicitChange({'target': m[mty][0]});
}
}
if (groupChecked)
{
// restore the checkbox and clean 'a' from other groups
for (var pkg in maintGroups)
{
if (pkg == ev.target.pkg)
maintGroups[pkg].a.checked = true;
else if (maintGroups[pkg].a.checked)
{
maintGroups[pkg].a.checked = false;
maintGroups[pkg].cc.checked = true;
}
}
}
}
// Handle an explicit change of 'cc' group
function groupCCExplicitChange(ev)
{
// store checked state as it will be cleaned
var groupChecked = ev.target.checked;
// mark all maintainers appropriately
for (var i = 0; i < ev.target.maintainers.length; ++i)
{
var m = maintElems[ev.target.maintainers[i]];
var mty = m.a[0].checked ? 'a' : 'cc';
m[mty][0].checked = groupChecked;
if (mty == 'a')
maintainerAssignExplicitChange({'target': m[mty][0]});
else
maintainerCCExplicitChange({'target': m[mty][0]});
}
// restore the checkbox
if (groupChecked)
maintGroups[ev.target.pkg].cc.checked = true;
}
// Update maintainer checkbox states according to bug info
function syncMaintainerStatesToBug()
{
var i;
// get the assignee
var assignReset = document.getElementById('set_default_assignee');
var assignInput = document.getElementById('assigned_to');
var assignee;
if (assignReset === null || !assignReset.checked)
assignee = assignInput.value.trim();
// get all CC-es into completeCCList
var newCCInput = document.getElementById('newcc');
if (newCCInput === null)
newCCInput = document.getElementById('cc');
var completeCCList = newCCInput.value.split(',');
var unCCBox = document.getElementById('removecc');
if (unCCBox !== null)
{
var unCCList = document.querySelectorAll('#cc option');
for (i = 0; i < unCCList.length; ++i)
{
if (!(unCCList[i].selected && unCCBox.checked))
completeCCList.push(unCCList[i].value);
}
}
// update maintainer states
for (var m in maintElems)
{
var foundInCC = false;
for (i = 0; i < completeCCList.length; ++i)
{
if (completeCCList[i] == m)
foundInCC = true;
}
// fun fact: here we can actually check box 'a' and 'cc'
setMaintainerCheckBox(m, 'a', assignee == m);
setMaintainerCheckBox(m, 'cc', foundInCC);
}
}
// Issue an xmlHttpRequest for maintainers of package pkg
// Triggers updateMaintainerTable() when all requests are finished
function fetchMaintainersForPackage(pkg)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4)
{
if (xmlhttp.status == 200)
{
var pkgmetadata = xmlhttp.responseXML.firstElementChild;
if (pkgmetadata.tagName == 'pkgmetadata') {
var results = [];
for (var i = 0; i < pkgmetadata.children.length; ++i) {
if (pkgmetadata.children[i].tagName != 'maintainer')
continue;
var maintainer = {};
for (var j = 0; j < pkgmetadata.children[i].children.length; ++j) {
maintainer[pkgmetadata.children[i].children[j].tagName] = pkgmetadata.children[i].children[j].innerHTML;
}
results.push(maintainer);
}
maintainers[pkg] = results;
}
}
else if (xmlhttp.status == 404)
{
}
else
{
// TODO: error handling
}
if (--reqInProgress === 0)
updateMaintainerTable();
}
};
++reqInProgress;
xmlhttp.overrideMimeType("application/xml");
// TODO: Gentoo gitweb is hitting CORS restrictions
//xmlhttp.open('GET', 'https://gitweb.gentoo.org/repo/gentoo.git/plain/' + pkg + '/metadata.xml', true);
xmlhttp.open('GET', 'https://raw.githubusercontent.com/gentoo/gentoo/master/' + pkg + '/metadata.xml', true);
xmlhttp.send();
}
// trigger asynchronous maintainer list update
function updatePackages()
{
var topbox = document.getElementById('bug-assign-table');
packageList = getPackageNames();
// mark as being refreshed
topbox.innerHTML = '<div>Update in progress...</div>' + topbox.innerHTML;
maintainers = {};
for (var i in packageList)
fetchMaintainersForPackage(packageList[i]);
// add the entry for special addresses
addSpecialAddresses();
// _special makes it 1
if (packageList.length <= 1)
updateMaintainerTable();
}
// add special addresses that are displayed for all bugs
function addSpecialAddresses()
{
packageList.push('_special');
maintainers['_special'] = [
{
'email': 'cross@gentoo.org',
},
{
'email': 'licenses@gentoo.org',
},
{
'email': 'maintainer-needed@gentoo.org',
},
{
'email': 'maintainer-wanted@gentoo.org',
},
{
'email': 'prefix@gentoo.org',
},
{
'email': 'proxy-maint@gentoo.org',
},
{
'email': 'qa@gentoo.org',
},
{
'email': 'security@gentoo.org',
},
{
'email': 'treecleaner@gentoo.org',
},
];
}
createBox();
addTriggers();
updatePackages();
})();