Skip to content

Commit

Permalink
Merge pull request #16 from cloudingcity/feat/mr-create-cmd
Browse files Browse the repository at this point in the history
Add mr create cmd
  • Loading branch information
cloudingcity authored Feb 16, 2020
2 parents 5e65c84 + efe95d5 commit b8763a3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/mr.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var mrCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(mrCmd)
mrCmd.AddCommand(mrCreateCmd)
mrCmd.AddCommand(mrListCmd)
mrCmd.AddCommand(mrOpenCmd)
mrCmd.AddCommand(mrSearchCmd)
Expand Down
13 changes: 13 additions & 0 deletions cmd/mr_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cmd

import (
"github.com/spf13/cobra"
)

var mrCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a merge request",
RunE: func(cmd *cobra.Command, args []string) error {
return projectManager(nil).MergeRequest.Create()
},
}
15 changes: 15 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git

import (
"bytes"
"log"
"os"
"os/exec"
Expand All @@ -12,6 +13,11 @@ var command = func(args ...string) *exec.Cmd {
return exec.Command("git", args...)
}

// Push update remote refs along with associated objects
func Push(ref string) error {
return command("push", "--set-upstream", "origin", ref).Run()
}

// Clone clone a repository form GitLab.
func Clone(repo, dir string) error {
args := []string{"clone", repo}
Expand All @@ -33,3 +39,12 @@ func CurrentRepo() string {
}
return utils.ParseGitProject(string(output))
}

// CurrentBranch returns current branch.
func CurrentBranch() string {
output, err := command("rev-parse", "--abbrev-ref", "HEAD").CombinedOutput()
if err != nil {
log.Fatal(string(output))
}
return string(bytes.Trim(output, "\n"))
}
5 changes: 5 additions & 0 deletions internal/gitlab/contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type GitlabValidate interface {
Lint(content string, options ...gitlab.OptionFunc) (*gitlab.LintResult, *gitlab.Response, error)
}

// GitlabProject is go-gitlab project service interface.
type GitlabProject interface {
GetProject(pid interface{}, opt *gitlab.GetProjectOptions, options ...gitlab.OptionFunc) (*gitlab.Project, *gitlab.Response, error)
}

// GitlabGroup is go-gitlab group service interface.
type GitlabGroup interface {
ListGroupProjects(gid interface{}, opt *gitlab.ListGroupProjectsOptions, options ...gitlab.OptionFunc) ([]*gitlab.Project, *gitlab.Response, error)
Expand Down
11 changes: 6 additions & 5 deletions internal/gitlab/project/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ type Manager struct {
func NewManager(c *gitlab.Client, project string, w io.Writer) *Manager {
m := &Manager{}
m.MergeRequest = &mergeRequestsService{
project: project,
gitlabMR: c.MergeRequests,
out: w,
baseURL: c.BaseURL(),
openURL: browser.OpenURL,
project: project,
gitlabMR: c.MergeRequests,
gitlabProject: c.Projects,
out: w,
baseURL: c.BaseURL(),
openURL: browser.OpenURL,
}
m.Search = &searchService{
project: project,
Expand Down
39 changes: 34 additions & 5 deletions internal/gitlab/project/merge_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import (
"path"
"strconv"

"github.com/cloudingcity/golab/internal/git"
"github.com/cloudingcity/golab/internal/gitlab/contract"
"github.com/cloudingcity/golab/internal/gitlab/render"
"github.com/xanzy/go-gitlab"
)

type mergeRequestsService struct {
project string
gitlabMR contract.GitlabMergeRequests
out io.Writer
baseURL *url.URL
openURL func(url string) error
project string
gitlabMR contract.GitlabMergeRequests
gitlabProject contract.GitlabProject
out io.Writer
baseURL *url.URL
openURL func(url string) error
}

// List lists merge requests on a project.
Expand Down Expand Up @@ -50,3 +52,30 @@ func (s *mergeRequestsService) Show(mrID int) error {
render.New(s.out).MR(mr)
return nil
}

// Create create a merge request.
func (s *mergeRequestsService) Create() error {
project, _, err := s.gitlabProject.GetProject(s.project, &gitlab.GetProjectOptions{})
if err != nil {
return err
}

defaultBranch := project.DefaultBranch
currentBranch := git.CurrentBranch()
if defaultBranch == currentBranch {
return fmt.Errorf("must be on a branch named differently than %q", defaultBranch)
}

if err = git.Push(currentBranch); err != nil {
return err
}

u := *s.baseURL
u.Path = path.Join(s.project, "merge_requests", "new")
q := make(url.Values)
q.Set("merge_request[source_branch]", currentBranch)
u.RawQuery = q.Encode()

fmt.Fprintf(s.out, "Opening %s in your browser\n", u.String())
return s.openURL(u.String())
}

0 comments on commit b8763a3

Please sign in to comment.