This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
/
issue_update.go
167 lines (150 loc) · 5.71 KB
/
issue_update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package update
import (
"errors"
"fmt"
"strings"
"github.com/profclems/glab/api"
"github.com/profclems/glab/commands/cmdutils"
"github.com/profclems/glab/commands/issue/issueutils"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
)
func NewCmdUpdate(f *cmdutils.Factory) *cobra.Command {
var issueUpdateCmd = &cobra.Command{
Use: "update <id>",
Short: `Update issue`,
Long: ``,
Example: heredoc.Doc(`
$ glab issue update 42 --label ui,ux
$ glab issue update 42 --unlabel working
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
var actions []string
var ua *cmdutils.UserAssignments
out := f.IO.StdOut
c := f.IO.Color()
if cmd.Flags().Changed("unassign") && cmd.Flags().Changed("assignee") {
return &cmdutils.FlagError{Err: fmt.Errorf("--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")}
}
apiClient, err := f.HttpClient()
if err != nil {
return err
}
issue, repo, err := issueutils.IssueFromArg(apiClient, f.BaseRepo, 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 = gitlab.String(m)
}
if m, _ := cmd.Flags().GetBool("lock-discussion"); m {
actions = append(actions, "locked discussion")
l.DiscussionLocked = gitlab.Bool(m)
}
if m, _ := cmd.Flags().GetBool("unlock-discussion"); m {
actions = append(actions, "unlocked dicussion")
l.DiscussionLocked = gitlab.Bool(false)
}
if m, _ := cmd.Flags().GetString("description"); m != "" {
actions = append(actions, "updated description")
l.Description = gitlab.String(m)
}
if m, _ := cmd.Flags().GetStringSlice("label"); len(m) != 0 {
actions = append(actions, fmt.Sprintf("added labels %s", strings.Join(m, " ")))
l.AddLabels = gitlab.Labels(m)
}
if m, _ := cmd.Flags().GetStringSlice("unlabel"); len(m) != 0 {
actions = append(actions, fmt.Sprintf("removed labels %s", strings.Join(m, " ")))
l.RemoveLabels = gitlab.Labels(m)
}
if m, _ := cmd.Flags().GetBool("public"); m {
actions = append(actions, "made public")
l.Confidential = gitlab.Bool(false)
}
if m, _ := cmd.Flags().GetBool("confidential"); m {
actions = append(actions, "made confidential")
l.Confidential = gitlab.Bool(true)
}
if ok := cmd.Flags().Changed("milestone"); ok {
if m, _ := cmd.Flags().GetString("milestone"); m != "" || m == "0" {
mID, err := cmdutils.ParseMilestone(apiClient, repo, m)
if err != nil {
return err
}
actions = append(actions, fmt.Sprintf("added milestone %q", m))
l.MilestoneID = gitlab.Int(mID)
} else {
// Unassign the Milestone
actions = append(actions, "unassigned milestone")
l.MilestoneID = gitlab.Int(0)
}
}
if cmd.Flags().Changed("unassign") {
l.AssigneeIDs = []int{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(apiClient, actions)
if err != nil {
return err
}
} else if len(ua.ToAdd) != 0 || len(ua.ToRemove) != 0 {
issue, err := api.GetIssue(apiClient, repo.FullName(), issue.IID)
if err != nil {
return err
}
l.AssigneeIDs, actions, err = ua.UsersFromAddRemove(issue.Assignees, nil, apiClient, actions)
if err != nil {
return err
}
}
}
fmt.Fprintf(out, "- Updating issue #%d\n", issue.IID)
issue, err = api.UpdateIssue(apiClient, repo.FullName(), issue.IID, l)
if err != nil {
return err
}
for _, s := range actions {
fmt.Fprintln(out, c.GreenCheck(), s)
}
fmt.Fprintln(out, issueutils.DisplayIssue(c, issue))
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")
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, pass \"\" or 0 to unassign")
issueUpdateCmd.Flags().StringSliceP("assignee", "a", []string{}, "assign users via username, prefix with '!' or '-' to remove from existing assignees, '+' to add, otherwise replace existing assignees with given users")
issueUpdateCmd.Flags().Bool("unassign", false, "unassign all users")
return issueUpdateCmd
}