Skip to content

Commit

Permalink
cmd/edit: accept linebreak flag for editing description
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bmeneg committed Feb 2, 2022
1 parent 6211a8a commit 66c580f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
8 changes: 1 addition & 7 deletions cmd/edit_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
Expand Down
30 changes: 19 additions & 11 deletions cmd/issue_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,32 @@ 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
if abortUpdate {
log.Fatal("aborting: no changes")
}

if linebreak {
body = textToMarkdown(body)
}

opts := &gitlab.UpdateIssueOptions{
Title: &title,
Description: &body,
Expand Down
22 changes: 13 additions & 9 deletions cmd/mr_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:") ||
Expand Down

0 comments on commit 66c580f

Please sign in to comment.