Skip to content

Commit

Permalink
Merge pull request #1646 from rsteube/tea-pullrequest
Browse files Browse the repository at this point in the history
tea: complete pull requests
  • Loading branch information
rsteube authored Apr 9, 2023
2 parents dc34686 + 8669664 commit cf2b19f
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 11 deletions.
18 changes: 18 additions & 0 deletions completers/tea_completer/cmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ func ActionIssues(cmd *cobra.Command, open bool) carapace.Action {
})
}

func ActionPullrequests(cmd *cobra.Command, open bool) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
opts := tea.PullrequestOpts{}
if f := cmd.Flag("login"); f != nil {
opts.Login = f.Value.String()
}
if f := cmd.Flag("remote"); f != nil {
opts.Remote = f.Value.String()
}
if f := cmd.Flag("repo"); f != nil {
opts.Repo = f.Value.String()
}
opts.Open = open
opts.Closed = !open
return tea.ActionPullrequests(opts)
})
}

func ActionLabels(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
opts := tea.RepoOpts{}
Expand Down
5 changes: 5 additions & 0 deletions completers/tea_completer/cmd/pulls_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/tea_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tea"
"github.com/spf13/cobra"
Expand All @@ -28,4 +29,8 @@ func init() {
"output": tea.ActionOutputFormats(),
"remote": git.ActionRemotes(),
})

carapace.Gen(pulls_approveCmd).PositionalCompletion(
action.ActionPullrequests(pulls_approveCmd, true),
)
}
5 changes: 5 additions & 0 deletions completers/tea_completer/cmd/pulls_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/tea_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tea"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -29,4 +30,8 @@ func init() {
"output": tea.ActionOutputFormats(),
"remote": git.ActionRemotes(),
})

carapace.Gen(pulls_checkoutCmd).PositionalCompletion(
action.ActionPullrequests(pulls_checkoutCmd, true),
)
}
5 changes: 5 additions & 0 deletions completers/tea_completer/cmd/pulls_clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/tea_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tea"
"github.com/spf13/cobra"
Expand All @@ -28,4 +29,8 @@ func init() {
"output": tea.ActionOutputFormats(),
"remote": git.ActionRemotes(),
})

carapace.Gen(pulls_cleanCmd).PositionalCompletion(
action.ActionPullrequests(pulls_cleanCmd, false),
)
}
5 changes: 5 additions & 0 deletions completers/tea_completer/cmd/pulls_close.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/tea_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tea"
"github.com/spf13/cobra"
)
Expand All @@ -25,4 +26,8 @@ func init() {
carapace.Gen(pulls_closeCmd).FlagCompletion(carapace.ActionMap{
"output": tea.ActionOutputFormats(),
})

carapace.Gen(pulls_closeCmd).PositionalCompletion(
action.ActionPullrequests(pulls_closeCmd, true),
)
}
5 changes: 5 additions & 0 deletions completers/tea_completer/cmd/pulls_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/tea_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tea"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -32,4 +33,8 @@ func init() {
"remote": git.ActionRemotes(),
"style": carapace.ActionValues("merge", "rebase", "squash", "rebase-merge"),
})

carapace.Gen(pulls_mergeCmd).PositionalCompletion(
action.ActionPullrequests(pulls_mergeCmd, true),
)
}
5 changes: 5 additions & 0 deletions completers/tea_completer/cmd/pulls_reject.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/tea_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tea"
"github.com/spf13/cobra"
Expand All @@ -27,4 +28,8 @@ func init() {
"output": tea.ActionOutputFormats(),
"remote": git.ActionRemotes(),
})

carapace.Gen(pulls_rejectCmd).PositionalCompletion(
action.ActionPullrequests(pulls_rejectCmd, true),
)
}
5 changes: 5 additions & 0 deletions completers/tea_completer/cmd/pulls_reopen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/tea_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tea"
"github.com/spf13/cobra"
Expand All @@ -28,4 +29,8 @@ func init() {
"output": tea.ActionOutputFormats(),
"remote": git.ActionRemotes(),
})

carapace.Gen(pulls_reopenCmd).PositionalCompletion(
action.ActionPullrequests(pulls_reopenCmd, false),
)
}
5 changes: 5 additions & 0 deletions completers/tea_completer/cmd/pulls_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/tea_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/tools/git"
"github.com/rsteube/carapace-bin/pkg/actions/tools/tea"
"github.com/spf13/cobra"
Expand All @@ -27,4 +28,8 @@ func init() {
"output": tea.ActionOutputFormats(),
"remote": git.ActionRemotes(),
})

carapace.Gen(pulls_reviewCmd).PositionalCompletion(
action.ActionPullrequests(pulls_reviewCmd, true),
)
}
12 changes: 2 additions & 10 deletions pkg/actions/tools/tea/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strconv"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
"github.com/rsteube/carapace-bin/pkg/styles"
)

// ActionIssueFields completes issue fields
Expand Down Expand Up @@ -64,15 +64,7 @@ func ActionIssues(opts IssueOpts) carapace.Action {
return actionYamlQuery(repoOpts, &issues, args...)(func() carapace.Action {
vals := make([]string, 0)
for _, issue := range issues {
s := ""
switch issue.State {
case "open":
s = style.Green
case "closed":
s = style.Red
}

vals = append(vals, strconv.Itoa(issue.Index), issue.Title, s)
vals = append(vals, strconv.Itoa(issue.Index), issue.Title, styles.Tea.ForState(issue.State, c))
}
return carapace.ActionStyledValuesDescribed(vals...)
})
Expand Down
47 changes: 46 additions & 1 deletion pkg/actions/tools/tea/pullrequest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package tea

import "github.com/rsteube/carapace"
import (
"strconv"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/styles"
)

// ActionPullrequestFields completes pullrequest fields
//
Expand Down Expand Up @@ -30,3 +35,43 @@ func ActionPullrequestFields() carapace.Action {
"comments",
)
}

type PullrequestOpts struct {
Login string
Remote string
Repo string
Open bool
Closed bool
}

// ActionPullrequests completes pull requests
func ActionPullrequests(opts PullrequestOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var pullrequests []struct {
Index int `yaml:"index"`
Title string `yaml:"title"`
State string `yaml:"state"`
}

args := []string{"pull", "list"}
switch {
case opts.Closed && opts.Open:
args = append(args, "--state", "all")
case opts.Closed:
args = append(args, "--state", "closed")
case opts.Open:
args = append(args, "--state", "open")
default:
return carapace.ActionValues()
}

repoOpts := RepoOpts{Login: opts.Login, Remote: opts.Remote, Repo: opts.Repo}
return actionYamlQuery(repoOpts, &pullrequests, args...)(func() carapace.Action {
vals := make([]string, 0)
for _, pr := range pullrequests {
vals = append(vals, strconv.Itoa(pr.Index), pr.Title, styles.Tea.ForState(pr.State, c))
}
return carapace.ActionStyledValuesDescribed(vals...)
})
})
}
32 changes: 32 additions & 0 deletions pkg/styles/tea.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package styles

import "github.com/rsteube/carapace/pkg/style"

type tea struct {
StateClosed string `desc:"closed issues/pulls" tag:"issue styles"`
StateMerged string `desc:"merged pulls" tag:"issue styles"`
StateOpen string `desc:"open issues/pulls" tag:"issue styles"`
}

var Tea = tea{
StateClosed: style.Red,
StateMerged: style.Magenta,
StateOpen: style.Green,
}

func init() {
style.Register("tea", &Tea)
}

func (_tea *tea) ForState(s string, _ style.Context) string {
switch s {
case "closed":
return _tea.StateClosed
case "merged":
return _tea.StateMerged
case "open":
return _tea.StateOpen
default:
return style.Default
}
}

0 comments on commit cf2b19f

Please sign in to comment.