gitlink-cli/shortcuts/workflow/trigger.go

131 lines
3.4 KiB
Go

package workflow
import (
"fmt"
"os"
"os/signal"
"time"
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
)
// Watch polls the first step (or watchStep) every interval and triggers the
// full workflow (with AI) only when data changes. Blocks until Ctrl+C.
func Watch(ctx *common.RuntimeContext, wf *WorkflowDef, interval time.Duration, watchStep string) error {
if watchStep == "" && len(wf.Steps) > 0 {
watchStep = wf.Steps[0].Name
}
fmt.Printf("👀 Watching %s/%s for %q changes every %v\n", ctx.Owner, ctx.Repo, watchStep, interval)
fmt.Printf(" Trigger: %s on %s\n", wf.Trigger.Type, wf.Trigger.On)
fmt.Println(" Press Ctrl+C to stop")
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
state, _ := LoadState(wf.Name)
tick := time.NewTicker(interval)
defer tick.Stop()
for {
select {
case <-sig:
fmt.Println("\n👋 watch stopped")
return nil
case t := <-tick.C:
// Phase 1: cheap dry-run to check for changes
dryResult, err := Run(ctx, wf, true)
if err != nil {
fmt.Printf("[%s] ❌ error: %v\n", t.Format("15:04:05"), err)
continue
}
changed := state.Diff(dryResult.Steps)
if len(changed) == 0 && state.TotalRuns > 0 {
fmt.Printf("[%s] ✓ no changes\n", t.Format("15:04:05"))
continue
}
fmt.Printf("[%s] 🔔 change detected: %v\n", t.Format("15:04:05"), changed)
// Phase 2: full run with AI
result, err := Run(ctx, wf, false)
if err != nil {
fmt.Printf("[%s] ❌ AI run error: %v\n", t.Format("15:04:05"), err)
continue
}
state.UpdateSnapshots(result.Steps)
state.TotalRuns++
state.Save()
for _, sr := range result.Steps {
if sr.OK {
fmt.Printf(" ✓ %s\n", sr.Step)
} else {
fmt.Printf(" ✗ %s: %s\n", sr.Step, sr.Error)
}
}
}
}
}
// Schedule runs the full workflow on a repeating interval. Blocks until Ctrl+C.
// Schedule always runs with AI (cron-style workflows like weekly reports always need fresh output).
func Schedule(ctx *common.RuntimeContext, wf *WorkflowDef, interval time.Duration) error {
fmt.Printf("⏰ Scheduled %q every %v on %s/%s\n", wf.Name, interval, ctx.Owner, ctx.Repo)
fmt.Println(" Press Ctrl+C to stop")
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
tick := time.NewTicker(interval)
defer tick.Stop()
// Run immediately on start (dry-run to establish baseline)
state, _ := LoadState(wf.Name)
dryResult, _ := Run(ctx, wf, true)
state.UpdateSnapshots(dryResult.Steps)
state.TotalRuns++
state.Save()
for {
select {
case <-sig:
fmt.Println("\n👋 schedule stopped")
return nil
case t := <-tick.C:
// Phase 1: dry-run to check for changes
dryResult, err := Run(ctx, wf, true)
if err != nil {
fmt.Printf("[%s] ❌ error: %v\n", t.Format("15:04:05"), err)
continue
}
changed := state.Diff(dryResult.Steps)
state.UpdateSnapshots(dryResult.Steps)
state.TotalRuns++
state.Save()
if len(changed) == 0 {
fmt.Printf("[%s] ✓ no changes, skipped AI run\n", t.Format("15:04:05"))
continue
}
// Phase 2: full run with AI
fmt.Printf("[%s] ⏳ changes detected, running %q with AI...\n", t.Format("15:04:05"), wf.Name)
result, err := Run(ctx, wf, false)
if err != nil {
fmt.Printf("❌ error: %v\n", err)
continue
}
ok, total := 0, len(result.Steps)
for _, sr := range result.Steps {
if sr.OK {
ok++
}
}
fmt.Printf("✅ %d/%d steps OK\n", ok, total)
}
}
}