Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open the current merge request with lab mr b #221

Merged
merged 9 commits into from
Oct 18, 2018
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions cmd/mr_browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (

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

var mrBrowseCmd = &cobra.Command{
Expand All @@ -32,6 +35,27 @@ var mrBrowseCmd = &cobra.Command{
hostURL.Path = path.Join(hostURL.Path, rn, "merge_requests")
if num > 0 {
hostURL.Path = path.Join(hostURL.Path, strconv.FormatInt(num, 10))
} else {
currentBranch, err := git.CurrentBranch()
if err != nil {
log.Fatal(err)
}
mrs, err := lab.MRList(rn, gitlab.ListProjectMergeRequestsOptions{
ListOptions: gitlab.ListOptions{
PerPage: 10,
},
Labels: mrLabels,
State: &mrState,
OrderBy: gitlab.String("updated_at"),
SourceBranch: gitlab.String(currentBranch),
}, -1)
if err != nil {
log.Fatal(err)
}
if len(mrs) > 0 {
num = int64(mrs[0].IID)
hostURL.Path = path.Join(hostURL.Path, strconv.FormatInt(num, 10))
}
}

err = browse(hostURL.String())
Expand Down
25 changes: 24 additions & 1 deletion cmd/mr_browse_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package cmd

import (
"os/exec"
"testing"

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

func Test_mrBrowse(t *testing.T) {
func Test_mrBrowseWithParameter(t *testing.T) {
oldBrowse := browse
defer func() { browse = oldBrowse }()

Expand All @@ -17,3 +18,25 @@ func Test_mrBrowse(t *testing.T) {

mrBrowseCmd.Run(nil, []string{"1"})
}

func Test_mrBrowseCurrent(t *testing.T) {
t.Parallel()
repo := copyTestRepo(t)
git := exec.Command("git", "checkout", "mrtest")
git.Dir = repo
b, err := git.CombinedOutput()
if err != nil {
t.Log(string(b))
t.Fatal(err)
}

oldBrowse := browse
defer func() { browse = oldBrowse }()

browse = func(url string) error {
require.Equal(t, "https://gitlab.com/zaquestion/test/merge_requests/1", url)
return nil
}

mrBrowseCmd.Run(nil, nil)
}