forked from jasonklarsen/HudsonHawk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhudsonHawk.html
466 lines (406 loc) · 17.3 KB
/
hudsonHawk.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<html>
<head>
<title>Hudson Hawk</title>
<link type="text/css" href="http://jqueryui.com/latest/themes/base/jquery.ui.all.css" rel="stylesheet" />
<style>
.notready { background-color: grey; }
.ready { background-color: green; }
.job { margin: 6px; text-shadow: 2px 2px 5px #000; font-weight:bold; color:#fff; -webkit-box-shadow: 3px 3px 4px #000; -webkit-border-radius: 10px; position: relative; text-align: center; border: 2px black solid; font-family: sans-serif; font-size: 400%; white-space: nowrap; cursor: pointer; }
.details { }
.jobdetail { border: 4px black solid; font-size: 500%; white-space: nowrap; }
.broken, .ui-dialog div.broken { background-color: darkred; }
.failed, .ui-dialog div.failed { background-color: red; }
.fixed, .ui-dialog div.fixed { background-color: orange; }
.flicker, .ui-dialog div.flicker { background-color: purple; color: white; }
.passed, .ui-dialog div.passed { background-color: green; }
.unknown, .ui-dialog div.unknown { background-color: yellow; }
input.url { width: 350px; }
#settings { position: absolute; bottom: 0; right: 0; display: none; }
#settings button { padding: 3px; font-size: 150%; }
.ready #settings { display: block; }
</style>
</head>
<body class="notready">
<div id="jobs">
<div id="settings"><button type="button">Settings</button></div>
</div>
<div id="details" class="details"></div>
<div id="popup" style="display: none"></div>
<script src="http://www.google.com/jsapi"></script>
<script>google.load("jquery", "1.4.2"); google.load("jqueryui", "1.7.2");</script>
<script src="http://jquery-json.googlecode.com/files/jquery.json-2.2.min.js"></script>
<script>
/** Watcher of Hudson jobs. */
var Hawk = function(hudson, prey, territory) {
var awaken = function() {
territory.recallViewAndThen(function(view) {
hudson.failedJobsFor(view, loadInto(prey));
var flightPlan = function() {
hudson.failedJobsFor(view, addTo(prey));
setTimeout(flightPlan, 30000);
}
setTimeout(flightPlan, 30000);
});
$("#settings").click(function() {
territory.openSettingsAndThen(function(view) {
refreshPage();
});
});
};
var refreshPage = function() {
location.reload(true);
};
var loadInto = function(prey) {
return function(jobs) {
prey.initializeWith(jobs);
$('body').removeClass('notready').addClass('ready');
};
};
var addTo = function(prey) {
return function(jobs) {
prey.target(jobs);
};
};
return { awaken: awaken };
};
/** Hudson jobs to watch for */
var Prey = function(radar) {
var getState = function(color, newDescription) {
var colorToState = { red: "broken", red_anime: "broken", yellow: "failed", yellow_anime: "failed",
blue: "passed", blue_anime: "passed", aborted: "unknown", aborted_anime: "unknown" }
var descriptionBased = function(newDescription) {
return newDescription.indexOf('Build: Flicker') === 0 ? "flicker" : (newDescription.indexOf('Build: Fixed') === 0 ? "fixed" : null);
};
return descriptionBased(newDescription) || colorToState[color] || 'unknown';
};
var Listing = function(job) {
var newDescription = job.lastCompletedBuild && job.lastCompletedBuild.description ? job.lastCompletedBuild.description: "";
return { name: job.name,
state: getState(job.color, newDescription),
url: job.lastCompletedBuild.__url,
inProgress: job.color.indexOf('anime') > -1,
description: newDescription,
buildNumber: job.lastCompletedBuild ? job.lastCompletedBuild.number: null };
};
var Listings = function(jobs) {
var names = {}, values = [];
$(jobs).each(function(i, job) {
var value = Listing(job);
names[job.name] = value;
values.push(value);
});
return { names: names, values: values };
};
var currentListings = Listings([]);
var initialize = function(jobs) {
currentListings = Listings(jobs);
$(currentListings.values).each(function(i, listing) { radar.load(listing); });
};
var target = function(jobs) {
var newListings = Listings(jobs);
var notInNewList = function(i, listing) { return !newListings.names[listing.name]; };
var notInOldList = function(i, listing) { return !currentListings.names[listing.name]; };
var withChangedBuildNumbers = function(i, listing) { return currentListings.names[listing.name] && listing.buildNumber !== currentListings.names[listing.name].buildNumber; };
var nowInProgress = function(i, listing) { return listing.inProgress; };
var withChangedState = function(i, listing) { return currentListings.names[listing.name] && listing.state !== currentListings.names[listing.name].state; };
filter(currentListings, notInNewList).andForEach(function(i, listing) { radar.remove(listing); });
filter(newListings, notInOldList).andForEach(function(i, listing) { radar.add(listing); });
filter(newListings, withChangedBuildNumbers).andForEach(function(i, listing) { radar.readd(listing); });
filter(newListings, nowInProgress).andForEach(function(i, listing) { radar.highlight(listing); });
filter(newListings, withChangedState).andForEach(function(i, listing) { radar.changeState(listing); });
currentListings = newListings;
};
var filter = function(listings, filterFxn) {
return { andForEach: function(closure) {
$(listings.values).filter(filterFxn).each(closure);
} };
}
return {
target: target,
initializeWith: initialize
};
};
/** View of the Prey data (i.e. screen) */
var Radar = function(hudson) {
var currentlyExplodedView = false;
var readd = function(listing) {
removeAnd(listing, function() { add(listing); });
};
var remove = function(listing) {
removeAnd(listing, function() {});
};
var removeAnd = function(listing, fxn) {
$("#" + listing.name).hide("blind", { direction: "vertical" }, 3000);
setTimeout(function() { $("#" + listing.name).dialog('destroy'); $("#" + listing.name).remove();
$("#" + listing.name + "--detail").dialog('destroy'); $("#" + listing.name + "--detail").remove();
fxn(); }, 3000);
}
var describeAndMark = function(listing, description, newClass) {
hudson.changeDescription(listing.name, listing.buildNumber, description);
removeAllClasses(listing, newClass);
};
var removeAllClasses = function(listing, newClass) {
$("#" + listing.name).removeClass("broken failed unknown passed flicker fixed").addClass(newClass);
$("#" + listing.name + "--detail").removeClass("broken failed unknown passed flicker fixed").addClass(newClass);
}
var explodeDetail = function(listing, closeAfter, andThen) {
if (currentlyExplodedView) {
setTimeout(function() { explodeDetail(listing, closeAfter, andThen); }, 500);
return;
}
currentlyExplodedView = true;
$("#jobs").hide('puff', {}, 2000);
$("#" + listing.name + '--detail').dialog({
buttons: { 'Mark as Fixed': function() { $(this).dialog("close"); describeAndMark(listing, 'Build: Fixed', 'fixed'); },
'Flicker': function() { $(this).dialog("close"); describeAndMark(listing, 'Build: Flicker', 'flicker'); },
'View In Hudson': function() { window.open(listing.url); }
},
hide: "scale",
width: 'auto',
height: 'auto',
modal: true,
resizable: false,
closeOnEscape: true,
close: function() { andThen(); currentlyExplodedView = false; }
});
if (closeAfter > 0) {
setTimeout(function() { $("#" + listing.name + '--detail').dialog('close'); }, closeAfter);
}
};
var add = function(listing) {
create(listing);
explodeDetail(listing, 5000, function() {
$("#jobs").show("blind", { direction: "vertical" }, 1100);
setTimeout(function() { $("#" + listing.name).show("blind", { direction: "vertical" }, 2000); }, 1100);
});
};
var load = function(listing) {
create(listing);
$("#" + listing.name).show();
};
var create = function(listing) {
$('#jobs').prepend('<div id="' + listing.name + '" class="' + listing.state + ' job" style="display: none">' + listing.name + '</div>');
$('#details').prepend('<div id="' + listing.name + '--detail" class="' + listing.state + ' jobdetail" style="display: none">' + listing.name + '</div>');
$("#" + listing.name)
.click(function() { explodeDetail(listing, 0, function() { $("#jobs").show("blind", { direction: "vertical" }, 2000); }); });
};
var highlight = function(listing) {
$("#" + listing.name).effect("pulsate", { times: 10000 }, 5000);
};
var changeState = function(listing) {
removeAllClasses(listing, listing.state);
};
return { remove: remove, add: add, highlight: highlight, readd: readd, load: load, changeState: changeState };
};
/** Storage utils (via Cookies or HTML5 storage if supported) */
var Memory = (function() {
var cookieStorage = {
write: function(name, value) {
var date = new Date();
date.setTime(date.getTime() + (500 * 24 * 60 * 60 * 1000)); // 500 days from now
document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
},
read: function(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
}
var hasLocalStorage = function() { return !$.browser.mozilla && !!localStorage; } ;// Firefox 3.5 has localStorage but it doesn't seem to work right....
var storage = hasLocalStorage() ? { write: function(name, value) { localStorage.setItem(name, value); }, read: function(name) { return localStorage.getItem(name); } } : cookieStorage;
return {
write: storage.write,
erase: function(name) { storage.write(name, ""); },
read: storage.read
}
})();
/** Configuration options */
var Territory = function(memory, hudson) {
var serverUrl = function() { return memory.read('HudsonHawk.serverUrl') };
var viewName = function() { return memory.read('HudsonHawk.viewName') };
var userName = function() { return memory.read('HudsonHawk.userName') };
var password = function() { return memory.read('HudsonHawk.password') };
var popupRequestForInfo = function(title, formBuilder, okCallback) {
var popup = $('#popup');
popup.empty();
popup.dialog({
buttons: { 'OK': function() { $(this).dialog("close"); okCallback(); } },
hide: "slide",
modal: true,
width: 'auto',
maxWidth: 400,
height: 'auto',
resizable: false,
maxHeight: 800,
closeOnEscape: true,
title: title,
open: function(event, ui) { formBuilder(popup); }
});
};
var openSettingsIfNotFilledIn = function(callbackOnOk) {
if (serverUrl() && viewName() && userName()) {
hudson.redirectTo(serverUrl(), userName(), password());
callbackOnOk(viewName());
} else {
openSettings(callbackOnOk);
}
};
var openSettings = function(callbackOnOk) {
if (serverUrl()) {
hudson.redirectTo(serverUrl(), userName(), password());
refreshViewsOnPopup();
refreshUsersOnPopup();
}
popupRequestForInfo("Settings", settingsForm, function() {
var newUrl = $('#urlOption').val();
memory.write('HudsonHawk.serverUrl', newUrl);
var newViewName = $('#viewOption').val();
memory.write('HudsonHawk.viewName', newViewName);
var newUsername = $('#userOption').val();
memory.write('HudsonHawk.userName', newUsername);
var newPassword = $('#passwordOption').val();
memory.write('HudsonHawk.password', newPassword);
hudson.redirectTo(newUrl, newUsername, newPassword);
callbackOnOk(newViewName);
});
};
var generateSelect = function(id, values) {
var selectBox = ["<select id='" + id + "'>"];
$.each(values, function(i, value) {
selectBox[i + 1] = "<option value='" + value + "'>" + value + "</option>";
});
selectBox.push("</select>");
$('#' + id).replaceWith(selectBox.join(""));
};
var refreshViewsOnPopup = function() {
$('#viewOption').replaceWith("<i id='viewOption'>Refreshing views from Hudson...</i>");
hudson.viewsFor(function(views) {
generateSelect("viewOption", $(views).map(function(i, view) { return view.name; }));
$('#viewOption').val(memory.read('HudsonHawk.viewName'));
});
};
var refreshUsersOnPopup = function() {
$('#userOption').replaceWith("<i id='userOption'>Refreshing users from Hudson...</i>");
hudson.usersFor(function(users) {
generateSelect("userOption", $(users).map(function(i, user) { return user.user.fullName; }));
$('#userOption').val(memory.read('HudsonHawk.userName'));
});
};
var settingsForm = function(popup) {
var table = "<table><tbody>"
+ row("Hudson URL:", "<input id='urlOption' class='url' value='" + (serverUrl() || "http://") + "'></input>")
+ row("View:", "<i id='viewOption'>" + (viewName() || "<none>") + "</i>")
+ row("User:", "<i id='userOption'>" + (userName() || "<none>") + "</i>")
+ row("Password:", "<input id='passwordOption' type='password' value='" + (password() || "") + "'></input>")
+ "</tbody></table>";
popup.append(table);
$('#urlOption').change(function() {
hudson.redirectTo($('#urlOption').val(), userName(), password());
refreshViewsOnPopup();
refreshUsersOnPopup();
});
};
var row = function(prompt, formElement) {
return "<tr><td>" + prompt + "</td><td>" + formElement + "</td></tr>";
};
return { recallViewAndThen: openSettingsIfNotFilledIn, openSettingsAndThen: openSettings };
};
/** Hudson wrapper */
var Hudson = (function() {
var hudsonUrl;
var username;
var password;
var callHudsonFor = function(depth, callback, directory) {
var requestUrl = hudsonUrl + directory;
$.ajax({
url: requestUrl + 'api/json',
dataType: 'jsonp',
jsonp: 'jsonp',
data: 'depth=' + depth,
success: function(hudson) { hudson.__url = requestUrl; callback(hudson); },
error: function(request, status, error) {
alert("Error: " + requestUrl);
}
});
};
var viewsFor = function(callback) {
callHudsonFor(0, function(hudson) { callback(hudson.views); }, "");
};
var usersFor = function(callback) {
callHudsonFor(0, function(hudson) { callback(hudson.users); }, "people/");
};
var failedJobsFor = function(view, callback) {
var viewName = view ? "view/" + view + "/" : '';
callHudsonFor(0, function(hudson) {
var failedJobs = $(hudson.jobs).filter(function() { return this.color !== 'blue' && this.color !== 'blue_anime'; });
var count = failedJobs.length;
failedJobs.each(function(i, job) {
callHudsonFor(0, function(lastBuild) { job.lastCompletedBuild = lastBuild; count--; }, viewName + "job/" + job.name + "/lastCompletedBuild/");
});
var waitUntilJobDetailIsGathered = function() {
if (count === 0) {
callback(failedJobs);
} else {
setTimeout(waitUntilJobDetailIsGathered, 100);
}
};
setTimeout(waitUntilJobDetailIsGathered, 100);
}, viewName);
};
var redirectTo = function(dirtyUrl, newUsername, newPassword) {
var cleanerUrl = dirtyUrl.indexOf('http') > -1 ? dirtyUrl : "http://" + dirtyUrl;
hudsonUrl = cleanerUrl.charAt(cleanerUrl.length - 1) === '/' ? cleanerUrl : cleanerUrl + "/";
username = newUsername;
password = newPassword;
};
var changeDescription = function(jobName, buildNumber, newDescription) {
var iframeId = "authFrame" + new Date().getTime();
var params = { j_username: username, j_password: password, remember_me: true, from: "/" };
var src = hudsonUrl + "/j_acegi_security_check?Submit=log+in&" + $.param(params) + "&json=" + $.param($.toJSON(params));
$('body').prepend('<iframe id="' + iframeId + '" style="display:none" src="' + src + '"></iframe>');
var iframe = $('iframe#' + iframeId);
iframe.load(function() {
setTimeout(function() {
iframe.unbind();
iframe.attr('src', hudsonUrl + '/job/' + jobName + '/' + buildNumber + '/submitDescription?Submit=Submit&description=' + encodeURI(newDescription) + '&json=' + $.param({ description: newDescription }));
iframe.load(function() {
setTimeout(function() {
iframe.unbind();
iframe.attr('src', hudsonUrl + '/logout');
iframe.load(function() {
setTimeout(function() {
$('iframe#' + iframeId).remove();
}, 100);
});
}, 100);
});
}, 100);
});
}
return {
viewsFor: viewsFor,
failedJobsFor: failedJobsFor,
usersFor: usersFor,
redirectTo: redirectTo,
changeDescription: changeDescription
};
})();
var hudson = Hudson;
var radar = Radar(hudson);
var prey = Prey(radar);
var memory = Memory;
var territory = Territory(memory, hudson);
var hawk = Hawk(hudson, prey, territory);
hawk.awaken();
</script>
</body>
</html>