Skip to content

Commit

Permalink
Merge pull request #2302 from carapace-sh/gh-updates
Browse files Browse the repository at this point in the history
gh: updates from 2.45.0
  • Loading branch information
rsteube authored Mar 14, 2024
2 parents 355d917 + 424a6fe commit 5b63580
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 8 deletions.
9 changes: 9 additions & 0 deletions completers/gh_completer/cmd/action/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ func ActionCaches(cmd *cobra.Command) carapace.Action {
return gh.ActionCaches(gh.RepoOpts{Host: repo.RepoHost(), Owner: repo.RepoOwner(), Name: repo.RepoName()})
})
}
func ActionCacheRefs(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
if err != nil {
return carapace.ActionMessage(err.Error())
}
return gh.ActionCacheRefs(gh.RepoOpts{Host: repo.RepoHost(), Owner: repo.RepoOwner(), Name: repo.RepoName()})
})
}
21 changes: 21 additions & 0 deletions completers/gh_completer/cmd/action/team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package action

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/gh"
"github.com/spf13/cobra"
)

func ActionTeams(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
repo, err := repoOverride(cmd)
if err != nil {
return carapace.ActionMessage(err.Error())
}
return gh.ActionTeams(gh.OwnerOpts{
Host: repo.RepoHost(),
Owner: repo.RepoOwner(),
})

})
}
3 changes: 3 additions & 0 deletions completers/gh_completer/cmd/cache_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace-bin/completers/gh_completer/cmd/action"
"github.com/carapace-sh/carapace-bin/pkg/actions/tools/gh"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/spf13/cobra"
Expand All @@ -22,13 +23,15 @@ func init() {
cache_listCmd.Flags().StringP("key", "k", "", "Filter by cache key prefix")
cache_listCmd.Flags().StringP("limit", "L", "", "Maximum number of caches to fetch")
cache_listCmd.Flags().StringP("order", "O", "", "Order of caches returned: {asc|desc}")
cache_listCmd.Flags().StringP("ref", "r", "", "Filter by ref, formatted as refs/heads/<branch name> or refs/pull/<number>/merge")
cache_listCmd.Flags().StringP("sort", "S", "", "Sort fetched caches: {created_at|last_accessed_at|size_in_bytes}")
cache_listCmd.Flags().StringP("template", "t", "", "Format JSON output using a Go template; see \"gh help formatting\"")
cacheCmd.AddCommand(cache_listCmd)

carapace.Gen(cache_listCmd).FlagCompletion(carapace.ActionMap{
"json": gh.ActionCacheFields().UniqueList(","),
"order": carapace.ActionValues("asc", "desc").StyleF(style.ForKeyword),
"ref": action.ActionCacheRefs(cache_listCmd),
"sort": carapace.ActionValues("created_at", "last_accessed_at", "size_in_bytes"),
})
}
33 changes: 33 additions & 0 deletions completers/gh_completer/cmd/project_link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

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

var project_linkCmd = &cobra.Command{
Use: "link [<number>] [flag]",
Short: "Link a project to a repository or a team",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(project_linkCmd).Standalone()

project_linkCmd.Flags().String("owner", "", "Login of the owner. Use \"@me\" for the current user.")
project_linkCmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `[HOST/]OWNER/REPO` format")
project_linkCmd.Flags().StringP("team", "T", "", "The team to be linked to this project")
projectCmd.AddCommand(project_linkCmd)

carapace.Gen(project_linkCmd).FlagCompletion(carapace.ActionMap{
"owner": gh.ActionOwners(gh.HostOpts{}),
"repo": action.ActionRepoOverride(project_linkCmd),
"team": action.ActionTeams(project_linkCmd),
})

carapace.Gen(project_linkCmd).PositionalCompletion(
action.ActionProjects(project_linkCmd, action.ProjectOpts{Open: true}),
)
}
33 changes: 33 additions & 0 deletions completers/gh_completer/cmd/project_unlink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

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

var project_unlinkCmd = &cobra.Command{
Use: "unlink [<number>] [flag]",
Short: "Unlink a project from a repository or a team",
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(project_unlinkCmd).Standalone()

project_unlinkCmd.Flags().String("owner", "", "Login of the owner. Use \"@me\" for the current user.")
project_unlinkCmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `[HOST/]OWNER/REPO` format")
project_unlinkCmd.Flags().StringP("team", "T", "", "The team to be unlinked from this project")
projectCmd.AddCommand(project_unlinkCmd)

carapace.Gen(project_unlinkCmd).FlagCompletion(carapace.ActionMap{
"owner": gh.ActionOwners(gh.HostOpts{}),
"repo": action.ActionRepoOverride(project_linkCmd),
"team": action.ActionTeams(project_linkCmd),
})

carapace.Gen(project_unlinkCmd).PositionalCompletion(
action.ActionProjects(project_linkCmd, action.ProjectOpts{Open: true}),
)
}
2 changes: 1 addition & 1 deletion completers/gh_completer/cmd/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func init() {

carapace.Gen(repo_listCmd).FlagCompletion(carapace.ActionMap{
"json": action.ActionRepositoryFields().UniqueList(","),
"language": action.ActionLanguages(),
"language": gh.ActionLanguages(),
"topic": carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if len(c.Args) > 0 {
return action.ActionTopics(repo_listCmd, c.Args[0]).UniqueList(",")
Expand Down
4 changes: 2 additions & 2 deletions completers/gh_completer/cmd/search_issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
carapace.Gen(search_issuesCmd).Standalone()

search_issuesCmd.Flags().String("app", "", "Filter by GitHub App author")
search_issuesCmd.Flags().Bool("archived", false, "Restrict search to archived repositories")
search_issuesCmd.Flags().Bool("archived", false, "Filter based on the repository archived state {true|false}")
search_issuesCmd.Flags().String("assignee", "", "Filter by assignee")
search_issuesCmd.Flags().String("author", "", "Filter by author")
search_issuesCmd.Flags().String("closed", "", "Filter on closed at `date`")
Expand Down Expand Up @@ -70,7 +70,7 @@ func init() {
"label": action.ActionSearchMultiRepo(search_issuesCmd, func(cmd *cobra.Command) carapace.Action {
return action.ActionLabels(cmd).UniqueList(",")
}),
"language": action.ActionLanguages(),
"language": gh.ActionLanguages(),
"match": carapace.ActionValues("title", "body", "comments").UniqueList(","),
"mentions": gh.ActionUsers(gh.HostOpts{}),
"milestone": action.ActionSearchMultiRepo(search_issuesCmd, func(cmd *cobra.Command) carapace.Action {
Expand Down
4 changes: 2 additions & 2 deletions completers/gh_completer/cmd/search_prs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
carapace.Gen(search_prsCmd).Standalone()

search_prsCmd.Flags().String("app", "", "Filter by GitHub App author")
search_prsCmd.Flags().Bool("archived", false, "Restrict search to archived repositories")
search_prsCmd.Flags().Bool("archived", false, "Filter based on the repository archived state {true|false}")
search_prsCmd.Flags().String("assignee", "", "Filter by assignee")
search_prsCmd.Flags().String("author", "", "Filter by author")
search_prsCmd.Flags().StringP("base", "B", "", "Filter on base branch name")
Expand Down Expand Up @@ -85,7 +85,7 @@ func init() {
"label": action.ActionSearchMultiRepo(search_prsCmd, func(cmd *cobra.Command) carapace.Action {
return action.ActionLabels(cmd).UniqueList(",")
}),
"language": action.ActionLanguages(),
"language": gh.ActionLanguages(),
"match": carapace.ActionValues("title", "body", "comments").UniqueList(","),
"mentions": gh.ActionUsers(gh.HostOpts{}),
"merged-at": action.ActionSearchRange(time.ActionDateTime(time.DateTimeOpts{Strict: true})),
Expand Down
4 changes: 2 additions & 2 deletions completers/gh_completer/cmd/search_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var search_reposCmd = &cobra.Command{
func init() {
carapace.Gen(search_reposCmd).Standalone()

search_reposCmd.Flags().Bool("archived", false, "Filter based on archive state")
search_reposCmd.Flags().Bool("archived", false, "Filter based on the repository archived state {true|false}")
search_reposCmd.Flags().String("created", "", "Filter based on created at `date`")
search_reposCmd.Flags().String("followers", "", "Filter based on `number` of followers")
search_reposCmd.Flags().String("forks", "", "Filter on `number` of forks")
Expand Down Expand Up @@ -47,7 +47,7 @@ func init() {
"created": action.ActionSearchRange(time.ActionDateTime(time.DateTimeOpts{Strict: true})),
"include-forks": carapace.ActionValues("false", "true", "only"),
"json": action.ActionSearchRepositoryFields().UniqueList(","),
"language": action.ActionLanguages(),
"language": gh.ActionLanguages(),
"license": gh.ActionLicenses(gh.HostOpts{}).UniqueList(","),
"match": carapace.ActionValues("name", "description", "readme").UniqueList(","),
"order": carapace.ActionValues("asc", "desc"),
Expand Down
29 changes: 29 additions & 0 deletions pkg/actions/tools/gh/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@ func ActionCaches(opts RepoOpts) carapace.Action {
}).Tag("caches")
}

// ActionCacheRefs completes cache refs
//
// refs/pull/1718/merge
// refs/heads/master
func ActionCacheRefs(opts RepoOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
// TODO handle this generally for apiV3 actions
if opts.Owner == "" {
opts.Owner = ":owner"
}
if opts.Name == "" {
opts.Name = ":repo"
}
var queryResult actionCache
return apiV3Action(opts, fmt.Sprintf("repos/%v/%v/actions/caches?per_page=100&sort=last_accessed_at&direction=desc", opts.Owner, opts.Name), &queryResult, func() carapace.Action {
unique := make(map[string]bool)
for _, cache := range queryResult.ActionsCaches {
unique[cache.Ref] = true
}

vals := make([]string, 0)
for ref := range unique {
vals = append(vals, ref)
}
return carapace.ActionValues(vals...)
})
}).Tag("caches")
}

// ActionCacheFields completes label fields
//
// id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package action
package gh

import (
"github.com/carapace-sh/carapace"
Expand Down
38 changes: 38 additions & 0 deletions pkg/actions/tools/gh/team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package gh

import (
"fmt"

"github.com/carapace-sh/carapace"
"github.com/carapace-sh/carapace/pkg/style"
)

type team struct {
Name string `json:"name"`
ID int `json:"id"`
Description string `json:"description"`
Privacy string `json:"privacy"`
}

// ActionTeams completes teams
//
// first-team (description of first team)
// second-team (description of second team)
func ActionTeams(opts OwnerOpts) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var queryResult []team
return apiV3Action(opts.repo(), fmt.Sprintf(`orgs/%v/teams`, opts.Owner), &queryResult, func() carapace.Action {
vals := make([]string, 0)
for _, team := range queryResult {
switch team.Privacy {
case "secret":
vals = append(vals, team.Name, team.Description, style.Carapace.KeywordNegative)
default:
vals = append(vals, team.Name, team.Description, style.Default)
}
}
return carapace.ActionStyledValuesDescribed(vals...)
})

})
}

0 comments on commit 5b63580

Please sign in to comment.