BlueGit

timfa bluegit

Files

bluegit / internal / server / static / pipeline.js

pipeline.js

feat: ververs de pipeline op het projectoverzicht
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed 42fd7379 History
pipeline.js 17.8 KiB Download Raw
1(function () {
2 document.querySelectorAll(".section-toggle").forEach(function (btn) {
3 btn.addEventListener("click", function () {
4 var sec = btn.parentElement;
5 sec.classList.toggle("open");
6 });
7 });
8
9 var log = document.querySelector("[data-log]");
10 var hits = [];
11 var hitAt = -1;
12
13 function clearHits() {
14 if (!log) {
15 return;
16 }
17 log.querySelectorAll(".hit").forEach(function (el) {
18 el.classList.remove("hit", "on");
19 });
20 hits = [];
21 hitAt = -1;
22 }
23
24 function showHit(i) {
25 if (!hits.length) {
26 return;
27 }
28 hitAt = (i + hits.length) % hits.length;
29 hits.forEach(function (el, n) {
30 el.classList.toggle("on", n === hitAt);
31 });
32 var current = hits[hitAt];
33 var sec = current.closest(".logsec");
34 if (sec) {
35 sec.classList.add("open");
36 }
37 current.scrollIntoView({ block: "center" });
38 }
39
40 var search = document.querySelector("[data-log-search]");
41 if (search && log) {
42 search.addEventListener("submit", function (ev) {
43 ev.preventDefault();
44 var q = (search.querySelector("input").value || "").toLowerCase();
45 clearHits();
46 if (!q) {
47 return;
48 }
49 log.querySelectorAll("[data-line]").forEach(function (row) {
50 var code = row.querySelector("code");
51 var text = code ? code.textContent.toLowerCase() : "";
52 if (text.indexOf(q) >= 0) {
53 row.classList.add("hit");
54 hits.push(row);
55 }
56 });
57 showHit(0);
58 });
59 }
60 var prev = document.querySelector("[data-log-prev]");
61 var next = document.querySelector("[data-log-next]");
62 if (prev) {
63 prev.addEventListener("click", function () { showHit(hitAt - 1); });
64 }
65 if (next) {
66 next.addEventListener("click", function () { showHit(hitAt + 1); });
67 }
68 var wrapBtn = document.querySelector("[data-log-wrap]");
69 if (wrapBtn && log) {
70 wrapBtn.setAttribute("aria-pressed", log.classList.contains("nowrap") ? "false" : "true");
71 wrapBtn.addEventListener("click", function () {
72 var off = log.classList.toggle("nowrap");
73 wrapBtn.setAttribute("aria-pressed", off ? "false" : "true");
74 });
75 }
76 var topBtn = document.querySelector("[data-log-top]");
77 var bottomBtn = document.querySelector("[data-log-bottom]");
78 if (topBtn && log) {
79 topBtn.addEventListener("click", function () { log.scrollTop = 0; });
80 }
81 if (bottomBtn && log) {
82 bottomBtn.addEventListener("click", function () { log.scrollTop = log.scrollHeight; });
83 }
84 var fullBtn = document.querySelector("[data-log-full]");
85 var wrapBox = document.querySelector("[data-console]");
86 if (fullBtn && wrapBox) {
87 fullBtn.addEventListener("click", function () {
88 if (document.fullscreenElement) {
89 document.exitFullscreen();
90 return;
91 }
92 if (wrapBox.requestFullscreen) {
93 wrapBox.requestFullscreen().catch(function () {
94 wrapBox.classList.toggle("full");
95 });
96 return;
97 }
98 wrapBox.classList.toggle("full");
99 });
100 }
101
102 var stageSelect = document.querySelector("[data-stage-select]");
103 if (stageSelect) {
104 stageSelect.addEventListener("change", function () {
105 var want = stageSelect.value;
106 document.querySelectorAll(".related li").forEach(function (li) {
107 li.hidden = li.getAttribute("data-stage") !== want;
108 });
109 });
110 }
111
112 function rowElement(html) {
113 var wrap = document.createElement("tbody");
114 wrap.innerHTML = (html || "").trim();
115 return wrap.querySelector("tr");
116 }
117
118 function stagesFromHTML(html) {
119 var row = rowElement(html);
120 if (row) {
121 return row.querySelector(".stages");
122 }
123 var wrap = document.createElement("div");
124 wrap.innerHTML = (html || "").trim();
125 return wrap.querySelector(".stages");
126 }
127
128 function findStage(root, name) {
129 var buttons = root.querySelectorAll("button.stage-dot[data-stage]");
130 for (var i = 0; i < buttons.length; i++) {
131 if (buttons[i].getAttribute("data-stage") === name) {
132 return buttons[i];
133 }
134 }
135 return null;
136 }
137
138 function setQueue(root, text) {
139 var q = root.querySelector("[data-queue]");
140 if (!text) {
141 if (q) {
142 q.remove();
143 }
144 return;
145 }
146 if (!q) {
147 q = document.createElement("span");
148 q.className = "queue";
149 q.setAttribute("data-queue", "");
150 var anchor = root.querySelector("a");
151 if (anchor && anchor.parentElement === root) {
152 anchor.insertAdjacentElement("afterend", q);
153 } else {
154 (anchor || root).appendChild(q);
155 }
156 }
157 q.textContent = text;
158 }
159
160 function patchPipeRow(live, row) {
161 var status = live.querySelector("[data-pipe-status]");
162 if (status) {
163 status.className = "status " + row.status;
164 var label = status.querySelector("[data-pipe-label]");
165 if (label) {
166 label.textContent = row.label || "";
167 }
168 }
169 var dur = live.querySelector("[data-pipe-duration]");
170 if (dur) {
171 dur.textContent = row.duration || "";
172 }
173 var when = live.querySelector("[data-pipe-when]");
174 if (when) {
175 when.textContent = row.when || "";
176 }
177 var stages = live.querySelector(".stages");
178 if (!stages) {
179 return;
180 }
181 var buttons = stages.querySelectorAll(":scope > .stage > button.stage-dot[data-stage]");
182 var next = row.stages || [];
183 var same = buttons.length === next.length;
184 for (var i = 0; same && i < buttons.length; i++) {
185 if (buttons[i].getAttribute("data-stage") !== next[i].name) {
186 same = false;
187 }
188 }
189 if (!same) {
190 var freshStages = stagesFromHTML(row.html);
191 if (!freshStages) {
192 return;
193 }
194 var openName = "";
195 var openPop = live.querySelector(".stage-pop:popover-open");
196 if (openPop) {
197 var openBtn = openPop.parentElement.querySelector("button.stage-dot");
198 openName = openBtn ? openBtn.getAttribute("data-stage") : "";
199 }
200 stages.innerHTML = freshStages.innerHTML;
201 if (openName) {
202 var again = findStage(live, openName);
203 if (again) {
204 again.click();
205 }
206 }
207 return;
208 }
209 next.forEach(function (st) {
210 var btn = findStage(live, st.name);
211 if (!btn) {
212 return;
213 }
214 btn.className = "stage-dot " + st.status;
215 btn.setAttribute("data-tip", st.tip || "");
216 btn.setAttribute("aria-label", st.tip || "");
217 var pop = btn.parentElement.querySelector(".stage-pop");
218 if (!pop) {
219 return;
220 }
221 (st.jobs || []).forEach(function (job) {
222 var li = pop.querySelector('[data-job="' + job.id + '"]');
223 if (!li) {
224 return;
225 }
226 var dot = li.querySelector(".stage-dot");
227 if (dot) {
228 dot.className = "stage-dot sm " + job.status;
229 }
230 setQueue(li, job.queue);
231 });
232 });
233 }
234
235 function applyPipeRows(rows, scrollX, scrollY, box, left) {
236 var body = document.querySelector("table.pipes tbody");
237 if (!body) {
238 return;
239 }
240 var ids = {};
241 rows.forEach(function (row) {
242 ids[String(row.id)] = true;
243 });
244 body.querySelectorAll("tr[data-pipe]").forEach(function (tr) {
245 if (ids[tr.getAttribute("data-pipe")]) {
246 return;
247 }
248 var rect = tr.getBoundingClientRect();
249 var h = tr.offsetHeight;
250 tr.remove();
251 if (rect.bottom <= 0) {
252 scrollY -= h;
253 }
254 });
255 rows.forEach(function (row, index) {
256 var live = body.querySelector('tr[data-pipe="' + row.id + '"]');
257 if (!live) {
258 var fresh = rowElement(row.html);
259 if (!fresh) {
260 return;
261 }
262 var existing = body.querySelectorAll("tr[data-pipe]");
263 var before = existing[index] || null;
264 if (before) {
265 body.insertBefore(fresh, before);
266 } else {
267 body.appendChild(fresh);
268 }
269 if (scrollY > 0) {
270 scrollY += fresh.offsetHeight;
271 }
272 return;
273 }
274 patchPipeRow(live, row);
275 var existing = body.querySelectorAll("tr[data-pipe]");
276 if (existing[index] && existing[index] !== live) {
277 body.insertBefore(live, existing[index]);
278 }
279 });
280 window.scrollTo(scrollX, scrollY);
281 if (box) {
282 box.scrollLeft = left;
283 }
284 }
285
286 function applyDetail(data) {
287 var pill = document.querySelector("[data-pipe-status]");
288 if (pill && data.status) {
289 pill.className = "status " + data.status;
290 var label = pill.querySelector("[data-pipe-label]");
291 if (label) {
292 label.textContent = data.label || "";
293 }
294 }
295 (data.jobs || []).forEach(function (job) {
296 var li = document.querySelector('.clean li[data-job="' + job.id + '"]');
297 if (!li) {
298 return;
299 }
300 var st = li.querySelector("[data-job-status]");
301 if (st) {
302 st.className = "status " + job.status;
303 var lab = st.querySelector("[data-job-label]");
304 if (lab) {
305 lab.textContent = job.label || "";
306 }
307 }
308 setQueue(li, job.queue);
309 });
310 var head = document.querySelector("[data-head-pipe]");
311 if (head) {
312 patchPipeRow(head, data);
313 }
314 }
315
316 var liveEl = document.querySelector("[data-live-on]");
317 if (liveEl) {
318 var liveURL = liveEl.getAttribute("data-live");
319 var liveTimer = function () {
320 if (document.hidden) {
321 setTimeout(liveTimer, 3000);
322 return;
323 }
324 var box = document.querySelector(".hscroll");
325 var scrollX = window.scrollX;
326 var scrollY = window.scrollY;
327 var left = box ? box.scrollLeft : 0;
328 fetch(liveURL, { headers: { Accept: "application/json" }, credentials: "same-origin", cache: "no-store" })
329 .then(function (res) {
330 if (!res.ok) {
331 throw new Error("live");
332 }
333 return res.json();
334 })
335 .then(function (data) {
336 if (data.rows) {
337 applyPipeRows(data.rows, scrollX, scrollY, box, left);
338 } else {
339 applyDetail(data);
340 window.scrollTo(scrollX, scrollY);
341 if (box) {
342 box.scrollLeft = left;
343 }
344 }
345 window.dispatchEvent(new Event("resize"));
346 if (data.active) {
347 setTimeout(liveTimer, 3000);
348 }
349 })
350 .catch(function () {
351 setTimeout(liveTimer, 3000);
352 });
353 };
354 setTimeout(liveTimer, 3000);
355 }
356
357 if (!wrapBox || wrapBox.getAttribute("data-running") !== "1" || !log) {
358 return;
359 }
360 var offset = parseInt(wrapBox.getAttribute("data-offset") || "0", 10);
361 var url = wrapBox.getAttribute("data-log-url");
362 var pending = "";
363 var lineNo = 0;
364 log.querySelectorAll("[data-line]").forEach(function (row) {
365 var n = parseInt(row.getAttribute("data-line") || "0", 10);
366 if (n > lineNo) {
367 lineNo = n;
368 }
369 });
370 var currentSec = log.querySelector(".logsec.open:last-of-type");
371
372 var ansiNames = ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"];
373 var follow = true;
374 log.scrollTop = log.scrollHeight;
375 log.addEventListener("scroll", function () {
376 follow = log.scrollHeight - log.scrollTop - log.clientHeight < 48;
377 });
378
379 function ansiClass(state) {
380 var parts = [];
381 if (state.bold) { parts.push("ansi-bold"); }
382 if (state.dim) { parts.push("ansi-dim"); }
383 if (state.italic) { parts.push("ansi-italic"); }
384 if (state.underline) { parts.push("ansi-underline"); }
385 if (state.fg) { parts.push("ansi-fg-" + state.fg); }
386 if (state.bg) { parts.push("ansi-bg-" + state.bg); }
387 return parts.join(" ");
388 }
389
390 function paint(code, text) {
391 var state = {};
392 var buf = "";
393 function flush() {
394 if (!buf) { return; }
395 var cls = ansiClass(state);
396 if (!cls) {
397 code.appendChild(document.createTextNode(buf));
398 } else {
399 var span = document.createElement("span");
400 span.className = cls;
401 span.textContent = buf;
402 code.appendChild(span);
403 }
404 buf = "";
405 }
406 for (var i = 0; i < text.length; i++) {
407 if (text.charCodeAt(i) !== 27 || i + 1 >= text.length) {
408 buf += text.charAt(i);
409 continue;
410 }
411 if (text.charAt(i + 1) === "[") {
412 var end = i + 2;
413 while (end < text.length) {
414 var c = text.charCodeAt(end);
415 if (c >= 32 && c <= 63) { end++; continue; }
416 break;
417 }
418 if (end < text.length && text.charCodeAt(end) >= 64 && text.charCodeAt(end) <= 126) {
419 flush();
420 if (text.charAt(end) === "m") {
421 applySGR(state, text.slice(i + 2, end));
422 }
423 i = end;
424 continue;
425 }
426 }
427 buf += text.charAt(i);
428 }
429 flush();
430 }
431
432 function applySGR(state, params) {
433 if (!params) {
434 state.bold = state.dim = state.italic = state.underline = false;
435 state.fg = state.bg = "";
436 return;
437 }
438 var nums = params.split(";").map(function (p) { return parseInt(p, 10) || 0; });
439 for (var i = 0; i < nums.length; i++) {
440 var n = nums[i];
441 if (n === 0) {
442 state.bold = state.dim = state.italic = state.underline = false;
443 state.fg = state.bg = "";
444 } else if (n === 1) { state.bold = true; state.dim = false; }
445 else if (n === 2) { state.dim = true; }
446 else if (n === 3) { state.italic = true; }
447 else if (n === 4) { state.underline = true; }
448 else if (n === 22) { state.bold = false; state.dim = false; }
449 else if (n === 23) { state.italic = false; }
450 else if (n === 24) { state.underline = false; }
451 else if (n >= 30 && n <= 37) { state.fg = ansiNames[n - 30]; }
452 else if (n === 39) { state.fg = ""; }
453 else if (n >= 40 && n <= 47) { state.bg = ansiNames[n - 40]; }
454 else if (n === 49) { state.bg = ""; }
455 else if (n >= 90 && n <= 97) { state.fg = "bright-" + ansiNames[n - 90]; }
456 else if (n >= 100 && n <= 107) { state.bg = "bright-" + ansiNames[n - 100]; }
457 else if ((n === 38 || n === 48) && nums[i + 1] === 5 && i + 2 < nums.length) {
458 var name = "";
459 if (nums[i + 2] >= 0 && nums[i + 2] <= 7) { name = ansiNames[nums[i + 2]]; }
460 else if (nums[i + 2] >= 8 && nums[i + 2] <= 15) { name = "bright-" + ansiNames[nums[i + 2] - 8]; }
461 if (name) {
462 if (n === 38) { state.fg = name; } else { state.bg = name; }
463 }
464 i += 2;
465 } else if ((n === 38 || n === 48) && nums[i + 1] === 2) {
466 i += 4;
467 }
468 }
469 }
470
471 function addLine(ts, text) {
472 lineNo += 1;
473 var row = document.createElement("div");
474 row.className = "logline";
475 row.setAttribute("data-line", String(lineNo));
476 var ln = document.createElement("span");
477 ln.className = "ln";
478 ln.textContent = String(lineNo);
479 var time = document.createElement("span");
480 time.className = "ts";
481 time.textContent = ts;
482 var code = document.createElement("code");
483 paint(code, text);
484 row.appendChild(ln);
485 row.appendChild(time);
486 row.appendChild(code);
487 if (currentSec) {
488 var list = currentSec.querySelector("ol");
489 var li = document.createElement("li");
490 li.className = "logline";
491 li.setAttribute("data-line", String(lineNo));
492 li.appendChild(ln);
493 li.appendChild(time);
494 li.appendChild(code);
495 list.appendChild(li);
496 row = li;
497 } else {
498 log.appendChild(row);
499 }
500 if (follow) {
501 log.scrollTop = log.scrollHeight;
502 }
503 }
504
505 function openSection(title) {
506 var sec = document.createElement("section");
507 sec.className = "logsec open";
508 var btn = document.createElement("button");
509 btn.type = "button";
510 btn.className = "section-toggle";
511 var chev = document.createElement("span");
512 chev.className = "chev";
513 var label = document.createElement("span");
514 label.className = "sectitle";
515 label.textContent = title;
516 btn.appendChild(chev);
517 btn.appendChild(label);
518 btn.addEventListener("click", function () { sec.classList.toggle("open"); });
519 var ol = document.createElement("ol");
520 sec.appendChild(btn);
521 sec.appendChild(ol);
522 log.appendChild(sec);
523 currentSec = sec;
524 }
525
526 function closeSection(dur) {
527 if (!currentSec) {
528 return;
529 }
530 if (dur) {
531 var pill = document.createElement("span");
532 pill.className = "pill";
533 pill.textContent = dur;
534 currentSec.querySelector("button").appendChild(pill);
535 }
536 currentSec = null;
537 }
538
539 function take(line) {
540 var tab = line.indexOf("\t");
541 var ts = "";
542 var text = line;
543 if (tab >= 0) {
544 ts = line.slice(0, tab);
545 text = line.slice(tab + 1);
546 }
547 if (ts.length >= 19 && ts.indexOf("T") === 10) {
548 ts = ts.slice(11, 19);
549 }
550 if (text.indexOf("section_start:") === 0) {
551 var body = text.slice("section_start:".length);
552 var colon = body.indexOf(":");
553 openSection(colon >= 0 ? body.slice(colon + 1) : body);
554 return;
555 }
556 if (text.indexOf("section_end:") === 0) {
557 var end = text.slice("section_end:".length);
558 var dtab = end.indexOf("\t");
559 closeSection(dtab >= 0 ? end.slice(dtab + 1) : "");
560 return;
561 }
562 addLine(ts, text);
563 }
564
565 function poll() {
566 fetch(url + "?offset=" + offset, { headers: { Accept: "application/json" } })
567 .then(function (res) { return res.json(); })
568 .then(function (data) {
569 var chunk = pending + (data.text || "");
570 var rows = chunk.split("\n");
571 pending = rows.pop();
572 rows.forEach(function (row) {
573 if (row) {
574 take(row);
575 }
576 });
577 offset = data.offset || offset;
578 var qEl = document.querySelector(".jobhead [data-queue]");
579 if (qEl) {
580 qEl.textContent = data.queue || "";
581 qEl.hidden = !data.queue;
582 }
583 if (data.status === "running" || data.status === "queued") {
584 setTimeout(poll, 1500);
585 return;
586 }
587 if (pending) {
588 take(pending);
589 pending = "";
590 }
591 window.location.reload();
592 })
593 .catch(function () {
594 setTimeout(poll, 3000);
595 });
596 }
597 setTimeout(poll, 1500);
598})();