Skip to content

Commit

Permalink
Merge pull request #1136 from rsteube/gh-expose-labels
Browse files Browse the repository at this point in the history
gh: expose labels macro
  • Loading branch information
rsteube authored May 14, 2022
2 parents e35ffea + 875237f commit 843f833
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 59 deletions.
3 changes: 1 addition & 2 deletions completers/gh_completer/cmd/action/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func ActionApiV3Paths(cmd *cobra.Command) carapace.Action {
case "{gitignore_name}":
return ActionGitignoreTemplates(cmd)
case "{label}":
fakeRepoFlag(cmd, matchedData["{owner}"], matchedData["{repo}"])
return ActionLabels(cmd)
return gh.ActionLabels(gh.RepoOpts{Owner: matchedData["{owner}"], Name: matchedData["{repo}"]})
case "{license}":
return gh.ActionLicenses(gh.HostOpts{})
case "{issue_number}":
Expand Down
42 changes: 7 additions & 35 deletions completers/gh_completer/cmd/action/label.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package action

import (
"time"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/gh"
"github.com/spf13/cobra"
)

Expand All @@ -13,39 +12,12 @@ type label struct {
Color string
}

type labelsQuery struct {
Data struct {
Repository struct {
Labels struct {
Nodes []label
}
}
}
}

func ActionLabels(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var queryResult labelsQuery
return GraphQlAction(cmd, `repository(owner: $owner, name: $repo){ labels(first: 100) { nodes { name, description, color } } }`, &queryResult, func() carapace.Action {
labels := queryResult.Data.Repository.Labels.Nodes
vals := make([]string, 0)
for _, label := range labels {
vals = append(vals, label.Name, label.Description, "#"+label.Color)
}
return carapace.ActionStyledValuesDescribed(vals...)
})
}).Cache(5*time.Minute, repoCacheKey(cmd))
}

func ActionLabelFields() carapace.Action {
return carapace.ActionValues(
"color",
"createdAt",
"description",
"id",
"isDefault",
"name",
"updatedAt",
"url",
)
repo, err := repoOverride(cmd)
if err != nil {
return carapace.ActionMessage(err.Error())
}
return gh.ActionLabels(gh.RepoOpts{Host: repo.RepoHost(), Owner: repo.RepoOwner(), Name: repo.RepoName()})
})
}
12 changes: 0 additions & 12 deletions completers/gh_completer/cmd/action/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@ package action

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/gh_completer/cmd/action/ghrepo"
"github.com/rsteube/carapace-bin/pkg/actions/tools/gh"
"github.com/rsteube/carapace/pkg/cache"
"github.com/spf13/cobra"
)

func repoCacheKey(cmd *cobra.Command) cache.Key {
return func() (string, error) {
if repo, err := repoOverride(cmd); err != nil {
return "", err
} else {
return ghrepo.FullName(repo), nil
}
}
}

func ActionOwnerRepositories(cmd *cobra.Command) carapace.Action {
return carapace.ActionMultiParts("/", func(c carapace.Context) carapace.Action {
// TODO hack to enable completion outside git repo - this needs to be fixed in GraphQlAction/repooverride though
Expand Down
4 changes: 2 additions & 2 deletions completers/gh_completer/cmd/label_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/completers/gh_completer/cmd/action"
"github.com/rsteube/carapace-bin/pkg/actions/tools/gh"
"github.com/spf13/cobra"
)

Expand All @@ -27,7 +27,7 @@ func init() {

carapace.Gen(label_listCmd).FlagCompletion(carapace.ActionMap{
"json": carapace.ActionMultiParts(",", func(c carapace.Context) carapace.Action {
return action.ActionLabelFields().Invoke(c).Filter(c.Parts).ToA()
return gh.ActionLabelFields().Invoke(c).Filter(c.Parts).ToA()
}),
"order": carapace.ActionValues("asc", "desc"),
"sort": carapace.ActionValues("created", "name"),
Expand Down
7 changes: 5 additions & 2 deletions pkg/actions/tools/gh/gh.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// package gh contains github related actions
package gh

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

type repo interface {
host() string
owner() string
Expand Down Expand Up @@ -49,5 +51,6 @@ func (o RepoOpts) host() string {
return o.Host
}
}
func (o RepoOpts) owner() string { return o.Owner }
func (o RepoOpts) name() string { return o.Name }
func (o RepoOpts) owner() string { return o.Owner }
func (o RepoOpts) name() string { return o.Name }
func (o RepoOpts) cacheKey() cache.Key { return cache.String(o.Host, o.Owner, o.Name) }
6 changes: 0 additions & 6 deletions pkg/actions/tools/gh/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ type issue struct {
}
}

type label struct {
Name string
Description string
Color string
}

type pinnedIssueQuery struct {
Data struct {
Repository struct {
Expand Down
56 changes: 56 additions & 0 deletions pkg/actions/tools/gh/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package gh

import (
"time"

"github.com/rsteube/carapace"
)

type label struct {
Name string
Description string
Color string
}

type labelsQuery struct {
Data struct {
Repository struct {
Labels struct {
Nodes []label
}
}
}
}

// ActionLabels completes labels
// enhancement (New feature or request)
// good first issue (Good for newcomers)
func ActionLabels(opts RepoOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var queryResult labelsQuery
return graphQlAction(opts, `repository(owner: $owner, name: $repo){ labels(first: 100) { nodes { name, description, color } } }`, &queryResult, func() carapace.Action {
labels := queryResult.Data.Repository.Labels.Nodes
vals := make([]string, 0)
for _, label := range labels {
vals = append(vals, label.Name, label.Description, "#"+label.Color)
}
return carapace.ActionStyledValuesDescribed(vals...)
})
}).Cache(5*time.Minute, opts.cacheKey())
}

// ActionLabelFields completes label fields
// color
// createdAt
func ActionLabelFields() carapace.Action {
return carapace.ActionValues(
"color",
"createdAt",
"description",
"id",
"isDefault",
"name",
"updatedAt",
"url",
)
}

0 comments on commit 843f833

Please sign in to comment.