Skip to content

Commit

Permalink
(mr, show) add mr show command
Browse files Browse the repository at this point in the history
  • Loading branch information
nkprince007 committed Dec 17, 2017
1 parent 5936736 commit be371ed
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
17 changes: 14 additions & 3 deletions cmd/mr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ import (
// mrCmd represents the mr command
var mrCmd = &cobra.Command{
Use: "mr",
Short: "Work with merge requests",
Long: ``,
Short: mrShowCmd.Short,
Long: mrShowCmd.Long,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
if list, _ := cmd.Flags().GetBool("list"); list {
listCmd.Run(cmd, args)
return
}

if len(args) == 0 || len(args) > 2 {
cmd.Help()
return
}

mrShowCmd.Run(cmd, args)
},
}

func init() {
mrCmd.Flags().BoolP("list", "l", false, "list MRs")
RootCmd.AddCommand(mrCmd)
}
83 changes: 83 additions & 0 deletions cmd/mrShow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cmd

import (
"fmt"
"log"
"strconv"
"strings"

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

var mrShowCmd = &cobra.Command{
Use: "show [remote]",
Aliases: []string{"get", "s"},
Short: "describe a merge request",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
remote, mrNum, err := parseArgsRemote(args)
if err != nil {
log.Fatal(err)
}
if remote == "" {
remote = forkedFromRemote
}
rn, err := git.PathWithNameSpace(remote)
if err != nil {
log.Fatal(err)
}

mr, err := lab.GetMR(rn, int(mrNum))
if err != nil {
log.Fatal(err)
}

printMR(mr, rn)
},
}

func printMR(mr *gitlab.MergeRequest, project string) {
assignee := "None"
milestone := "None"
labels := "None"
state := map[string]string{
"opened": "Open",
"closed": "Closed",
"merged": "Merged",
}[mr.State]
if mr.Assignee.Username != "" {
assignee = mr.Assignee.Username
}
if mr.Milestone != nil {
milestone = mr.Milestone.Title
}
if len(mr.Labels) > 0 {
labels = strings.Join(mr.Labels, ", ")
}

fmt.Printf(`
#%d %s
===================================
%s
-----------------------------------
Project: %s
Branches: %s->%s
Status: %s
Work in Progress: %s
Assignee: %s
Author: %s
Milestone: %s
Labels: %s
WebURL: %s
`,
mr.IID, mr.Title, mr.Description, project, mr.SourceBranch,
mr.TargetBranch, state, strconv.FormatBool(mr.WorkInProgress), assignee,
mr.Author.Username, milestone, labels, mr.WebURL)
}

func init() {
mrCmd.AddCommand(mrShowCmd)
}
36 changes: 36 additions & 0 deletions cmd/mrShow_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

func Test_mrShow(t *testing.T) {
repo := copyTestRepo(t)
cmd := exec.Command("../lab_bin", "mr", "4")
cmd.Dir = repo

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

require.Contains(t, string(b), `
#4 merged merge request
===================================
-----------------------------------
Project: zaquestion/test
Branches: merged->master
Status: Merged
Work in Progress: false
Assignee: None
Author: zaquestion
Milestone: None
Labels: None
WebURL: https://gitlab.com/zaquestion/test/merge_requests/4
`)
}

0 comments on commit be371ed

Please sign in to comment.