Skip to content

Commit

Permalink
note: Add reply --quote functionality
Browse files Browse the repository at this point in the history
Add a --quote parameter which quotes the replied-to comment.

Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Sep 26, 2020
1 parent 0e25db0 commit 4353b94
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
27 changes: 19 additions & 8 deletions cmd/issue_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ func NoteRunFn(cmd *cobra.Command, args []string) {
}

if reply != 0 {
ReplyNote(rn, isMR, int(idNum), reply, filename, linebreak)
quote, err := cmd.Flags().GetBool("quote")
if err != nil {
log.Fatal(err)
}
ReplyNote(rn, isMR, int(idNum), reply, quote, filename, linebreak)
return
}

Expand All @@ -89,7 +93,7 @@ func CreateNote(rn string, isMR bool, idNum int, msgs []string, filename string,
}
body = string(content)
} else {
body, err = noteMsg(msgs, isMR)
body, err = noteMsg(msgs, isMR, "\n")
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
Expand Down Expand Up @@ -123,12 +127,12 @@ func CreateNote(rn string, isMR bool, idNum int, msgs []string, filename string,
fmt.Println(noteURL)
}

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

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

func noteText() (string, error) {
func noteText(body string) (string, error) {
const tmpl = `{{.InitMsg}}
{{.CommentChar}} Write a message for this note. Commented lines are discarded.`

initMsg := "\n"
initMsg := body
commentChar := git.CommentChar()

t, err := template.New("tmpl").Parse(tmpl)
Expand All @@ -168,7 +172,7 @@ func noteText() (string, error) {
return b.String(), nil
}

func ReplyNote(rn string, isMR bool, idNum int, reply int, filename string, linebreak bool) {
func ReplyNote(rn string, isMR bool, idNum int, reply int, quote bool, filename string, linebreak bool) {

var (
discussions []*gitlab.Discussion
Expand Down Expand Up @@ -203,7 +207,13 @@ func ReplyNote(rn string, isMR bool, idNum int, reply int, filename string, line
}
body = string(content)
} else {
body, err = noteMsg([]string{}, isMR)
noteBody := ""
if quote {
noteBody = note.Body
noteBody = strings.Replace(noteBody, "\n", "\n>", -1)
noteBody = ">" + noteBody + "\n"
}
body, err = noteMsg([]string{}, isMR, noteBody)
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
Expand Down Expand Up @@ -241,6 +251,7 @@ func init() {
issueNoteCmd.Flags().StringArrayP("message", "m", []string{}, "Use the given <msg>; multiple -m are concatenated as separate paragraphs")
issueNoteCmd.Flags().StringP("file", "F", "", "Use the given file as the message")
issueNoteCmd.Flags().Bool("force-linebreak", false, "append 2 spaces to the end of each line to force markdown linebreaks")
issueNoteCmd.Flags().Bool("quote", false, "Quote note in reply (used with --reply only)")

issueCmd.AddCommand(issueNoteCmd)
carapace.Gen(issueNoteCmd).PositionalCompletion(
Expand Down
4 changes: 2 additions & 2 deletions cmd/issue_note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Test_issueNoteMsg(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
test := test
t.Parallel()
body, err := noteMsg(test.Msgs, false)
body, err := noteMsg(test.Msgs, false, "\n")
if err != nil {
t.Fatal(err)
}
Expand All @@ -55,7 +55,7 @@ func Test_issueNoteMsg(t *testing.T) {

func Test_issueNoteText(t *testing.T) {
t.Parallel()
text, err := noteText()
text, err := noteText("\n")
if err != nil {
t.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/mr_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func init() {
mrNoteCmd.Flags().StringArrayP("message", "m", []string{}, "Use the given <msg>; multiple -m are concatenated as separate paragraphs")
mrNoteCmd.Flags().StringP("file", "F", "", "Use the given file as the message")
mrNoteCmd.Flags().Bool("force-linebreak", false, "append 2 spaces to the end of each line to force markdown linebreaks")
mrNoteCmd.Flags().Bool("quote", false, "Quote note in reply (used with --reply only)")

mrCmd.AddCommand(mrNoteCmd)
carapace.Gen(mrNoteCmd).PositionalCompletion(
Expand Down
4 changes: 2 additions & 2 deletions cmd/mr_note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func Test_mrNoteMsg(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
test := test
t.Parallel()
body, err := noteMsg(test.Msgs, true)
body, err := noteMsg(test.Msgs, true, "\n")
if err != nil {
t.Fatal(err)
}
Expand All @@ -102,7 +102,7 @@ func Test_mrNoteMsg(t *testing.T) {

func Test_mrNoteText(t *testing.T) {
t.Parallel()
text, err := noteText()
text, err := noteText("\n")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 4353b94

Please sign in to comment.