Skip to content

Commit

Permalink
(issue,mr) add close and merge sub commands
Browse files Browse the repository at this point in the history
  • Loading branch information
zaquestion committed Dec 20, 2017
1 parent 0862f42 commit 27fc07a
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 11 deletions.
44 changes: 44 additions & 0 deletions cmd/issueClose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"log"

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

var issueCloseCmd = &cobra.Command{
Use: "close [remote]",
Aliases: []string{"delete"},
Short: "Close issue by id",
Long: ``,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
remote, id, err := parseArgsRemote(args)
if err != nil {
log.Fatal(err)
}
if remote == "" {
remote = forkedFromRemote
}
rn, err := git.PathWithNameSpace(remote)
if err != nil {
log.Fatal(err)
}

p, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}

err = lab.IssueClose(p.ID, int(id))
if err != nil {
log.Fatal(err)
}
},
}

func init() {
issueCmd.AddCommand(issueCloseCmd)
}
2 changes: 1 addition & 1 deletion cmd/mrCheckout.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var checkoutCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
mrs, err := lab.ListMRs(rn, &gitlab.ListProjectMergeRequestsOptions{
mrs, err := lab.MRList(rn, &gitlab.ListProjectMergeRequestsOptions{
IIDs: []int{mrID},
})
if err != nil {
Expand Down
44 changes: 44 additions & 0 deletions cmd/mrClose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"log"

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

var mrCloseCmd = &cobra.Command{
Use: "close [remote]",
Aliases: []string{"delete"},
Short: "Close mr by id",
Long: ``,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
remote, id, err := parseArgsRemote(args)
if err != nil {
log.Fatal(err)
}
if remote == "" {
remote = forkedFromRemote
}
rn, err := git.PathWithNameSpace(remote)
if err != nil {
log.Fatal(err)
}

p, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}

err = lab.MRClose(p.ID, int(id))
if err != nil {
log.Fatal(err)
}
},
}

func init() {
mrCmd.AddCommand(mrCloseCmd)
}
2 changes: 1 addition & 1 deletion cmd/mrCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func runMRCreate(cmd *cobra.Command, args []string) {
log.Fatal("aborting MR due to empty MR msg")
}

mrURL, err := lab.MergeRequest(sourceProjectName, &gitlab.CreateMergeRequestOptions{
mrURL, err := lab.MRCreate(sourceProjectName, &gitlab.CreateMergeRequestOptions{
SourceBranch: &branch,
TargetBranch: gitlab.String(targetBranch),
TargetProjectID: &targetProject.ID,
Expand Down
2 changes: 1 addition & 1 deletion cmd/mrList.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var listCmd = &cobra.Command{
log.Fatal(err)
}

mrs, err := lab.ListMRs(rn, &gitlab.ListProjectMergeRequestsOptions{
mrs, err := lab.MRList(rn, &gitlab.ListProjectMergeRequestsOptions{
ListOptions: gitlab.ListOptions{
Page: int(page),
PerPage: 10,
Expand Down
44 changes: 44 additions & 0 deletions cmd/mrMerge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"log"

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

var mrMergeCmd = &cobra.Command{
Use: "merge [remote]",
Aliases: []string{"delete"},
Short: "Merge mr by id",
Long: ``,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
remote, id, err := parseArgsRemote(args)
if err != nil {
log.Fatal(err)
}
if remote == "" {
remote = forkedFromRemote
}
rn, err := git.PathWithNameSpace(remote)
if err != nil {
log.Fatal(err)
}

p, err := lab.FindProject(rn)
if err != nil {
log.Fatal(err)
}

err = lab.MRMerge(p.ID, int(id))
if err != nil {
log.Fatal(err)
}
},
}

func init() {
mrCmd.AddCommand(mrMergeCmd)
}
2 changes: 1 addition & 1 deletion cmd/mrShow.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var mrShowCmd = &cobra.Command{
log.Fatal(err)
}

mr, err := lab.GetMR(rn, int(mrNum))
mr, err := lab.MRGet(rn, int(mrNum))
if err != nil {
log.Fatal(err)
}
Expand Down
45 changes: 38 additions & 7 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ func Fork(project string) (string, error) {
return fork.SSHURLToRepo, nil
}

// MergeRequest opens a merge request on GitLab
func MergeRequest(project string, opts *gitlab.CreateMergeRequestOptions) (string, error) {
// MRCreate opens a merge request on GitLab
func MRCreate(project string, opts *gitlab.CreateMergeRequestOptions) (string, error) {
if os.Getenv("DEBUG") != "" {
spew.Dump(opts)
}
Expand All @@ -182,8 +182,8 @@ func MergeRequest(project string, opts *gitlab.CreateMergeRequestOptions) (strin
return mr.WebURL, nil
}

// GetMR retrieves the merge request from GitLab project
func GetMR(project string, mrNum int) (*gitlab.MergeRequest, error) {
// MRGet retrieves the merge request from GitLab project
func MRGet(project string, mrNum int) (*gitlab.MergeRequest, error) {
p, err := FindProject(project)
if err != nil {
return nil, err
Expand All @@ -197,8 +197,8 @@ func GetMR(project string, mrNum int) (*gitlab.MergeRequest, error) {
return mr, nil
}

// ListMRs lists the MRs on a GitLab project
func ListMRs(project string, opts *gitlab.ListProjectMergeRequestsOptions) ([]*gitlab.MergeRequest, error) {
// MRList lists the MRs on a GitLab project
func MRList(project string, opts *gitlab.ListProjectMergeRequestsOptions) ([]*gitlab.MergeRequest, error) {
p, err := FindProject(project)
if err != nil {
return nil, err
Expand All @@ -211,6 +211,28 @@ func ListMRs(project string, opts *gitlab.ListProjectMergeRequestsOptions) ([]*g
return list, nil
}

// MRClose closes an mr on a GitLab project
func MRClose(pid interface{}, id int) error {
_, _, err := lab.MergeRequests.UpdateMergeRequest(pid, int(id), &gitlab.UpdateMergeRequestOptions{
StateEvent: gitlab.String("close"),
})
if err != nil {
return err
}
return nil
}

// MRMerge merges an mr on a GitLab project
func MRMerge(pid interface{}, id int) error {
_, _, err := lab.MergeRequests.AcceptMergeRequest(pid, int(id), &gitlab.AcceptMergeRequestOptions{
MergeWhenPipelineSucceeds: gitlab.Bool(true),
})
if err != nil {
return err
}
return nil
}

// IssueCreate opens a new issue on a GitLab Project
func IssueCreate(project string, opts *gitlab.CreateIssueOptions) (string, error) {
if os.Getenv("DEBUG") != "" {
Expand Down Expand Up @@ -262,7 +284,16 @@ func IssueList(project string, opts *gitlab.ListProjectIssuesOptions) ([]*gitlab
return list, nil
}

// BranchPushed checks if a branch exists on a GitLab Project
// IssueClose closes an issue on a GitLab project
func IssueClose(pid interface{}, id int) error {
_, err := lab.Issues.DeleteIssue(pid, int(id))
if err != nil {
return err
}
return nil
}

// BranchPushed checks if a branch exists on a GitLab project
func BranchPushed(project, branch string) bool {
p, err := FindProject(project)
if err != nil {
Expand Down

0 comments on commit 27fc07a

Please sign in to comment.