chore: update src/pages/settings/automations.astro
swadhinbiswas committed 8 months ago
bf58605
1 parent 659a2a1
commit bf58605
1 file changed
+146 -8
Showing 1 changed file+146-8
▼src/pages/settings/automations.astro
+146-8
| @@ -85,7 +85,7 @@ try { | ||
85 | 85 | actions: r.actions, |
86 | 86 | isEnabled: r.isEnabled ?? true, |
87 | 87 | runCount: r.runCount || 0, |
88 | - lastRunAt: r.lastRunAt || undefined, | |
88 | + lastRunAt: r.lastRunAt ? r.lastRunAt.toISOString() : undefined, | |
89 | 89 | })); |
90 | 90 | |
91 | 91 | // Get recent executions |
| @@ -106,7 +106,7 @@ try { | ||
106 | 106 | ruleName: e.rule?.name || "Unknown", |
107 | 107 | triggerEvent: e.triggerEvent, |
108 | 108 | status: e.status, |
109 | - createdAt: e.createdAt, | |
109 | + createdAt: e.createdAt.toISOString(), | |
110 | 110 | durationMs: e.durationMs || undefined, |
111 | 111 | })); |
112 | 112 | } |
| @@ -266,6 +266,10 @@ function parseActions(actionsJson: string): { type: string; label: string }[] { | ||
266 | 266 | <div |
267 | 267 | class="group flex items-start gap-4 p-4 transition-all hover:bg-white/[0.02]" |
268 | 268 | data-rule-id={rule.id} |
269 | + data-rule-name={rule.name} | |
270 | + data-rule-trigger={rule.trigger} | |
271 | + data-rule-description={rule.description || ""} | |
272 | + data-rule-actions={rule.actions} | |
269 | 273 | > |
270 | 274 | {/* Trigger Icon */} |
271 | 275 | <div class="flex-shrink-0 p-2 rounded-lg bg-white/5"> |
| @@ -322,6 +326,7 @@ function parseActions(actionsJson: string): { type: string; label: string }[] { | ||
322 | 326 | class="p-1.5 rounded hover:bg-white/10 text-gray-400 hover:text-white transition-colors" |
323 | 327 | title={rule.isEnabled ? "Disable" : "Enable"} |
324 | 328 | data-action="toggle" |
329 | + data-enabled={rule.isEnabled ? "true" : "false"} | |
325 | 330 | > |
326 | 331 | {rule.isEnabled ? ( |
327 | 332 | <Pause className="h-4 w-4" /> |
| @@ -448,14 +453,64 @@ function parseActions(actionsJson: string): { type: string; label: string }[] { | ||
448 | 453 | window.location.href = url.toString(); |
449 | 454 | }); |
450 | 455 | |
451 | - // Add rule button | |
452 | - document.getElementById("add-rule-btn")?.addEventListener("click", () => { | |
453 | - // For now, show a placeholder - full modal would be implemented | |
454 | - alert("Automation builder coming soon! Use templates below to get started."); | |
456 | + async function createRuleFromPrompts() { | |
457 | + if (!repoId) return; | |
458 | + | |
459 | + const name = prompt("Automation name", "New Automation Rule"); | |
460 | + if (!name) return; | |
461 | + | |
462 | + const description = prompt("Description (optional)", "") || ""; | |
463 | + const trigger = prompt( | |
464 | + "Trigger event (e.g. pr_opened, pr_approved, ci_failed)", | |
465 | + "pr_opened", | |
466 | + ); | |
467 | + if (!trigger) return; | |
468 | + | |
469 | + const actionType = prompt( | |
470 | + "Action type (e.g. assign_reviewer, add_to_merge_queue, trigger_ai_review)", | |
471 | + "trigger_ai_review", | |
472 | + ); | |
473 | + if (!actionType) return; | |
474 | + | |
475 | + const payload = { | |
476 | + repositoryId: repoId, | |
477 | + name, | |
478 | + description, | |
479 | + trigger, | |
480 | + actions: JSON.stringify([{ type: actionType, params: {} }]), | |
481 | + }; | |
482 | + | |
483 | + const res = await fetch("/api/automation-rules", { | |
484 | + method: "POST", | |
485 | + headers: { "Content-Type": "application/json" }, | |
486 | + body: JSON.stringify(payload), | |
487 | + }); | |
488 | + | |
489 | + if (!res.ok) { | |
490 | + alert("Failed to create automation"); | |
491 | + return; | |
492 | + } | |
493 | + | |
494 | + window.location.reload(); | |
495 | + } | |
496 | + | |
497 | + // Add rule buttons | |
498 | + document.getElementById("add-rule-btn")?.addEventListener("click", async () => { | |
499 | + try { | |
500 | + await createRuleFromPrompts(); | |
501 | + } catch (e) { | |
502 | + console.error("Failed to create automation:", e); | |
503 | + alert("Failed to create automation"); | |
504 | + } | |
455 | 505 | }); |
456 | 506 | |
457 | - document.getElementById("add-first-rule-btn")?.addEventListener("click", () => { | |
458 | - alert("Automation builder coming soon! Use templates below to get started."); | |
507 | + document.getElementById("add-first-rule-btn")?.addEventListener("click", async () => { | |
508 | + try { | |
509 | + await createRuleFromPrompts(); | |
510 | + } catch (e) { | |
511 | + console.error("Failed to create automation:", e); | |
512 | + alert("Failed to create automation"); | |
513 | + } | |
459 | 514 | }); |
460 | 515 | |
461 | 516 | // Template buttons |
| @@ -499,4 +554,87 @@ function parseActions(actionsJson: string): { type: string; label: string }[] { | ||
499 | 554 | } |
500 | 555 | }); |
501 | 556 | }); |
557 | + | |
558 | + // Rule row action handlers (toggle/edit/delete) | |
559 | + document.querySelectorAll("[data-rule-id]").forEach((row) => { | |
560 | + const ruleId = row.getAttribute("data-rule-id"); | |
561 | + if (!ruleId) return; | |
562 | + | |
563 | + row.querySelector('[data-action="toggle"]')?.addEventListener("click", async () => { | |
564 | + const toggleBtn = row.querySelector('[data-action="toggle"]'); | |
565 | + const enabled = toggleBtn?.getAttribute("data-enabled") === "true"; | |
566 | + | |
567 | + try { | |
568 | + const res = await fetch(`/api/automation-rules?id=${encodeURIComponent(ruleId)}`, { | |
569 | + method: "PATCH", | |
570 | + headers: { "Content-Type": "application/json" }, | |
571 | + body: JSON.stringify({ isEnabled: !enabled }), | |
572 | + }); | |
573 | + if (res.ok) { | |
574 | + window.location.reload(); | |
575 | + } else { | |
576 | + alert("Failed to update automation"); | |
577 | + } | |
578 | + } catch (e) { | |
579 | + console.error("Failed to toggle automation:", e); | |
580 | + } | |
581 | + }); | |
582 | + | |
583 | + row.querySelector('[data-action="edit"]')?.addEventListener("click", async () => { | |
584 | + const currentName = row.getAttribute("data-rule-name") || ""; | |
585 | + const currentDescription = row.getAttribute("data-rule-description") || ""; | |
586 | + const currentTrigger = row.getAttribute("data-rule-trigger") || "pr_opened"; | |
587 | + const actionsRaw = row.getAttribute("data-rule-actions") || "[]"; | |
588 | + | |
589 | + let currentActionType = "trigger_ai_review"; | |
590 | + try { | |
591 | + const parsed = JSON.parse(actionsRaw); | |
592 | + if (Array.isArray(parsed) && parsed[0]?.type) currentActionType = parsed[0].type; | |
593 | + } catch {} | |
594 | + | |
595 | + const name = prompt("Automation name", currentName); | |
596 | + if (!name) return; | |
597 | + const description = prompt("Description (optional)", currentDescription) || ""; | |
598 | + const trigger = prompt("Trigger event", currentTrigger); | |
599 | + if (!trigger) return; | |
600 | + const actionType = prompt("Action type", currentActionType); | |
601 | + if (!actionType) return; | |
602 | + | |
603 | + try { | |
604 | + const res = await fetch(`/api/automation-rules?id=${encodeURIComponent(ruleId)}`, { | |
605 | + method: "PATCH", | |
606 | + headers: { "Content-Type": "application/json" }, | |
607 | + body: JSON.stringify({ | |
608 | + name, | |
609 | + description, | |
610 | + trigger, | |
611 | + actions: JSON.stringify([{ type: actionType, params: {} }]), | |
612 | + }), | |
613 | + }); | |
614 | + if (res.ok) { | |
615 | + window.location.reload(); | |
616 | + } else { | |
617 | + alert("Failed to update automation"); | |
618 | + } | |
619 | + } catch (e) { | |
620 | + console.error("Failed to edit automation:", e); | |
621 | + } | |
622 | + }); | |
623 | + | |
624 | + row.querySelector('[data-action="delete"]')?.addEventListener("click", async () => { | |
625 | + if (!confirm("Delete this automation rule?")) return; | |
626 | + try { | |
627 | + const res = await fetch(`/api/automation-rules?id=${encodeURIComponent(ruleId)}`, { | |
628 | + method: "DELETE", | |
629 | + }); | |
630 | + if (res.ok) { | |
631 | + window.location.reload(); | |
632 | + } else { | |
633 | + alert("Failed to delete automation"); | |
634 | + } | |
635 | + } catch (e) { | |
636 | + console.error("Failed to delete automation:", e); | |
637 | + } | |
638 | + }); | |
639 | + }); | |
502 | 640 | </script> |
503 | 641 | |