Skip to content

Commit

Permalink
note: Add reply functionality
Browse files Browse the repository at this point in the history
Add the functionality to reply to a discussion note.  The format of the
command is

	lab [mr|note] note <id>:<comment_id>

and opens a $GITEDITOR to reply to the note.

Add reply functionality.

Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Sep 26, 2020
1 parent 1ee9cd5 commit 0e25db0
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 21 deletions.
118 changes: 103 additions & 15 deletions cmd/issue_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
)

var issueNoteCmd = &cobra.Command{
Use: "note [remote] <id>",
Aliases: []string{"comment"},
Use: "note [remote] <id>[:<comment_id>]",
Aliases: []string{"comment", "reply"},
Short: "Add a note or comment to an issue on GitLab",
Long: ``,
Args: cobra.MinimumNArgs(1),
Expand All @@ -30,11 +30,30 @@ var issueNoteCmd = &cobra.Command{
}

func NoteRunFn(cmd *cobra.Command, args []string) {
rn, idNum, err := parseArgs(args)

isMR := false
if os.Args[1] == "mr" {
isMR = true
}

rn, idString, err := parseArgsRemoteString(args)
if err != nil {
log.Fatal(err)
}

var (
idNum int = 0
reply int = 0
)

if strings.Contains(idString, ":") {
ids := strings.Split(idString, ":")
idNum, _ = strconv.Atoi(ids[0])
reply, _ = strconv.Atoi(ids[1])
} else {
idNum, _ = strconv.Atoi(idString)
}

msgs, err := cmd.Flags().GetStringArray("message")
if err != nil {
log.Fatal(err)
Expand All @@ -50,19 +69,18 @@ func NoteRunFn(cmd *cobra.Command, args []string) {
log.Fatal(err)
}

CreateNote(os.Args, rn, int(idNum), msgs, filename, linebreak)
}
if reply != 0 {
ReplyNote(rn, isMR, int(idNum), reply, filename, linebreak)
return
}

var is_mr bool = false
CreateNote(rn, isMR, int(idNum), msgs, filename, linebreak)
}

func CreateNote(args []string, rn string, idNum int, msgs []string, filename string, linebreak bool) {
func CreateNote(rn string, isMR bool, idNum int, msgs []string, filename string, linebreak bool) {

var err error

if args[1] == "mr" {
is_mr = true
}

body := ""
if filename != "" {
content, err := ioutil.ReadFile(filename)
Expand All @@ -71,7 +89,7 @@ func CreateNote(args []string, rn string, idNum int, msgs []string, filename str
}
body = string(content)
} else {
body, err = noteMsg(msgs)
body, err = noteMsg(msgs, isMR)
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
Expand All @@ -90,7 +108,7 @@ func CreateNote(args []string, rn string, idNum int, msgs []string, filename str
noteURL string
)

if is_mr {
if isMR {
noteURL, err = lab.MRCreateNote(rn, idNum, &gitlab.CreateMergeRequestNoteOptions{
Body: &body,
})
Expand All @@ -105,7 +123,7 @@ func CreateNote(args []string, rn string, idNum int, msgs []string, filename str
fmt.Println(noteURL)
}

func noteMsg(msgs []string) (string, error) {
func noteMsg(msgs []string, isMR bool) (string, error) {
if len(msgs) > 0 {
return strings.Join(msgs[0:], "\n\n"), nil
}
Expand All @@ -115,7 +133,7 @@ func noteMsg(msgs []string) (string, error) {
return "", err
}

if is_mr {
if isMR {
return git.EditFile("MR_NOTE", text)
}
return git.EditFile("ISSUE_NOTE", text)
Expand Down Expand Up @@ -150,10 +168,80 @@ func noteText() (string, error) {
return b.String(), nil
}

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

var (
discussions []*gitlab.Discussion
err error
NoteURL string
)

if isMR {
discussions, err = lab.MRListDiscussions(rn, idNum)
} else {
discussions, err = lab.IssueListDiscussions(rn, idNum)
}
if err != nil {
log.Fatal(err)
}
for _, discussion := range discussions {
for _, note := range discussion.Notes {

if note.System {
continue
}

if note.ID != reply {
continue
}

body := ""
if filename != "" {
content, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
body = string(content)
} else {
body, err = noteMsg([]string{}, isMR)
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
}
}

if body == "" {
log.Fatal("aborting note due to empty note msg")
}

if linebreak {
body = textToMarkdown(body)
}

if isMR {
opts := &gitlab.AddMergeRequestDiscussionNoteOptions{
Body: &body,
}
NoteURL, err = lab.AddMRDiscussionNote(rn, idNum, discussion.ID, opts)
} else {
opts := &gitlab.AddIssueDiscussionNoteOptions{
Body: &body,
}
NoteURL, err = lab.AddIssueDiscussionNote(rn, idNum, discussion.ID, opts)
}
if err != nil {
log.Fatal(err)
}
fmt.Println(NoteURL)
}
}
}

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")

issueCmd.AddCommand(issueNoteCmd)
carapace.Gen(issueNoteCmd).PositionalCompletion(
action.Remotes(),
Expand Down
2 changes: 1 addition & 1 deletion 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)
body, err := noteMsg(test.Msgs, false)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/issue_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ func PrintDiscussions(discussions []*gitlab.Discussion, since string, idstr stri
printit = color.New(color.Bold).PrintfFunc()
}
printit(`
%s%s %s at %s
%s#%d: %s %s at %s
%s%s
`,
indentHeader, note.Author.Username, commented, time.Time(*note.UpdatedAt).String(),
indentHeader, note.ID, note.Author.Username, commented, time.Time(*note.UpdatedAt).String(),
indentNote, note.Body)
}
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/mr_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

var mrNoteCmd = &cobra.Command{
Use: "note [remote] <id>",
Aliases: []string{"comment"},
Use: "note [remote] <id>[:<comment_id>]",
Aliases: []string{"comment", "reply"},
Short: "Add a note or comment to an MR on GitLab",
Long: ``,
Args: cobra.MinimumNArgs(1),
Expand All @@ -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")

mrCmd.AddCommand(mrNoteCmd)
carapace.Gen(mrNoteCmd).PositionalCompletion(
action.Remotes(),
Expand Down
2 changes: 1 addition & 1 deletion 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)
body, err := noteMsg(test.Msgs, true)
if err != nil {
t.Fatal(err)
}
Expand Down
28 changes: 28 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,3 +906,31 @@ func Labels(labels []string) *gitlab.Labels {
l := gitlab.Labels(labels)
return &l
}

// AddMRDiscussionNote adds a note to an existing MR discussion on GitLab
func AddMRDiscussionNote(project string, mrNum int, discussionID string, opts *gitlab.AddMergeRequestDiscussionNoteOptions) (string, error) {
p, err := FindProject(project)
if err != nil {
return "", err
}

note, _, err := lab.Discussions.AddMergeRequestDiscussionNote(p.ID, mrNum, discussionID, opts)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/merge_requests/%d#note_%d", p.WebURL, note.NoteableIID, note.ID), nil
}

// AddIssueDiscussionNote adds a note to an existing issue discussion on GitLab
func AddIssueDiscussionNote(project string, issueNum int, discussionID string, opts *gitlab.AddIssueDiscussionNoteOptions) (string, error) {
p, err := FindProject(project)
if err != nil {
return "", err
}

note, _, err := lab.Discussions.AddIssueDiscussionNote(p.ID, issueNum, discussionID, opts)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/issues/%d#note_%d", p.WebURL, note.NoteableIID, note.ID), nil
}

0 comments on commit 0e25db0

Please sign in to comment.