Skip to content

Commit

Permalink
note: Add mr note --file option
Browse files Browse the repository at this point in the history
Add the ability to update a merge request with the contents of a file.
This is especially useful when copying in testing contents and/or
boot logs.

Add mr note --file option which takes the location of the file containing
the text as an argument.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Aug 20, 2020
1 parent 10a5e27 commit d2c3dfe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cmd/mr_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"text/template"
"io/ioutil"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
Expand All @@ -34,11 +35,26 @@ var mrCreateNoteCmd = &cobra.Command{
log.Fatal(err)
}

body, err := mrNoteMsg(msgs)
filename, err := cmd.Flags().GetString("file")
if err != nil {
_, f, l, _ := runtime.Caller(0)
log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
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")
}
Expand All @@ -53,7 +69,6 @@ var mrCreateNoteCmd = &cobra.Command{
},
}

//
func mrNoteMsg(msgs []string) (string, error) {
if len(msgs) > 0 {
return strings.Join(msgs[0:], "\n\n"), nil
Expand Down Expand Up @@ -97,6 +112,7 @@ func mrNoteText() (string, error) {

func init() {
mrCreateNoteCmd.Flags().StringSliceP("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")

mrCmd.AddCommand(mrCreateNoteCmd)
carapace.Gen(mrCreateNoteCmd).PositionalCompletion(
Expand Down
23 changes: 23 additions & 0 deletions cmd/mr_note_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"os/exec"
"testing"
"io/ioutil"
"path/filepath"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -23,6 +25,27 @@ func Test_mrCreateNote(t *testing.T) {
require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/merge_requests/1#note_")
}

func Test_mrCreateNote_file(t *testing.T) {
repo := copyTestRepo(t)

err := ioutil.WriteFile(filepath.Join(repo, "hellolab.txt"), []byte("hello\nlab\n"), 0644)
if err != nil {
t.Fatal(err)
}

cmd := exec.Command(labBinaryPath, "mr", "note", "lab-testing", "1",
"-F", "hellolab.txt")
cmd.Dir = repo

b, err := cmd.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}

require.Contains(t, string(b), "https://gitlab.com/lab-testing/test/merge_requests/1#note_")
}

func Test_mrNoteMsg(t *testing.T) {
tests := []struct {
Name string
Expand Down

0 comments on commit d2c3dfe

Please sign in to comment.