BlueGit

timfa bluegit

Files

bluegit / internal / server / static / pipeline.js

pipeline.js

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