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

                                                     







                                                                                                              
  
             
                               
                                     
                           
                                         
                                                 

                                               
                                                            

                                                         
 
                
 
                                          
                                   
 
                                          
                                                     
                                             
       
 
 
   
  


                                                      
                                                       
                                             
                                                                
                                         
                
                             



                                                                    
                               
                                       
                    

                                      
                                           
            
                                


                                                  




                                                                                

                    


                                                 
 








                                                                            


                                                                                         




                                 
 
                                                                                                                 
 

                                                                     
                                                                                               

                   







                                                                                     

                                 

             

 
  


                                                  


                                                                   
 
                                                        
                         
                                                                                






                                                                                   
                                                                   
                                                  
                 
             
          
       
  

























































                                                                                    
/*jslint rhino: true, forin: true, onevar: false, browser: true, evil: true, laxbreak: true, undef: true, nomen: true, eqeqeq: false, 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 logger = require("logger");
var browser = require("tab-browser");
var self = require("self");
var Request = require("request").Request;
var preferences = require("preferences-service");
var pageMod = require("page-mod");
// var BTSPrefNS = require("bzpage").BTSPrefNS;
// Use my JSON for now before it is fixed for general public
var JSONURLDefault = "https://fedorahosted.org/released"+
    "/bugzilla-triage-scripts/Config_data.json";

var config = {};

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 titleElems = doc.getElementsByTagName("title");
    var titleStr = titleElems[0].textContent;
    var REArr = new RegExp("[bB]ug\\s+([0-9]+)").exec(titleStr);
    var hostname = doc.location.hostname;
    if (REArr) {
        var bugNo = REArr[1];
        doc.location = stemURL.replace("HOSTNAME",hostname) + bugNo;
    }
}

function initialize(callback) {
    var prefName = BTSPrefNS+"JSONURL";
    var urlStr = "";

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

    // Randomize URL to avoid caching
    // TODO see https://fedorahosted.org/bugzilla-triage-scripts/ticket/21
    // for more thorough discussion and possible further improvement
    urlStr += (urlStr.match(/\?/) == null ? "?" : "&") + (new Date()).getTime();

    Request({
        url: urlStr,
        onComplete: function (response) {
            if (response.status == 200) {
                config.gJSONData = response.json;

                // Get additional tables
                if ("downloadJSON" in config.gJSONData.configData) {
                    var URLsList = config.gJSONData.configData.downloadJSON;
                    var dwnldObj = "";
                    URLsList.forEach(function (arr) {
                        var title = arr[0];
                        var url = arr[1];
                        Request({
                            url: url,
                            onComplete: function(response) {
                                if (response.status == 200) {
                                    config.gJSONData.constantData[title] = response.json;
                                }
                            }
                        }).get();
                    }, this);
                }

                config.logger = new logger.Logger(JSON.parse(self.data.load("bugzillalabelAbbreviations.json")));

                config.matches = config.gJSONData.configData.matches;
                config.skipMatches = config.matches.map(function(x) {
                    return x.replace("show_bug.cgi.*","((process|post)_bug|attachment)\.cgi$");
                });

                config.objConstructor = {};
                var bzType = config.gJSONData.configData.objectStyle;
                if (bzType === "RH") {
                    config.objConstructor = require("rhbzpage").RHBugzillaPage;
                } else if (bzType === "MoFo") {
                    config.objConstructor = require("mozillabzpage").MozillaBugzilla;
                }

                callback(config);
            }
        }
    }).get();
}

/*
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; }

                if (isOurPage(window, config.matches)) {
                    try {
                        var curPage = new config.objConstructor(window, config);
                    } catch (ex) {
                        if (ex instanceof require("bzpage").NotLoggedinException) {
                            return ; // Bail out if the user is not logged in
                        } else {
                            throw ex; // rethrow the exception otherwise
                        }
                    }
                } else if (isOurPage(window, config.skipMatches)) {
                    skipThisPage(window.document);
                }
            }
        );
    });
};

*/
var messageHandler = exports.messageHandler = function messageHandler(worker, msg) {
    switch (msg.cmd) {
        case "LogMessage":
            console.log(msg.data);
            break;
        case "GetDuplicatorID":
            getDuplicatorID(msg.data.host, msg.data.bugID, function (msgObj) {
                worker.postMessage(msgObj);
            });
            break;
        case "GetPassword":
            getPassword(msg.data.login, msg.data.hostname, function (pass) {
                worker.postMessage(pass);
            });
            break;
        case "DeDeduplicateQueue":
            LoginData = {
                queue: msg.data.bugs,
                host: msg.data.hostname,
                login: msg.data.login,
                pass: msg.data.password,
                dupID: msg.data.duplicator,
                finalCallback: function (msgObj) {
                    worker.postMessage(msgObj);
                }
            };
            processReqQueue();
            break;
        case "testReady":
            // we ignore it here, interesting only in unit test
            break;
        default:
            console.error(msg.toSource());
    }
};

var contentScriptLibraries = {
    "bugzilla.redhat.com": [
        self.data.url("bzpage.js"),
        self.data.url("rhbzpage.js")
    ]
};

pageMod.PageMod({
    include: [
        "https://bugzilla.redhat.com/show_bug.cgi?id=*"
    ],
    contentScriptWhen: 'ready',
    // contentScriptFile: contentScriptLibraries["bugzilla.redhat.com"],
    onAttach: function onAttach(worker, msg) {
        console.log("worker: " + worker);
        worker.on('message', function (msg) {
            messageHandler(worker, msg);
        });
    }
});