aboutsummaryrefslogtreecommitdiffstats
path: root/lib/main.js
blob: c5df73dc22eebf81e28a6cfebc823bd8d14fcc0a (plain) (blame)
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
/*jslint rhino: true, forin: true, 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 selfMod = require("self");
var Request = require("request").Request;
var preferences = require("preferences-service");
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 (){
    var matchesAll = JSON.parse(selfMod.data.load("matches.json"));
    config.matches = matchesAll.matches;
    config.skipMatches = matchesAll.skipMatches;
})();

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 = doc.location.hostname;
    if (REArr) {
        var bugNo = REArr[1];
        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 = 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 () {
            config.gJSONData = this.response.json;

            var keys = "", key = "";
            for (key in config.gJSONData) {
                keys += key + " ";
            }

            // 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() {
                            config[title] = this.response.json;
                        }
                    }).get();
                }, this);
            }

            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);
        }
    }).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; }

                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);
                }
            }
        );
    });
};