BlueGit

timfa bluegit

Files

bluegit / internal / server / static / pipeline.js

pipeline.js

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