package update import ( "errors" "fmt" "strings" "github.com/MakeNowJust/heredoc/v2" "github.com/spf13/cobra" gitlab "gitlab.com/gitlab-org/api/client-go/v3" "gitlab.com/gitlab-org/cli/internal/api" "gitlab.com/gitlab-org/cli/internal/cmdutils" "gitlab.com/gitlab-org/cli/internal/commands/issue/issueutils" "gitlab.com/gitlab-org/cli/internal/mcpannotations" "gitlab.com/gitlab-org/cli/internal/text" "gitlab.com/gitlab-org/cli/internal/utils" ) func NewCmdUpdate(f cmdutils.Factory) *cobra.Command { var attach []string issueUpdateCmd := &cobra.Command{ Use: "update ", Short: `Update issue.`, Long: heredoc.Docf(` Change an issue's labels, assignees, milestone, title, or description. Use %[1]s--label%[1]s and %[1]s--unlabel%[1]s to add or remove labels. Use %[1]s--estimate%[1]s and %[1]s--spend%[1]s to track time on the issue, the same as the %[1]s/estimate%[1]s and %[1]s/spend%[1]s quick actions. Durations use the GitLab time tracking format, for example %[1]s1mo 2w 3d 4h 5m%[1]s. %[1]s--spend%[1]s adds to the time already spent. %[1]s--remove-estimate%[1]s and %[1]s--remove-time-spent%[1]s reset the estimate and the time spent. %[1]s--attach%[1]s uploads a file and references it at the end of the description. Repeat the flag for more than one file, or pass %[1]s-%[1]s to read the file from standard input. Without %[1]s--description%[1]s the references are added to the description the issue already has, instead of replacing it. %[2]s`, "`", fmt.Sprintf(text.ExperimentalFlagString, "`--attach`")), Example: heredoc.Doc(` glab issue update 42 --label ui,ux glab issue update 42 --unlabel working # Read the description from a file glab issue update 42 --description-file description.md # Read the description from standard input cat description.md | glab issue update 42 --description-file - # Add a screenshot to the existing description glab issue update 42 --attach ./screenshot.png # Set a time estimate and log time spent glab issue update 42 --estimate 2d --spend 1h30m # Reset the time estimate and the time spent glab issue update 42 --remove-estimate --remove-time-spent`), Args: cobra.ExactArgs(1), Annotations: map[string]string{ mcpannotations.Destructive: "true", }, RunE: func(cmd *cobra.Command, args []string) error { var err error var actions []string var ua *cmdutils.UserAssignments c := f.IO().Color() if cmd.Flags().Changed("unassign") && cmd.Flags().Changed("assignee") { return &cmdutils.FlagError{Err: errors.New("--assignee and --unassign are mutually exclusive")} } // Parse assignees early so we can fail early in case of conflicts if cmd.Flags().Changed("assignee") { givenAssignees, err := cmd.Flags().GetStringSlice("assignee") if err != nil { return err } ua = cmdutils.ParseAssignees(givenAssignees) err = ua.VerifyAssignees() if err != nil { return &cmdutils.FlagError{Err: fmt.Errorf("--assignee: %w", err)} } } if cmd.Flags().Changed("lock-discussion") && cmd.Flags().Changed("unlock-discussion") { return &cmdutils.FlagError{ Err: errors.New("--lock-discussion and --unlock-discussion can't be used together"), } } if cmd.Flags().Changed("confidential") && cmd.Flags().Changed("public") { return &cmdutils.FlagError{Err: errors.New("--public and --confidential can't be used together")} } for _, name := range []string{"estimate", "spend"} { if !cmd.Flags().Changed(name) { continue } if v, _ := cmd.Flags().GetString(name); strings.TrimSpace(v) == "" { return &cmdutils.FlagError{Err: fmt.Errorf("--%s can't be empty", name)} } } if err := cmdutils.ResolveDescriptionFile(f.IO(), cmd); err != nil { return err } client, err := f.GitLabClient() if err != nil { return err } issue, repo, err := issueutils.IssueFromArg(f.ApiClient, client, f.BaseRepo, f.DefaultHostname(), f.Config(), args[0]) if err != nil { return err } l := &gitlab.UpdateIssueOptions{} if m, _ := cmd.Flags().GetString("title"); m != "" { actions = append(actions, fmt.Sprintf("updated title to %q", m)) l.Title = new(m) } if m, _ := cmd.Flags().GetBool("lock-discussion"); m { actions = append(actions, "locked discussion") l.DiscussionLocked = new(m) } if m, _ := cmd.Flags().GetBool("unlock-discussion"); m { actions = append(actions, "unlocked discussion") l.DiscussionLocked = new(bool) } if cmd.Flags().Changed("weight") { weight, _ := cmd.Flags().GetInt("weight") if weight < 0 { return &cmdutils.FlagError{Err: errors.New("weight must be a positive integer or zero")} } actions = append(actions, fmt.Sprintf("set weight to %d", weight)) l.Weight = new(int64(weight)) } if m, _ := cmd.Flags().GetString("description"); m != "" { actions = append(actions, "updated description") // Edit the description via editor if m == "-" { // Fetch the current issue and description gitlabClient, err := f.GitLabClient() if err != nil { return err } issue, _, err := issueutils.IssueFromArg(f.ApiClient, gitlabClient, f.BaseRepo, f.DefaultHostname(), f.Config(), args[0]) if err != nil { return err } editor, err := cmdutils.GetEditor(f.Config) if err != nil { return err } l.Description = new("") err = cmdutils.EditorPrompt(cmd.Context(), f.IO(), l.Description, "Description", issue.Description, editor) if err != nil { return err } } else { l.Description = new(m) } } if len(attach) > 0 { body, err := cmdutils.AppendAttachmentsToUpdate(cmd.Context(), f.IO(), client, repo.FullName(), l.Description, func() (string, error) { return issue.Description, nil }, attach) if err != nil { return err } actions = append(actions, fmt.Sprintf("attached %s", utils.Pluralize(len(attach), "file"))) l.Description = &body } if m, _ := cmd.Flags().GetStringSlice("label"); len(m) != 0 { actions = append(actions, fmt.Sprintf("added labels %s", strings.Join(m, " "))) l.AddLabels = (*gitlab.LabelOptions)(&m) } if m, _ := cmd.Flags().GetStringSlice("unlabel"); len(m) != 0 { actions = append(actions, fmt.Sprintf("removed labels %s", strings.Join(m, " "))) l.RemoveLabels = (*gitlab.LabelOptions)(&m) } if m, _ := cmd.Flags().GetBool("public"); m { actions = append(actions, "made public") l.Confidential = new(false) } if m, _ := cmd.Flags().GetBool("confidential"); m { actions = append(actions, "made confidential") l.Confidential = new(true) } if ok := cmd.Flags().Changed("milestone"); ok { if m, _ := cmd.Flags().GetString("milestone"); m != "" || m == "0" { mID, err := cmdutils.ParseMilestone(client, repo, m) if err != nil { return err } actions = append(actions, fmt.Sprintf("added milestone %q", m)) l.MilestoneID = new(mID) } else { actions = append(actions, "unassigned milestone") l.MilestoneID = new(int64(0)) } } if cmd.Flags().Changed("unassign") { l.AssigneeIDs = &[]int64{0} // 0 or an empty int[] is the documented way to unassign actions = append(actions, "unassigned all users") } if ua != nil { if len(ua.ToReplace) != 0 { l.AssigneeIDs, actions, err = ua.UsersFromReplaces(client, actions) if err != nil { return err } } else if len(ua.ToAdd) != 0 || len(ua.ToRemove) != 0 { issue, err := api.GetIssue(client, repo.FullName(), issue.IID) if err != nil { return err } l.AssigneeIDs, actions, err = ua.UsersFromAddRemove(issue.Assignees, nil, client, actions) if err != nil { return err } } } if m, _ := cmd.Flags().GetString("due-date"); m != "" { actions = append(actions, "added due date") dueDate, err := gitlab.ParseISOTime(m) if err != nil { return err } l.DueDate = new(dueDate) } timeTrackingChanged := cmd.Flags().Changed("estimate") || cmd.Flags().Changed("remove-estimate") || cmd.Flags().Changed("spend") || cmd.Flags().Changed("remove-time-spent") f.IO().LogInfof("- Updating issue #%d\n", issue.IID) if *l != (gitlab.UpdateIssueOptions{}) || !timeTrackingChanged { issue, err = api.UpdateIssue(client, repo.FullName(), issue.IID, l) if err != nil { return err } } ctx := gitlab.WithContext(cmd.Context()) if cmd.Flags().Changed("estimate") { estimate, _ := cmd.Flags().GetString("estimate") _, _, err := client.Issues.SetTimeEstimate(repo.FullName(), issue.IID, &gitlab.SetTimeEstimateOptions{Duration: new(estimate)}, ctx) if err != nil { return err } actions = append(actions, fmt.Sprintf("set time estimate to %s", estimate)) } if cmd.Flags().Changed("remove-estimate") { _, _, err := client.Issues.ResetTimeEstimate(repo.FullName(), issue.IID, ctx) if err != nil { return err } actions = append(actions, "removed time estimate") } if cmd.Flags().Changed("spend") { spend, _ := cmd.Flags().GetString("spend") _, _, err := client.Issues.AddSpentTime(repo.FullName(), issue.IID, &gitlab.AddSpentTimeOptions{Duration: new(spend)}, ctx) if err != nil { return err } actions = append(actions, fmt.Sprintf("added %s of time spent", spend)) } if cmd.Flags().Changed("remove-time-spent") { _, _, err := client.Issues.ResetSpentTime(repo.FullName(), issue.IID, ctx) if err != nil { return err } actions = append(actions, "removed time spent") } for _, s := range actions { f.IO().LogInfo(c.GreenCheck(), s) } f.IO().LogInfo(issueutils.DisplayIssue(c, issue, f.IO().IsaTTY)) return nil }, } issueUpdateCmd.Flags().StringP("title", "t", "", "Title of issue.") issueUpdateCmd.Flags().BoolP("lock-discussion", "", false, "Lock discussion on issue.") issueUpdateCmd.Flags().BoolP("unlock-discussion", "", false, "Unlock discussion on issue.") issueUpdateCmd.Flags().StringP("description", "d", "", "Issue description. Set to \"-\" to open an editor.") issueUpdateCmd.Flags().StringSliceP("label", "l", []string{}, "Add labels.") issueUpdateCmd.Flags().StringSliceP("unlabel", "u", []string{}, "Remove labels.") issueUpdateCmd.Flags().BoolP("public", "p", false, "Make issue public.") issueUpdateCmd.Flags().BoolP("confidential", "c", false, "Make issue confidential.") issueUpdateCmd.Flags().StringP("milestone", "m", "", "Title of the milestone to assign Set to \"\" or 0 to unassign.") issueUpdateCmd.Flags(). StringSliceP("assignee", "a", []string{}, "Assign users by username. Prefix with '!' or '-' to remove from existing assignees, or '+' to add new. Otherwise, replace existing assignees with these users. Multiple usernames can be comma-separated or specified by repeating the flag.") issueUpdateCmd.Flags().Bool("unassign", false, "Unassign all users.") issueUpdateCmd.Flags().IntP("weight", "w", 0, "Set weight of the issue.") issueUpdateCmd.Flags().StringP("due-date", "", "", "A date in 'YYYY-MM-DD' format.") issueUpdateCmd.Flags().String("estimate", "", "Set the time estimate for the issue, for example '2d' or '3h30m'.") issueUpdateCmd.Flags().Bool("remove-estimate", false, "Remove the time estimate from the issue.") issueUpdateCmd.Flags().String("spend", "", "Add time spent on the issue, for example '1h30m'.") issueUpdateCmd.Flags().Bool("remove-time-spent", false, "Remove all time spent from the issue.") issueUpdateCmd.MarkFlagsMutuallyExclusive("estimate", "remove-estimate") issueUpdateCmd.MarkFlagsMutuallyExclusive("spend", "remove-time-spent") cmdutils.AddDescriptionFileFlag(issueUpdateCmd, "issue") cmdutils.AddAttachFlag(issueUpdateCmd, &attach, "description") return issueUpdateCmd }