add

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

cmd/project/board/add/add.go

Index

Constants

This section is empty.

Variables

View Source
var AddCmd = &cobra.Command{
	Use:   "add",
	Short: "Interactively add issues to a project board.",
	RunE: func(cmd *cobra.Command, args []string) error {
		presenter := ui.NewPresenter(cmd.OutOrStdout(), cmd.ErrOrStderr())
		ctx := cmd.Context()

		ghClient, err := newGHClient(ctx, globals.AppLogger, globals.LoadedAppConfig)
		if err != nil {
			presenter.Error("Failed to initialize GitHub client: %v", err)
			return err
		}

		presenter.Summary("Step 1: Select a Project Board")
		projects, err := ghClient.ListProjects(ctx)
		if err != nil {
			presenter.Error("Failed to fetch project boards: %v", err)
			presenter.Advice("Please ensure your GITHUB_TOKEN has the 'read:project' and 'write:project' scopes.")
			return err
		}
		if len(projects) == 0 {
			presenter.Info("No project boards found.")
			return nil
		}
		projectOptions := make([]string, len(projects))
		for i, p := range projects {
			projectOptions[i] = fmt.Sprintf("#%d: %s", p.Number, p.Title)
		}
		selectedProjectStr, err := presenter.PromptForSelect("Which project board do you want to add issues to?", projectOptions)
		if err != nil {
			return err
		}
		projectNumberStr := strings.Split(selectedProjectStr, ":")[0][1:]
		projectNumber, _ := strconv.Atoi(projectNumberStr)
		project, err := ghClient.GetProjectByNumber(ctx, projectNumber)
		if err != nil {
			presenter.Error("Failed to get details for project #%d: %v", projectNumber, err)
			return err
		}
		presenter.Success("✓ Selected board '%s'", project.Title)
		presenter.Newline()

		presenter.Summary("Step 2: Select Issues to Add")
		provider, err := newProvider(ctx, globals.AppLogger, globals.LoadedAppConfig)
		if err != nil {
			presenter.Error("Failed to initialize work item provider: %v", err)
			return err
		}
		listOpts := workitem.ListOptions{State: workitem.StateOpen, Limit: 100}
		issues, err := provider.ListItems(ctx, listOpts)
		if err != nil {
			presenter.Error("Failed to list open issues: %v", err)
			return err
		}
		if len(issues) == 0 {
			presenter.Info("No open issues found to add.")
			return nil
		}
		issueOptions := make([]string, len(issues))
		for i, issue := range issues {
			issueOptions[i] = fmt.Sprintf("#%d: %s", issue.Number, issue.Title)
		}
		selectedIssueStrs, err := presenter.PromptForMultiSelect("Which issues would you like to add?", issueOptions)
		if err != nil {
			return err
		}
		if len(selectedIssueStrs) == 0 {
			presenter.Info("No issues selected. Aborting.")
			return nil
		}
		presenter.Newline()

		presenter.Summary("Step 3: Adding %d Issue(s) to '%s'", len(selectedIssueStrs), project.Title)
		for _, issueStr := range selectedIssueStrs {
			issueNumberStr := strings.Split(issueStr, ":")[0][1:]
			issueNumber, _ := strconv.Atoi(issueNumberStr)

			presenter.Step("Adding issue #%d...", issueNumber)
			item, err := provider.GetItem(ctx, issueNumber, false)
			if err != nil {
				presenter.Error("  ! Failed to get details for issue #%d: %v", issueNumber, err)
				continue
			}
			if item.ID == "" {
				presenter.Error("  ! Could not find GraphQL Node ID for issue #%d.", issueNumber)
				continue
			}

			err = ghClient.AddIssueToProject(ctx, project.ID, item.ID)
			if err != nil {
				presenter.Error("  ! Failed to add issue #%d to project: %v", issueNumber, err)
			} else {
				presenter.Success("  ✓ Successfully added issue #%d.", issueNumber)
			}
		}

		presenter.Newline()
		presenter.Success("Finished adding issues. View the board at: %s", project.URL)
		return nil
	},
}

AddCmd represents the project board add command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL