package edit import ( "fmt" "net/http" "sort" "strings" "sync" "time" "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" fd "github.com/cli/cli/v2/internal/featuredetection" "github.com/cli/cli/v2/internal/gh" "github.com/cli/cli/v2/internal/ghrepo" "github.com/cli/cli/v2/internal/text" issueShared "github.com/cli/cli/v2/pkg/cmd/issue/shared" prShared "github.com/cli/cli/v2/pkg/cmd/pr/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" "github.com/spf13/cobra" ) type EditOptions struct { HttpClient func() (*http.Client, error) IO *iostreams.IOStreams BaseRepo func() (ghrepo.Interface, error) Prompter prShared.EditPrompter Detector fd.Detector DetermineEditor func() (string, error) FieldsToEditSurvey func(prShared.EditPrompter, *prShared.Editable) error EditFieldsSurvey func(prShared.EditPrompter, *prShared.Editable, string) error FetchOptions func(*api.Client, ghrepo.Interface, *prShared.Editable, gh.ProjectsV1Support) error IssueNumbers []int Interactive bool SetParent string RemoveParent bool AddSubIssues []string RemoveSubIssues []string AddBlockedBy []string RemoveBlockedBy []string AddBlocking []string RemoveBlocking []string prShared.Editable } func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command { opts := &EditOptions{ IO: f.IOStreams, HttpClient: f.HttpClient, DetermineEditor: func() (string, error) { return cmdutil.DetermineEditor(f.Config) }, FieldsToEditSurvey: prShared.FieldsToEditSurvey, EditFieldsSurvey: prShared.EditFieldsSurvey, FetchOptions: prShared.FetchOptions, Prompter: f.Prompter, } var bodyFile string var removeMilestone bool cmd := &cobra.Command{ Use: "edit { | }", Short: "Edit issues", Long: heredoc.Docf(` Edit one or more issues within the same repository. Editing issues' projects requires authorization with the %[1]sproject%[1]s scope. To authorize, run %[1]sgh auth refresh -s project%[1]s. The %[1]s--add-assignee%[1]s and %[1]s--remove-assignee%[1]s flags both support the following special values: - %[1]s@me%[1]s: assign or unassign yourself - %[1]s@copilot%[1]s: assign or unassign Copilot (not supported on GitHub Enterprise Server) `, "`"), Example: heredoc.Doc(` $ gh issue edit 23 --title "I found a bug" --body "Nothing works" $ gh issue edit 23 --add-label "bug,help wanted" --remove-label "core" $ gh issue edit 23 --add-assignee "@me" --remove-assignee monalisa,hubot $ gh issue edit 23 --add-assignee "@copilot" $ gh issue edit 23 --add-project "Roadmap" --remove-project v1,v2 $ gh issue edit 23 --milestone "Version 1" $ gh issue edit 23 --remove-milestone $ gh issue edit 23 --body-file body.txt $ gh issue edit 23 34 --add-label "help wanted" $ gh issue edit 23 --type Bug $ gh issue edit 23 --set-parent 100 $ gh issue edit 23 --remove-parent $ gh issue edit 100 --add-sub-issue 123,124 $ gh issue edit 123 --add-blocked-by 200 --add-blocking 300,301 `), Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { issueNumbers, baseRepo, err := issueShared.ParseIssuesFromArgs(args) if err != nil { return err } // If the args provided the base repo then use that directly. if baseRepo, present := baseRepo.Value(); present { opts.BaseRepo = func() (ghrepo.Interface, error) { return baseRepo, nil } } else { // support `-R, --repo` override opts.BaseRepo = f.BaseRepo } opts.IssueNumbers = issueNumbers flags := cmd.Flags() bodyProvided := flags.Changed("body") bodyFileProvided := bodyFile != "" if err := cmdutil.MutuallyExclusive( "specify only one of `--body` or `--body-file`", bodyProvided, bodyFileProvided, ); err != nil { return err } if bodyProvided || bodyFileProvided { opts.Editable.Body.Edited = true if bodyFileProvided { b, err := cmdutil.ReadFile(bodyFile, opts.IO.In) if err != nil { return err } opts.Editable.Body.Value = string(b) } } if err := cmdutil.MutuallyExclusive( "specify only one of `--milestone` or `--remove-milestone`", flags.Changed("milestone"), removeMilestone, ); err != nil { return err } if err := cmdutil.MutuallyExclusive( "specify only one of --set-parent or --remove-parent", flags.Changed("set-parent"), opts.RemoveParent, ); err != nil { return err } if flags.Changed("title") { opts.Editable.Title.Edited = true } if flags.Changed("add-assignee") || flags.Changed("remove-assignee") { opts.Editable.Assignees.Edited = true } if flags.Changed("add-label") || flags.Changed("remove-label") { opts.Editable.Labels.Edited = true } if flags.Changed("add-project") || flags.Changed("remove-project") { opts.Editable.Projects.Edited = true } if flags.Changed("milestone") || removeMilestone { opts.Editable.Milestone.Edited = true // Note that when `--remove-milestone` is provided, the value of // `opts.Editable.Milestone.Value` will automatically be empty, // which results in milestone association removal. For reference, // see the `Editable.MilestoneId` method. } if flags.Changed("type") { opts.Editable.IssueType.Edited = true } if flags.Changed("set-parent") || opts.RemoveParent { opts.Editable.Parent.Edited = true if opts.RemoveParent { opts.Editable.Parent.Value = "" } else { opts.Editable.Parent.Value = opts.SetParent } } // Sub-issue and relationship flags are outside the Editable pattern // but still need to prevent interactive mode. hasRelationshipFlags := len(opts.AddSubIssues) > 0 || len(opts.RemoveSubIssues) > 0 || len(opts.AddBlockedBy) > 0 || len(opts.RemoveBlockedBy) > 0 || len(opts.AddBlocking) > 0 || len(opts.RemoveBlocking) > 0 if !opts.Editable.Dirty() && !hasRelationshipFlags { opts.Interactive = true } if opts.Interactive && !opts.IO.CanPrompt() { return cmdutil.FlagErrorf("field to edit flag required when not running interactively") } if opts.Interactive && len(opts.IssueNumbers) > 1 { return cmdutil.FlagErrorf("multiple issues cannot be edited interactively") } if runF != nil { return runF(opts) } return editRun(opts) }, } cmd.Flags().StringVarP(&opts.Editable.Title.Value, "title", "t", "", "Set the new title.") cmd.Flags().StringVarP(&opts.Editable.Body.Value, "body", "b", "", "Set the new body.") cmd.Flags().StringVarP(&bodyFile, "body-file", "F", "", "Read body text from `file` (use \"-\" to read from standard input)") cmd.Flags().StringSliceVar(&opts.Editable.Assignees.Add, "add-assignee", nil, "Add assigned users by their `login`. Use \"@me\" to assign yourself, or \"@copilot\" to assign Copilot.") cmd.Flags().StringSliceVar(&opts.Editable.Assignees.Remove, "remove-assignee", nil, "Remove assigned users by their `login`. Use \"@me\" to unassign yourself, or \"@copilot\" to unassign Copilot.") cmd.Flags().StringSliceVar(&opts.Editable.Labels.Add, "add-label", nil, "Add labels by `name`") cmd.Flags().StringSliceVar(&opts.Editable.Labels.Remove, "remove-label", nil, "Remove labels by `name`") cmd.Flags().StringSliceVar(&opts.Editable.Projects.Add, "add-project", nil, "Add the issue to projects by `title`") cmd.Flags().StringSliceVar(&opts.Editable.Projects.Remove, "remove-project", nil, "Remove the issue from projects by `title`") cmd.Flags().StringVarP(&opts.Editable.Milestone.Value, "milestone", "m", "", "Edit the milestone the issue belongs to by `name`") cmd.Flags().BoolVar(&removeMilestone, "remove-milestone", false, "Remove the milestone association from the issue") cmd.Flags().StringVar(&opts.Editable.IssueType.Value, "type", "", "Set the issue type by `name`") cmd.Flags().StringVar(&opts.SetParent, "set-parent", "", "Set the parent issue by `number` or URL") cmd.Flags().BoolVar(&opts.RemoveParent, "remove-parent", false, "Remove the parent issue") cmd.Flags().StringSliceVar(&opts.AddSubIssues, "add-sub-issue", nil, "Add sub-issues by `number` or URL") cmd.Flags().StringSliceVar(&opts.RemoveSubIssues, "remove-sub-issue", nil, "Remove sub-issues by `number` or URL") cmd.Flags().StringSliceVar(&opts.AddBlockedBy, "add-blocked-by", nil, "Add 'blocked by' relationships by issue `number` or URL") cmd.Flags().StringSliceVar(&opts.RemoveBlockedBy, "remove-blocked-by", nil, "Remove 'blocked by' relationships by issue `number` or URL") cmd.Flags().StringSliceVar(&opts.AddBlocking, "add-blocking", nil, "Add 'blocking' relationships by issue `number` or URL") cmd.Flags().StringSliceVar(&opts.RemoveBlocking, "remove-blocking", nil, "Remove 'blocking' relationships by issue `number` or URL") return cmd } func editRun(opts *EditOptions) error { httpClient, err := opts.HttpClient() if err != nil { return err } baseRepo, err := opts.BaseRepo() if err != nil { return err } // Prompt the user which fields they'd like to edit. editable := opts.Editable editable.IssueType.Allowed = true editable.Parent.Allowed = true if opts.Interactive { err = opts.FieldsToEditSurvey(opts.Prompter, &editable) if err != nil { return err } } if opts.Detector == nil { cachedClient := api.NewCachedHTTPClient(httpClient, time.Hour*24) opts.Detector = fd.NewDetector(cachedClient, baseRepo.RepoHost()) } issueFeatures, err := opts.Detector.IssueFeatures() if err != nil { return err } lookupFields := []string{"id", "number", "title", "body", "url"} if editable.Assignees.Edited { // TODO ApiActorsSupported if issueFeatures.ApiActorsSupported { editable.ApiActorsSupported = true lookupFields = append(lookupFields, "assignedActors") } else { lookupFields = append(lookupFields, "assignees") } } if editable.Labels.Edited { lookupFields = append(lookupFields, "labels") } if editable.Projects.Edited { // TODO projectsV1Deprecation // Remove this section as we should no longer add projectCards projectsV1Support := opts.Detector.ProjectsV1() if projectsV1Support == gh.ProjectsV1Supported { lookupFields = append(lookupFields, "projectCards") } lookupFields = append(lookupFields, "projectItems") } if editable.Milestone.Edited { lookupFields = append(lookupFields, "milestone") } if editable.IssueType.Edited { lookupFields = append(lookupFields, "issueType") } if editable.Parent.Edited || opts.RemoveParent { lookupFields = append(lookupFields, "parent") } // Get all specified issues and make sure they are within the same repo. issues, err := issueShared.FindIssuesOrPRs(httpClient, baseRepo, opts.IssueNumbers, lookupFields) if err != nil { return err } // Fetch editable shared fields once for all issues. apiClient := api.NewClientFromHTTP(httpClient) // Wire up search function for assignees when ApiActorsSupported is available. // Interactive mode only supports a single issue, so we use its ID for the search query. if issueFeatures.ApiActorsSupported && opts.Interactive && len(issues) == 1 { editable.AssigneeSearchFunc = prShared.AssigneeSearchFunc(apiClient, baseRepo, issues[0].ID) } opts.IO.StartProgressIndicatorWithLabel("Fetching repository information") err = opts.FetchOptions(apiClient, baseRepo, &editable, opts.Detector.ProjectsV1()) opts.IO.StopProgressIndicator() if err != nil { return err } // Update all issues in parallel. editedIssueChan := make(chan string, len(issues)) failedIssueChan := make(chan string, len(issues)) g := sync.WaitGroup{} // Only show progress if we will not prompt below or the survey will break up the progress indicator. if !opts.Interactive { opts.IO.StartProgressIndicatorWithLabel(fmt.Sprintf("Updating %d issues", len(issues))) } for _, issue := range issues { // Copy variables to capture in the go routine below. editable := editable.Clone() editable.Title.Default = issue.Title editable.Body.Default = issue.Body // We use Actors as the default assignees if Actors are assignable // on this GitHub host. // TODO ApiActorsSupported if editable.ApiActorsSupported { editable.Assignees.Default = issue.AssignedActors.DisplayNames() editable.Assignees.DefaultLogins = issue.AssignedActors.Logins() } else { editable.Assignees.Default = issue.Assignees.Logins() } editable.Labels.Default = issue.Labels.Names() editable.Projects.Default = append(issue.ProjectCards.ProjectNames(), issue.ProjectItems.ProjectTitles()...) projectItems := map[string]string{} for _, n := range issue.ProjectItems.Nodes { projectItems[n.Project.ID] = n.ID } editable.Projects.ProjectItems = projectItems if issue.Milestone != nil { editable.Milestone.Default = issue.Milestone.Title } if issue.IssueType != nil { editable.IssueType.Default = issue.IssueType.Name } if issue.Parent != nil { editable.Parent.Default = fmt.Sprintf("#%d", issue.Parent.Number) } // Allow interactive prompts for one issue; failed earlier if multiple issues specified. if opts.Interactive { editorCommand, err := opts.DetermineEditor() if err != nil { return err } err = opts.EditFieldsSurvey(opts.Prompter, &editable, editorCommand) if err != nil { return err } } // Look up the issue type ID using the map populated by FetchOptions var issueTypeID string if editable.IssueType.Edited && editable.IssueType.Value != "" { id, ok := editable.IssueTypeNameToID[editable.IssueType.Value] if !ok { return fmt.Errorf("type %q not found; available types: %s", editable.IssueType.Value, strings.Join(editable.IssueType.Options, ", ")) } issueTypeID = id } g.Add(1) go func(issue *api.Issue) { defer g.Done() err := prShared.UpdateIssue(httpClient, baseRepo, issue.ID, issue.IsPullRequest(), editable) if err != nil { failedIssueChan <- fmt.Sprintf("failed to update %s: %s", issue.URL, err) return } // Issue type mutation if editable.IssueType.Edited && editable.IssueType.Value != "" { if err := api.UpdateIssueIssueType(apiClient, baseRepo.RepoHost(), issue.ID, issueTypeID); err != nil { failedIssueChan <- fmt.Sprintf("failed to update type for %s: %s", issue.URL, err) return } } // Parent mutation if editable.Parent.Edited { if err := applyEditParent(apiClient, baseRepo, issue, editable.Parent.Value); err != nil { failedIssueChan <- fmt.Sprintf("failed to update parent for %s: %s", issue.URL, err) return } } // Sub-issue mutations if err := applyEditSubIssues(apiClient, baseRepo, issue, opts); err != nil { failedIssueChan <- fmt.Sprintf("failed to update sub-issues for %s: %s", issue.URL, err) return } // Relationship mutations if err := applyEditRelationships(apiClient, baseRepo, issue, opts, issueFeatures); err != nil { failedIssueChan <- fmt.Sprintf("failed to update relationships for %s: %s", issue.URL, err) return } editedIssueChan <- issue.URL }(issue) } g.Wait() close(editedIssueChan) close(failedIssueChan) // Does nothing if progress was not started above. opts.IO.StopProgressIndicator() // Print a sorted list of successfully edited issue URLs to stdout. editedIssueURLs := make([]string, 0, len(issues)) for editedIssueURL := range editedIssueChan { editedIssueURLs = append(editedIssueURLs, editedIssueURL) } sort.Strings(editedIssueURLs) for _, editedIssueURL := range editedIssueURLs { fmt.Fprintln(opts.IO.Out, editedIssueURL) } // Print a sorted list of failures to stderr. failedIssueErrors := make([]string, 0, len(issues)) for failedIssueError := range failedIssueChan { failedIssueErrors = append(failedIssueErrors, failedIssueError) } sort.Strings(failedIssueErrors) for _, failedIssueError := range failedIssueErrors { fmt.Fprintln(opts.IO.ErrOut, failedIssueError) } if len(failedIssueErrors) > 0 { return fmt.Errorf("failed to update %s", text.Pluralize(len(failedIssueErrors), "issue")) } return nil } func applyEditParent(client *api.Client, baseRepo ghrepo.Interface, issue *api.Issue, parentRef string) error { hostname := baseRepo.RepoHost() if parentRef == "" { // Remove parent - use the parent's ID from the fetched issue data if issue.Parent == nil { return nil // no parent to remove } return api.RemoveSubIssue(client, hostname, issue.Parent.ID, issue.ID) } // Set parent with replaceParent=true parentID, err := issueShared.ResolveIssueRef(client, baseRepo, parentRef) if err != nil { return fmt.Errorf("resolving parent: %w", err) } return api.AddSubIssue(client, hostname, parentID, issue.ID, true) } func applyEditSubIssues(client *api.Client, baseRepo ghrepo.Interface, issue *api.Issue, opts *EditOptions) error { hostname := baseRepo.RepoHost() for _, ref := range opts.AddSubIssues { subID, err := issueShared.ResolveIssueRef(client, baseRepo, ref) if err != nil { return fmt.Errorf("resolving --add-sub-issue reference %q: %w", ref, err) } if err := api.AddSubIssue(client, hostname, issue.ID, subID, false); err != nil { return err } } for _, ref := range opts.RemoveSubIssues { subID, err := issueShared.ResolveIssueRef(client, baseRepo, ref) if err != nil { return fmt.Errorf("resolving --remove-sub-issue reference %q: %w", ref, err) } if err := api.RemoveSubIssue(client, hostname, issue.ID, subID); err != nil { return err } } return nil } func applyEditRelationships(client *api.Client, baseRepo ghrepo.Interface, issue *api.Issue, opts *EditOptions, features fd.IssueFeatures) error { hasRelationshipFlags := len(opts.AddBlockedBy) > 0 || len(opts.RemoveBlockedBy) > 0 || len(opts.AddBlocking) > 0 || len(opts.RemoveBlocking) > 0 if !hasRelationshipFlags { return nil } // TODO IssueRelationshipsCleanup if !features.IssueRelationshipsSupported { return fmt.Errorf("issue relationships are not supported on this GitHub Enterprise Server version") } hostname := baseRepo.RepoHost() for _, ref := range opts.AddBlockedBy { blockingID, err := issueShared.ResolveIssueRef(client, baseRepo, ref) if err != nil { return fmt.Errorf("resolving --add-blocked-by reference %q: %w", ref, err) } if err := api.AddBlockedBy(client, hostname, issue.ID, blockingID); err != nil { return err } } for _, ref := range opts.RemoveBlockedBy { blockingID, err := issueShared.ResolveIssueRef(client, baseRepo, ref) if err != nil { return fmt.Errorf("resolving --remove-blocked-by reference %q: %w", ref, err) } if err := api.RemoveBlockedBy(client, hostname, issue.ID, blockingID); err != nil { return err } } for _, ref := range opts.AddBlocking { // --add-blocking swaps args: the OTHER issue is blocked by THIS issue blockedID, err := issueShared.ResolveIssueRef(client, baseRepo, ref) if err != nil { return fmt.Errorf("resolving --add-blocking reference %q: %w", ref, err) } if err := api.AddBlockedBy(client, hostname, blockedID, issue.ID); err != nil { return err } } for _, ref := range opts.RemoveBlocking { // --remove-blocking swaps args: the OTHER issue is no longer blocked by THIS issue blockedID, err := issueShared.ResolveIssueRef(client, baseRepo, ref) if err != nil { return fmt.Errorf("resolving --remove-blocking reference %q: %w", ref, err) } if err := api.RemoveBlockedBy(client, hostname, blockedID, issue.ID); err != nil { return err } } return nil }