Skip to content

Commit

Permalink
mr discussion: Drop mrDiscussionText()
Browse files Browse the repository at this point in the history
Drop mrDiscussionText() and use noteText() instead.

Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Aug 25, 2021
1 parent b723634 commit ad90d0e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 47 deletions.
48 changes: 4 additions & 44 deletions cmd/mr_discussion.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"runtime"
"strconv"
"strings"
"text/template"

"github.com/MakeNowJust/heredoc/v2"
"github.com/rsteube/carapace"
Expand Down Expand Up @@ -94,7 +92,8 @@ func mrDiscussionMsg(mrNum int, state string, commit string, msgs []string, body
return strings.Join(msgs[0:], "\n\n"), nil
}

text, err := mrDiscussionText(mrNum, state, commit, body)
tmpl := mrDiscussionGetTemplate(commit)
text, err := noteText(mrNum, state, commit, body, tmpl)
if err != nil {
return "", err
}
Expand All @@ -105,55 +104,16 @@ func mrDiscussionGetTemplate(commit string) string {
if commit == "" {
return heredoc.Doc(`
{{.InitMsg}}
{{.CommentChar}} This thread is being started on {{.State}} Merge Request {{.MRnum}}.
{{.CommentChar}} This thread is being started on {{.State}} Merge Request {{.IDnum}}.
{{.CommentChar}} Comment lines beginning with '{{.CommentChar}}' are discarded.`)
}
return heredoc.Doc(`
{{.InitMsg}}
{{.CommentChar}} This thread is being started on {{.State}} Merge Request {{.MRnum}} commit {{.Commit}}.
{{.CommentChar}} This thread is being started on {{.State}} Merge Request {{.IDnum}} commit {{.Commit}}.
{{.CommentChar}} Do not delete patch tracking lines that begin with '|'.
{{.CommentChar}} Comment lines beginning with '{{.CommentChar}}' are discarded.`)
}

func mrDiscussionText(mrNum int, state string, commit string, body string) (string, error) {
tmpl := mrDiscussionGetTemplate(commit)
initMsg := body
commentChar := git.CommentChar()

if commit != "" {
if len(commit) > 11 {
commit = commit[:12]
}
}

t, err := template.New("tmpl").Parse(tmpl)
if err != nil {
return "", err
}

msg := &struct {
InitMsg string
CommentChar string
State string
MRnum int
Commit string
}{
InitMsg: initMsg,
CommentChar: commentChar,
State: state,
MRnum: mrNum,
Commit: commit,
}

var b bytes.Buffer
err = t.Execute(&b, msg)
if err != nil {
return "", err
}

return b.String(), nil
}

func init() {
mrCreateDiscussionCmd.Flags().StringSliceP("message", "m", []string{}, "use the given <msg>; multiple -m are concatenated as separate paragraphs")
mrCreateDiscussionCmd.Flags().StringP("file", "F", "", "use the given file as the message")
Expand Down
12 changes: 9 additions & 3 deletions cmd/note_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ func noteMsg(msgs []string, isMR bool, idNum int, state string, commit string, b
return strings.Join(msgs[0:], "\n\n"), nil
}

text, err := noteText(isMR, idNum, state, commit ,body)
tmpl := noteGetTemplate(isMR, commit)
text, err := noteText(idNum, state, commit, body, tmpl)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -322,11 +323,16 @@ func noteGetTemplate(isMR bool, commit string) string {
{{.CommentChar}} Comment lines beginning with '{{.CommentChar}}' are discarded.`)
}

func noteText(isMR bool, idNum int, state string, commit string, body string) (string, error) {
tmpl := noteGetTemplate(isMR, commit)
func noteText(idNum int, state string, commit string, body string, tmpl string) (string, error) {
initMsg := body
commentChar := git.CommentChar()

if commit != "" {
if len(commit) > 11 {
commit = commit[:12]
}
}

t, err := template.New("tmpl").Parse(tmpl)
if err != nil {
return "", err
Expand Down

0 comments on commit ad90d0e

Please sign in to comment.