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