woodpecker/static/scripts/build.js

169 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-09-30 01:21:17 +00:00
function JobViewModel(repo, build, job, status) {
var self = this;
self.status = status;
2015-09-30 06:11:27 +00:00
self.stream = function() {
$( "#output" ).html("");
$("#restart").hide();
$("#cancel").show();
2015-09-30 06:11:27 +00:00
var buf = new Drone.Buffer();
buf.start(document.getElementById("output"));
$( "#tail" ).show();
$( "#tail" ).click(function() {
buf.autoFollow = !buf.autoFollow;
if (buf.autoFollow) {
$( "#tail i" ).text("pause");
$( "#tail" ).show();
// scroll to the bottom of the page
window.scrollTo(0, document.body.scrollHeight);
} else {
$( "#tail i" ).text("expand_more");
$( "#tail" ).show();
}
})
Stream(repo, build, job, function(out){
buf.write(out);
});
};
2015-09-30 01:21:17 +00:00
if (status !== "running" && status !== "pending") {
Logs(repo, build, job);
$("#restart").show();
2015-09-30 01:21:17 +00:00
}
if (status === "running") {
2015-09-30 06:11:27 +00:00
self.stream();
2015-09-30 01:21:17 +00:00
}
$("#restart").click(function() {
$("#restart").hide();
$("#output").html("");
$(".status").attr("class", "status pending").text("pending");
$.ajax({
url: "/api/repos/"+repo+"/builds/"+build,
type: "POST",
success: function( data ) { },
error: function( data ) {
console.log(data);
}
});
})
$("#cancel").click(function() {
$("#cancel").hide();
$.ajax({
url: "/api/repos/"+repo+"/builds/"+build+"/"+job,
type: "DELETE",
success: function( data ) { },
error: function( data ) {
console.log(data);
}
});
})
2015-09-30 01:21:17 +00:00
Subscribe(repo, function(data){
if (!data.jobs) {
return;
}
if (data.number !== build) {
return;
}
2015-09-30 01:21:17 +00:00
var before = self.status;
self.status = data.jobs[job-1].status;
// update the status for each job in the view
for (var i=0;i<data.jobs.length;i++) {
var job_ = data.jobs[i];
var el = $("[data-job="+job_.number+"]");
el.find(".status")
2015-09-30 01:21:17 +00:00
.attr("class", "status "+job_.status).text(job_.status);
switch (job_.status) {
case "running":
el.find(".msg-running").find("span").attr("data-livestamp", job_.started_at);
el.find(".msg-pending").hide();
el.find(".msg-running").show();
el.find(".msg-finished").hide();
el.find(".msg-exited").hide();
break;
case "pending":
el.find(".msg-pending").show();
el.find(".msg-running").hide();
el.find(".msg-finished").hide();
el.find(".msg-exited").hide();
break;
default:
el.find(".msg-finished").find("span").attr("data-livestamp", job_.finished_at);
el.find(".msg-exited").find("span").text(job_.exit_code);
el.find(".msg-pending").hide();
el.find(".msg-running").hide();
el.find(".msg-finished").show();
el.find(".msg-exited").show();
break;
}
2015-09-30 01:21:17 +00:00
}
var after = self.status;
// if the status has changed we should start
// streaming the build contents.
if (before !== after && after === "running") {
2015-09-30 06:11:27 +00:00
self.stream();
2015-09-30 01:21:17 +00:00
}
// if the status is changes to complete, we can show
// the restart button and hide the tail button.
if (after !== "pending" && after !== "running") {
$("#restart").show();
$("#cancel").hide();
2015-09-30 01:21:17 +00:00
$("#tail").hide();
}
}.bind(this));
}
function Logs(repo, build, job) {
$.get( "/api/repos/"+repo+"/logs/"+build+"/"+job, function( data ) {
var convert = new Filter({stream: false, newline: false});
var escaped = convert.toHtml(escapeHTML(data));
$( "#output" ).html( escaped );
});
}
function Stream(repo, build, job, _callback) {
var callback = _callback;
var events = new EventSource("/api/stream/" + repo + "/" + build + "/" + job, {withCredentials: true});
events.onmessage = function (event) {
if (callback !== undefined) {
callback(event.data);
}
};
events.onerror = function (event) {
callback = undefined;
if (events !== undefined) {
events.close();
events = undefined;
}
console.log('user event stream closed due to error.', event);
};
};