From 0a30640ddb3033f4ac937e8cc8ffd2c4d3f4ad6c Mon Sep 17 00:00:00 2001 From: Wil Moore III Date: Wed, 29 Jan 2014 02:32:34 -0700 Subject: [PATCH 1/3] Correct "Build Now" and "Build with Parameters" buttons. Attempts to fix #5 by: - Adding a `getButtonInfo` function which returns normalized button key/values (i.e. innerHTML, href, hasParams?) - Follows same approach as links in that there is a simple `onclick` vs. `jQuery.click`. - Delegates to the Jenkins intrinsic `Ajax.Request` for dispatching request. - Uses `hoverNotification` --- doony.js | 61 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/doony.js b/doony.js index 29e3e55..5dfef30 100644 --- a/doony.js +++ b/doony.js @@ -356,6 +356,24 @@ return hash; }; + var getButtonInfo = function(path) { + var links = Array.prototype.slice.call(document.querySelectorAll('a.task-link')); + var out = {}; + var idx = -1 + var end = links.length; + + while (++idx < end) { + if (links[idx].innerText.match(/Build with Parameters|Build Now/i)) { + out.innerHTML = links[idx].innerText; + out.href = links[idx].href; + out.hasParams = links[idx].innerText === 'Build with Parameters'; + break; + } + }; + + return out; + }; + var isJobPage = function(path) { return path.match(/^\/job\/.*?\//) !== null; }; @@ -591,37 +609,22 @@ } if (isJobPage(window.location.pathname)) { + var buttonInfo = getButtonInfo(); var button = document.createElement('button'); + button.className = "btn btn-primary doony-build"; - button.innerHTML = "Build Now"; - $(button).click(function() { - var jobUrl = getRootJobUrl(window.location.pathname); - // The build post endpoint doesn't tell you the number of the next - // build, so get it before we create a build. - $.getJSON(jobUrl + 'api/json?depth=1&tree=nextBuildNumber,lastBuild[building]', function(data) { - $.post(jobUrl + 'build', function() { - // in case there's an immediate redirect, don't show the - // bar. - var message = "Build #" + data.nextBuildNumber + " created, you will be redirected when it is ready."; - if (JSON.stringify(data) !== "{}" && - 'lastBuild' in data && - data.lastBuild !== null && - data.lastBuild.building - ) { - message += " Cancel the current build"; - } - showButterBar(message, Alert.WARNING); - redirectToNewJobConsole(getJobUrl(window.location.pathname), - data.nextBuildNumber); - }).fail(function(jqXHR, textStatus, errorThrown) { - if (jqXHR.status === 403) { - showButterBar("Cannot create build. Maybe you need to log in or have the 'build' permission.", Alert.ERROR); - } else { - showButterBar("An error occured. Please try again.", Alert.ERROR); - } - }); - }); - }); + button.innerHTML = buttonInfo.innerHTML; + + button.onclick = function() { + if (buttonInfo.hasParams) { + window.location.href = buttonInfo.href; + return false; + } + + new Ajax.Request(buttonInfo.href); + hoverNotification('Build scheduled', button, -100); + return false; + }; $(document).on('click', '#doony-clear-build', function(e) { e.preventDefault(); From aa8d59f539b0f242eefc60600d4f9847f1f912b4 Mon Sep 17 00:00:00 2001 From: Wil Moore III Date: Thu, 30 Jan 2014 10:51:50 -0700 Subject: [PATCH 2/3] Refactor build button to honor security policy. --- doony.js | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/doony.js b/doony.js index 5dfef30..4130844 100644 --- a/doony.js +++ b/doony.js @@ -356,7 +356,7 @@ return hash; }; - var getButtonInfo = function(path) { + var getButtonInfo = function() { var links = Array.prototype.slice.call(document.querySelectorAll('a.task-link')); var out = {}; var idx = -1 @@ -374,6 +374,31 @@ return out; }; + var createBuildButton = function() { + var buttonInfo = getButtonInfo(); + var button; + + if (buttonInfo.innerHTML) { + button = document.createElement('button'); + + button.className = "btn btn-primary doony-build"; + button.innerHTML = buttonInfo.innerHTML; + + button.onclick = function() { + if (buttonInfo.hasParams) { + window.location.href = buttonInfo.href; + return false; + } + + new Ajax.Request(buttonInfo.href); + hoverNotification('Build scheduled', button, -100); + return false; + }; + } + + return button; + }; + var isJobPage = function(path) { return path.match(/^\/job\/.*?\//) !== null; }; @@ -609,22 +634,7 @@ } if (isJobPage(window.location.pathname)) { - var buttonInfo = getButtonInfo(); - var button = document.createElement('button'); - - button.className = "btn btn-primary doony-build"; - button.innerHTML = buttonInfo.innerHTML; - - button.onclick = function() { - if (buttonInfo.hasParams) { - window.location.href = buttonInfo.href; - return false; - } - - new Ajax.Request(buttonInfo.href); - hoverNotification('Build scheduled', button, -100); - return false; - }; + var button = createBuildButton(); $(document).on('click', '#doony-clear-build', function(e) { e.preventDefault(); @@ -635,11 +645,12 @@ }); var title = $("#main-panel h1").first(); + if (title.children("div").length) { - title.append(button); + button && title.append(button); } else { title.css('display', 'inline-block'); - title.after(button); + button && title.after(button); } } From eb927e7eb3b6e31cbe52542b9da48e21649df241 Mon Sep 17 00:00:00 2001 From: Wil Moore III Date: Wed, 4 Jun 2014 12:06:41 -0600 Subject: [PATCH 3/3] switch to jQuery for getButtonInfo function --- doony.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/doony.js b/doony.js index 4130844..be72e78 100644 --- a/doony.js +++ b/doony.js @@ -357,21 +357,22 @@ }; var getButtonInfo = function() { - var links = Array.prototype.slice.call(document.querySelectorAll('a.task-link')); - var out = {}; - var idx = -1 - var end = links.length; - - while (++idx < end) { - if (links[idx].innerText.match(/Build with Parameters|Build Now/i)) { - out.innerHTML = links[idx].innerText; - out.href = links[idx].href; - out.hasParams = links[idx].innerText === 'Build with Parameters'; - break; - } - }; - - return out; + var buttonInfo = {}; + + $("a.task-link").each(function(index) { + var link = $(this); + + if (!link.text().match(/Build with Parameters|Build Now/i)) { + return true; + } + + buttonInfo.innerHTML = link.text(); + buttonInfo.href = link.attr("href"); + buttonInfo.hasParams = link.text() === "Build with Parameters"; + return false; + }); + + return buttonInfo; }; var createBuildButton = function() {