2a1f7c2edf
* feat: sync workflows from a configured github repo Per-workspace setting pointing at a GitHub repo directory of portable workflow export files. A background poller fetches it on an interval and creates, updates, or deletes the matching workflows; a force-sync endpoint bypasses the content-hash check. Synced workflows carry source/source_path provenance and coexist with manual ones; changes that cannot be applied safely (steps or workflows that still hold tasks, ambiguous step names, unparseable files) surface as warnings in the new settings section. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): fill workflow sync form from a pasted github link A new Repository link field parses plain repo URLs, SSH remotes, and tree/blob links (which also carry branch and directory) into the structured owner/repo/branch/path fields. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): simplify workflow sync form to link, branch, and interval The repository link is now the only repo input: owner, repo, and directory resolve from the pasted URL (echoed as a summary under the field), and a stored config renders back into a canonical tree link via buildGitHubRepoUrl. Branch and poll interval stay individually editable. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): collapse workflow sync into a button, dialog, and status card The Workflows settings page now shows only a GitHub Sync button by default; the link/branch/interval form lives in a dialog that closes on successful save or removal. Once configured, a compact status card (mirroring the GitHub connection-status card) shows the synced repo, branch, directory, last result, warnings, and a Sync now action. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): move github sync button into the workflows page header The GitHub Sync trigger now stacks under the Workspace settings button on the top right, and the extra separator around the sync area is gone — unconfigured workspaces show no sync chrome at all, configured ones show just the status card above the workflow list. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): move github sync button next to the workflow actions GitHub Sync now sits in the Workflows section toolbar alongside Export All, Import, and Add Workflow; the status card renders inside the section above the workflow list. The toolbar moved to its own component to keep the page under the file-size limit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): separate sync status card from the workflow list Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(web): align sync status card with the workflow list gutter Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: repair local edits to synced workflows on periodic sync Periodic syncs previously skipped applying when the repo content hash was unchanged, so local UI edits to a synced workflow were only reverted by a manual Sync now. Every sync now reconciles against the repo; the applier compares each step against its definition and only writes (and broadcasts) actual differences, so a no-drift sync stays silent. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: make github-synced workflows read-only Workflows with source=github reject UI mutations: workflow rename, description, agent-profile, and delete return 409 from the task-domain handlers; step create/update/delete/reorder and template application are guarded in the workflow controller. The sync applier writes through the service/repo layer and stays exempt. The frontend disables the card's inputs, save/delete, and the step pipeline editor for synced workflows, with a tooltip pointing at the synced repository. Drift repair on sync remains as the backstop. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: optional workflow sync polling and repo-derived branch picker A per-workspace auto-sync toggle (poll_enabled, default on) controls the background polling loop; when off the workspace only syncs via Sync now. The dialog's branch field becomes a select fed by the repository's actual branches (same loader the task-create dialog uses), falling back to free text until branches load. Existing dev databases gain the new column via an idempotent ALTER. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: release synced workflows when the sync config is removed Deleting a workspace's workflow sync configuration converts its GitHub-sourced workflows back to manual ownership (source=manual), so they become editable instead of being stranded read-only. Release runs before the config row is deleted so a failure keeps the config for retry. The settings page reloads after removal (and after syncs that changed workflows) so cards unlock without a manual refresh. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): drop branch selector and compact the auto-sync row The pasted link already carries the branch (main by default), so the dialog no longer needs a branch field — the resolved summary echoes it. The auto-sync toggle is now one compact row with the interval inline and a single short helper line. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: address PR review feedback Backend: fail-open read-only guard on workflow lookup errors (fixes MCP step handler CI failures); poller Stop holds its mutex through drain; per-workspace lock serializes syncs against config delete/replace; rollback workflow creation when provenance stamping fails; idiomatic duplicate-column migration via db.IsDuplicateColumnError; branch-name validation and a 30-day interval cap; generic 500s with logged detail; WS read-only rejections use CONFLICT with the real reason; e2e reset aborts when sync-config cleanup fails; test hygiene (t.Cleanup, SetMaxOpenConns). Frontend: malformed percent escapes parse as invalid instead of throwing; save disabled during initial load and for invalid intervals; Remove disabled while saving; stale async config responses discarded; delete-dialog confirms re-check read-only; focusable Synced badge; explicit button types; failed polls labeled 'last attempt'; unique warning list keys; test cleanup via afterEach. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: guard workflow sync test fake against concurrent poller access The poller invokes the fake applier from its own goroutine while the synctest test asserts on call counts; -race caught the unguarded reads. Also documents that the per-workspace lock is deliberately held across the GitHub fetch. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
282 lines
9.5 KiB
Go
282 lines
9.5 KiB
Go
package workflowsync
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"github.com/kandev/kandev/internal/common/logger"
|
|
"github.com/kandev/kandev/internal/github"
|
|
workflowmodels "github.com/kandev/kandev/internal/workflow/models"
|
|
workflowservice "github.com/kandev/kandev/internal/workflow/service"
|
|
)
|
|
|
|
// Applier applies parsed workflow definition files to a workspace and
|
|
// releases synced workflows back to manual ownership when syncing stops.
|
|
// Satisfied by the workflow service.
|
|
type Applier interface {
|
|
ApplySyncedWorkflows(ctx context.Context, workspaceID string, files []workflowservice.SyncFileExport) (*workflowservice.SyncApplyResult, error)
|
|
ReleaseSyncedWorkflows(ctx context.Context, workspaceID string) ([]string, error)
|
|
}
|
|
|
|
// ClientProvider hands out the GitHub client used to read the sync repo.
|
|
// Satisfied by the GitHub service; Client() may return nil when GitHub is not
|
|
// authenticated.
|
|
type ClientProvider interface {
|
|
Client() github.Client
|
|
}
|
|
|
|
// Service owns workflow sync configuration and sync execution.
|
|
type Service struct {
|
|
store *Store
|
|
clients ClientProvider
|
|
applier Applier
|
|
logger *logger.Logger
|
|
// locks serializes syncs and config mutations per workspace so a force
|
|
// sync cannot interleave with a config delete/replace and apply stale
|
|
// definitions (or re-stamp workflows that were just released). The lock
|
|
// is deliberately held across the GitHub fetch too: a config change or
|
|
// delete for that workspace waits (bounded by the HTTP client timeout)
|
|
// rather than racing an in-flight apply.
|
|
locks sync.Map // workspaceID → *sync.Mutex
|
|
}
|
|
|
|
func (s *Service) workspaceLock(workspaceID string) *sync.Mutex {
|
|
lock, _ := s.locks.LoadOrStore(workspaceID, &sync.Mutex{})
|
|
return lock.(*sync.Mutex)
|
|
}
|
|
|
|
// NewService creates a workflow sync service.
|
|
func NewService(store *Store, clients ClientProvider, applier Applier, log *logger.Logger) *Service {
|
|
return &Service{
|
|
store: store,
|
|
clients: clients,
|
|
applier: applier,
|
|
logger: log.WithFields(zap.String("component", "workflowsync-service")),
|
|
}
|
|
}
|
|
|
|
// Store exposes the config store (e2e reset cascade).
|
|
func (s *Service) Store() *Store {
|
|
return s.store
|
|
}
|
|
|
|
// GetConfigForWorkspace returns the workspace's config, or nil when unset.
|
|
func (s *Service) GetConfigForWorkspace(ctx context.Context, workspaceID string) (*Config, error) {
|
|
return s.store.GetConfigForWorkspace(ctx, workspaceID)
|
|
}
|
|
|
|
// SetConfigForWorkspace validates and stores the workspace's config.
|
|
func (s *Service) SetConfigForWorkspace(ctx context.Context, workspaceID string, req *SetConfigRequest) (*Config, error) {
|
|
if err := req.Normalize(); err != nil {
|
|
return nil, err
|
|
}
|
|
lock := s.workspaceLock(workspaceID)
|
|
lock.Lock()
|
|
defer lock.Unlock()
|
|
return s.store.UpsertConfigForWorkspace(ctx, workspaceID, req)
|
|
}
|
|
|
|
// DeleteConfigForWorkspace removes the workspace's config. Previously-synced
|
|
// workflows are released back to manual ownership first so they become
|
|
// editable again — a failed release keeps the config so the user can retry.
|
|
func (s *Service) DeleteConfigForWorkspace(ctx context.Context, workspaceID string) error {
|
|
lock := s.workspaceLock(workspaceID)
|
|
lock.Lock()
|
|
defer lock.Unlock()
|
|
released, err := s.applier.ReleaseSyncedWorkflows(ctx, workspaceID)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to release synced workflows: %w", err)
|
|
}
|
|
if len(released) > 0 {
|
|
s.logger.Info("released synced workflows",
|
|
zap.String("workspace_id", workspaceID), zap.Int("count", len(released)))
|
|
}
|
|
return s.store.DeleteConfigForWorkspace(ctx, workspaceID)
|
|
}
|
|
|
|
// syncableExtensions are the file extensions read from the sync directory.
|
|
var syncableExtensions = []string{".yml", ".yaml", ".json"}
|
|
|
|
func isSyncableFile(name string) bool {
|
|
lower := strings.ToLower(name)
|
|
for _, ext := range syncableExtensions {
|
|
if strings.HasSuffix(lower, ext) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// fetchedFile is one definition file read from the repo.
|
|
type fetchedFile struct {
|
|
path string
|
|
content []byte
|
|
}
|
|
|
|
// SyncWorkspace fetches the configured repo directory and reconciles the
|
|
// workspace's synced workflows with it. Every sync applies the definitions —
|
|
// including repairing local edits to synced workflows — but the applier only
|
|
// writes (and broadcasts) what actually differs, so a no-drift sync is
|
|
// silent. The outcome (including failures) is recorded on the config row so
|
|
// the UI can surface it.
|
|
func (s *Service) SyncWorkspace(ctx context.Context, workspaceID string) (*SyncResult, error) {
|
|
lock := s.workspaceLock(workspaceID)
|
|
lock.Lock()
|
|
defer lock.Unlock()
|
|
cfg, err := s.store.GetConfigForWorkspace(ctx, workspaceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if cfg == nil {
|
|
return nil, ErrNotConfigured
|
|
}
|
|
|
|
files, err := s.fetchFiles(ctx, cfg)
|
|
if err != nil {
|
|
s.recordFailure(ctx, workspaceID, err)
|
|
return nil, err
|
|
}
|
|
|
|
parsed, warnings := parseFiles(files)
|
|
applied, err := s.applier.ApplySyncedWorkflows(ctx, workspaceID, parsed)
|
|
if err != nil {
|
|
s.recordFailure(ctx, workspaceID, err)
|
|
return nil, err
|
|
}
|
|
warnings = append(warnings, applied.Warnings...)
|
|
if err := s.store.RecordSyncStatus(ctx, workspaceID, true, "", warnings, contentHash(files), time.Now().UTC()); err != nil {
|
|
return nil, err
|
|
}
|
|
return &SyncResult{
|
|
Created: applied.Created,
|
|
Updated: applied.Updated,
|
|
Deleted: applied.Deleted,
|
|
Warnings: warnings,
|
|
Unchanged: len(applied.Created)+len(applied.Updated)+len(applied.Deleted) == 0 && len(warnings) == 0,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Service) recordFailure(ctx context.Context, workspaceID string, syncErr error) {
|
|
// Clear the hash so the next successful fetch re-applies from scratch.
|
|
if err := s.store.RecordSyncStatus(ctx, workspaceID, false, syncErr.Error(), nil, "", time.Now().UTC()); err != nil {
|
|
s.logger.Warn("failed to record sync failure",
|
|
zap.String("workspace_id", workspaceID), zap.Error(err))
|
|
}
|
|
}
|
|
|
|
// fetchFiles lists the configured directory and downloads every workflow
|
|
// definition file in it (non-recursive).
|
|
func (s *Service) fetchFiles(ctx context.Context, cfg *Config) ([]fetchedFile, error) {
|
|
client := s.clients.Client()
|
|
if client == nil {
|
|
return nil, fmt.Errorf("GitHub is not authenticated; configure a GitHub token to sync workflows")
|
|
}
|
|
entries, err := client.ListRepoDirectory(ctx, cfg.RepoOwner, cfg.RepoName, cfg.Path, cfg.Branch)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to list %s/%s@%s:%s: %w", cfg.RepoOwner, cfg.RepoName, cfg.Branch, cfg.Path, err)
|
|
}
|
|
var files []fetchedFile
|
|
for _, entry := range entries {
|
|
if entry.Type != "file" || !isSyncableFile(entry.Name) {
|
|
continue
|
|
}
|
|
content, err := client.GetRepoFileContent(ctx, cfg.RepoOwner, cfg.RepoName, entry.Path, cfg.Branch)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to fetch %s: %w", entry.Path, err)
|
|
}
|
|
files = append(files, fetchedFile{path: entry.Path, content: content})
|
|
}
|
|
sort.Slice(files, func(i, j int) bool { return files[i].path < files[j].path })
|
|
return files, nil
|
|
}
|
|
|
|
// contentHash is a stable digest of the fetched file set. It is recorded on
|
|
// the config row for observability only — every sync reconciles regardless
|
|
// (repairing local drift), with the applier writing only actual differences.
|
|
func contentHash(files []fetchedFile) string {
|
|
h := sha256.New()
|
|
for _, f := range files {
|
|
_, _ = fmt.Fprintf(h, "%s\x00%d\x00", f.path, len(f.content))
|
|
h.Write(f.content)
|
|
}
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
// parseFiles decodes and validates each fetched file. Files that fail to
|
|
// parse are reported as warnings and passed through with a nil export, which
|
|
// tells the applier to leave their previously-synced workflows untouched.
|
|
func parseFiles(files []fetchedFile) ([]workflowservice.SyncFileExport, []string) {
|
|
parsed := make([]workflowservice.SyncFileExport, 0, len(files))
|
|
var warnings []string
|
|
for _, f := range files {
|
|
export, err := parseExport(f.path, f.content)
|
|
if err != nil {
|
|
warnings = append(warnings, fmt.Sprintf("%s: %v", f.path, err))
|
|
parsed = append(parsed, workflowservice.SyncFileExport{Path: f.path})
|
|
continue
|
|
}
|
|
parsed = append(parsed, workflowservice.SyncFileExport{Path: f.path, Export: export})
|
|
}
|
|
return parsed, warnings
|
|
}
|
|
|
|
func parseExport(path string, data []byte) (*workflowmodels.WorkflowExport, error) {
|
|
export := &workflowmodels.WorkflowExport{}
|
|
var err error
|
|
if strings.HasSuffix(strings.ToLower(path), ".json") {
|
|
err = json.Unmarshal(data, export)
|
|
} else {
|
|
err = yaml.Unmarshal(data, export)
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("not a valid workflow export file: %w", err)
|
|
}
|
|
if err := export.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
return export, nil
|
|
}
|
|
|
|
// SyncDueConfigs runs a periodic sync for every workspace whose interval has
|
|
// elapsed. Failures are recorded on the config row and logged, never fatal.
|
|
func (s *Service) SyncDueConfigs(ctx context.Context) {
|
|
configs, err := s.store.ListConfigs(ctx)
|
|
if err != nil {
|
|
s.logger.Warn("failed to list workflow sync configs", zap.Error(err))
|
|
return
|
|
}
|
|
now := time.Now().UTC()
|
|
for _, cfg := range configs {
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
if !isSyncDue(cfg, now) {
|
|
continue
|
|
}
|
|
if _, err := s.SyncWorkspace(ctx, cfg.WorkspaceID); err != nil {
|
|
s.logger.Warn("periodic workflow sync failed",
|
|
zap.String("workspace_id", cfg.WorkspaceID), zap.Error(err))
|
|
}
|
|
}
|
|
}
|
|
|
|
func isSyncDue(cfg *Config, now time.Time) bool {
|
|
if !cfg.PollEnabled {
|
|
return false
|
|
}
|
|
if cfg.LastSyncedAt == nil {
|
|
return true
|
|
}
|
|
return now.Sub(*cfg.LastSyncedAt) >= time.Duration(cfg.IntervalSeconds)*time.Second
|
|
}
|