GitLabgitlab-org

cli

main
ply8 git calls497 ms99 KiB5 cachedprotocol v2
152 lines4.8 KiBRaw
1package _for
3import (
4 "fmt"
5 "strings"
7 "github.com/MakeNowJust/heredoc/v2"
8 "github.com/spf13/cobra"
10 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
12 "gitlab.com/gitlab-org/cli/internal/api"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/commands/mr/mrutils"
15 "gitlab.com/gitlab-org/cli/internal/git"
16 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
17 "gitlab.com/gitlab-org/cli/internal/utils"
20func NewCmdFor(f cmdutils.Factory) *cobra.Command {
21 mrForCmd := &cobra.Command{
22 Use: "for",
23 Short: `Create a new merge request for an issue.`,
24 Long: heredoc.Docf(`
25 Creates a branch and opens a merge request linked to the issue.
26 Use %[1]s--draft%[1]s to mark the merge request as a draft.
27 `, "`"),
28 Aliases: []string{"new-for", "create-for", "for-issue"},
29 Example: heredoc.Doc(`
30 # Create merge request for issue 34
31 $ glab mr for 34
33 # Create merge request for issue 34 and mark as work in progress
34 $ glab mr for 34 --wip
36 $ glab mr new-for 34
37 $ glab mr create-for 34
38 `),
39 Args: cobra.ExactArgs(1),
40 Annotations: map[string]string{
41 mcpannotations.Destructive: "true",
42 },
43 RunE: func(cmd *cobra.Command, args []string) error {
44 var err error
46 client, err := f.GitLabClient()
47 if err != nil {
48 return err
49 }
51 repo, err := f.BaseRepo()
52 if err != nil {
53 return err
54 }
56 issueID := utils.StringToInt(args[0])
57 issue, err := api.GetIssue(client, repo.FullName(), int64(issueID))
58 if err != nil {
59 return err
60 }
62 var targetBranch string
63 if t, _ := cmd.Flags().GetString("target-branch"); t != "" {
64 targetBranch = strings.TrimSpace(t)
65 } else {
66 project, err := api.GetProject(client, repo.FullName())
67 if err != nil {
68 return fmt.Errorf("error getting project details: %w", err)
69 }
70 if project.DefaultBranch != "" {
71 targetBranch = project.DefaultBranch
72 } else {
73 targetBranch = git.DefaultBranchName
74 }
75 }
77 issueBranch := fmt.Sprintf("%d-%s", issue.IID, utils.ReplaceNonAlphaNumericChars(strings.ToLower(issue.Title), "-"))
78 sourceBranch, err := mrutils.CreateUniqueBranch(cmd.Context(), client, repo.FullName(), issueBranch, targetBranch)
79 if err != nil {
80 return err
81 }
83 var mergeTitle string
84 mergeTitle = fmt.Sprintf("Resolve %q", issue.Title)
86 isDraft, _ := cmd.Flags().GetBool("draft")
87 isWIP, _ := cmd.Flags().GetBool("wip")
88 if isDraft || isWIP {
89 if isWIP {
90 mergeTitle = "WIP: " + mergeTitle
91 } else {
92 mergeTitle = "Draft: " + mergeTitle
93 }
94 }
96 mergeLabel, _ := cmd.Flags().GetString("label")
98 l := &gitlab.CreateMergeRequestOptions{}
99 l.Title = new(mergeTitle)
100 l.Description = new(fmt.Sprintf("Closes #%d", issue.IID))
101 l.Labels = &gitlab.LabelOptions{mergeLabel}
102 l.SourceBranch = new(sourceBranch)
103 l.TargetBranch = new(targetBranch)
104 if milestone, _ := cmd.Flags().GetInt("milestone"); milestone != -1 {
105 l.MilestoneID = new(int64(milestone))
106 }
107 if allowCol, _ := cmd.Flags().GetBool("allow-collaboration"); allowCol {
108 l.AllowCollaboration = new(true)
109 }
110 if removeSource, _ := cmd.Flags().GetBool("remove-source-branch"); removeSource {
111 l.RemoveSourceBranch = new(true)
112 }
113 if withLabels, _ := cmd.Flags().GetBool("with-labels"); withLabels {
114 l.Labels = (*gitlab.LabelOptions)(&issue.Labels)
115 }
117 if a, _ := cmd.Flags().GetString("assignee"); a != "" {
118 arrIds := strings.Split(strings.Trim(a, "[] "), ",")
119 var t2 []int64
121 for _, i := range arrIds {
122 j := utils.StringToInt(i)
123 t2 = append(t2, int64(j))
124 }
125 l.AssigneeIDs = &t2
126 }
128 mr, _, err := client.MergeRequests.CreateMergeRequest(repo.FullName(), l)
129 if err != nil {
130 return err
131 }
133 f.IO().LogInfo(mrutils.DisplayMR(f.IO().Color(), &mr.BasicMergeRequest, f.IO().IsaTTY))
135 return nil
136 },
137 }
139 mrForCmd.Flags().BoolP("draft", "", true, "Mark merge request as a draft.")
140 mrForCmd.Flags().BoolP("wip", "", false, "Mark merge request as a work in progress. Overrides --draft.")
141 mrForCmd.Flags().StringP("label", "l", "", "Add label by name. Multiple labels should be comma-separated.")
142 mrForCmd.Flags().StringP("assignee", "a", "", "Assign merge request to people by their IDs. Multiple values should be comma-separated.")
143 mrForCmd.Flags().BoolP("allow-collaboration", "", false, "Allow commits from other members.")
144 mrForCmd.Flags().BoolP("remove-source-branch", "", false, "Remove source branch on merge.")
145 mrForCmd.Flags().IntP("milestone", "m", -1, "Add milestone by <id> for this merge request.")
146 mrForCmd.Flags().StringP("target-branch", "b", "", "The target or base branch into which you want your code merged.")
147 mrForCmd.Flags().BoolP("with-labels", "", false, "Copy labels from issue to the merge request.")
149 mrForCmd.Deprecated = "use `glab mr create --related-issue <issueID>`."
151 return mrForCmd
Diagnostics8 git calls · 497 ms
CallStatusSizeTime0497 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
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 KiB2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=UPDATING
git.response.duration_ms=2
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=3
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 · hit3 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=32926
git.response.cache=HIT
git.response.duration_ms=3
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=44
Open pack map
fetchtree:02001.8 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=1857
git.response.cache=HIT
git.response.duration_ms=2
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=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200746 B · hit3 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=746
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=f3d413a22af5a9c2fe9aa49709b1cb36d1351a7f
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=746
git.pkt.payload.max=719
git.sideband.band1.bytes=719
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=719
git.pack.trailer=330baf80c59a4208b55120dff7dd824b7952b790
Open pack map
fetchtree:0200138 B · hit3 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=138
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=540aeef5a4ed67adfd5329954920f5bd0e26077c
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=138
git.pkt.payload.max=111
git.sideband.band1.bytes=111
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=111
git.pack.trailer=aac17a6af649190c335a55dd0b043493d961025e
Open pack map
fetch2001.8 KiB485 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=485
git.response.bytes=1814
git.response.cache=MISS
git.response.duration_ms=485
git.fetch.wants=9464ef95671058583bed09e3d67837a1d8de6033
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=1814
git.pkt.payload.max=1787
git.sideband.band1.bytes=1787
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
a5e2ec7b0b7f77be
Request
f2a5fad7…
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