Skip to content

Commit

Permalink
note: Unify mr and issue note codebases
Browse files Browse the repository at this point in the history
The mr and issue note codebases are very similar and likely will require
the same edits moving forward.

Uniffy the mr and issue note codebases for simplicity.  Testing is still
handled by the cmd/mr_note_test.go and cmd/issue_note_test.go files.

Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Sep 25, 2020
1 parent 97ffd5e commit aa00f8b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 138 deletions.
105 changes: 77 additions & 28 deletions cmd/issue_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"strconv"
"strings"
Expand All @@ -17,49 +19,92 @@ import (
lab "github.com/zaquestion/lab/internal/gitlab"
)

var issueCreateNoteCmd = &cobra.Command{
var issueNoteCmd = &cobra.Command{
Use: "note [remote] <id>",
Aliases: []string{"comment"},
Short: "Add a note or comment to an issue on GitLab",
Long: ``,
Args: cobra.MinimumNArgs(1),
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, issueNum, err := parseArgs(args)
if err != nil {
log.Fatal(err)
}
Run: NoteRunFn,
}

func NoteRunFn(cmd *cobra.Command, args []string) {
rn, idNum, err := parseArgs(args)
if err != nil {
log.Fatal(err)
}

msgs, err := cmd.Flags().GetStringArray("message")
if err != nil {
log.Fatal(err)
}

filename, err := cmd.Flags().GetString("file")
if err != nil {
log.Fatal(err)
}

linebreak, err := cmd.Flags().GetBool("force-linebreak")
if err != nil {
log.Fatal(err)
}

msgs, err := cmd.Flags().GetStringArray("message")
CreateNote(os.Args, rn, int(idNum), msgs, filename, linebreak)
}

var is_mr bool = false

func CreateNote(args []string, rn string, 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)
if err != nil {
log.Fatal(err)
}

body, err := noteMsg(msgs)
body = string(content)
} else {
body, err = noteMsg(msgs)
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")
}
}

linebreak, _ := cmd.Flags().GetBool("force-linebreak")
if linebreak {
body = textToMarkdown(body)
}
if body == "" {
log.Fatal("aborting note due to empty note msg")
}

noteURL, err := lab.IssueCreateNote(rn, int(issueNum), &gitlab.CreateIssueNoteOptions{
if linebreak {
body = textToMarkdown(body)
}

var (
noteURL string
)

if is_mr {
noteURL, err = lab.MRCreateNote(rn, idNum, &gitlab.CreateMergeRequestNoteOptions{
Body: &body,
})
if err != nil {
log.Fatal(err)
}
fmt.Println(noteURL)
},
} else {
noteURL, err = lab.IssueCreateNote(rn, idNum, &gitlab.CreateIssueNoteOptions{
Body: &body,
})
}
if err != nil {
log.Fatal(err)
}
fmt.Println(noteURL)
}

//
func noteMsg(msgs []string) (string, error) {
if len(msgs) > 0 {
return strings.Join(msgs[0:], "\n\n"), nil
Expand All @@ -69,6 +114,10 @@ func noteMsg(msgs []string) (string, error) {
if err != nil {
return "", err
}

if is_mr {
return git.EditFile("MR_NOTE", text)
}
return git.EditFile("ISSUE_NOTE", text)
}

Expand Down Expand Up @@ -102,11 +151,11 @@ func noteText() (string, error) {
}

func init() {
issueCreateNoteCmd.Flags().StringArrayP("message", "m", []string{}, "Use the given <msg>; multiple -m are concatenated as separate paragraphs")
issueCreateNoteCmd.Flags().Bool("force-linebreak", false, "append 2 spaces to the end of each line to force markdown linebreaks")

issueCmd.AddCommand(issueCreateNoteCmd)
carapace.Gen(issueCreateCmd).PositionalCompletion(
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(),
action.Issues(issueList),
)
Expand Down
115 changes: 7 additions & 108 deletions cmd/mr_note.go
Original file line number Diff line number Diff line change
@@ -1,128 +1,27 @@
package cmd

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

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)

var mrCreateNoteCmd = &cobra.Command{
var mrNoteCmd = &cobra.Command{
Use: "note [remote] <id>",
Aliases: []string{"comment"},
Short: "Add a note or comment to an MR on GitLab",
Long: ``,
Args: cobra.MinimumNArgs(1),
PersistentPreRun: LabPersistentPreRun,
Run: func(cmd *cobra.Command, args []string) {
rn, mrNum, err := parseArgs(args)
if err != nil {
log.Fatal(err)
}

msgs, err := cmd.Flags().GetStringArray("message")
if err != nil {
log.Fatal(err)
}

filename, err := cmd.Flags().GetString("file")
if err != nil {
log.Fatal(err)
}

body := ""
if filename != "" {
content, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
body = string(content)
} else {
body, err = mrNoteMsg(msgs)
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")
}

linebreak, _ := cmd.Flags().GetBool("force-linebreak")
if linebreak {
body = textToMarkdown(body)
}

noteURL, err := lab.MRCreateNote(rn, int(mrNum), &gitlab.CreateMergeRequestNoteOptions{
Body: &body,
})
if err != nil {
log.Fatal(err)
}
fmt.Println(noteURL)
},
}

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

text, err := mrNoteText()
if err != nil {
return "", err
}
return git.EditFile("MR_NOTE", text)
}

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

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

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

msg := &struct {
InitMsg string
CommentChar string
}{
InitMsg: initMsg,
CommentChar: commentChar,
}

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

return b.String(), nil
Run: NoteRunFn,
}

func init() {
mrCreateNoteCmd.Flags().StringArrayP("message", "m", []string{}, "Use the given <msg>; multiple -m are concatenated as separate paragraphs")
mrCreateNoteCmd.Flags().StringP("file", "F", "", "Use the given file as the message")
mrCreateNoteCmd.Flags().Bool("force-linebreak", false, "append 2 spaces to the end of each line to force markdown linebreaks")

mrCmd.AddCommand(mrCreateNoteCmd)
carapace.Gen(mrCreateNoteCmd).PositionalCompletion(
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(),
action.MergeRequests(mrList),
)
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 := mrNoteMsg(test.Msgs)
body, err := noteMsg(test.Msgs)
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 := mrNoteText()
text, err := noteText()
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit aa00f8b

Please sign in to comment.