From 66c580f8cf1651d24fdfe0fbf1425a539862cc7c Mon Sep 17 00:00:00 2001 From: Bruno Meneguele Date: Wed, 2 Feb 2022 12:59:47 -0300 Subject: [PATCH] cmd/edit: accept linebreak flag for editing description While editing an MR or issue description if any flag different from -m and -F is used the editor is not opened. Add the --linebreak to this list of "accepted" flags to be used for editing the description. --- cmd/edit_common.go | 8 +------- cmd/issue_edit.go | 30 +++++++++++++++++++----------- cmd/mr_edit.go | 22 +++++++++++++--------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/cmd/edit_common.go b/cmd/edit_common.go index 54b55ff3..f7515e62 100644 --- a/cmd/edit_common.go +++ b/cmd/edit_common.go @@ -41,7 +41,7 @@ func getUpdateUsers(currentUsers []string, users []string, remove []string) ([]i // editGetTitleDescription returns a title and description based on the // current issue title and description and various flags from the command // line -func editGetTitleDescription(title string, body string, msgs []string, nFlag int) (string, string, error) { +func editGetTitleDescription(title string, body string, msgs []string) (string, string, error) { if len(msgs) > 0 { title = msgs[0] @@ -53,12 +53,6 @@ func editGetTitleDescription(title string, body string, msgs []string, nFlag int return title, body, nil } - // if other flags were given (eg label), then skip the editor and return - // what we already have - if nFlag != 0 { - return title, body, nil - } - text, err := editText(title, body) if err != nil { return "", "", err diff --git a/cmd/issue_edit.go b/cmd/issue_edit.go index 666dde35..a8ec6479 100644 --- a/cmd/issue_edit.go +++ b/cmd/issue_edit.go @@ -130,13 +130,25 @@ var issueEditCmd = &cobra.Command{ if err != nil { log.Fatal(err) } - title, body, err := editGetTitleDescription(issue.Title, issue.Description, msgs, cmd.Flags().NFlag()) - if err != nil { - _, f, l, _ := runtime.Caller(0) - log.Fatal(f+":"+strconv.Itoa(l)+" ", err) - } - if title == "" { - log.Fatal("aborting: empty issue title") + + title := issue.Title + body := issue.Description + + // We only consider editing an issue with -m or when no other flag is + // passed, but --linebreak. + if len(msgs) > 0 || cmd.Flags().NFlag() == 0 || (cmd.Flags().NFlag() == 1 && linebreak) { + title, body, err = editGetTitleDescription(issue.Title, issue.Description, msgs) + if err != nil { + _, f, l, _ := runtime.Caller(0) + log.Fatal(f+":"+strconv.Itoa(l)+" ", err) + } + if title == "" { + log.Fatal("aborting: empty issue title") + } + + if linebreak { + body = textToMarkdown(body) + } } abortUpdate := title == issue.Title && body == issue.Description && !labelsChanged && !assigneesChanged && !updateMilestone @@ -144,10 +156,6 @@ var issueEditCmd = &cobra.Command{ log.Fatal("aborting: no changes") } - if linebreak { - body = textToMarkdown(body) - } - opts := &gitlab.UpdateIssueOptions{ Title: &title, Description: &body, diff --git a/cmd/mr_edit.go b/cmd/mr_edit.go index 8e62103c..8ea10d5b 100644 --- a/cmd/mr_edit.go +++ b/cmd/mr_edit.go @@ -189,7 +189,8 @@ var mrEditCmd = &cobra.Command{ log.Fatal(err) } - var title, body string + title := mr.Title + body := mr.Description if filename != "" { if len(msgs) > 0 { @@ -201,16 +202,19 @@ var mrEditCmd = &cobra.Command{ log.Fatal(err) } } else { - title, body, err = editGetTitleDescription( - mr.Title, mr.Description, msgs, cmd.Flags().NFlag()) - if err != nil { - _, f, l, _ := runtime.Caller(0) - log.Fatal(f+":"+strconv.Itoa(l)+" ", err) + // We only consider editing an mr with -m, -F or when no other flag + // is passed, but --linebreak. + if len(msgs) > 0 || cmd.Flags().NFlag() == 0 || (cmd.Flags().NFlag() == 1 && linebreak) { + title, body, err = editGetTitleDescription(mr.Title, mr.Description, msgs) + if err != nil { + _, f, l, _ := runtime.Caller(0) + log.Fatal(f+":"+strconv.Itoa(l)+" ", err) + } } - } - if title == "" { - log.Fatal("aborting: empty mr title") + if title == "" { + log.Fatal("aborting: empty mr title") + } } isWIP := hasPrefix(title, "wip:") ||