diff options
-rw-r--r-- | data/bzpage.js | 104 | ||||
-rw-r--r-- | jsons/Config_data.json | 2 | ||||
-rw-r--r-- | lib/libbugzilla.js | 43 | ||||
-rw-r--r-- | lib/logger.js | 48 | ||||
-rw-r--r-- | lib/main.js | 47 | ||||
-rw-r--r-- | lib/util.js | 65 |
6 files changed, 219 insertions, 90 deletions
diff --git a/data/bzpage.js b/data/bzpage.js index 11b38c7..ced0181 100644 --- a/data/bzpage.js +++ b/data/bzpage.js @@ -30,7 +30,6 @@ function Message(cmd, data) { console.log("Message: cmd = " + cmd + ", data = " + data); this.cmd = cmd; this.data = data; - console.log("Message JSONified: " + JSON.stringify(this)); } function log(msg) { @@ -50,7 +49,6 @@ NotLoggedinException.prototype.toString = function () { * central handler processing messages from the main script. */ onMessage = function onMessage(msg) { - console.log("onMessage: msg = " + msg.toSource()); switch (msg.cmd) { case "ReloadThePage": document.location.reload(true); @@ -64,7 +62,7 @@ onMessage = function onMessage(msg) { break; case "CreateButtons": constantData = msg.data.constData; - generateButtons(msg.data.instPkgs); + generateButtons(msg.data.instPkgs, msg.data.kNodes); break; case "Error": alert("Error " + msg.data); @@ -191,6 +189,29 @@ function centralCommandDispatch (cmdLabel, cmdParams) { } /** + * remove elements from the page based on their IDs + * + * @param doc Document object + * @param target String/Array with ID(s) + * @param remove Boolean indicating whether the node should be + * actually removed or just hidden. + * @return none + * TODO remove parameter could be replaced by function which would + * do actual activity. + */ +function killNodes(doc, target, remove) { + var targetArr = target instanceof Array ? target : target.trim().split(/[,\s]+/); + targetArr.forEach(function(x) { + if (remove) { + var targetNode = doc.getElementById(x); + targetNode.parentNode.removeChild(targetNode); + } else { + x.style.display = "none"; + } + }); +} + +/** * Change assignee of the bug * * @param newAssignee String with the email address of new assigneeAElement @@ -370,10 +391,18 @@ function createNewButton (location, after, cmdObj) { /** * Generate button based on */ -function generateButtons (pkgs) { +function generateButtons (pkgs, kNodes) { var topRowPosition = "topRowPositionID"; var bottomRowPosition = "commit"; + setUpLogging(); + + // ========================================================= + if (kNodes && window.location.hostname in kNodes) { + var killConf = killNodes[window.location.hostname]; + killNodes(document, killConf[0], killConf[1]); + } + // create anchor for the top toolbar var commentBox = document.getElementById("comment"); var brElement = document.createElement("br"); @@ -386,10 +415,10 @@ function generateButtons (pkgs) { var cmdObj = pkgs[pkg][cmdIdx]; switch (cmdObj.position) { case "topRow": - createNewButton(topRowPosition, cmdObj, false); + createNewButton(topRowPosition, false, cmdObj); break; case "bottomRow": - createNewButton(bottomRowPosition, cmdObj, false); + createNewButton(bottomRowPosition, false, cmdObj); break; case "dropDown": addToCommentsDropdown(cmdObj); @@ -744,8 +773,27 @@ function getLogin () { return loginStr; } +function addLogRecord() { + var rec = {}; + rec.date = new Date(); + rec.url = document.location.toString(); + rec.title = document.title; + var comment = window.prompt( + "Enter comments for this comment"); + if (comment && comment.length > 0) { + comment = comment.trim(); + rec.comment = comment; + var dateStr = getISODate(rec.date); + var urlStr = window.location.hostname; + rec.key = dateStr + "+" + + urlStr + "+" + getBugNo(); + postMessage(new Message("AddLogRecord", rec)); + return rec; + } + return null; +} + /** - * TODO THIS IS COMPLETELY BROKEN AND NEED TO BE REWRITTEN */ function setUpLogging () { // Protection against double-call @@ -753,9 +801,6 @@ function setUpLogging () { return ; } - // TODO fix later - return ; // switched off for now - // For adding additional buttons to the top toolbar var additionalButtons = document.querySelector("#bugzilla-body *.related_actions"); var that = this; @@ -763,9 +808,7 @@ function setUpLogging () { // logging all submits for timesheet if (!submitHandlerInstalled) { document.forms.namedItem("changeform").addEventListener("submit",function (evt) { - // TODO this is probably another RPC call - var resp = that.log.addLogRecord(that); - if (resp === null) { + if (addLogRecord() === null) { evt.stopPropagation(); evt.preventDefault(); } @@ -773,27 +816,23 @@ function setUpLogging () { submitHandlerInstalled = true; } - // TODO another RPC call // (id, text, parent, callback, params, before, covered, accesskey) - this.createDeadLink("generateTSButton", "Generate TS", additionalButtons, + createDeadLink("generateTSButton", "Generate TS", additionalButtons, function(evt) { - that.log.createBlankPage.call(that.log, "TimeSheet", - that.log.generateTimeSheet); + postMessage(new Message("GenerateTS")); }, [], "dash", "li"); - // TODO another RPC call - this.createDeadLink("clearLogs", "Clear TS", additionalButtons, + createDeadLink("clearLogs", "Clear TS", additionalButtons, function(evt) { - that.log.clearStore(this); + postMessage(new Message("ClearTS")); }, [], "dash", "li"); - // TODO another RPC call - this.createDeadLink("importTSButton", "Import TS", additionalButtons, + createDeadLink("importTSButton", "Import TS", additionalButtons, function(evt) { - jsonPaths = prompts.promptFileOpenPicker(that.win); - that.log.importOtherStore(jsonPaths, clearLogAElem); + postMessage(new Message("ImportTS")); }, [], "dash", "li"); + /* TODO var clearLogAElem = document.getElementById("clearLogs"); if (this.log.isEmpty()) { clearLogAElem.style.color = this.log.EmptyLogsColor; @@ -802,6 +841,7 @@ function setUpLogging () { clearLogAElem.style.color = this.log.FullLogsColor; clearLogAElem.style.fontWeight = "bolder"; } + */ } function getSelection () { @@ -921,6 +961,22 @@ function startup() { // doing any harm to anybody. } + // Prepare for query buttons + // element ID brElementPlace_location is later used in JSON files + // Stay with this add_comment element even if RH BZ upgrades, this seems + // to be generally much more stable (even with other bugzillas, e.g. b.gnome.org) + // then some getElementById. + var commentArea = document.getElementsByName("add_comment")[0].parentNode; + if (commentArea) { + var brElementPlacer = commentArea.getElementsByTagName("br"); + brElementPlacer = brElementPlacer[0]; + if (brElementPlacer) { + brElementPlacer.setAttribute("id","brElementPlacer_location"); + brElementPlacer.parentNode.insertBefore(document.createElement("br"), + brElementPlacer); + } + } + // TODO Probably could be ignored ... used only once on line 973 of rhbzpage.js // if (parseAbrtBacktraces && this.RE.Abrt.test(this.title)) { // title = document.getElementById("short_desc_nonedit_display").textContent; diff --git a/jsons/Config_data.json b/jsons/Config_data.json index 8db7b27..01775f1 100644 --- a/jsons/Config_data.json +++ b/jsons/Config_data.json @@ -116,7 +116,7 @@ "https://bugzilla.gnome.org/show_bug.cgi.*" ], "enabledPackages":{ - "bugzilla.redhat.com":"all" + "bugzilla.redhat.com":"any" }, "killNodes":{ "bugzilla.gnome.org":[ diff --git a/lib/libbugzilla.js b/lib/libbugzilla.js index e074072..1c5f7a2 100644 --- a/lib/libbugzilla.js +++ b/lib/libbugzilla.js @@ -10,14 +10,14 @@ 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 JSONURLDefault = "https://fedorahosted.org/released"+ "/bugzilla-triage-scripts/Config_data.json"; var BTSPrefNS = "bugzilla-triage.setting."; var BTSPassRealm = "BTSXMLRPCPass"; -var config = {}; - function Message(cmd, data) { console.log("Message: cmd = " + cmd + ", data = " + data); this.cmd = cmd; @@ -122,20 +122,23 @@ exports.changeJSONURL = function changeJSONURL() { libbz.getInstalledPackages(msg.data, function (pkgsMsg) { worker.postMessage(pkgsMsg); */ -exports.getInstalledPackages = function getInstalledPackages(location, callback) { +exports.getInstalledPackages = function getInstalledPackages(location, config, callback) { var installedPackages = {}; var enabledPackages = []; - console.log("location = " + location.toString()); - console.log("location = " + typeof location); + if (typeof location == "string") { + location = new urlMod.URL(location); + } + + console.log("location = " + location); + console.log("typeof location = " + typeof location); // Collect enabled packages per hostname (plus default ones) - console.log("getInstalledPackages: config = " + config.toSource()); if (config.gJSONData && ("commentPackages" in config.gJSONData)) { if ("enabledPackages" in config.gJSONData.configData) { var epObject = config.gJSONData.configData.enabledPackages; - if (location.hostname in epObject) { - enabledPackages = enabledPackages.concat(epObject[location.hostname].split(/[,\s]+/)); + if (location.host in epObject) { + enabledPackages = enabledPackages.concat(epObject[location.host].split(/[,\s]+/)); } if ("any" in epObject) { enabledPackages = enabledPackages.concat(epObject.any.split(/[,\s]+/)); @@ -149,12 +152,9 @@ exports.getInstalledPackages = function getInstalledPackages(location, callback) } } - console.log("getInstalledPackages: enabledPackages = " + enabledPackages.toSource()); - // 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) { - console.log("getInstalledPackages: pkg = " + pkg); if (pkg in config.gJSONData.commentPackages) { installedPackages[pkg] = config.gJSONData.commentPackages[pkg]; } @@ -162,23 +162,21 @@ exports.getInstalledPackages = function getInstalledPackages(location, callback) } // Expand commentIdx properties into full comments - console.log("before // installedPackages = " + installedPackages.toSource()); var cmdObj = {}; for (var pkgKey in installedPackages) { for (var cmdObjKey in installedPackages[pkgKey]) { cmdObj = installedPackages[pkgKey][cmdObjKey]; if ("commentIdx" in cmdObj) { - cmdObj.comment = config.commentStrings[cmdObj.commentIdx]; + cmdObj.comment = config.gJSONData.commentStrings[cmdObj.commentIdx]; delete cmdObj.commentIdx; } } } - console.log("after // installedPackages = " + installedPackages.toSource()); - callback(new Message("CreateButtons", { instPkgs: installedPackages, - constData: config.constantData + constData: config.constantData, + kNodes: config.gJSONData.configData.killNodes })); }; @@ -199,7 +197,7 @@ exports.openURLinNewTab = function openURLinNewTab(urlStr) { }); }; -function initialize() { +exports.initialize = function initialize(config, callback) { var prefName = BTSPrefNS+"JSONURL"; var urlStr = ""; @@ -239,7 +237,7 @@ function initialize() { }); } - config.logger = new logger.Logger(JSON.parse(self.data.load("bugzillalabelAbbreviations.json"))); + // 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) { @@ -280,16 +278,9 @@ function initialize() { if ("submitsLogging" in config.gJSONData.configData && config.gJSONData.configData.submitsLogging) { - config.log = config.logger; + // config.log = config.logger; // FIXME this.setUpLogging(); } - - if ("killNodes" in config.gJSONData.configData && - window.location.hostname in config.gJSONData.configData.killNodes) { - var killConf = config.gJSONData.configData.killNodes[window.location.hostname]; - killNodes(config.doc, killConf[0], killConf[1]); - } - } callback(); } diff --git a/lib/logger.js b/lib/logger.js index cbe670d..659faac 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -10,6 +10,29 @@ var apiUtils = require("api-utils"); var xrpc = require("xmlrpc"); var myStorage = require("simple-storage"); +exports.addLogRecord = function addLogRecord(rec) { + if (myStorage.storage.logs[rec.key]) { + myStorage.storage.logs[rec.key].comment += "<br/>\n" + comment; + } else { + myStorage.storage.logs[rec.key] = rec; + } +}; + +exports.generateTimeSheet = function generateTimeSheet() { + ; // FIXME +}; + +exports.clearTimeSheet = function clearTimeSheet() { + ; // FIXME +}; + +exports.importTimeSheet = function importTimeSheet() { + ; /* FIXME + jsonPaths = prompts.promptFileOpenPicker(that.win); + that.log.importOtherStore(jsonPaths, clearLogAElem); + */ +}; + var Logger = exports.Logger = function Logger(abbsMap) { this.EmptyLogsColor = rgb(0, 255, 0); this.FullLogsColor = rgb(0, 40, 103); @@ -59,31 +82,6 @@ Logger.prototype.importOtherStore = function importOtherStore (filename, clearLi } }; -Logger.prototype.addLogRecord = function addLogRecord(page) { - var rec = {}; - rec.date = new Date(); - rec.url = page.doc.location.toString(); - rec.title = page.title; - var comment = prompts.prompt( - "Enter comments for this comment"); - if (comment && comment.length > 0) { - comment = comment.trim(); - rec.comment = comment; - var dateStr = utilMod.getISODate(rec.date); - var urlStr = urlMod.URL(rec.url).host; - var recKey = dateStr + "+" - + urlStr - + "+" + page.bugNo; - - if (myStorage.storage.logs[recKey]) { - myStorage.storage.logs[recKey].comment += "<br/>\n" + comment; - } else { - myStorage.storage.logs[recKey] = rec; - } - } - return comment; -}; - Logger.prototype.getBugzillaAbbr = function(url) { // for https://bugzilla.redhat.com/show_bug.cgi?id=579123 get RH // for https://bugzilla.mozilla.org/show_bug.cgi?id=579123 get MoFo diff --git a/lib/main.js b/lib/main.js index 0afa67d..bf34514 100644 --- a/lib/main.js +++ b/lib/main.js @@ -16,6 +16,10 @@ var self = require("self"); var Request = require("request").Request; var pageMod = require("page-mod"); var libbz = require("libbugzilla"); +var tabs = require("tabs"); +var logger = require("logger"); + +var config = {}; function isOurPage(window, matchingURLs) { var url = window.location.href; @@ -50,10 +54,22 @@ var messageHandler = exports.messageHandler = function messageHandler(worker, ms case "ExecCmd": libbz.executeCommand(msg.data); break; + case "AddLogRecord": + logger.addLogRecord(msg.data); + break; + case "GenerateTS": + logger.generateTimeSheet(); + break; + case "ClearTS": + logger.clearTimeSheet(); + break; + case "ImportTS": + logger.importTimeSheet(); + break; case "GetInstalledPackages": // send message with packages back console.log("msg = " + msg.toSource()); - libbz.getInstalledPackages(msg.data, function (pkgsMsg) { + libbz.getInstalledPackages(msg.data, config, function (pkgsMsg) { worker.postMessage(pkgsMsg); }); break; @@ -85,17 +101,20 @@ var contentScriptLibraries = { ] }; -libbz.initialize(); -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); - }); - } +libbz.initialize(config, function () { + 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); + }); + } + }); }); + +tabs.open("https://bugzilla.redhat.com/show_bug.cgi?id=673153"); diff --git a/lib/util.js b/lib/util.js new file mode 100644 index 0000000..8556951 --- /dev/null +++ b/lib/util.js @@ -0,0 +1,65 @@ +/*global exports: false, require: false, console: false, Cc: false, Ci: false */ +/*jslint onevar: false */ +// Released under the MIT/X11 license +// http://www.opensource.org/licenses/mit-license.php +"use strict"; +// ============================================================== +var Cc = require("chrome").Cc; +var Ci = require("chrome").Ci; +var urlMod = require("url"); + +/** + * get parameters of URL as an object (name, value) + */ +function getParamsFromURL (url, base) { + if (!url || (url.toString().length === 0)) { + throw new Error("Missing URL value!"); + } + + if (!(url instanceof urlMod.URL)) { + url = new urlMod.URL(url.toString(), base); + } + + var paramsArr = url.path.split("?"); + if (paramsArr.length === 1) { + return {}; + } + + // get convert URL parameters to an Object + var params = {}, s = []; + paramsArr[1].split('&').forEach(function(par) { + s = par.split('='); + params[s[0]] = s[1]; + }); + return params; +} + +/** + * Get a bug no from URL ... fails with aliases + * It should theoretically belong to bzpage.js, but we don't have + * unit tests there yet, so keeping here. + * + * @param url String with URL to be analyzed + * @return String with the bug ID (hopefully number, but not for aliases) + */ +exports.getBugNo = function getBugNo(url) { + var params = getParamsFromURL(url); + if (params && params.id) { + return params.id; + } +}; + +/** + * format date to be in ISO format (just day part) + * + * @param date + * @return string with the formatted date + */ +exports.getISODate = function getISODate(dateStr) { + function pad(n) { + return n < 10 ? '0' + n : n; + } + var date = new Date(dateStr); + return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + + pad(date.getDate()); +}; |