Skip to content

Commit

Permalink
mr_discussion: Provide better discussion information
Browse files Browse the repository at this point in the history
In edit mode 'lab mr discussion' provides very basic instructions about
comment lines being ignored.  This is useful but it is not enough
information on the discussion thread being created.

Add the status of the MR, the MR ID, and if specified, the commit ID, to
the discussion information in edit mode.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Aug 25, 2021
1 parent a49cfa0 commit 0723109
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions cmd/mr_discussion.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ var mrCreateDiscussionCmd = &cobra.Command{
log.Fatal(err)
}

mr, err := lab.MRGet(rn, int(mrNum))
if err != nil {
log.Fatal(err)
}

state := map[string]string{
"opened": "OPEN",
"closed": "CLOSED",
"merged": "MERGED",
}[mr.State]

body := ""
if filename != "" {
content, err := ioutil.ReadFile(filename)
Expand All @@ -57,14 +68,14 @@ var mrCreateDiscussionCmd = &cobra.Command{
}
body = string(content)
} else if commit == "" {
body, err = mrDiscussionMsg(msgs, "\n")
body, err = mrDiscussionMsg(int(mrNum), state, commit, msgs, "\n")
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
}
} else {
body = getCommitBody(rn, commit)
body, err = mrDiscussionMsg(nil, body)
body, err = mrDiscussionMsg(int(mrNum), state, commit, nil, body)
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
Expand All @@ -87,26 +98,43 @@ var mrCreateDiscussionCmd = &cobra.Command{
},
}

func mrDiscussionMsg(msgs []string, body string) (string, error) {
func mrDiscussionMsg(mrNum int, state string, commit string, msgs []string, body string) (string, error) {
if len(msgs) > 0 {
return strings.Join(msgs[0:], "\n\n"), nil
}

text, err := mrDiscussionText(body)
text, err := mrDiscussionText(mrNum, state, commit, body)
if err != nil {
return "", err
}
return git.EditFile("MR_DISCUSSION", text)
}

func mrDiscussionText(body string) (string, error) {
tmpl := heredoc.Doc(`
func mrDiscussionGetTemplate(commit string) string {
if commit == "" {
return heredoc.Doc(`
{{.InitMsg}}
{{.CommentChar}} Write a message for this discussion. Commented lines are discarded.`)
{{.CommentChar}} This thread is being started on {{.State}} Merge Request {{.MRnum}}.
{{.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}} 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
Expand All @@ -115,9 +143,15 @@ func mrDiscussionText(body string) (string, error) {
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
Expand Down

0 comments on commit 0723109

Please sign in to comment.