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>
272 lines
8.7 KiB
Go
272 lines
8.7 KiB
Go
package sqlite
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/kandev/kandev/internal/db/dialect"
|
|
"github.com/kandev/kandev/internal/task/models"
|
|
)
|
|
|
|
// AddTaskToWorkflow adds a task to a workflow with placement
|
|
func (r *Repository) AddTaskToWorkflow(ctx context.Context, taskID, workflowID, workflowStepID string, position int) error {
|
|
_, err := r.db.ExecContext(ctx, r.db.Rebind(`
|
|
UPDATE tasks SET workflow_id = ?, workflow_step_id = ?, position = ?, updated_at = ? WHERE id = ?
|
|
`), workflowID, workflowStepID, position, time.Now().UTC(), taskID)
|
|
return err
|
|
}
|
|
|
|
// RemoveTaskFromWorkflow removes a task from a workflow
|
|
func (r *Repository) RemoveTaskFromWorkflow(ctx context.Context, taskID, workflowID string) error {
|
|
_, err := r.db.ExecContext(ctx, r.db.Rebind(`
|
|
UPDATE tasks SET workflow_id = '', workflow_step_id = '', position = 0, updated_at = ? WHERE id = ? AND workflow_id = ?
|
|
`), time.Now().UTC(), taskID, workflowID)
|
|
return err
|
|
}
|
|
|
|
// Workflow operations
|
|
|
|
// CreateWorkflow creates a new workflow
|
|
func (r *Repository) CreateWorkflow(ctx context.Context, workflow *models.Workflow) error {
|
|
if workflow.ID == "" {
|
|
workflow.ID = uuid.New().String()
|
|
}
|
|
now := time.Now().UTC()
|
|
workflow.CreatedAt = now
|
|
workflow.UpdatedAt = now
|
|
|
|
// Auto-assign sort_order as max+1 within the workspace.
|
|
// Use writer connection (r.db) to avoid stale reads under concurrent creation.
|
|
var maxOrder int
|
|
err := r.db.QueryRowContext(ctx, r.db.Rebind(
|
|
`SELECT COALESCE(MAX(sort_order), -1) FROM workflows WHERE workspace_id = ?`,
|
|
), workflow.WorkspaceID).Scan(&maxOrder)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get max sort_order: %w", err)
|
|
}
|
|
workflow.SortOrder = maxOrder + 1
|
|
|
|
_, err = r.db.ExecContext(ctx, r.db.Rebind(`
|
|
INSERT INTO workflows (id, workspace_id, name, description, agent_profile_id, workflow_template_id, sort_order, hidden, style, source, source_path, created_at, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
`), workflow.ID, workflow.WorkspaceID, workflow.Name, workflow.Description, workflow.AgentProfileID, workflow.WorkflowTemplateID, workflow.SortOrder, dialect.BoolToInt(workflow.Hidden), normalizeWorkflowStyle(workflow.Style), normalizeWorkflowSource(workflow.Source), workflow.SourcePath, workflow.CreatedAt, workflow.UpdatedAt)
|
|
|
|
return err
|
|
}
|
|
|
|
// normalizeWorkflowSource returns a value fit for the workflows.source column.
|
|
// Empty or unknown sources collapse to the manual default.
|
|
func normalizeWorkflowSource(source string) string {
|
|
switch source {
|
|
case models.WorkflowSourceManual, models.WorkflowSourceGitHub:
|
|
return source
|
|
}
|
|
return models.WorkflowSourceManual
|
|
}
|
|
|
|
// normalizeWorkflowStyle returns a value fit for the workflows.style column.
|
|
// Empty or unknown styles collapse to the kanban default so existing callers
|
|
// stay schema-compliant without having to set the field explicitly.
|
|
func normalizeWorkflowStyle(style string) string {
|
|
switch style {
|
|
case models.WorkflowStyleKanban, models.WorkflowStyleOffice, models.WorkflowStyleCustom:
|
|
return style
|
|
}
|
|
return models.WorkflowStyleKanban
|
|
}
|
|
|
|
const workflowSelectColumns = `
|
|
id, workspace_id, name, description, agent_profile_id,
|
|
workflow_template_id, sort_order, hidden, style, source, source_path, created_at, updated_at
|
|
`
|
|
|
|
type workflowScanner interface {
|
|
Scan(dest ...interface{}) error
|
|
}
|
|
|
|
func scanWorkflowRow(scanner workflowScanner) (*models.Workflow, error) {
|
|
workflow := &models.Workflow{}
|
|
var workflowTemplateID, agentProfileID, style, source, sourcePath sql.NullString
|
|
var hidden int
|
|
if err := scanner.Scan(
|
|
&workflow.ID,
|
|
&workflow.WorkspaceID,
|
|
&workflow.Name,
|
|
&workflow.Description,
|
|
&agentProfileID,
|
|
&workflowTemplateID,
|
|
&workflow.SortOrder,
|
|
&hidden,
|
|
&style,
|
|
&source,
|
|
&sourcePath,
|
|
&workflow.CreatedAt,
|
|
&workflow.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
workflow.Hidden = hidden != 0
|
|
if agentProfileID.Valid {
|
|
workflow.AgentProfileID = agentProfileID.String
|
|
}
|
|
if workflowTemplateID.Valid {
|
|
workflow.WorkflowTemplateID = &workflowTemplateID.String
|
|
}
|
|
if style.Valid && style.String != "" {
|
|
workflow.Style = style.String
|
|
} else {
|
|
workflow.Style = models.WorkflowStyleKanban
|
|
}
|
|
workflow.Source = normalizeWorkflowSource(source.String)
|
|
if sourcePath.Valid {
|
|
workflow.SourcePath = sourcePath.String
|
|
}
|
|
return workflow, nil
|
|
}
|
|
|
|
func scanWorkflowRows(rows *sql.Rows) ([]*models.Workflow, error) {
|
|
var result []*models.Workflow
|
|
for rows.Next() {
|
|
workflow, err := scanWorkflowRow(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result = append(result, workflow)
|
|
}
|
|
return result, rows.Err()
|
|
}
|
|
|
|
// GetWorkflow retrieves a workflow by ID
|
|
func (r *Repository) GetWorkflow(ctx context.Context, id string) (*models.Workflow, error) {
|
|
workflow, err := scanWorkflowRow(r.ro.QueryRowContext(ctx, r.ro.Rebind(fmt.Sprintf(`
|
|
SELECT %s
|
|
FROM workflows WHERE id = ?
|
|
`, workflowSelectColumns)), id))
|
|
if err == sql.ErrNoRows {
|
|
return nil, fmt.Errorf("workflow not found: %s", id)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return workflow, nil
|
|
}
|
|
|
|
// UpdateWorkflow updates an existing workflow
|
|
func (r *Repository) UpdateWorkflow(ctx context.Context, workflow *models.Workflow) error {
|
|
workflow.UpdatedAt = time.Now().UTC()
|
|
|
|
result, err := r.db.ExecContext(ctx, r.db.Rebind(`
|
|
UPDATE workflows SET name = ?, description = ?, agent_profile_id = ?, workflow_template_id = ?, hidden = ?, style = ?, source = ?, source_path = ?, updated_at = ? WHERE id = ?
|
|
`), workflow.Name, workflow.Description, workflow.AgentProfileID, workflow.WorkflowTemplateID, dialect.BoolToInt(workflow.Hidden), normalizeWorkflowStyle(workflow.Style), normalizeWorkflowSource(workflow.Source), workflow.SourcePath, workflow.UpdatedAt, workflow.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
rows, _ := result.RowsAffected()
|
|
if rows == 0 {
|
|
return fmt.Errorf("workflow not found: %s", workflow.ID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteWorkflowsByWorkspace deletes all workflows for a workspace except the excluded IDs (E2E cleanup).
|
|
// Relies on CASCADE foreign keys to remove workflow_steps.
|
|
func (r *Repository) DeleteWorkflowsByWorkspace(ctx context.Context, workspaceID string, excludeIDs []string) (int64, error) {
|
|
if len(excludeIDs) == 0 {
|
|
result, err := r.db.ExecContext(ctx, r.db.Rebind(`DELETE FROM workflows WHERE workspace_id = ?`), workspaceID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
rows, _ := result.RowsAffected()
|
|
return rows, nil
|
|
}
|
|
|
|
query, args, err := sqlx.In(`DELETE FROM workflows WHERE workspace_id = ? AND id NOT IN (?)`, workspaceID, excludeIDs)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
result, err := r.db.ExecContext(ctx, r.db.Rebind(query), args...)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
rows, _ := result.RowsAffected()
|
|
return rows, nil
|
|
}
|
|
|
|
// DeleteWorkflow deletes a workflow by ID
|
|
func (r *Repository) DeleteWorkflow(ctx context.Context, id string) error {
|
|
result, err := r.db.ExecContext(ctx, r.db.Rebind(`DELETE FROM workflows WHERE id = ?`), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
rows, _ := result.RowsAffected()
|
|
if rows == 0 {
|
|
return fmt.Errorf("workflow not found: %s", id)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListWorkflows returns workflows for the given workspace, excluding hidden by default.
|
|
// Pass includeHidden=true to also return system-only workflows like Improve Kandev.
|
|
func (r *Repository) ListWorkflows(ctx context.Context, workspaceID string, includeHidden bool) ([]*models.Workflow, error) {
|
|
query := fmt.Sprintf(`
|
|
SELECT %s
|
|
FROM workflows
|
|
`, workflowSelectColumns)
|
|
var args []interface{}
|
|
var conditions []string
|
|
if workspaceID != "" {
|
|
conditions = append(conditions, "workspace_id = ?")
|
|
args = append(args, workspaceID)
|
|
}
|
|
if !includeHidden {
|
|
conditions = append(conditions, "hidden = 0")
|
|
}
|
|
for i, c := range conditions {
|
|
if i == 0 {
|
|
query += " WHERE " + c
|
|
} else {
|
|
query += " AND " + c
|
|
}
|
|
}
|
|
query += " ORDER BY sort_order ASC, created_at ASC"
|
|
|
|
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query), args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = rows.Close() }()
|
|
|
|
return scanWorkflowRows(rows)
|
|
}
|
|
|
|
// ReorderWorkflows updates sort_order for workflows within a workspace using a transaction.
|
|
func (r *Repository) ReorderWorkflows(ctx context.Context, workspaceID string, workflowIDs []string) error {
|
|
tx, err := r.db.BeginTxx(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to begin transaction: %w", err)
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
now := time.Now().UTC()
|
|
for i, id := range workflowIDs {
|
|
result, err := tx.ExecContext(ctx, r.db.Rebind(
|
|
`UPDATE workflows SET sort_order = ?, updated_at = ? WHERE id = ? AND workspace_id = ?`,
|
|
), i, now, id, workspaceID)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to update sort_order for workflow %s: %w", id, err)
|
|
}
|
|
rows, _ := result.RowsAffected()
|
|
if rows == 0 {
|
|
return fmt.Errorf("workflow not found in workspace: %s", id)
|
|
}
|
|
}
|
|
return tx.Commit()
|
|
}
|