Skip to content

Commit

Permalink
mr note: Use templates for note information
Browse files Browse the repository at this point in the history
In edit mode 'lab mr discussion' provides good instructions about
comment lines being ignored, the issue or MR ID, etc.  This code is a mix
of template and string usage and can be cleaned up.

Use templates for note information.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Aug 25, 2021
1 parent 0723109 commit b723634
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 47 deletions.
11 changes: 1 addition & 10 deletions cmd/mr_discussion.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,7 @@ 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]
state := noteGetState(rn, true, int(mrNum))

body := ""
if filename != "" {
Expand Down
102 changes: 65 additions & 37 deletions cmd/note_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,34 @@ func createCommitComments(project string, mrID int, commit string, body string,
}

}

func noteGetState(rn string, isMR bool, idNum int) (state string) {
if isMR {
mr, err := lab.MRGet(rn, idNum)
if err != nil {
log.Fatal(err)
}

state = map[string]string{
"opened": "OPEN",
"closed": "CLOSED",
"merged": "MERGED",
}[mr.State]
} else {
issue, err := lab.IssueGet(rn, idNum)
if err != nil {
log.Fatal(err)
}

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

return state
}

func createNote(rn string, isMR bool, idNum int, msgs []string, filename string, linebreak bool, commit string) {
var err error

Expand All @@ -214,39 +242,13 @@ func createNote(rn string, isMR bool, idNum int, msgs []string, filename string,
}
body = string(content)
} else {
if isMR {
mr, err := lab.MRGet(rn, idNum)
if err != nil {
log.Fatal(err)
}

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

if commit != "" {
body = getCommitBody(rn, commit)
body += fmt.Sprintf("\n# This comment is being applied to %s Merge Request %d commit %s.\n# Do not delete patch tracking lines that begin with '|'.", state, idNum, commit[:len(commit)])
} else {
body += fmt.Sprintf("\n# This comment is being applied to %s Merge Request %d.", state, idNum)
}
} else {
issue, err := lab.IssueGet(rn, idNum)
if err != nil {
log.Fatal(err)
}
state := noteGetState(rn, isMR, idNum)

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

body = fmt.Sprintf("\n# This comment is being applied to %s Issue %d.", state, idNum)
if isMR && commit != "" {
body = getCommitBody(rn, commit)
}

body, err = noteMsg(msgs, isMR, body)
body, err = noteMsg(msgs, isMR, idNum, state, commit, body)
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
Expand Down Expand Up @@ -284,12 +286,12 @@ func createNote(rn string, isMR bool, idNum int, msgs []string, filename string,
fmt.Println(noteURL)
}

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

text, err := noteText(body)
text, err := noteText(isMR, idNum, state, commit ,body)
if err != nil {
return "", err
}
Expand All @@ -300,11 +302,28 @@ func noteMsg(msgs []string, isMR bool, body string) (string, error) {
return git.EditFile("ISSUE_NOTE", text)
}

func noteText(body string) (string, error) {
tmpl := heredoc.Doc(`
func noteGetTemplate(isMR bool, commit string) string {
if !isMR {
return heredoc.Doc(`
{{.InitMsg}}
{{.CommentChar}} This comment is being applied to {{.State}} Issue {{.IDnum}}.
{{.CommentChar}} Comment lines beginning with '{{.CommentChar}}' are discarded.`)
}
if isMR && commit == "" {
return heredoc.Doc(`
{{.InitMsg}}
{{.CommentChar}} This comment is being applied to {{.State}} Merge Request {{.IDnum}}.
{{.CommentChar}} Comment lines beginning with '{{.CommentChar}}' are discarded.`)
}
return heredoc.Doc(`
{{.InitMsg}}
{{.CommentChar}} Write a message for this note. Commented lines are discarded.`)
{{.CommentChar}} This comment is being applied to {{.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 noteText(isMR bool, idNum int, state string, commit string, body string) (string, error) {
tmpl := noteGetTemplate(isMR, commit)
initMsg := body
commentChar := git.CommentChar()

Expand All @@ -316,9 +335,15 @@ func noteText(body string) (string, error) {
msg := &struct {
InitMsg string
CommentChar string
State string
IDnum int
Commit string
}{
InitMsg: initMsg,
CommentChar: commentChar,
State: state,
IDnum: idNum,
Commit: commit,
}

var b bytes.Buffer
Expand Down Expand Up @@ -346,6 +371,9 @@ func replyNote(rn string, isMR bool, idNum int, reply int, quote bool, update bo
if err != nil {
log.Fatal(err)
}

state := noteGetState(rn, isMR, idNum)

for _, discussion := range discussions {
for _, note := range discussion.Notes {

Expand All @@ -362,7 +390,7 @@ func replyNote(rn string, isMR bool, idNum int, reply int, quote bool, update bo

body := ""
if len(msgs) != 0 {
body, err = noteMsg(msgs, isMR, note.Body)
body, err = noteMsg(msgs, isMR, idNum, state, "", body)
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
Expand All @@ -382,7 +410,7 @@ func replyNote(rn string, isMR bool, idNum int, reply int, quote bool, update bo
noteBody = ">" + noteBody + "\n"
}
}
body, err = noteMsg([]string{}, isMR, noteBody)
body, err = noteMsg([]string{}, isMR, idNum, state, "", noteBody)
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
Expand Down

0 comments on commit b723634

Please sign in to comment.