Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
refactor: persist --repo flag across mr and issue subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
profclems committed Aug 12, 2020
1 parent 77a15fe commit f643dbf
Show file tree
Hide file tree
Showing 31 changed files with 113 additions and 29 deletions.
3 changes: 2 additions & 1 deletion commands/issue.go
Expand Up @@ -52,7 +52,7 @@ func displayIssue(hm *gitlab.Issue) {

// mrCmd is merge request command
var issueCmd = &cobra.Command{
Use: "issue <command> [flags]",
Use: "issue [command] [flags]",
Short: `Create, view and manage remote issues`,
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -64,5 +64,6 @@ var issueCmd = &cobra.Command{
}

func init() {
issueCmd.PersistentFlags().StringP("repo", "R","", "Select another repository using the OWNER/REPO format or the project ID. Supports group namespaces")
RootCmd.AddCommand(issueCmd)
}
6 changes: 5 additions & 1 deletion commands/issue_close.go
Expand Up @@ -11,10 +11,11 @@ import (
)

var issueCloseCmd = &cobra.Command{
Use: "close",
Use: "close <id>",
Short: `Close an issue`,
Long: ``,
Aliases: []string{"unsub"},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
cmdErr(cmd, args)
Expand All @@ -23,6 +24,9 @@ var issueCloseCmd = &cobra.Command{
if len(args) > 0 {
issueID := strings.TrimSpace(args[0])
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
l := &gitlab.UpdateIssueOptions{}
l.StateEvent = gitlab.String("close")
arrIds := strings.Split(strings.Trim(issueID, "[] "), ",")
Expand Down
4 changes: 4 additions & 0 deletions commands/issue_create.go
Expand Up @@ -15,6 +15,7 @@ var issueCreateCmd = &cobra.Command{
Short: `Create an issue`,
Long: ``,
Aliases: []string{"new"},
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
cmdErr(cmd, args)
Expand Down Expand Up @@ -69,6 +70,9 @@ var issueCreateCmd = &cobra.Command{
l.AssigneeIDs = t2
}
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
issue, _, err := gitlabClient.Issues.CreateIssue(repo, l)
if err != nil {
log.Fatal(err)
Expand Down
7 changes: 5 additions & 2 deletions commands/issue_delete.go
Expand Up @@ -9,10 +9,11 @@ import (
)

var issueDeleteCmd = &cobra.Command{
Use: "delete",
Use: "delete <id>",
Short: `Delete an issue`,
Long: ``,
Aliases: []string{"del"},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
cmdErr(cmd, args)
Expand All @@ -21,7 +22,9 @@ var issueDeleteCmd = &cobra.Command{
if len(args) > 0 {
issueID := strings.TrimSpace(args[0])
gitlabClient, repo := git.InitGitlabClient()

if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
arrIds := strings.Split(strings.Trim(issueID, "[] "), ",")
for _, i2 := range arrIds {
fmt.Println("Deleting Issue #" + i2)
Expand Down
6 changes: 4 additions & 2 deletions commands/issue_list.go
Expand Up @@ -12,7 +12,7 @@ var issueListCmd = &cobra.Command{
Short: `List project issues`,
Long: ``,
Aliases: []string{"ls"},
Args: cobra.MaximumNArgs(3),
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var state string
if lb, _ := cmd.Flags().GetBool("all"); lb {
Expand Down Expand Up @@ -40,7 +40,9 @@ var issueListCmd = &cobra.Command{
}

gitlabClient, repo := git.InitGitlabClient()

if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
issues, _, err := gitlabClient.Issues.ListProjectIssues(repo, l)
if err != nil {
log.Fatal(err)
Expand Down
6 changes: 5 additions & 1 deletion commands/issue_reopen.go
Expand Up @@ -11,10 +11,11 @@ import (
)

var issueReopenCmd = &cobra.Command{
Use: "reopen",
Use: "reopen <id>",
Short: `Reopen a closed issue`,
Long: ``,
Aliases: []string{"open"},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
cmdErr(cmd, args)
Expand All @@ -23,6 +24,9 @@ var issueReopenCmd = &cobra.Command{
if len(args) > 0 {
issueID := strings.TrimSpace(args[0])
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
l := &gitlab.UpdateIssueOptions{}
l.StateEvent = gitlab.String("reopen")
arrIds := strings.Split(strings.Trim(issueID, "[] "), ",")
Expand Down
6 changes: 5 additions & 1 deletion commands/issue_subscribe.go
Expand Up @@ -9,10 +9,11 @@ import (
)

var issueSubscribeCmd = &cobra.Command{
Use: "subscribe",
Use: "subscribe <id>",
Short: `Subscribe to an issue`,
Long: ``,
Aliases: []string{"sub"},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
cmdErr(cmd, args)
Expand All @@ -21,6 +22,9 @@ var issueSubscribeCmd = &cobra.Command{
if len(args) > 0 {
mergeID := strings.TrimSpace(args[0])
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
arrIds := strings.Split(strings.Trim(mergeID, "[] "), ",")
for _, i2 := range arrIds {
fmt.Println("Subscribing to Issue #" + i2)
Expand Down
6 changes: 5 additions & 1 deletion commands/issue_unsubscribe.go
Expand Up @@ -9,10 +9,11 @@ import (
)

var issueUnsubscribeCmd = &cobra.Command{
Use: "unsubscribe",
Use: "unsubscribe <id>",
Short: `Unsubscribe to an issue`,
Long: ``,
Aliases: []string{"unsub"},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
cmdErr(cmd, args)
Expand All @@ -21,6 +22,9 @@ var issueUnsubscribeCmd = &cobra.Command{
if len(args) > 0 {
mergeID := strings.TrimSpace(args[0])
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
arrIds := strings.Split(strings.Trim(mergeID, "[] "), ",")
for _, i2 := range arrIds {
fmt.Println("Unsubscribing to Issue #" + i2)
Expand Down
4 changes: 2 additions & 2 deletions commands/issue_view.go
Expand Up @@ -17,11 +17,11 @@ import (
)

var issueViewCmd = &cobra.Command{
Use: "view <id> [flags]",
Use: "view <id>",
Short: `Display the title, body, and other information about an issue.`,
Long: ``,
Aliases: []string{"show"},
Args: cobra.MaximumNArgs(3),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 || len(args) >1 {
cmdErr(cmd, args)
Expand Down
1 change: 1 addition & 0 deletions commands/label.go
Expand Up @@ -16,5 +16,6 @@ var labelCmd = &cobra.Command{
}

func init() {
labelCmd.PersistentFlags().StringP("repo", "R","", "Select another repository using the OWNER/REPO format. Supports group namespaces")
RootCmd.AddCommand(labelCmd)
}
7 changes: 5 additions & 2 deletions commands/label_create.go
Expand Up @@ -8,17 +8,20 @@ import (
)

var labelCreateCmd = &cobra.Command{
Use: "create <id> [flags]",
Use: "create [flags]",
Short: `Create labels for repository/project`,
Long: ``,
Aliases: []string{"new"},
Args: cobra.MaximumNArgs(1),
Args: cobra.ExactArgs(0),
Run: createLabel,
}

func createLabel(cmd *cobra.Command, args []string) {

gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
l := &gitlab.CreateLabelOptions{}

if s, _ := cmd.Flags().GetString("name"); s != "" {
Expand Down
7 changes: 5 additions & 2 deletions commands/label_list.go
Expand Up @@ -9,16 +9,19 @@ import (
)

var labelListCmd = &cobra.Command{
Use: "list <id> [flags]",
Use: "list [flags]",
Short: `List labels in repository`,
Long: ``,
Aliases: []string{"ls"},
Args: cobra.MaximumNArgs(1),
Args: cobra.ExactArgs(0),
Run: listLabels,
}

func listLabels(cmd *cobra.Command, args []string) {
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
// List all labels
labels, _, err := gitlabClient.Labels.ListLabels(repo, nil)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions commands/mr.go
Expand Up @@ -55,5 +55,6 @@ var mrCmd = &cobra.Command{
}

func init() {
mrCmd.PersistentFlags().StringP("repo", "R","", "Select another repository using the OWNER/REPO format or the project ID. Supports group namespaces")
RootCmd.AddCommand(mrCmd)
}
5 changes: 4 additions & 1 deletion commands/mr_approve.go
Expand Up @@ -16,7 +16,7 @@ var mrApproveCmd = &cobra.Command{
Short: `Approve merge requests`,
Long: ``,
Aliases: []string{"ls"},
Args: cobra.MaximumNArgs(1),
Args: cobra.ExactArgs(1),
Run: approveMergeRequest,
}

Expand All @@ -33,6 +33,9 @@ func approveMergeRequest(cmd *cobra.Command, args []string) {

fmt.Println(aurora.Yellow("Approving Merge Request #" + mergeID + "..."))
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
_, resp, _ := gitlabClient.MergeRequestApprovals.ApproveMergeRequest(repo, manip.StringToInt(mergeID), l)
if resp != nil {
if resp.StatusCode == 201 {
Expand Down
6 changes: 4 additions & 2 deletions commands/mr_close.go
Expand Up @@ -14,15 +14,17 @@ var mrCloseCmd = &cobra.Command{
Use: "close <id>",
Short: `Close merge requests`,
Long: ``,
Aliases: []string{"ls"},
Args: cobra.MaximumNArgs(1),
Args: cobra.ExactArgs(1),
Run: closeMergeRequestState,
}

func closeMergeRequestState(cmd *cobra.Command, args []string) {
if len(args) > 0 {
mergeID := strings.Trim(args[0], " ")
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
l := &gitlab.UpdateMergeRequestOptions{}
l.StateEvent = gitlab.String("close")
arrIds := strings.Split(strings.Trim(mergeID, "[] "), ",")
Expand Down
4 changes: 4 additions & 0 deletions commands/mr_create.go
Expand Up @@ -15,6 +15,7 @@ var mrCreateCmd = &cobra.Command{
Short: `Create new merge request`,
Long: ``,
Aliases: []string{"new"},
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 {
cmdErr(cmd, args)
Expand Down Expand Up @@ -84,6 +85,9 @@ var mrCreateCmd = &cobra.Command{
}

gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
if c, _ := cmd.Flags().GetBool("create-source-branch"); c {
lb := &gitlab.CreateBranchOptions{
Branch: gitlab.String(sourceBranch),
Expand Down
6 changes: 4 additions & 2 deletions commands/mr_delete.go
Expand Up @@ -15,7 +15,7 @@ var mrDeleteCmd = &cobra.Command{
Short: `Delete merge requests`,
Long: ``,
Aliases: []string{"del"},
Args: cobra.MaximumNArgs(1),
Args: cobra.ExactArgs(1),
Example: "$ glab delete 123",
RunE: deleteMergeRequest,
}
Expand All @@ -25,7 +25,9 @@ func deleteMergeRequest(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
mergeID := strings.Trim(args[0], " ")
gitlabClient, repo := git.InitGitlabClient()

if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
arrIds := strings.Split(strings.Trim(mergeID, "[] "), ",")
for _, i2 := range arrIds {
fmt.Println("Deleting Merge Request #" + i2)
Expand Down
5 changes: 4 additions & 1 deletion commands/mr_issues.go
Expand Up @@ -14,7 +14,7 @@ var mrIssuesCmd = &cobra.Command{
Short: `Get issues related to a particular merge request.`,
Long: ``,
Aliases: []string{"issue"},
Args: cobra.MaximumNArgs(1),
Args: cobra.ExactArgs(1),
Example: "$ glab mr issues 46",
Run: issuesRelatedMergeRequest,
}
Expand All @@ -24,6 +24,9 @@ func issuesRelatedMergeRequest(cmd *cobra.Command, args []string) {
mergeID := strings.Trim(args[0], " ")
l := &gitlab.GetIssuesClosedOnMergeOptions{}
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
mr, _, err := gitlabClient.MergeRequests.GetIssuesClosedOnMerge(repo, manip.StringToInt(mergeID), l)
if err != nil {
er(err)
Expand Down
5 changes: 4 additions & 1 deletion commands/mr_list.go
Expand Up @@ -11,7 +11,7 @@ var mrListCmd = &cobra.Command{
Short: `List merge requests`,
Long: ``,
Aliases: []string{"ls"},
Args: cobra.MaximumNArgs(3),
Args: cobra.ExactArgs(0),
RunE: listMergeRequest,
}

Expand Down Expand Up @@ -41,6 +41,9 @@ func listMergeRequest(cmd *cobra.Command, args []string) error {
}

gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
mergeRequests, _, err := gitlabClient.MergeRequests.ListProjectMergeRequests(repo, l)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions commands/mr_merge.go
Expand Up @@ -17,6 +17,7 @@ var mrMergeCmd = &cobra.Command{
Short: `Merge/Accept merge requests`,
Long: ``,
Aliases: []string{"accept"},
Args: cobra.ExactArgs(1),
Run: acceptMergeRequest,
}

Expand All @@ -42,6 +43,9 @@ func acceptMergeRequest(cmd *cobra.Command, args []string) {
l.SHA = gitlab.String(m)
}
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
mr, resp, _ := gitlabClient.MergeRequests.AcceptMergeRequest(repo, manip.StringToInt(mergeID), l)

fmt.Println(aurora.Yellow("Accepting Merge Request #" + mergeID + "..."))
Expand Down
5 changes: 4 additions & 1 deletion commands/mr_reopen.go
Expand Up @@ -15,14 +15,17 @@ var mrReopenCmd = &cobra.Command{
Short: `Reopen merge requests`,
Long: ``,
Aliases: []string{"open"},
Args: cobra.MaximumNArgs(1),
Args: cobra.ExactArgs(1),
Run: reopenMergeRequestState,
}

func reopenMergeRequestState(cmd *cobra.Command, args []string) {
if len(args) > 0 {
mergeID := strings.Trim(args[0], " ")
gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
l := &gitlab.UpdateMergeRequestOptions{}
l.StateEvent = gitlab.String("reopen")
arrIds := strings.Split(strings.Trim(mergeID, "[] "), ",")
Expand Down

0 comments on commit f643dbf

Please sign in to comment.