diff --git a/cmd/issueClose.go b/cmd/issueClose.go new file mode 100644 index 00000000..8a282668 --- /dev/null +++ b/cmd/issueClose.go @@ -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) +} diff --git a/cmd/mrCheckout.go b/cmd/mrCheckout.go index 698c2bf3..64f379d5 100644 --- a/cmd/mrCheckout.go +++ b/cmd/mrCheckout.go @@ -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 { diff --git a/cmd/mrClose.go b/cmd/mrClose.go new file mode 100644 index 00000000..d2713d5b --- /dev/null +++ b/cmd/mrClose.go @@ -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) +} diff --git a/cmd/mrCreate.go b/cmd/mrCreate.go index bf66eb0b..0ce59e57 100644 --- a/cmd/mrCreate.go +++ b/cmd/mrCreate.go @@ -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, diff --git a/cmd/mrList.go b/cmd/mrList.go index d2d327fc..c9cb6c9d 100644 --- a/cmd/mrList.go +++ b/cmd/mrList.go @@ -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, diff --git a/cmd/mrMerge.go b/cmd/mrMerge.go new file mode 100644 index 00000000..941050b8 --- /dev/null +++ b/cmd/mrMerge.go @@ -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) +} diff --git a/cmd/mrShow.go b/cmd/mrShow.go index 5c5eb3c9..deba8cfc 100644 --- a/cmd/mrShow.go +++ b/cmd/mrShow.go @@ -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) } diff --git a/internal/gitlab/gitlab.go b/internal/gitlab/gitlab.go index 0ab8e4fe..5a6add2b 100644 --- a/internal/gitlab/gitlab.go +++ b/internal/gitlab/gitlab.go @@ -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) } @@ -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 @@ -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 @@ -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") != "" { @@ -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 {