Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct "Build Now" and "Build with Parameters" buttons. #53

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 48 additions & 33 deletions doony.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,50 @@
return hash;
};

var getButtonInfo = function() {
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() {
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a slight note that this could fail to be initialized if a "Build Now" button is not found and that could cause consequences down the line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit that I've only tested a few usage scenarios so I may be missing some edge cases. That being said, this does only get called for pages that pass the test isJobPage(window.location.pathname). Is this safe enough or do we want to be more cautious?

};

var isJobPage = function(path) {
return path.match(/^\/job\/.*?\//) !== null;
};
Expand Down Expand Up @@ -591,37 +635,7 @@
}

if (isJobPage(window.location.pathname)) {
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 += " <a href='#' id='doony-clear-build'>Cancel the current build</a>";
}
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);
}
});
});
});
var button = createBuildButton();

$(document).on('click', '#doony-clear-build', function(e) {
e.preventDefault();
Expand All @@ -632,11 +646,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);
}
}

Expand Down