diff --git a/cmd/bd/ado.go b/cmd/bd/ado.go index d7bd214d2..50c817ee8 100644 --- a/cmd/bd/ado.go +++ b/cmd/bd/ado.go @@ -162,6 +162,7 @@ func init() { adoSyncCmd.Flags().StringVar(&adoFilterTypes, "types", "", "Filter to work item types, comma-separated (e.g., \"Bug,Task,User Story\")") adoSyncCmd.Flags().StringVar(&adoFilterStates, "states", "", "Filter to ADO states, comma-separated (e.g., \"New,Active,Resolved\")") adoSyncCmd.Flags().StringSlice("project", nil, "Project name(s) to sync (overrides configured project/projects)") + registerSelectiveSyncFlags(adoSyncCmd) // Register ado command with root rootCmd.AddCommand(adoCmd) @@ -537,6 +538,10 @@ func runADOSync(cmd *cobra.Command, _ []string) error { DryRun: adoSyncDryRun, } + if err := applySelectiveSyncFlags(cmd, &opts, push); err != nil { + return err + } + // Map conflict resolution switch conflictStrategy { case ADOConflictPreferLocal: diff --git a/cmd/bd/github.go b/cmd/bd/github.go index 7a9ab6ce0..c9f2a9c46 100644 --- a/cmd/bd/github.go +++ b/cmd/bd/github.go @@ -152,6 +152,7 @@ func init() { githubSyncCmd.Flags().BoolVar(&githubPreferLocal, "prefer-local", false, "On conflict, keep local beads version") githubSyncCmd.Flags().BoolVar(&githubPreferGitHub, "prefer-github", false, "On conflict, use GitHub version") githubSyncCmd.Flags().BoolVar(&githubPreferNewer, "prefer-newer", false, "On conflict, use most recent version (default)") + registerSelectiveSyncFlags(githubSyncCmd) // Register github command with root rootCmd.AddCommand(githubCmd) @@ -380,6 +381,10 @@ func runGitHubSync(cmd *cobra.Command, args []string) error { DryRun: githubSyncDryRun, } + if err := applySelectiveSyncFlags(cmd, &opts, push); err != nil { + return err + } + // Map conflict resolution switch conflictStrategy { case GitHubConflictPreferLocal: diff --git a/cmd/bd/gitlab.go b/cmd/bd/gitlab.go index 432fbe296..e2bc06b6a 100644 --- a/cmd/bd/gitlab.go +++ b/cmd/bd/gitlab.go @@ -191,6 +191,7 @@ func init() { gitlabSyncCmd.Flags().StringVar(&gitlabFilterProject, "project", "", "Filter to issues from this project ID (group mode)") gitlabSyncCmd.Flags().StringVar(&gitlabFilterMilestone, "milestone", "", "Filter by milestone title") gitlabSyncCmd.Flags().StringVar(&gitlabFilterAssignee, "assignee", "", "Filter by assignee username") + registerSelectiveSyncFlags(gitlabSyncCmd) // Type filtering flags gitlabSyncCmd.Flags().StringVar(&gitlabTypeFilter, "type", "", "Only sync these issue types (comma-separated, e.g. 'epic,feature,task')") @@ -472,6 +473,10 @@ func runGitLabSync(cmd *cobra.Command, args []string) error { ExcludeTypes: excludeTypes, } + if err := applySelectiveSyncFlags(cmd, &opts, push); err != nil { + return err + } + // Map conflict resolution switch conflictStrategy { case ConflictStrategyPreferLocal: diff --git a/cmd/bd/jira.go b/cmd/bd/jira.go index cbdd8d466..c4fbc2d7c 100644 --- a/cmd/bd/jira.go +++ b/cmd/bd/jira.go @@ -83,6 +83,7 @@ func init() { jiraSyncCmd.Flags().Bool("create-only", false, "Only create new issues, don't update existing") jiraSyncCmd.Flags().String("state", "all", "Issue state to sync: open, closed, all") jiraSyncCmd.Flags().StringSlice("project", nil, "Project key(s) to sync (overrides configured project/projects)") + registerSelectiveSyncFlags(jiraSyncCmd) jiraCmd.AddCommand(jiraSyncCmd) jiraCmd.AddCommand(jiraStatusCmd) @@ -143,6 +144,10 @@ func runJiraSync(cmd *cobra.Command, args []string) { State: state, } + if err := applySelectiveSyncFlags(cmd, &opts, push); err != nil { + FatalError("%v", err) + } + // Map conflict resolution if preferLocal { opts.ConflictResolution = tracker.ConflictLocal diff --git a/cmd/bd/linear.go b/cmd/bd/linear.go index 363c4f0e4..e60838a9f 100644 --- a/cmd/bd/linear.go +++ b/cmd/bd/linear.go @@ -152,6 +152,7 @@ func init() { linearSyncCmd.Flags().Bool("include-ephemeral", false, "Include ephemeral issues (wisps, etc.) when pushing to Linear") linearSyncCmd.Flags().String("parent", "", "Limit push to this beads ticket and its descendants") linearSyncCmd.Flags().StringSlice("team", nil, "Team ID(s) to sync (overrides configured team_id/team_ids)") + registerSelectiveSyncFlags(linearSyncCmd) linearCmd.AddCommand(linearSyncCmd) linearCmd.AddCommand(linearStatusCmd) @@ -170,13 +171,8 @@ func runLinearSync(cmd *cobra.Command, args []string) { typeFilters, _ := cmd.Flags().GetStringSlice("type") excludeTypes, _ := cmd.Flags().GetStringSlice("exclude-type") includeEphemeral, _ := cmd.Flags().GetBool("include-ephemeral") - parentID, _ := cmd.Flags().GetString("parent") cliTeams, _ := cmd.Flags().GetStringSlice("team") - if parentID != "" && !push { - FatalError("--parent requires --push") - } - if !dryRun { CheckReadonly("linear sync") } @@ -239,7 +235,10 @@ func runLinearSync(cmd *cobra.Command, args []string) { if !includeEphemeral { opts.ExcludeEphemeral = true } - opts.ParentID = parentID + + if err := applySelectiveSyncFlags(cmd, &opts, push); err != nil { + FatalError("%v", err) + } // Map conflict resolution if preferLocal { diff --git a/cmd/bd/notion.go b/cmd/bd/notion.go index 2471324dd..1b9ccdcb3 100644 --- a/cmd/bd/notion.go +++ b/cmd/bd/notion.go @@ -123,6 +123,7 @@ func init() { notionSyncCmd.Flags().BoolVar(¬ionPreferNotion, "prefer-notion", false, "On conflict, use the Notion version") notionSyncCmd.Flags().BoolVar(¬ionCreateOnly, "create-only", false, "Only create missing remote pages, do not update existing ones") notionSyncCmd.Flags().StringVar(¬ionSyncState, "state", "all", "Issue state to sync: open, closed, or all") + registerSelectiveSyncFlags(notionSyncCmd) notionCmd.AddCommand( notionInitCmd, @@ -449,7 +450,7 @@ func runNotionSync(cmd *cobra.Command, _ []string) error { conflictResolution = tracker.ConflictExternal } - result, err := engine.Sync(ctx, tracker.SyncOptions{ + syncOpts := tracker.SyncOptions{ Pull: pull, Push: push, DryRun: notionSyncDryRun, @@ -457,7 +458,13 @@ func runNotionSync(cmd *cobra.Command, _ []string) error { State: notionSyncState, ExcludeEphemeral: true, ConflictResolution: conflictResolution, - }) + } + + if err := applySelectiveSyncFlags(cmd, &syncOpts, push); err != nil { + return err + } + + result, err := engine.Sync(ctx, syncOpts) if err != nil { return err } diff --git a/cmd/bd/sync_flags.go b/cmd/bd/sync_flags.go new file mode 100644 index 000000000..70c7af312 --- /dev/null +++ b/cmd/bd/sync_flags.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + + "github.com/spf13/cobra" + "github.com/steveyegge/beads/internal/tracker" +) + +// registerSelectiveSyncFlags adds --issues and --parent flags to a tracker sync command. +func registerSelectiveSyncFlags(cmd *cobra.Command) { + cmd.Flags().String("issues", "", "Comma-separated bead IDs to sync selectively (e.g., bd-abc,bd-def)") + if cmd.Flags().Lookup("parent") == nil { + cmd.Flags().String("parent", "", "Limit push to this bead and its descendants") + } +} + +// applySelectiveSyncFlags parses --issues and --parent from cmd and applies them to opts. +// Returns an error if --parent is used without push. +func applySelectiveSyncFlags(cmd *cobra.Command, opts *tracker.SyncOptions, push bool) error { + if issuesFlag, _ := cmd.Flags().GetString("issues"); issuesFlag != "" { + opts.IssueIDs = splitCSV(issuesFlag) + } + if parentID, _ := cmd.Flags().GetString("parent"); parentID != "" { + if !push { + return fmt.Errorf("--parent requires push (cannot use with --pull-only)") + } + opts.ParentID = parentID + } + return nil +} diff --git a/cmd/bd/sync_issues_flag_test.go b/cmd/bd/sync_issues_flag_test.go new file mode 100644 index 000000000..0845690bd --- /dev/null +++ b/cmd/bd/sync_issues_flag_test.go @@ -0,0 +1,66 @@ +package main + +import ( + "testing" + + "github.com/spf13/cobra" +) + +// TestIssuesFlagRegistered verifies that --issues is registered on all tracker sync commands. +func TestIssuesFlagRegistered(t *testing.T) { + // Not parallel: accesses shared cobra command tree. + + trackers := []struct { + name string + syncCmd *cobra.Command + }{ + {"ado", adoSyncCmd}, + {"jira", jiraSyncCmd}, + {"linear", linearSyncCmd}, + {"github", githubSyncCmd}, + {"gitlab", gitlabSyncCmd}, + {"notion", notionSyncCmd}, + } + + for _, tc := range trackers { + t.Run(tc.name, func(t *testing.T) { + flag := tc.syncCmd.Flags().Lookup("issues") + if flag == nil { + t.Fatalf("%s sync command missing --issues flag", tc.name) + } + if flag.DefValue != "" { + t.Errorf("%s --issues default = %q, want empty", tc.name, flag.DefValue) + } + }) + } +} + +// TestIssuesFlagParsing verifies that --issues parses comma-separated IDs correctly. +func TestIssuesFlagParsing(t *testing.T) { + t.Parallel() + + tests := []struct { + input string + want []string + }{ + {"bd-abc,bd-def", []string{"bd-abc", "bd-def"}}, + {"bd-abc", []string{"bd-abc"}}, + {" bd-abc , bd-def ", []string{"bd-abc", "bd-def"}}, + {"", nil}, + } + + for _, tc := range tests { + t.Run(tc.input, func(t *testing.T) { + got := splitCSV(tc.input) + if len(got) != len(tc.want) { + t.Fatalf("splitCSV(%q) = %v (len %d), want %v (len %d)", + tc.input, got, len(got), tc.want, len(tc.want)) + } + for i, v := range got { + if v != tc.want[i] { + t.Errorf("splitCSV(%q)[%d] = %q, want %q", tc.input, i, v, tc.want[i]) + } + } + }) + } +} diff --git a/cmd/bd/sync_parent_flag_test.go b/cmd/bd/sync_parent_flag_test.go new file mode 100644 index 000000000..02b735c3b --- /dev/null +++ b/cmd/bd/sync_parent_flag_test.go @@ -0,0 +1,49 @@ +package main + +import ( + "testing" + + "github.com/spf13/cobra" +) + +// TestParentFlagRegistered verifies --parent flag exists on all tracker sync commands. +func TestParentFlagRegistered(t *testing.T) { + // Not parallel: accesses shared cobra command tree. + + trackers := []struct { + name string + syncCmd *cobra.Command + }{ + {"ado", adoSyncCmd}, + {"jira", jiraSyncCmd}, + {"linear", linearSyncCmd}, + {"github", githubSyncCmd}, + {"gitlab", gitlabSyncCmd}, + } + + for _, tc := range trackers { + t.Run(tc.name, func(t *testing.T) { + flag := tc.syncCmd.Flags().Lookup("parent") + if flag == nil { + t.Fatalf("%s sync command missing --parent flag", tc.name) + } + if flag.DefValue != "" { + t.Errorf("%s --parent default = %q, want empty", tc.name, flag.DefValue) + } + }) + } +} + +// TestParentFlagNotOnNotion verifies Notion sync doesn't have --parent +// since Notion doesn't have a parent-child hierarchy in the same way. +func TestParentFlagNotOnNotion(t *testing.T) { + // Not parallel: accesses shared cobra command tree. + + flag := notionSyncCmd.Flags().Lookup("parent") + // Notion init has a --parent flag (for page parent), but sync should not + // accidentally inherit it. The sync command's --parent would be for tree-scoped push. + // For now, we don't add it to Notion since Notion doesn't support parent-child deps. + if flag != nil { + t.Log("Notion sync has --parent flag (may be intentional for future use)") + } +} diff --git a/cmd/bd/sync_push_pull.go b/cmd/bd/sync_push_pull.go new file mode 100644 index 000000000..108542cb9 --- /dev/null +++ b/cmd/bd/sync_push_pull.go @@ -0,0 +1,723 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/spf13/cobra" + "github.com/steveyegge/beads/internal/ado" + "github.com/steveyegge/beads/internal/github" + "github.com/steveyegge/beads/internal/gitlab" + "github.com/steveyegge/beads/internal/jira" + "github.com/steveyegge/beads/internal/linear" + "github.com/steveyegge/beads/internal/notion" + "github.com/steveyegge/beads/internal/tracker" +) + +// trackerPushPullFlags holds shared flags for push/pull subcommands. +type trackerPushPullFlags struct { + dryRun bool +} + +// --- ADO push/pull --- + +var adoPushCmd = &cobra.Command{ + Use: "push [bead-ids...]", + Short: "Push specific beads to Azure DevOps", + Long: `Push one or more beads issues to Azure DevOps. + +Accepts bead IDs as positional arguments. +Equivalent to: bd ado sync --push-only --issues `, + RunE: runADOPush, +} + +var adoPullCmd = &cobra.Command{ + Use: "pull [refs...]", + Short: "Pull specific items from Azure DevOps", + Long: `Pull one or more items from Azure DevOps. + +Accepts bead IDs or external references as positional arguments. +Equivalent to: bd ado sync --pull-only --issues `, + RunE: runADOPull, +} + +// --- Jira push/pull --- + +var jiraPushCmd = &cobra.Command{ + Use: "push [bead-ids...]", + Short: "Push specific beads to Jira", + Long: `Push one or more beads issues to Jira. + +Accepts bead IDs as positional arguments. +Equivalent to: bd jira sync --push --issues `, + Run: runJiraPush, +} + +var jiraPullCmd = &cobra.Command{ + Use: "pull [refs...]", + Short: "Pull specific items from Jira", + Long: `Pull one or more items from Jira. + +Accepts bead IDs or external references as positional arguments. +Equivalent to: bd jira sync --pull --issues `, + Run: runJiraPull, +} + +// --- Linear push/pull --- + +var linearPushCmd = &cobra.Command{ + Use: "push [bead-ids...]", + Short: "Push specific beads to Linear", + Long: `Push one or more beads issues to Linear. + +Accepts bead IDs as positional arguments. +Equivalent to: bd linear sync --push --issues `, + Run: runLinearPush, +} + +var linearPullCmd = &cobra.Command{ + Use: "pull [refs...]", + Short: "Pull specific items from Linear", + Long: `Pull one or more items from Linear. + +Accepts bead IDs or external references as positional arguments. +Equivalent to: bd linear sync --pull --issues `, + Run: runLinearPull, +} + +// --- GitHub push/pull --- + +var githubPushCmd = &cobra.Command{ + Use: "push [bead-ids...]", + Short: "Push specific beads to GitHub", + Long: `Push one or more beads issues to GitHub. + +Accepts bead IDs as positional arguments. +Equivalent to: bd github sync --push-only --issues `, + RunE: runGitHubPush, +} + +var githubPullCmd = &cobra.Command{ + Use: "pull [refs...]", + Short: "Pull specific items from GitHub", + Long: `Pull one or more items from GitHub. + +Accepts bead IDs or external references as positional arguments. +Equivalent to: bd github sync --pull-only --issues `, + RunE: runGitHubPull, +} + +// --- GitLab push/pull --- + +var gitlabPushCmd = &cobra.Command{ + Use: "push [bead-ids...]", + Short: "Push specific beads to GitLab", + Long: `Push one or more beads issues to GitLab. + +Accepts bead IDs as positional arguments. +Equivalent to: bd gitlab sync --push-only --issues `, + RunE: runGitLabPush, +} + +var gitlabPullCmd = &cobra.Command{ + Use: "pull [refs...]", + Short: "Pull specific items from GitLab", + Long: `Pull one or more items from GitLab. + +Accepts bead IDs or external references as positional arguments. +Equivalent to: bd gitlab sync --pull-only --issues `, + RunE: runGitLabPull, +} + +// --- Notion push/pull --- + +var notionPushCmd = &cobra.Command{ + Use: "push [bead-ids...]", + Short: "Push specific beads to Notion", + Long: `Push one or more beads issues to Notion. + +Accepts bead IDs as positional arguments. +Equivalent to: bd notion sync --push --issues `, + RunE: runNotionPush, +} + +var notionPullCmd = &cobra.Command{ + Use: "pull [refs...]", + Short: "Pull specific items from Notion", + Long: `Pull one or more items from Notion. + +Accepts bead IDs or external references as positional arguments. +Equivalent to: bd notion sync --pull --issues `, + RunE: runNotionPull, +} + +func init() { + // ADO push/pull + adoPushCmd.Flags().Bool("dry-run", false, "Preview push without making changes") + adoPullCmd.Flags().Bool("dry-run", false, "Preview pull without making changes") + adoCmd.AddCommand(adoPushCmd) + adoCmd.AddCommand(adoPullCmd) + + // Jira push/pull + jiraPushCmd.Flags().Bool("dry-run", false, "Preview push without making changes") + jiraPullCmd.Flags().Bool("dry-run", false, "Preview pull without making changes") + jiraCmd.AddCommand(jiraPushCmd) + jiraCmd.AddCommand(jiraPullCmd) + + // Linear push/pull + linearPushCmd.Flags().Bool("dry-run", false, "Preview push without making changes") + linearPullCmd.Flags().Bool("dry-run", false, "Preview pull without making changes") + linearCmd.AddCommand(linearPushCmd) + linearCmd.AddCommand(linearPullCmd) + + // GitHub push/pull + githubPushCmd.Flags().Bool("dry-run", false, "Preview push without making changes") + githubPullCmd.Flags().Bool("dry-run", false, "Preview pull without making changes") + githubCmd.AddCommand(githubPushCmd) + githubCmd.AddCommand(githubPullCmd) + + // GitLab push/pull + gitlabPushCmd.Flags().Bool("dry-run", false, "Preview push without making changes") + gitlabPullCmd.Flags().Bool("dry-run", false, "Preview pull without making changes") + gitlabCmd.AddCommand(gitlabPushCmd) + gitlabCmd.AddCommand(gitlabPullCmd) + + // Notion push/pull + notionPushCmd.Flags().Bool("dry-run", false, "Preview push without making changes") + notionPullCmd.Flags().Bool("dry-run", false, "Preview pull without making changes") + notionCmd.AddCommand(notionPushCmd) + notionCmd.AddCommand(notionPullCmd) +} + +// outputSyncResult writes sync results as JSON or human-readable text. +func outputSyncResult(result *tracker.SyncResult, dryRun bool) { + if jsonOutput { + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + _ = enc.Encode(result) + return + } + if dryRun { + fmt.Println("Dry run mode - no changes will be made") + } + if result.Stats.Pulled > 0 { + fmt.Printf("✓ Pulled %d issues (%d created, %d updated)\n", + result.Stats.Pulled, result.Stats.Created, result.Stats.Updated) + } + if result.Stats.Pushed > 0 { + fmt.Printf("✓ Pushed %d issues\n", result.Stats.Pushed) + } + if dryRun { + fmt.Println("\nRun without --dry-run to apply changes") + } +} + +// --- ADO implementations --- + +func runADOPush(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("at least one bead ID is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("ado push") + } + + cfg := getADOConfig() + if err := validateADOConfig(cfg); err != nil { + return err + } + if err := ensureStoreActive(); err != nil { + return fmt.Errorf("database not available: %w", err) + } + + ctx := cmd.Context() + at := &ado.Tracker{} + if err := at.Init(ctx, store); err != nil { + return fmt.Errorf("initializing Azure DevOps tracker: %w", err) + } + + engine := tracker.NewEngine(at, store, actor) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + + result, err := engine.Sync(ctx, tracker.SyncOptions{ + Push: true, + Pull: false, + DryRun: dryRun, + IssueIDs: args, + }) + if err != nil { + return err + } + outputSyncResult(result, dryRun) + return nil +} + +func runADOPull(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("at least one bead ID or external reference is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("ado pull") + } + + cfg := getADOConfig() + if err := validateADOConfig(cfg); err != nil { + return err + } + if err := ensureStoreActive(); err != nil { + return fmt.Errorf("database not available: %w", err) + } + + ctx := cmd.Context() + at := &ado.Tracker{} + if err := at.Init(ctx, store); err != nil { + return fmt.Errorf("initializing Azure DevOps tracker: %w", err) + } + + engine := tracker.NewEngine(at, store, actor) + engine.PullHooks = buildADOPullHooks(ctx, at, false, false, new(int), engine.OnWarning) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + + result, err := engine.Sync(ctx, tracker.SyncOptions{ + Pull: true, + Push: false, + DryRun: dryRun, + IssueIDs: args, + }) + if err != nil { + return err + } + outputSyncResult(result, dryRun) + return nil +} + +// --- Jira implementations --- + +func runJiraPush(cmd *cobra.Command, args []string) { + if len(args) == 0 { + FatalError("at least one bead ID is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("jira push") + } + + if err := ensureStoreActive(); err != nil { + FatalError("database not available: %v", err) + } + if err := validateJiraConfig(); err != nil { + FatalError("%v", err) + } + + ctx := rootCtx + jt := &jira.Tracker{} + if err := jt.Init(ctx, store); err != nil { + FatalError("initializing Jira tracker: %v", err) + } + + engine := tracker.NewEngine(jt, store, actor) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + engine.PushHooks = buildJiraPushHooks(ctx) + + result, err := engine.Sync(ctx, tracker.SyncOptions{ + Push: true, + Pull: false, + DryRun: dryRun, + IssueIDs: args, + }) + if err != nil { + FatalError("sync failed: %v", err) + } + outputSyncResult(result, dryRun) +} + +func runJiraPull(cmd *cobra.Command, args []string) { + if len(args) == 0 { + FatalError("at least one bead ID or external reference is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("jira pull") + } + + if err := ensureStoreActive(); err != nil { + FatalError("database not available: %v", err) + } + if err := validateJiraConfig(); err != nil { + FatalError("%v", err) + } + + ctx := rootCtx + jt := &jira.Tracker{} + if err := jt.Init(ctx, store); err != nil { + FatalError("initializing Jira tracker: %v", err) + } + + engine := tracker.NewEngine(jt, store, actor) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + + result, err := engine.Sync(ctx, tracker.SyncOptions{ + Pull: true, + Push: false, + DryRun: dryRun, + IssueIDs: args, + }) + if err != nil { + FatalError("sync failed: %v", err) + } + outputSyncResult(result, dryRun) +} + +// --- Linear implementations --- + +func runLinearPush(cmd *cobra.Command, args []string) { + if len(args) == 0 { + FatalError("at least one bead ID is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("linear push") + } + + if err := ensureStoreActive(); err != nil { + FatalError("database not available: %v", err) + } + if err := validateLinearConfig(nil); err != nil { + FatalError("%v", err) + } + + ctx := rootCtx + teamIDs := getLinearTeamIDs(ctx, nil) + + lt := &linear.Tracker{} + lt.SetTeamIDs(teamIDs) + if err := lt.Init(ctx, store); err != nil { + FatalError("initializing Linear tracker: %v", err) + } + + engine := tracker.NewEngine(lt, store, actor) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + engine.PushHooks = buildLinearPushHooks(ctx, lt) + + result, err := engine.Sync(ctx, tracker.SyncOptions{ + Push: true, + Pull: false, + DryRun: dryRun, + ExcludeEphemeral: true, + IssueIDs: args, + }) + if err != nil { + FatalError("sync failed: %v", err) + } + outputSyncResult(result, dryRun) +} + +func runLinearPull(cmd *cobra.Command, args []string) { + if len(args) == 0 { + FatalError("at least one bead ID or external reference is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("linear pull") + } + + if err := ensureStoreActive(); err != nil { + FatalError("database not available: %v", err) + } + if err := validateLinearConfig(nil); err != nil { + FatalError("%v", err) + } + + ctx := rootCtx + teamIDs := getLinearTeamIDs(ctx, nil) + + lt := &linear.Tracker{} + lt.SetTeamIDs(teamIDs) + if err := lt.Init(ctx, store); err != nil { + FatalError("initializing Linear tracker: %v", err) + } + + engine := tracker.NewEngine(lt, store, actor) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + engine.PullHooks = buildLinearPullHooks(ctx) + + result, err := engine.Sync(ctx, tracker.SyncOptions{ + Pull: true, + Push: false, + DryRun: dryRun, + IssueIDs: args, + }) + if err != nil { + FatalError("sync failed: %v", err) + } + outputSyncResult(result, dryRun) +} + +// --- GitHub implementations --- + +func runGitHubPush(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("at least one bead ID is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("github push") + } + + config := getGitHubConfig() + if err := validateGitHubConfig(config); err != nil { + return err + } + if err := ensureStoreActive(); err != nil { + return fmt.Errorf("database not available: %w", err) + } + + ctx := cmd.Context() + gt := &github.Tracker{} + if err := gt.Init(ctx, store); err != nil { + return fmt.Errorf("initializing GitHub tracker: %w", err) + } + + engine := tracker.NewEngine(gt, store, actor) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + + result, err := engine.Sync(ctx, tracker.SyncOptions{ + Push: true, + Pull: false, + DryRun: dryRun, + IssueIDs: args, + }) + if err != nil { + return err + } + outputSyncResult(result, dryRun) + return nil +} + +func runGitHubPull(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("at least one bead ID or external reference is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("github pull") + } + + config := getGitHubConfig() + if err := validateGitHubConfig(config); err != nil { + return err + } + if err := ensureStoreActive(); err != nil { + return fmt.Errorf("database not available: %w", err) + } + + ctx := cmd.Context() + gt := &github.Tracker{} + if err := gt.Init(ctx, store); err != nil { + return fmt.Errorf("initializing GitHub tracker: %w", err) + } + + engine := tracker.NewEngine(gt, store, actor) + engine.PullHooks = buildGitHubPullHooks(ctx) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + + result, err := engine.Sync(ctx, tracker.SyncOptions{ + Pull: true, + Push: false, + DryRun: dryRun, + IssueIDs: args, + }) + if err != nil { + return err + } + outputSyncResult(result, dryRun) + return nil +} + +// --- GitLab implementations --- + +func runGitLabPush(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("at least one bead ID is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("gitlab push") + } + + config := getGitLabConfig() + if err := validateGitLabConfig(config); err != nil { + return err + } + if err := ensureStoreActive(); err != nil { + return fmt.Errorf("database not available: %w", err) + } + + ctx := cmd.Context() + gt := &gitlab.Tracker{} + if err := gt.Init(ctx, store); err != nil { + return fmt.Errorf("initializing GitLab tracker: %w", err) + } + + engine := tracker.NewEngine(gt, store, actor) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + + result, err := engine.Sync(ctx, tracker.SyncOptions{ + Push: true, + Pull: false, + DryRun: dryRun, + IssueIDs: args, + }) + if err != nil { + return err + } + outputSyncResult(result, dryRun) + return nil +} + +func runGitLabPull(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("at least one bead ID or external reference is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("gitlab pull") + } + + config := getGitLabConfig() + if err := validateGitLabConfig(config); err != nil { + return err + } + if err := ensureStoreActive(); err != nil { + return fmt.Errorf("database not available: %w", err) + } + + ctx := cmd.Context() + gt := &gitlab.Tracker{} + if err := gt.Init(ctx, store); err != nil { + return fmt.Errorf("initializing GitLab tracker: %w", err) + } + + engine := tracker.NewEngine(gt, store, actor) + engine.PullHooks = buildGitLabPullHooks(ctx) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + + result, err := engine.Sync(ctx, tracker.SyncOptions{ + Pull: true, + Push: false, + DryRun: dryRun, + IssueIDs: args, + }) + if err != nil { + return err + } + outputSyncResult(result, dryRun) + return nil +} + +// --- Notion implementations --- + +func runNotionPush(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("at least one bead ID is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("notion push") + } + + cfg := getNotionConfig() + auth, err := resolveNotionAuth(cmd.Context()) + if err != nil { + return err + } + if err := validateNotionConfig(cfg, auth); err != nil { + return err + } + if err := ensureStoreActive(); err != nil { + return fmt.Errorf("database not available: %w", err) + } + + ctx := cmd.Context() + nt := ¬ion.Tracker{} + if err := nt.Init(ctx, store); err != nil { + return fmt.Errorf("initializing Notion tracker: %w", err) + } + + engine := tracker.NewEngine(nt, store, actor) + unsupportedStats := newNotionUnsupportedPushStats() + engine.PushHooks = buildNotionPushHooks(ctx, nt, unsupportedStats) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + + result, syncErr := engine.Sync(ctx, tracker.SyncOptions{ + Push: true, + Pull: false, + DryRun: dryRun, + ExcludeEphemeral: true, + IssueIDs: args, + }) + if syncErr != nil { + return syncErr + } + outputSyncResult(result, dryRun) + return nil +} + +func runNotionPull(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("at least one bead ID or external reference is required") + } + dryRun, _ := cmd.Flags().GetBool("dry-run") + if !dryRun { + CheckReadonly("notion pull") + } + + cfg := getNotionConfig() + auth, err := resolveNotionAuth(cmd.Context()) + if err != nil { + return err + } + if err := validateNotionConfig(cfg, auth); err != nil { + return err + } + if err := ensureStoreActive(); err != nil { + return fmt.Errorf("database not available: %w", err) + } + + ctx := cmd.Context() + nt := ¬ion.Tracker{} + if err := nt.Init(ctx, store); err != nil { + return fmt.Errorf("initializing Notion tracker: %w", err) + } + + engine := tracker.NewEngine(nt, store, actor) + engine.PullHooks = buildNotionPullHooks(ctx) + engine.OnMessage = func(msg string) { fmt.Println(" " + msg) } + engine.OnWarning = func(msg string) { fmt.Fprintf(os.Stderr, "Warning: %s\n", msg) } + + result, syncErr := engine.Sync(ctx, tracker.SyncOptions{ + Pull: true, + Push: false, + DryRun: dryRun, + IssueIDs: args, + }) + if syncErr != nil { + return syncErr + } + outputSyncResult(result, dryRun) + return nil +} diff --git a/cmd/bd/sync_push_pull_test.go b/cmd/bd/sync_push_pull_test.go new file mode 100644 index 000000000..aa4760cb4 --- /dev/null +++ b/cmd/bd/sync_push_pull_test.go @@ -0,0 +1,100 @@ +package main + +import ( + "testing" + + "github.com/spf13/cobra" +) + +// TestPushPullSubcommandsRegistered verifies push/pull subcommands exist on all tracker commands. +func TestPushPullSubcommandsRegistered(t *testing.T) { + // Not parallel: accesses shared cobra command tree (races with InheritedFlags/Find). + + trackers := []struct { + name string + cmd *cobra.Command + }{ + {"ado", adoCmd}, + {"jira", jiraCmd}, + {"linear", linearCmd}, + {"github", githubCmd}, + {"gitlab", gitlabCmd}, + {"notion", notionCmd}, + } + + for _, tc := range trackers { + t.Run(tc.name+"/push", func(t *testing.T) { + sub, _, err := tc.cmd.Find([]string{"push"}) + if err != nil { + t.Fatalf("%s missing push subcommand: %v", tc.name, err) + } + if sub.Name() != "push" { + t.Fatalf("%s push resolved to %q", tc.name, sub.Name()) + } + // Verify --dry-run flag exists + if sub.Flags().Lookup("dry-run") == nil { + t.Fatalf("%s push missing --dry-run flag", tc.name) + } + }) + + t.Run(tc.name+"/pull", func(t *testing.T) { + sub, _, err := tc.cmd.Find([]string{"pull"}) + if err != nil { + t.Fatalf("%s missing pull subcommand: %v", tc.name, err) + } + if sub.Name() != "pull" { + t.Fatalf("%s pull resolved to %q", tc.name, sub.Name()) + } + // Verify --dry-run flag exists + if sub.Flags().Lookup("dry-run") == nil { + t.Fatalf("%s pull missing --dry-run flag", tc.name) + } + }) + } +} + +// TestPushSubcommandRequiresArgs verifies push commands require at least one argument. +func TestPushSubcommandRequiresArgs(t *testing.T) { + // Not parallel: accesses shared cobra command tree. + + // Test ADO push (uses RunE, so we can test it directly) + err := adoPushCmd.RunE(adoPushCmd, []string{}) + if err == nil { + t.Error("ado push with no args should return error") + } + + // Test GitHub push (uses RunE) + err = githubPushCmd.RunE(githubPushCmd, []string{}) + if err == nil { + t.Error("github push with no args should return error") + } + + // Test GitLab push (uses RunE) + err = gitlabPushCmd.RunE(gitlabPushCmd, []string{}) + if err == nil { + t.Error("gitlab push with no args should return error") + } +} + +// TestPullSubcommandRequiresArgs verifies pull commands require at least one argument. +func TestPullSubcommandRequiresArgs(t *testing.T) { + // Not parallel: accesses shared cobra command tree. + + // Test ADO pull (uses RunE) + err := adoPullCmd.RunE(adoPullCmd, []string{}) + if err == nil { + t.Error("ado pull with no args should return error") + } + + // Test GitHub pull (uses RunE) + err = githubPullCmd.RunE(githubPullCmd, []string{}) + if err == nil { + t.Error("github pull with no args should return error") + } + + // Test GitLab pull (uses RunE) + err = gitlabPullCmd.RunE(gitlabPullCmd, []string{}) + if err == nil { + t.Error("gitlab pull with no args should return error") + } +}