GitLabgitlab-org
cli
main
1package create
3import (
4 "context"
5 "fmt"
6 "strings"
8 "github.com/MakeNowJust/heredoc/v2"
9 "github.com/spf13/cobra"
11 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/commands/workitems/api"
15 "gitlab.com/gitlab-org/cli/internal/commands/workitems/utils"
16 "gitlab.com/gitlab-org/cli/internal/config"
17 "gitlab.com/gitlab-org/cli/internal/glrepo"
18 "gitlab.com/gitlab-org/cli/internal/iostreams"
19 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
20 "gitlab.com/gitlab-org/cli/internal/text"
21)
23type options struct {
24 // Dependencies
25 io *iostreams.IOStreams
26 baseRepo func() (glrepo.Interface, error)
27 gitlabClient func() (*gitlab.Client, error)
28 config func() config.Config
30 // Flags
31 group string
32 title string
33 workItemType string
34 description string
35 attach []string
36 confidential bool
37 iteration int64
38 parent string
40 // Internal state
41 needsPrompt bool
42 iterationSet bool
43 scope *api.ScopeInfo
45 // Output
46 outputFormat string
47}
49func NewCmd(f cmdutils.Factory) *cobra.Command {
50 opts := &options{
51 io: f.IO(),
52 baseRepo: f.BaseRepo,
53 gitlabClient: f.GitLabClient,
54 config: f.Config,
55 }
57 cmd := &cobra.Command{
58 Use: "create [flags]",
59 Short: "Create work items in a project or group. (EXPERIMENTAL)",
60 Long: heredoc.Docf(`Use %[1]s--type%[1]s to specify the kind of work item to create.
61 The command uses your repository context to detect scope automatically.
63 %[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. Uploads are project-scoped, so %[1]s--attach%[1]s cannot be combined with %[1]s--group%[1]s.
65 %[1]s--parent%[1]s accepts a work item ID in the current scope, a reference such as %[1]sgroup/project#123%[1]s, or a URL.
66 `, "`") + text.ExperimentalString,
67 Example: heredoc.Doc(`
68 # Create a work item in the current project
69 glab work-items create --type issue
71 # Create a work item in a group
72 glab work-items create --type epic --group my-group
74 # Read the description from a file
75 glab work-items create --type issue --title "Add feature" --description-file description.md
77 # Read the description from standard input
78 cat description.md | glab work-items create --type issue --title "Add feature" --description-file -
80 # Attach a screenshot to the description
81 glab work-items create --type issue --title "Add feature" --attach ./screenshot.png
83 # Create a work item in an iteration
84 glab work-items create --type issue --title "Add feature" --iteration 123
86 # Create a task under work item 42 in the current project
87 glab work-items create --type task --title "Write tests" --parent 42
89 # Create a task under a work item in another project
90 glab work-items create --type task --title "Write tests" --parent my-group/my-project#42
91 `),
92 Args: cobra.NoArgs,
93 ValidArgsFunction: cobra.NoFileCompletions,
94 Annotations: map[string]string{
95 mcpannotations.Destructive: "true",
96 },
97 RunE: func(cmd *cobra.Command, args []string) error {
98 if err := opts.complete(cmd.Context(), cmd); err != nil {
99 return err
100 }
101 if err := opts.validate(); err != nil {
102 return err
103 }
104 return opts.run(cmd.Context())
105 },
106 }
108 // Enable -R flag for repo override
109 cmdutils.EnableRepoOverride(cmd, f)
111 cmdutils.EnableJSONOutput(cmd, opts.io, &opts.outputFormat)
113 // Flags
114 cmd.Flags().StringVarP(&opts.group, "group", "g", "", "Create work items for a group or subgroup.")
115 cmd.Flags().StringVarP(&opts.workItemType, "type", "T", "", "Type of work item ("+strings.Join(utils.ValidTypeNames(), ", ")+").")
117 cmd.Flags().StringVarP(&opts.title, "title", "t", "", "Add a title for the work item.")
118 cmd.Flags().StringVarP(&opts.description, "description", "d", "", "Description of the work item. Set to \"-\" to open an editor.")
119 cmd.Flags().BoolVarP(&opts.confidential, "confidential", "c", false, "Mark work item confidential.")
120 cmd.Flags().Int64Var(&opts.iteration, "iteration", 0, "Add the work item to the iteration with this ID.")
121 cmd.Flags().StringVar(&opts.parent, "parent", "", "Set the parent work item by <id>, reference, or URL.")
123 cmdutils.AddDescriptionFileFlag(cmd, "work item")
124 cmdutils.AddAttachFlag(cmd, &opts.attach, "description")
126 _ = cmd.MarkFlagRequired("type")
127 cmd.MarkFlagsMutuallyExclusive("group", "repo")
128 // Uploads are project-scoped, and GitLab has no group-level equivalent.
129 cmd.MarkFlagsMutuallyExclusive("group", "attach")
131 return cmd
132}
134func (opts *options) complete(ctx context.Context, cmd *cobra.Command) error {
135 group, err := cmdutils.GroupOverride(cmd)
136 if err != nil {
137 return err
138 }
139 opts.group = group
140 opts.needsPrompt = !cmd.Flags().Changed("title")
141 opts.iterationSet = cmd.Flags().Changed("iteration")
143 if err := cmdutils.ResolveDescriptionFile(opts.io, cmd); err != nil {
144 return err
145 }
147 if err := cmdutils.HandleDescriptionEditor(ctx, &opts.description, opts.io, opts.config, nil); err != nil {
148 return err
149 }
151 scope, err := utils.DetectScope(opts.group, opts.baseRepo)
152 if err != nil {
153 return err
154 }
155 opts.scope = scope
157 return nil
158}
160func (opts *options) validate() error {
161 if _, err := utils.ResolveTypeID(opts.workItemType); err != nil {
162 return cmdutils.FlagError{Err: err}
163 }
165 if opts.needsPrompt && !opts.io.IsInteractive() {
166 return cmdutils.FlagError{Err: fmt.Errorf("--title required for non-interactive mode")}
167 }
169 return nil
170}
172func (opts *options) run(ctx context.Context) error {
173 client, err := opts.gitlabClient()
174 if err != nil {
175 return fmt.Errorf("failed to get GitLab client: %w", err)
176 }
178 typeID, err := utils.ResolveTypeID(opts.workItemType)
179 if err != nil {
180 return err
181 }
183 createOpts := &gitlab.CreateWorkItemOptions{
184 Title: opts.title,
185 }
187 if opts.parent != "" {
188 parentID, err := utils.ResolveParentID(ctx, client, opts.config(), opts.parent, opts.scope.Path)
189 if err != nil {
190 return err
191 }
192 createOpts.ParentID = new(parentID)
193 }
195 description, err := cmdutils.AppendAttachments(ctx, opts.io, client, opts.scope.Path, opts.description, opts.attach)
196 if err != nil {
197 return err
198 }
199 if description != "" {
200 createOpts.Description = new(description)
201 }
203 if opts.confidential {
204 createOpts.Confidential = new(true)
205 }
207 if opts.iterationSet {
208 createOpts.IterationID = new(opts.iteration)
209 }
211 wi, _, err := client.WorkItems.CreateWorkItem(opts.scope.Path, typeID, createOpts, gitlab.WithContext(ctx))
212 if err != nil {
213 return err
214 }
216 switch opts.outputFormat {
217 case "json":
218 return opts.io.PrintJSON(wi)
219 default:
220 opts.io.LogInfo("- Creating work item in", opts.scope.Path)
222 if opts.io.IsaTTY {
223 opts.io.LogInfof("#%d %s\n%s\n", wi.IID, wi.Title, wi.WebURL)
224 } else {
225 opts.io.LogInfo(wi.WebURL)
226 }
227 }
229 return nil
230}
Diagnostics8 git calls · 312 ms
CallStatusSizeTime0312 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchtree:02001.8 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200317 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=317 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=33cd62a3f1bc55e6bd56a9b96f181ff12c30955a git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=317 git.pkt.payload.max=290 git.sideband.band1.bytes=290 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=290 git.pack.trailer=5711d3cc007ef3bd902f87854e977c46d0913d04Open pack map
fetchtree:0200169 B · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=295 git.response.bytes=169 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=e2a55b61933487e8e6f2e62d12464503856ecc34 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=169 git.pkt.payload.max=142 git.sideband.band1.bytes=142 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=142 git.pack.trailer=009c97babab804d87b16c1c6fb6145fa33f218b8Open pack map
fetch2002.3 KiB295 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2396 git.response.cache=MISS git.response.duration_ms=295 git.fetch.wants=75cc54501016b9249a9f29ef1a773d5e96b0f071 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2396 git.pkt.payload.max=2369 git.sideband.band1.bytes=2369 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e1da0bcbe4a151
- Request
- 62b9fa18…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format