This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
/
pipe.js
369 lines (311 loc) · 14 KB
/
pipe.js
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
function updatePipelines(divNames, errorDiv, view, fullscreen, showChanges, timeout) {
Q.ajax({
url: view.apiBaseUrl + 'api/json',
dataType: 'json',
async: true,
cache: false,
timeout: 20000,
success: function (data) {
refreshPipelines(data, divNames, errorDiv, view, fullscreen, showChanges);
setTimeout(function () {
updatePipelines(divNames, errorDiv, view, fullscreen, showChanges, timeout)
}, timeout);
},
error: function (xhr, status, error) {
Q("#" + errorDiv).html('Error communicating to server! ' + htmlEncode(error));
Q("#" + errorDiv).show();
plumb.repaintEverything();
setTimeout(function () {
updatePipelines(divNames, errorDiv, view, fullscreen, showChanges, timeout)
}, timeout);
}
});
}
function refreshPipelines(data, divNames, errorDiv, view, showAvatars, showChanges) {
var lastUpdate = data.lastUpdated;
if (data.error) {
Q("#" + errorDiv).html('Error: ' + data.error);
Q("#" + errorDiv).show();
} else {
Q("#" + errorDiv).html('');
Q("#" + errorDiv).hide();
}
if (lastResponse == null || JSON.stringify(data.pipelines) != JSON.stringify(lastResponse.pipelines)) {
for (var z = 0; z < divNames.length; z++) {
Q("#" + divNames[z]).html('');
}
var tasks = [];
if (!data.pipelines || data.pipelines.length == 0) {
Q("#pipeline-message").html('No pipelines configured or found. Please review the <a href="configure">configuration</a>')
}
plumb.reset();
for (var c = 0; c < data.pipelines.length; c++) {
var component = data.pipelines[c];
var html = "<section class='pipeline-component'>";
html = html + "<h1>" + htmlEncode(component.name) + "</h1>";
if (component.pipelines.length == 0) {
html = html + "No builds done yet.";
}
for (var i = 0; i < component.pipelines.length; i++) {
var pipeline = component.pipelines[i];
var triggered = "";
if (pipeline.triggeredBy && pipeline.triggeredBy.length > 0) {
for (var y = 0; y < pipeline.triggeredBy.length; y++) {
var trigger = pipeline.triggeredBy[y];
triggered = triggered + ' <span class="' + trigger.type + '">' + htmlEncode(trigger.description) + '</span>';
}
if (y < pipeline.triggeredBy.length - 1) {
triggered = triggered + ", ";
}
}
var contributors = [];
if (pipeline.contributors) {
Q.each(pipeline.contributors, function (index, contributor) {
contributors.push(htmlEncode(contributor.name));
});
}
if (contributors.length > 0) {
triggered = triggered + " changes by " + contributors.join(", ");
}
if (pipeline.aggregated) {
if (component.pipelines.length > 1) {
html = html + '<h2>Aggregated view</h2>'
}
} else {
html = html + '<h2>' + htmlEncode(pipeline.version);
if (triggered != "") {
html = html + " triggered by " + triggered;
}
html = html + ' started <span id="' + pipeline.id + '\">' + formatDate(pipeline.timestamp, lastUpdate) + '</span></h2>';
if (showChanges && pipeline.changes && pipeline.changes.length > 0) {
html = html + generateChangeLog(pipeline.changes);
}
}
html = html + '<section class="pipeline">';
var row = 0;
var column = 0;
html = html + '<div class="pipeline-row">';
for (var j = 0; j < pipeline.stages.length; j++) {
var stage = pipeline.stages[j];
if (stage.row > row) {
html = html + '</div><div class="pipeline-row">';
column = 0;
row++;
}
if (stage.column > column) {
for (var as = column; as < stage.column; as++) {
html = html + '<div class="pipeline-cell"><div class="stage hide"></div></div>';
column++;
}
}
html = html + '<div class="pipeline-cell">';
html = html + '<div id="' + getStageId(stage.id + "", i) + '" class="stage ' + getStageClassName(stage.name) + '">';
html = html + '<div class="stage-header"><div class="stage-name">' + htmlEncode(stage.name) + '</div>';
if (!pipeline.aggregated) {
html = html + '</div>'
} else {
var stageversion = stage.version;
if (!stageversion) {
stageversion = "N/A"
}
html = html + ' <div class="stage-version">' + htmlEncode(stageversion) + '</div></div>'
}
for (var k = 0; k < stage.tasks.length; k++) {
var task = stage.tasks[k];
var id = getTaskId(task.id, i);
var timestamp = formatDate(task.status.timestamp, lastUpdate);
tasks.push({id: id, taskId: task.id, buildId: task.buildId});
var progress = 100;
var progressClass = "task-progress-notrunning";
if (task.status.percentage) {
progress = task.status.percentage;
progressClass = "task-progress-running";
}
html = html + "<div id=\"" + id + "\" class=\"stage-task " + task.status.type +
"\"><div class=\"task-progress " + progressClass + "\" style=\"width: " + progress + "%;\"><div class=\"task-content\">" +
"<div class=\"task-header\"><div class=\"taskname\"><a href=\"" + task.link + "\">" + htmlEncode(task.name) + "</a></div>";
if (data.allowManualTriggers && task.manual && task.manualStep.enabled && task.manualStep.permission) {
html = html + '<div class="task-manual" id="manual-' + id + '" onclick="triggerManual(\'' + id + '\', \'' + task.id + '\', \'' + task.manualStep.upstreamProject + '\', \'' + task.manualStep.upstreamId + '\');">';
html = html + "</div>"
}
html = html + '</div><div class="task-details">';
if (timestamp != "") {
html = html + "<div id=\"" + id + ".timestamp\" class='timestamp'>" + timestamp + "</div>"
}
if (task.status.duration >= 0)
html = html + "<div class='duration'>" + formatDuration(task.status.duration) + "</div>";
html = html + "</div></div></div></div>"
}
html = html + "</div>";
html = html + '</div>';
column++;
}
html = html + '</div>';
html = html + "</section>";
}
html = html + "</section>";
Q("#" + divNames[c % divNames.length]).append(html);
Q("#pipeline-message").html('');
}
var index = 0;
lastResponse = data;
equalheight(".pipeline-row .stage");
Q.each(data.pipelines, function (i, component) {
Q.each(component.pipelines, function (j, pipeline) {
var index = j;
Q.each(pipeline.stages, function (k, stage) {
if (stage.downstreamStages) {
Q.each(stage.downstreamStageIds, function (l, value) {
var source = getStageId(stage.id + "", index);
var target = getStageId(value + "", index);
plumb.connect({
source: source,
target: target,
anchors: ["RightMiddle", "LeftMiddle"],
overlays: [
[ "Arrow", { location: 1}]
],
cssClass: "relation",
connector: ["Flowchart", { stub: 25, gap: 2, midpoint: 1, alwaysRespectStubs: true } ],
paintStyle: { lineWidth: 2, strokeStyle: "rgba(0,0,0,0.5)" },
drawEndpoints: false
});
});
}
});
});
});
} else {
for (var p = 0; p < data.pipelines.length; p++) {
var comp = data.pipelines[p];
for (var d = 0; d < comp.pipelines.length; d++) {
var pipe = comp.pipelines[d];
var head = document.getElementById(pipe.id);
if (head) {
head.innerHTML = formatDate(pipe.timestamp, lastUpdate)
}
for (var l = 0; l < pipe.stages.length; l++) {
var st = pipe.stages[l];
for (var m = 0; m < st.tasks.length; m++) {
var ta = st.tasks[m];
var time = document.getElementById(getTaskId(ta.id, d) + ".timestamp");
if (time) {
time.innerHTML = formatDate(ta.status.timestamp, lastUpdate);
}
}
}
}
}
}
plumb.repaintEverything();
}
function generateChangeLog(changes) {
var html = '<div class="changes">';
html = html + '<h1>Changes:</h1>';
for (var i = 0; i < changes.length; i++) {
html = html + '<div class="change">';
var change = changes[i];
html = html + '<div class="change-author">' + htmlEncode(change.author.name) + '</div>';
if (change.changeLink) {
html = html + '<div class="change-message"><a href="' + change.changeLink + '">' + htmlEncode(change.message) + '</a></div>';
} else {
html = html + '<div class="change-message">' + htmlEncode(change.message) + '</div>';
}
html = html + '</div>';
}
html = html + '</div>';
return html;
}
function getStageClassName(stagename) {
return "stage_" + replace(stagename, " ", "_");
}
function getTaskId(taskname, count) {
return "task-" + replace(replace(taskname, " ", "_"), "/", "_") + count;
}
function replace(string, replace, replaceWith) {
var re = new RegExp(replace, 'g');
return string.replace(re, replaceWith);
}
function formatDate(date, currentTime) {
if (date != null) {
return moment(date, "YYYY-MM-DDTHH:mm:ss").from(moment(currentTime, "YYYY-MM-DDTHH:mm:ss"))
} else {
return "";
}
}
function formatDuration(millis) {
if (millis > 0) {
var seconds = Math.floor(millis / 1000);
var minutes = Math.floor(seconds / 60);
seconds = seconds % 60;
var minstr;
if (minutes == 0)
minstr = "";
else
minstr = minutes + " min ";
var secstr = "" + seconds + " sec";
return minstr + secstr;
}
return "0 sec";
}
function triggerManual(taskId, downstreamProject, upstreamProject, upstreamBuild) {
Q("#manual-" + taskId).hide();
var formData = {project: downstreamProject, upstream: upstreamProject, buildId: upstreamBuild};
var before;
if (crumb.value != "") {
console.info("Crumb found and will be added to request header");
before = function(xhr){xhr.setRequestHeader(crumb.fieldName, crumb.value);}
} else {
console.info("Crumb not needed");
before = function(xhr){}
}
Q.ajax({
url: view.apiBaseUrl + 'api/manualStep',
type: "POST",
data: formData,
beforeSend: before,
timeout: 20000,
success: function (data, textStatus, jqXHR) {
console.info("Triggered build of " + downstreamProject + " successfully!")
},
error: function (jqXHR, textStatus, errorThrown) {
window.alert("Could not trigger build! error: " + errorThrown + " status: " + textStatus)
}
});
}
function htmlEncode(html) {
html = document.createElement('a').appendChild(
document.createTextNode(html)).parentNode.innerHTML;
return html.replace(/\n/g, '<br/>');
}
function getStageId(name, count) {
var re = new RegExp(' ', 'g');
return name.replace(re, '_') + "_" + count;
}
function equalheight(container) {
var currentTallest = 0,
currentRowStart = 0,
rowDivs = new Array(),
$el,
topPosition = 0;
Q(container).each(function () {
$el = Q(this);
Q($el).height('auto');
topPostion = $el.position().top;
if (currentRowStart != topPostion) {
for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
rowDivs[currentDiv].height(currentTallest);
}
rowDivs.length = 0; // empty the array
currentRowStart = topPostion;
currentTallest = $el.height();
rowDivs.push($el);
} else {
rowDivs.push($el);
currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
}
for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
rowDivs[currentDiv].height(currentTallest);
}
});
}