Advertisement
Guest User

Untitled

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