aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2010-06-30 01:13:19 +0200
committerMatěj Cepl <mcepl@redhat.com>2010-06-30 01:13:19 +0200
commit10ba5aaadd5c4f4bc5a769c2778ef26f952bb5a7 (patch)
tree09761465da097e065c398d2805332d8b7447dcae /lib
parent984c66b9db41ccb8d338df993cf16cde2ff68068 (diff)
downloadbugzilla-triage-10ba5aaadd5c4f4bc5a769c2778ef26f952bb5a7.tar.gz
Use 'request' module instead of home brewed load{Text,JSON} and httpPOST
Diffstat (limited to 'lib')
-rw-r--r--lib/main.js71
-rw-r--r--lib/rhbzpage.js74
-rw-r--r--lib/util.js85
3 files changed, 84 insertions, 146 deletions
diff --git a/lib/main.js b/lib/main.js
index 3d59d6e..71e4dee 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -17,6 +17,7 @@ var myStorage = require("simple-storage").storage;
var browser = require("tab-browser");
var urlMod = require("url");
var selfMod = require("self");
+var Request = require("request").Request;
var preferences = require("preferences-service");
var BSTPrefNS = require("bzpage").BSTPrefNS;
// Use my JSON for now before it is fixed for general public
@@ -32,7 +33,6 @@ var config = {};
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 = {
@@ -76,7 +76,6 @@ function skipThisPage(doc) {
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,"");
@@ -87,48 +86,54 @@ function skipThisPage(doc) {
function initialize(callback) {
var prefName = BSTPrefNS+"JSONURL";
- var url = "";
+ var urlStr = "";
if (preferences.isSet(prefName)) {
- url = preferences.get(prefName);
+ urlStr = preferences.get(prefName);
} else {
- url = JSONURLDefault;
+ urlStr = JSONURLDefault;
preferences.set(prefName, JSONURLDefault);
}
- util.loadJSON(url, function(parsedData) {
- config.gJSONData = parsedData;
+ Request({
+ url: urlStr,
+ onComplete: function () {
+ config.gJSONData = this.response.json;
- 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;
- });
+ var keys = "", key = "";
+ for (key in config.gJSONData) {
+ keys += key + " ";
}
- };
+ console.log("loaded JSON object keys: " + keys);
- if (!myStorage.logs) {
- console.log("myStorage.logs empty!");
- myStorage.logs = {};
- }
+ // Get additional tables
+ 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] + ".");
+ Request({
+ url: URLsList[dwnldObj],
+ onComplete: function() {
+ config[dwnldObj] = this.response.json;
+ }
+ }).get();
+ }
+ };
- var logConstructor = logger.Logger;
- config.logger = new logConstructor(myStorage.logs,
- JSON.parse(selfMod.data.load("bugzillalabelAbbreviations.json")));
+ 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);
+ callback(config);
+ }
+ }).get();
}
exports.main = function main(options, callbacks) {
diff --git a/lib/rhbzpage.js b/lib/rhbzpage.js
index f61a88b..17b852e 100644
--- a/lib/rhbzpage.js
+++ b/lib/rhbzpage.js
@@ -5,11 +5,11 @@
var util = require("util");
var xrpc = require("xmlrpc");
var apiUtils = require("api-utils");
-var xhr = require("xhr");
var self = require("self");
var clip = require("clipboard");
var Color = require("color").Color;
var BZPage = require("bzpage").BZPage;
+var Request = require("request").Request;
var url = require("url");
var timer = require("timer");
var selection = require("selection");
@@ -263,14 +263,18 @@ RHBugzillaPage.prototype.pasteBacktraceInComments = function() {
attURL = "https://bugzilla.redhat.com/attachment.cgi?id="
+ x[1];
if (!this.btSnippet) {
- var btRaw = util.loadText(attURL, function(ret) {
- this.btSnippet = this.parseBacktrace(ret);
- if (this.btSnippet) {
- this.addStuffToTextBox("comment", this.btSnippet);
- this.addStuffToTextBox("status_whiteboard",
- "btparsed");
+ var that = this;
+ Request({
+ url: attURL,
+ onComplete: function() {
+ that.btSnippet = that.parseBacktrace(this.response.text);
+ if (that.btSnippet) {
+ that.addStuffToTextBox("comment", that.btSnippet);
+ that.addStuffToTextBox("status_whiteboard",
+ "btparsed");
+ }
}
- }, this);
+ }).get();
}
}, this);
}
@@ -506,27 +510,30 @@ RHBugzillaPage.prototype.fillInChipMagic = function () {
XorgLogAttID = this.XorgLogAttList[this.XorgLogAttListIndex][1];
attURL = "https://bugzilla.redhat.com/attachment.cgi?id="+XorgLogAttID;
- that = this;
+ var that = this;
// parse Xorg.0.log
- util.loadText(attURL, function(ret){
- var interestingLineArr = ret.split("\n").
- filter(function (v,i,a) {
- return that.RE.Chipset.test(v);
- });
- if (interestingLineArr.length >0) {
- // TODO we are parsing only the first found line; is it alright?
- interestingArray = that.RE.Chipset.exec(interestingLineArr[0]);
- interestingLine = interestingArray[2].
- replace(/[\s"]+/g," ").trim();
- // Persuade createNewButton to have mercy and to actually add
- // non-default button
- that.constantData.chipMagicTrigger = true;
- that.chipMagicInterestingLine = interestingLine+"\t"+interestingArray[1]
- .toUpperCase();
- that.createNewButton("status_whiteboard", true, "rh-xorg", "chipMagic");
+ Request({
+ url: attURL,
+ onComplete: function () {
+ var interestingLineArr = this.response.text.split("\n").
+ filter(function (v,i,a) {
+ return that.RE.Chipset.test(v);
+ });
+ if (interestingLineArr.length >0) {
+ // TODO we are parsing only the first found line; is it alright?
+ interestingArray = that.RE.Chipset.exec(interestingLineArr[0]);
+ interestingLine = interestingArray[2].
+ replace(/[\s"]+/g," ").trim();
+ // Persuade createNewButton to have mercy and to actually add
+ // non-default button
+ that.constantData.chipMagicTrigger = true;
+ that.chipMagicInterestingLine = interestingLine+"\t"+interestingArray[1]
+ .toUpperCase();
+ that.createNewButton("status_whiteboard", true, "rh-xorg", "chipMagic");
+ }
}
- });
+ }).get();
this.XorgLogAttListIndex++;
};
@@ -693,7 +700,7 @@ RHBugzillaPage.prototype.fixElement = function(elem, beforeText, accKey, afterTe
*/
RHBugzillaPage.prototype.getBugzillaName = function(URLhostname) {
var bugzillaID = "";
- var bzLabelNames = JSON.parse(self.data.load("bugzillalabelNames.json"));
+ var bzLabelNames = JSON.parse(self.data.load("bugzillalabelNames.json"));
if (bzLabelNames[URLhostname]) {
bugzillaID = bzLabelNames[URLhostname];
} else {
@@ -752,6 +759,7 @@ RHBugzillaPage.prototype.fixAttachById = function(id, type, email) {
email = false;
}
+ var that = this;
var msg = new xrpc.XMLRPCMessage("bugzilla.updateAttachMimeType");
msg.addParameter( {
'attach_id' : id,
@@ -764,8 +772,16 @@ RHBugzillaPage.prototype.fixAttachById = function(id, type, email) {
// https://bugzilla.redhat.com/\
// docs/en/html/api/extensions/compat_xmlrpc/code/webservice.html
// test on https://bugzilla.redhat.com/show_bug.cgi?id=485145
- util.httpPOST(this.XMLRPCurl, msg.xml(), this.fixingMIMECallBack,
- this, "text/xml", "text/xml");
+ Require({
+ url: this.XMLRPCurl,
+ onComplete: function() {
+ that.fixingMIMECallBack.call(that);
+ },
+ data: msg.xml(),
+ contentType: "text/xml",
+ });
+// util.httpPOST(this.XMLRPCurl, msg.xml(), this.fixingMIMECallBack,
+// this, "text/xml", "text/xml");
this.reqCounter++;
};
diff --git a/lib/util.js b/lib/util.js
index b2a093c..6348598 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -215,87 +215,4 @@ var getPassword = exports.getPassword = function getPassword() {
} else {
return undefined;
}
-};
-
-/**
- * Load text from URL
- *
- * @param URL String
- * @param cb_function Function to be called when the download happens with
- * the downloaded body of the HTTP message as the only parameter
- * @param what optional Object argument representing this for this call
- * @return none
- */
-var loadText = exports.loadText = function loadText(URL, cb_function, what) {
- if (what === undefined) { // missing optional argument
- what = this;
- }
-
- var req = new xhrMod.XMLHttpRequest();
- req.open("GET", URL, true);
- req.onreadystatechange = function (aEvt) {
- if (req.readyState === 4) {
- if (req.status === 200) {
- cb_function.call(what, req.responseText);
- } else {
- throw "Getting " + URL + "failed!";
- }
- }
- };
- req.send("");
-};
-
-/**
- * Load JSON from URL
- *
- * @param URL String
- * @param cb_function Function to be called when the download happens with
- * the downloaded JSON as the only parameter
- * @param what optional Object argument representing this for this call
- * @return none
- */
-exports.loadJSON = function loadJSON(URL, cb_function, what) {
- if (what === undefined) { // missing optional argument
- what = this;
- }
-
- loadText(URL, function (text) {
- var data = JSON.parse(text);
- cb_function.call(what, data);
- }, what);
-};
-
-/**
- * run HTTP POST request
- *
- * @param URL String with URL; required
- * @param data Object/String with data ; required
- * @param cb_function Function called when the request succeeds, with
- * the only parameter being request object ; required
- * @param what Object which will represent this for the cb_function ; optional
- * @param mimeData String with MIME type of data
- * @param mimeGet String with MIME type expected on return
- */
-exports.httpPOST = function httpPOST(URL, data, cb_function, what, mimeData, mimeGet) {
- what = what === undefined ? this : what;
- mimeData = mimeData === undefined ? "application/x-www-form-urlencoded" : mimeData;
- mimeGet = mimeGet === undefined ? "text/plain" : mimeGet;
-
- var req = new xhrMod.XMLHttpRequest();
- console.log("req = " + req.toSource());
- req.open("POST", URL, true);
-
- req.overrideMimeType(mimeGet);
- req.setRequestHeader("Content-type", mimeData);
- req.onreadystatechange = function(aEvt) {
- if (req.readyState === 4) {
- if (req.status === 200) {
- console.log("POST success!");
- cb_function.call(what, req);
- } else {
- console.error("POST failed!");
- }
- }
- };
- req.send(data);
-};
+}; \ No newline at end of file