aboutsummaryrefslogblamecommitdiffstats
path: root/lib/main.js
blob: 3d59d6e7e2d1503fae4293d4b2ae6e50aac3dea8 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
                                                                                                                                                                                                                   

                                                     







                                                                                                              
  
             
                           
                               
                                                  
                                     
                            
                              



                                                                                
 


                                                         
 
                





                                                                   






















                                                                                    
 
                                          
                                   
 
                                          
                                                     
                                             
       
 
 
   
  



                                                                    
                                                                

                                                      
                             








                                                                        
                               










                                                  








                                                        










                                                                    
 




                                                 

                                                          
                                                                              
 



                         


                                                  


                                                                   







                                                                         
                                                        
                                                                
                                                                   
                                                  


                                                                         
             
          
       
  
/*jslint onevar: false, browser: true, evil: true, laxbreak: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, maxerr: 1000, immed: false, white: false, plusplus: false, regexp: false, undef: false */
// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
//
// Links to read through
// http://ehsanakhgari.org/blog/2010-01-07/bugzilla-tweaks-enhanced
// http://hg.mozilla.org/users/ehsan.akhgari_gmail.com/extensions/file/tip/bugzillatweaks
// http://hg.mozilla.org/users/ehsan.akhgari_gmail.com/extensions/file/ecfa0f028b81/bugzillatweaks/lib/main.js
// http://hg.mozilla.org/users/avarma_mozilla.com/atul-packages/file/42ac1e99a107/packages\
//     /facebook-acquaintances/lib/main.js#l11
// http://ehsanakhgari.org/blog/2010-05-31/my-experience-jetpack-sdk#comment-1253
//
"use strict";
var util = require("util");
var logger = require("logger");
var myStorage = require("simple-storage").storage;
var browser = require("tab-browser");
var urlMod = require("url");
var selfMod = require("self");
var preferences = require("preferences-service");
var BSTPrefNS = require("bzpage").BSTPrefNS;
// Use my JSON for now before it is fixed for general public
var JSONURLDefault = "http://matej.ceplovi.cz/progs/data/RH_Data-packages.json";

var TriagedDistro = 13;
var NumberOfFrames = 7;
var XMLRPCurl = "https://bugzilla.redhat.com/xmlrpc.cgi";

var config = {};
(function (){
    var matchesAll = JSON.parse(selfMod.data.load("matches.json"));
    config.matches = matchesAll.matches;
    config.skipMatches = matchesAll.skipMatches;
})();
console.log("config.matches = " + config.matches.toSource());
// ==============================================================
// https://wiki.mozilla.org/Labs/Jetpack/JEP/24
var WillBemanifest = {
    settings : [
        {
            name : "BZpassword",
            type : "password",
            label : "Bugzilla password"
        },
        {
            name : "JSONURL",
            type : "text",
            label : "Configuration file URL",
            "default" : "http://mcepl.fedorapeople.org/scripts/BugZappers_data.json"
        },
        {
            name : "enabledPacks",
            type : "text",
            label : "comment packs which should be enabled",
            "default" : ""
        }
    ]
};

function isOurPage(window, matchingURLs) {
    var url = window.location.href;

    // like ["regexp-url1", "regexp-url2"]
    return matchingURLs.some(function (element,i,a) {
        return new RegExp(element).test(url);
    });
}

/**
 *
 */
function skipThisPage(doc) {
    var stemURL = "https://HOSTNAME/show_bug.cgi?id=";
    var titleStr = doc.getElementsByTagName("title")[0].textContent;
    var REArr = new RegExp("[bB]ug\\s+([0-9]+)").exec(titleStr);
    var hostname = urlMod.URL(doc.location.href).host;
    if (REArr) {
        var bugNo = REArr[1];
        console.log("bugNo = " + bugNo + ", hostname = " + hostname);
        var emailsSent = doc.
        querySelector("#bugzilla-body > dl:nth-of-type(1)").textContent;
        emailsSent = emailsSent.replace(/^(\s*)$/mg,"");
        util.notification(emailsSent);
        doc.location = stemURL.replace("HOSTNAME",hostname) + bugNo;
    }
}

function initialize(callback) {
    var prefName = BSTPrefNS+"JSONURL";
    var url = "";

    if (preferences.isSet(prefName)) {
        url = preferences.get(prefName);
    } else {
        url = JSONURLDefault;
        preferences.set(prefName, JSONURLDefault);
    }

    util.loadJSON(url, function(parsedData) {
        config.gJSONData = parsedData;

        var keys = "", key = "";
        for (key in config.gJSONData) {
            keys += key + " ";
        }
        console.log("loaded JSON object keys: " + keys);

        // Get card translation table
        if ("downloadJSON" in config.gJSONData.configData) {
            var URLsList = config.gJSONData.configData.downloadJSON;
            for (var dwnldObj in URLsList) {
                console.log("Downloading " + 
                    dwnldObj + " from URL " +
                    URLsList[dwnldObj] + ".");
                util.loadJSON(URLsList[dwnldObj],function(response){
                    config[dwnldObj] = response;
                });
            }
        };

        if (!myStorage.logs) {
            console.log("myStorage.logs empty!");
            myStorage.logs = {};
        }

        var logConstructor = logger.Logger;
        config.logger = new logConstructor(myStorage.logs,
            JSON.parse(selfMod.data.load("bugzillalabelAbbreviations.json")));

        callback(config);
    }, this);
}

exports.main = function main(options, callbacks) {
    initialize(function (config) {
        browser.whenContentLoaded(
            function(window) {
                // is this good for anything?
                if ("window" in window) { window = window.window; }

                var construct = {};
                var bzType = config.gJSONData.configData.objectStyle;
                if (bzType === "RH") {
                    construct = require("rhbzpage").RHBugzillaPage;
                } else if (bzType === "MoFo") {
                    construct = require("mozillabzpage").MozillaBugzilla;
                }
                if (isOurPage(window, config.matches)) {
                    var curPage = new construct(window, config);
                } else if (isOurPage(window, config.skipMatches)) {
                    skipThisPage(window.document);
                } else {
                    console.log("Not our page: " + window.location.href);
                }
            }
        );
    });
};