aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libbugzilla.js
blob: 03ae6ebf4dc8706eb8aaab2c2a1dc078e582ac49 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php

"use strict";
var preferences = require("preferences-service");
var prompts = require("prompts");
var clipboard = require("clipboard");
var tabs = require("tabs");
var logger = require("logger");
var passUtils = require("passwords");
var Request = require("request").Request;
var selfMod = require("self");
var urlMod = require("url");
var xhrMod = require("xhr");
var panelMod = require("panel");
var myStorage = require("simple-storage");

var JSONURLDefault = "https://fedorahosted.org/released"+
"/bugzilla-triage-scripts/Config_data.json";
var BTSPrefNS = "bugzilla-triage.setting.";
var BTSPassRealm = "BTSXMLRPCPass";
var copiedAttributes = [ "queryButton", "upstreamButton", "parseAbrtBacktraces",
                         "submitsLogging", "XorgLogAnalysis", "objectStyle", "signature",
                         "suspiciousComponents", "verboseInlineHistory" ];


var config = exports.config = {};
var debugOption = null;

function Message(cmd, data) {
  debug("Message: cmd = " + cmd + ", data = " + data);
  this.cmd = cmd;
  this.data = data;
}

function debug(str) {
  if (debugOption) {
    console.log(str);
  }
}

/**
 * parse XML object out of string working around various bugs in Gecko
 * implementation see https://developer.mozilla.org/en/E4X for more information
 *
 * @param inStr
 *          String with unparsed XML string
 * @return XML object
 */
function parseXMLfromString (inStuff) {
  // if (typeof inStuff !== 'string') In future we should recognize
  // this.response
  // and get just .text property out of it. TODO
  var respStr = inStuff.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, ""); // bug
  // 336551
  return new XML(respStr);
}

/**
 * In case URL contains alias, not the real bug number, get the real bug no from
 * the XML representation. Sets correct value to this.bugNo.
 *
 * This is a slow variant for bugs other than actual window
 */
function getRealBugNoSlow(bugNo, location, callback) {
  debug("We have to deal with bug aliased as " + this.bugNo);
  // https://bugzilla.redhat.com/show_bug.cgi?ctype=xml&id=serialWacom
  Request({
    url: location.href+"&ctype=xml",
    onComplete: function(response) {
      if (response.status === 200) {
        var xmlRepr = parseXMLfromString(response.text);
        // TODO this probably wrong, both XPath and .text attribute
        var bugID = parseInt(xmlRepr.bug.bug_id.text, 10);
        if (isNaN(bugID)) {
          throw new Error("Cannot get bug no. even from XML representation!");
        }
        debug("The real bug no. is " + bugID);
        callback(bugID)
      }
    }
  }).get();
}

function getPassword(login, domain, callback) {
  var passPrompt = "Enter your Bugzilla password " +
    "for accessing JSONRPC services";
  var switchPrompt = "Do you want to switch off XML-RPC " +
    "for domain ";
  var prefName = BTSPrefNS+"withoutPassowrd", prefValue = [];
  var retObject = {
      password:  null, // password string or null if no password provided
      withoutPass: [] // whether user doesn't want to use password at all
  };

  if (preferences.has(prefName)) {
    prefValue = JSON.parse(preferences.get(prefName, null));
    debug("getPassword: prefValue = " + prefValue);
    if ((prefValue === true) || (prefValue === false)) {
      console.log("Clearing previous scheme of " + prefName +
        " preference " + prefValue + ".");
      preferences.set(prefName, JSON.stringify([]));
      prefValue = [];
    }
  }

  if (prefValue.indexOf(domain) == -1) {
    passUtils.search({
      username: login,
      url: domain,
      realm: BTSPassRealm,
      onComplete: function onComplete([credential]) {
        if (credential) {
          // We found the password, just go ahead and use it
          retObject.password = credential.password;
          callback(retObject);
        }
        else {
          // We don't have a stored password, ask for one
          var passwordText = prompts.promptPassword(passPrompt);
          if (passwordText && passwordText.length > 0) {
            // Right, we've got it … store it and then use it.
            retObject.password = passwordText;
            passUtils.store({
              username: login,
              password: passwordText,
              url: domain,
              realm: BTSPassRealm,
              onComplete: function onComplete() {
                callback(retObject);
              }
            });
          }
          else {
            // We don't have password, and user haven't entered one?
            // Does he want to live passwordless for this domain?
            var switchOff = prompts.promptYesNoCancel(switchPrompt + domain + "?");
            if (switchOff) {
              prefValue.push(domain);
              preferences.set(prefName, JSON.stringify(prefValue));
            }
            retObject.withoutPass = prefValue;
            callback(retObject);
          }
        }
      }
    });
  }
}

// Change URL of the configuration JSON file
exports.changeJSONURL = function changeJSONURL() {
  var prfNm = BTSPrefNS+"JSONURL";
  var url = preferences.get(prfNm, JSONURLDefault);

  var reply = prompts.prompt("New location of JSON configuration file", url);
  if (reply && (reply != url)) {
    preferences.set(prfNm, reply.trim());
    // TODO Restartless add-on needs to resolve this.
    prompts.alert("For now, you should really restart Firefox!");
  }
};

/**
 *
 * libbz.getInstalledPackages(msg.data, function (pkgsMsg) {
 * worker.postMessage(pkgsMsg);
 *
 * locationLoginObj: { location: window.location.href, login: getLogin() }
 */
exports.getInstalledPackages = function getInstalledPackages(locationLoginObj, callback) {
  var installedPackages = {};
  var enabledPackages = [];
  var location = locationLoginObj.location;

  if (typeof location == "string") {
    location = new urlMod.URL(location);
  }

  // Collect enabled packages per hostname (plus default ones)
  if (config.gJSONData && ("commentPackages" in config.gJSONData)) {
    if ("enabledPackages" in config.gJSONData.configData) {
      var epObject = config.gJSONData.configData.enabledPackages;
      if (location.host in epObject) {
        enabledPackages = enabledPackages.concat(epObject[location.host].split(/[,\s]+/));
      }
      if ("any" in epObject) {
        enabledPackages = enabledPackages.concat(epObject.any.split(/[,\s]+/));
      }
    }

    var allIdx = null;
    if ((allIdx = enabledPackages.indexOf("all")) != -1) {
      enabledPackages.splice(allIdx, 1);
      enabledPackages = enabledPackages.concat(Object.keys(config.gJSONData.commentPackages));
    }

    // TODO To be decided, whether we cannot just eliminate packages in
    // installedPackages and having it just as a plain list of all cmdObjects.
    enabledPackages.forEach(function (pkg, idx, arr) {
      if (pkg in config.gJSONData.commentPackages) {
        installedPackages[pkg] = config.gJSONData.commentPackages[pkg];
      }
    });
  }

  // Expand commentIdx properties into full comments
  var cmdObj = {};
  for (var pkgKey in installedPackages) {
    for (var cmdObjKey in installedPackages[pkgKey]) {
      cmdObj = installedPackages[pkgKey][cmdObjKey];
      if ("commentIdx" in cmdObj) {
        cmdObj.comment = config.gJSONData.commentStrings[cmdObj.commentIdx];
        delete cmdObj.commentIdx;
      }
    }
  }

  if (config.gJSONData.commentStrings &&
      "sentUpstreamString" in config.gJSONData.commentStrings) {
    config.constantData.commentStrings = {};
    config.constantData.commentStrings.sentUpstreamString =
      config.gJSONData.commentStrings["sentUpstreamString"];
  }

  var locURL = new urlMod.URL(locationLoginObj.location);
  var passDomain = locURL.scheme + "://" + locURL.host;
  getPassword(locationLoginObj.login, passDomain, function (passwObj) {
    // In order to avoid sending whole password to the content script,
    // we are sending just these two Booleans.
    config.constantData.passwordState = {
      passAvailable: (passwObj.password !== null),
      withoutPass: passwObj.withoutPass.indexOf(passDomain) === -1
    };

    callback(new Message("CreateButtons", {
      instPkgs: installedPackages,
      constData: config.constantData,
      config: config.configData,
      kNodes: config.gJSONData.configData.killNodes
    }));
  });
};

exports.getClipboard = function getClipboard(cb) {
  cb(clipboard.get());
};

exports.setClipboard = function setClipboard(stuff) {
  clipboard.set(stuff, "text");
};

var openURLInNewPanel = exports.openURLInNewPanel = function openURLInNewPanel(url) {
  var panel = panelMod.Panel({
    contentURL: url,
    width: 704,
    height: 768
  });
  panel.show();
};

var openURLInNewTab = exports.openURLInNewTab = function openURLInNewTab(url) {
  tabs.open({
    url: url,
    inBackground: true,
    onReady: function(t) {
      t.activate();
    }
  });
};

exports.createUpstreamBug = function createUpstreamBug(urlStr, subjectStr, commentStr) {
  var payload = JSON.stringify({
    subject: subjectStr,
    comment: commentStr
  });
  tabs.open({
    url: urlStr,
    inBackground: true,
    onReady: function (tab) {
      tab.attach({
        contentScriptFile: selfMod.data.url("internalMods/createBugElsewhere.js"),
        contentScript: "fillTheForm(" + payload + ");",
        onMessage: function(str) {
          tab.activate();
        }
      });

    }
  });
};


function processPageModeREs() {
  var confD = config.configData;
  ['bugPageMatch', 'skipMatch'].forEach(function (key) {
    confD[key] = confD[key+"Str"].map(function (REStr) {
      return new RegExp(REStr);
    });
  });
}


function loginToAllBugzillas(callback) {
  var loginCallsCounter = 0, bugzilla = "";

  // This is not a good place to run this, but I do not have currently
  // better place where all execution paths in this module meets in the
  // end. TODO Fix this.
  processPageModeREs();

  if ("enabledPackages" in config.gJSONData.configData) {
    // For all bugzillas we are interested in ...
    for (var bugzillaHost in config.gJSONData.configData.enabledPackages) {
      passUtils.search({
        url: "https://" + bugzillaHost,
        realm: BTSPassRealm,
        // ... and for which we have credentials on file ...
        onComplete: function onComplete(credentials) {
          // (we can have more than one set of credentials per bugzilla;
          // well, theoretically)
          debug("loginToAllBugzillas: credentials found:\n" +
            credentials.toSource());
          credentials.forEach(function(credential) {
            // ... login!
            makeJSONRPCCall(credential.url + "/jsonrpc.cgi",
              "User.login", {
                login: credential.username,
                password: credential.password,
                remember: false
              }, function(logResult) {
                debug("Logging as " + credential.username + " to " + credential.url);
                loginCallsCounter--;
                // When we complete all logins, execute the callback
                if (loginCallsCounter <= 0) {
                  debug("All logins done!");
                  callback(config);
                }
            });
            // Increment call counter
            loginCallsCounter++;
          });
          if (loginCallsCounter == 0) {
            debug("No credentials!");
            callback(config);
          }
        },
        onError: function onError() {
          console.error("No credentials were found for " + bugzillaHost + "!");
        }
      });
    }
  }
}

// Make a JSONL-RPC call ... most of the business logic should stay in the
// content script
// http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
var makeJSONRPCCall = exports.makeJSONRPCCall = function makeJSONRPCCall(url, method, params, callback) {

  var msg = {
      "version": "1.1",
      "method": method,
      "params": params
  };

  debug("makeJSONRPCCall: out = " + JSON.stringify(msg));

  Request({
    url: url,
    onComplete: function(response) {
      if (response.status == 200) {
        debug("makeJSONRPCCall: in = " + response.text);
        if ("error" in response.json) {
          throw new Error("Error in JSON-RPC call:\n" + response.json.error);
        }
        callback(response.json.result);
      }
    },
    content: JSON.stringify(msg),
    contentType: "application/json"
  }).post();
};

/**
 * Preprocess JSON into config data structure.
 *
 * Should be completely side-effects free pure function.
 */
function processConfigJSON(rawJSON) {
  var config = {};
  config.gJSONData = rawJSON;
  var origConstData = config.gJSONData.configData;

  // Get additional tables
  if ("downloadJSON" in config.gJSONData.configData) {
    var URLsList = config.gJSONData.configData.downloadJSON;
    if (!config.constantData) {
      config.constantData = {};
    }
    URLsList.forEach(function (arr) {
      var title = arr[0];
      var url = arr[1];
      Request({
        url: url,
        onComplete: function(response) {
          if (response.status == 200) {
            config.constantData[title] = response.json;
          } else {
            console.error("Cannot download " + title + " from URL " + url);
          }
        }
      }).get();
    });
  }

  config.configData = {};

  // should we spit out a lot of debugging output
  var prefDebugName = BTSPrefNS+"debug";
  debugOption = preferences.get(prefDebugName, false);
  preferences.set(prefDebugName, debugOption);
  config.configData.debuggingVerbose = debugOption;

  // Include properties for the main PageMod and for the skip-process-page one.
  const skippingURLParts = [
    "process_bug.cgi",
    "post_bug.cgi",
    "attachment.cgi"
  ];
  config.configData.pageModIncludeRE = origConstData.pageModIncludeRE;
  if ("matches" in origConstData) {
    if (config.configData.pageModIncludeRE) {
      config.configData.bugPageMatchStr = origConstData.matches;
      config.configData.skipMatchStr = origConstData.matches.
        map(function(x) {
          return x.replace("show_bug.cgi\\?id=.*",
            "(process_bug|post_bug|attachment).cgi$");
        });
    }
    else {
      config.configData.bugPageMatchStr = origConstData.matches;
      config.configData.skipMatchStr = [];
      origConstData.matches.
        forEach(function(x) {
          skippingURLParts.forEach(function (part) {
            config.configData.skipMatchStr.push(x.
              replace("show_bug.cgi.*", part + ".cgi"));
          });
        });
    }
  }

  config.constantData = {};
  if ("constantData" in config.gJSONData) {
    config.constantData = config.gJSONData.constantData;
    config.constantData.queryUpstreamBug = JSON.parse(
        selfMod.data.load("queryUpstreamBug.json"));
    config.constantData.bugzillaLabelNames =
      JSON.parse(selfMod.data.load("bugzillalabelNames.json"));
    config.constantData.newUpstreamBug =
      JSON.parse(selfMod.data.load("newUpstreamBug.json"));
    config.constantData.ProfessionalProducts =
      JSON.parse(selfMod.data.load("professionalProducts.json"));
    config.constantData.BugzillaAbbreviations =
     JSON.parse(selfMod.data.load("bugzillalabelAbbreviations.json"));
  }

  if ("CCmaintainer" in config.constantData) {
    config.configData.defBugzillaMaintainerArr = config.constantData.CCmaintainer;
  }

  copiedAttributes.forEach(function (attrib) {
    if (attrib in origConstData) {
      config.configData[attrib] = origConstData[attrib];
    }
  });

  return(config);
}

function ConfigurationLoadError(msg) {
  this.name = "ConfigurationLoadError";
  this.message = msg || "Cannot load configuration!";
}
ConfigurationLoadError.prototype = new Error();
ConfigurationLoadError.constructor = ConfigurationLoadError;


function fetchConfigurationJSON(url, callback) {
  debug("Fetching configuration JSON from " + url);
  var retValue = null;
  Request({
    url: url,
    onComplete: function (response) {
      if (response.status == 200) {
        config = processConfigJSON(response.json);
        debug("Loaded configuration: " + config);
      }
      else {
        throw new ConfigurationLoadError("Cannot load configuration from " + url);
      }
      if ("submitsLogging" in config.gJSONData.configData &&
        config.gJSONData.configData.submitsLogging) {
          logger.initialize();
      }
      loginToAllBugzillas(callback);
    }
  }).get();
};

exports.initialize = function initialize(callback) {
  var prefJSONURLName = BTSPrefNS+"JSONURL";
  var urlStr = preferences.get(prefJSONURLName, JSONURLDefault);
  preferences.set(prefJSONURLName, urlStr);

  debug("Starting initialize!");
  fetchConfigurationJSON(urlStr, callback);
}

//vim: set ts=2 et sw=2 textwidth=80: