Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Firefox for desktop - list fixed bugs in Mercurial - 2
  3. // @namespace   darkred
  4. // @authors     darkred, johnp
  5. // @description It generates a list of recently fixed bugs related to Firefox for desktop in Mozilla Mercurial pushlogs
  6. // @include     /^https?:\/\/hg\.mozilla\.org.*pushloghtml.*/
  7. // @version     4.1
  8. // @grant       GM_xmlhttpRequest
  9. // @grant       GM_addStyle
  10. // @grant       GM_getResourceText
  11. // @require     https://code.jquery.com/jquery-2.1.4.min.js
  12. // @require     https://code.jquery.com/ui/1.11.4/jquery-ui.min.js
  13. // ==/UserScript==
  14.  
  15. var silent = false;
  16. var debug = false;
  17.  
  18. time("MozillaMercurial");
  19.  
  20. String.prototype.escapeHTML = function() {
  21.     var tagsToReplace = {
  22.         '&': '&',
  23.         '<': '&lt;',
  24.         '>': '&gt;'
  25.     };
  26.     return this.replace(/[&<>]/g, function(tag) {
  27.         return tagsToReplace[tag] || tag;
  28.     });
  29. };
  30.  
  31. // theme for the jQuery dialog
  32. $("head").append(
  33.     '<link ' +
  34.     'href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.min.css" ' +
  35.     // 'href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.min.css" ' +                 // uncomment this (and comment #19)  in order to change theme
  36.     'rel="stylesheet" type="text/css">'
  37. );
  38.  
  39.  
  40. var regex = /^https:\/\/bugzilla\.mozilla\.org\/show_bug\.cgi\?id=(.*)$/;
  41. var base_url = "https://bugzilla.mozilla.org/rest/bug?include_fields=id,summary,status,resolution,product,component,op_sys,platform,whiteboard&id=";
  42. var bugIds = [];
  43. var bugsComplete = [];
  44.  
  45. var table = document.getElementsByTagName("table")[0];
  46. var links = table.getElementsByTagName("a");
  47. var len = links.length;
  48. for (let i = 0; i < len; i++) {
  49.   let n = links[i].href.match(regex);
  50.   if (n !== null && n.length > 0) {
  51.     let id = parseInt(n[1]);
  52.     if (bugIds.indexOf(id) === -1) {
  53.       bugIds.push(id);
  54.     }
  55.   }
  56. }
  57.  
  58. var numBugs = bugIds.length;
  59. var counter = 0;
  60.  
  61. var rest_url = base_url + bugIds.join();
  62. time("MozillaMercurial-REST");
  63. $.getJSON(rest_url, function(data) {
  64.   timeEnd("MozillaMercurial-REST");
  65.   data.bugs.sort(function(a, b) {
  66.     return (a.product + ": " + a.component) > (b.product + ": " + b.component);
  67.   });
  68.   $.each(data.bugs, function(index) {
  69.     let bug = data.bugs[index];
  70.     // process bug (let "shorthands" just to simplify things during refactoring)
  71.     let status = bug.status;
  72.     if (bug.resolution !== "") status += " " + bug.resolution;
  73.     let product = bug.product;
  74.     let component = bug.component;
  75.     let platform = bug.platform;
  76.     if (platform == 'Unspecified') {
  77.       platform = 'Uns';
  78.     }
  79.     if (bug.op_sys !== "" && bug.op_sys !== "Unspecified") {
  80.       platform += '/' + bug.op_sys;
  81.     }
  82.     let whiteboard = bug.whiteboard === "" ? "[]" : bug.whiteboard;
  83.     // todo: message???
  84.  
  85.      log('----------------------------------------------------------------------------------------------------------------------------------');
  86.      log((index + 1) + "/" + numBugs); // Progression counter
  87.      log('BugNo: ' + bug.id + '\nTitle: ' + bug.summary + '\nStatus: ' + status + '\nProduct: ' + product + '\nComponent: ' + component + '\nPlatform: ' + platform + '\nWhiteboard: ' + whiteboard);
  88.  
  89.     if (isRelevant(bug)) {
  90.       // add html code for this bug
  91.       bugsComplete.push('<a href="'
  92.                         + 'https://bugzilla.mozilla.org/show_bug.cgi?id='+ bug.id + '">#'
  93.                         + bug.id
  94.                         + '</a>'
  95.                         + ' (' + product + ': ' + component + ') '
  96.                         + bug.summary.escapeHTML() + ' [' + platform + ']' + whiteboard.escapeHTML() + '<br>');
  97.     }
  98.     counter++; // increase counter
  99.     // remove processed bug from bugIds
  100.     let i = bugIds.indexOf(bug.id);
  101.     if (i !== -1) bugIds[i] = null;
  102.   });
  103.   log("==============\nReceived " + counter + " of " + numBugs + " bugs.");
  104.  
  105.   // process remaining bugs one-by-one
  106.   var requests = []
  107.   time("MozillaMercurial-missing");
  108.   $.each(bugIds, function(index) {
  109.     let id = bugIds[index];
  110.     if (id !== null) {
  111.       time("Requesting missing bug " + id);
  112.       let promise = $.getJSON("https://bugzilla.mozilla.org/rest/bug/" + id,
  113.                 function(json) {
  114.         // I've not end up here yet, so cry if we do
  115.         console.error("Request succeeded unexpectedly!");
  116.         console.error("Please submit this information to the script authors:");
  117.         timeEnd("Requesting missing bug " + id);
  118.         console.log(json);
  119.         let bug = json.bugs[0];
  120.         console.log(bug);
  121.         // TODO: display as much information as possible
  122.       });
  123.       // Actually, we usually get an error
  124.       promise.error(function(req, status, error) {
  125.         timeEnd("Requesting missing bug " + id);
  126.         if (error == "Authorization Required") {
  127.           // log("Bug " + id + " requires authorization!");
  128.           log("https://bugzilla.mozilla.org/show_bug.cgi?id=" + id + " requires authorization!");
  129.           let text = " requires authorization!<br>";
  130.  
  131.           bugsComplete.push('<a href="'
  132.                         + 'https://bugzilla.mozilla.org/show_bug.cgi?id='+ id + '">#'
  133.                         + id + '</a>' + text);
  134.         } else {
  135.           console.error("Unexpected error encountered (Bug" + id + "): " + status + " " + error);
  136.         }
  137.       });
  138.       requests.push(promise);
  139.     }
  140.   });
  141.   // wait for all requests to be settled, then join them together
  142.   // Source: https://stackoverflow.com/questions/19177087/deferred-how-to-detect-when-every-promise-has-been-executed
  143.   $.when.apply($, $.map(requests, function(p) {
  144.                 return p.then(null, function() {
  145.                   return $.Deferred().resolveWith(this, arguments);
  146.                 });
  147.               })).always(function() {
  148.     timeEnd("MozillaMercurial-missing");
  149.     // Variable that will contain all values of the bugsComplete array, and will be displayed in the 'dialog' below
  150.     var docu = '';
  151.     docu = bugsComplete.join('');
  152.  
  153.     var div = document.createElement('div');
  154.     $('div.page_footer').append(div);
  155.     div.id = 'dialog';
  156.     docu = '<div id="dialog_content" title="Relevant Bugs">' + docu + '</div>';
  157.     div.innerHTML = docu;
  158.     $("#dialog").hide();
  159.  
  160.     $(function() {
  161.       $("#dialog").dialog({
  162.         title: 'List of recently fixed bugs of Firefox for Desktop (' + bugsComplete.length + ')',
  163.         width: '1350px'
  164.       });
  165.     });
  166.  
  167.     log('ALL IS DONE');
  168.     timeEnd("MozillaMercurial");
  169.   });
  170. });
  171.  
  172. function isRelevant(bug) {
  173.     if (!bug.id) return false;
  174.     if (bug.status && bug.status != 'RESOLVED' && bug.status != 'VERIFIED') {
  175.         log('    IRRELEVANT because of it\'s Status --> ' + bug.status);
  176.         return false;
  177.     }
  178.     if (bug.component && bug.product && bug.component == 'Build Config' && (bug.product == 'Toolkit' || bug.product == 'Firefox')) {
  179.         log('    IRRELEVANT because of it\'s Product --> ' + bug.product + 'having component --> ' + bug.component);
  180.         return false;
  181.     }
  182.     if (bug.product && bug.product != 'Add-on SDK'  &&
  183.               bug.product != 'Cloud Services'       &&
  184.               bug.product != 'Core'                 &&
  185.               bug.product != 'Firefox'              &&
  186.               bug.product != 'Hello (Loop)'         &&
  187.               bug.product != 'Toolkit') {
  188.         log('    IRRELEVANT because of it\'s Product --> ' + bug.product);
  189.         return false;
  190.     }
  191.     if (bug.component && bug.component == 'AutoConfig'       ||
  192.                bug.component == 'Build Config'               ||
  193.                bug.component == 'DMD'                        ||
  194.                bug.component == 'Embedding: GRE Core'        ||
  195.                bug.component == 'Embedding: Mac'             ||
  196.                bug.component == 'Embedding: MFC Embed'       ||
  197.                bug.component == 'Embedding: Packaging'       ||
  198.                bug.component == 'Hardware Abstraction Layer' ||
  199.                bug.component == 'mach'                       ||
  200.                bug.component == 'Nanojit'                    ||
  201.                bug.component == 'QuickLaunch'                ||
  202.                bug.component == 'Widget: Gonk') {
  203.         log('    IRRELEVANT because of it\'s Component --> ' + bug.component);
  204.         return false;
  205.     }
  206.  
  207.     log('    OK  ' + 'https://bugzilla.mozilla.org/show_bug.cgi?id=' + bug.id);
  208.     return true;
  209. }
  210.  
  211. function log(str) {
  212.   if (!silent) {
  213.     console.log(str);
  214.   }
  215. }
  216.  
  217. function time(str) {
  218.   if (debug) {
  219.     console.time(str);
  220.   }
  221. }
  222.  
  223. function timeEnd(str) {
  224.   if (debug) {
  225.     console.timeEnd(str);
  226.   }
  227. }
  228.  
  229. // $(function() {
  230. $("#dialog").dialog({
  231.         modal: false,
  232.         title: "Draggable, sizeable dialog",
  233.         position: {
  234.             my: "top",
  235.             at: "top",
  236.             of: document,
  237.             collision: "none"
  238.         },
  239.         width: "auto",
  240.         minWidth: 400,
  241.         minHeight: 200,
  242.         zIndex: 3666
  243.     })
  244.     .dialog("widget").draggable("option", "containment", "none");
  245.  
  246. //-- Fix crazy bug in FF! ...
  247. $("#dialog").parent().css({
  248.     position: "fixed",
  249.     top: 0,
  250.     left: "4em",
  251.     width: "75ex"
  252. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement