From da5b76ebad9cdcc9b0810270c7c96e7add329b56 Mon Sep 17 00:00:00 2001 From: rsteube Date: Thu, 14 Mar 2024 12:19:49 +0100 Subject: [PATCH] gh: updates from 2.45.0 --- completers/gh_completer/cmd/action/cache.go | 9 +++++ completers/gh_completer/cmd/action/team.go | 21 ++++++++++ completers/gh_completer/cmd/cache_list.go | 3 ++ completers/gh_completer/cmd/project_link.go | 33 ++++++++++++++++ completers/gh_completer/cmd/project_unlink.go | 33 ++++++++++++++++ completers/gh_completer/cmd/repo_list.go | 2 +- completers/gh_completer/cmd/search_issues.go | 4 +- completers/gh_completer/cmd/search_prs.go | 4 +- completers/gh_completer/cmd/search_repos.go | 4 +- pkg/actions/tools/gh/cache.go | 29 ++++++++++++++ .../actions/tools/gh}/languages.go | 2 +- pkg/actions/tools/gh/team.go | 38 +++++++++++++++++++ 12 files changed, 174 insertions(+), 8 deletions(-) create mode 100644 completers/gh_completer/cmd/action/team.go create mode 100644 completers/gh_completer/cmd/project_link.go create mode 100644 completers/gh_completer/cmd/project_unlink.go rename {completers/gh_completer/cmd/action => pkg/actions/tools/gh}/languages.go (99%) create mode 100644 pkg/actions/tools/gh/team.go diff --git a/completers/gh_completer/cmd/action/cache.go b/completers/gh_completer/cmd/action/cache.go index 94e021dd0a..0f66bfd322 100644 --- a/completers/gh_completer/cmd/action/cache.go +++ b/completers/gh_completer/cmd/action/cache.go @@ -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()}) + }) +} diff --git a/completers/gh_completer/cmd/action/team.go b/completers/gh_completer/cmd/action/team.go new file mode 100644 index 0000000000..c0af3cc40d --- /dev/null +++ b/completers/gh_completer/cmd/action/team.go @@ -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(), + }) + + }) +} diff --git a/completers/gh_completer/cmd/cache_list.go b/completers/gh_completer/cmd/cache_list.go index 3157464ab6..7e98f5f502 100644 --- a/completers/gh_completer/cmd/cache_list.go +++ b/completers/gh_completer/cmd/cache_list.go @@ -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" @@ -22,6 +23,7 @@ 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/ or refs/pull//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) @@ -29,6 +31,7 @@ func init() { 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"), }) } diff --git a/completers/gh_completer/cmd/project_link.go b/completers/gh_completer/cmd/project_link.go new file mode 100644 index 0000000000..142f39c200 --- /dev/null +++ b/completers/gh_completer/cmd/project_link.go @@ -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 [] [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}), + ) +} diff --git a/completers/gh_completer/cmd/project_unlink.go b/completers/gh_completer/cmd/project_unlink.go new file mode 100644 index 0000000000..460ebf7b26 --- /dev/null +++ b/completers/gh_completer/cmd/project_unlink.go @@ -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 [] [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}), + ) +} diff --git a/completers/gh_completer/cmd/repo_list.go b/completers/gh_completer/cmd/repo_list.go index 663514fb52..06c55eb137 100644 --- a/completers/gh_completer/cmd/repo_list.go +++ b/completers/gh_completer/cmd/repo_list.go @@ -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(",") diff --git a/completers/gh_completer/cmd/search_issues.go b/completers/gh_completer/cmd/search_issues.go index 393ae3680f..60b1808675 100644 --- a/completers/gh_completer/cmd/search_issues.go +++ b/completers/gh_completer/cmd/search_issues.go @@ -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`") @@ -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 { diff --git a/completers/gh_completer/cmd/search_prs.go b/completers/gh_completer/cmd/search_prs.go index 8ef3bdb950..4549c9f024 100644 --- a/completers/gh_completer/cmd/search_prs.go +++ b/completers/gh_completer/cmd/search_prs.go @@ -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") @@ -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})), diff --git a/completers/gh_completer/cmd/search_repos.go b/completers/gh_completer/cmd/search_repos.go index cbc590b051..aad280ed3d 100644 --- a/completers/gh_completer/cmd/search_repos.go +++ b/completers/gh_completer/cmd/search_repos.go @@ -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") @@ -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"), diff --git a/pkg/actions/tools/gh/cache.go b/pkg/actions/tools/gh/cache.go index 8f3f114f68..bced8e9a87 100644 --- a/pkg/actions/tools/gh/cache.go +++ b/pkg/actions/tools/gh/cache.go @@ -38,6 +38,35 @@ func ActionCaches(opts RepoOpts) carapace.Action { }).Tag("caches") } +// ActionRefs 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 diff --git a/completers/gh_completer/cmd/action/languages.go b/pkg/actions/tools/gh/languages.go similarity index 99% rename from completers/gh_completer/cmd/action/languages.go rename to pkg/actions/tools/gh/languages.go index 560232d2f8..01ec476e43 100644 --- a/completers/gh_completer/cmd/action/languages.go +++ b/pkg/actions/tools/gh/languages.go @@ -1,4 +1,4 @@ -package action +package gh import ( "github.com/carapace-sh/carapace" diff --git a/pkg/actions/tools/gh/team.go b/pkg/actions/tools/gh/team.go new file mode 100644 index 0000000000..6f9b09b4ca --- /dev/null +++ b/pkg/actions/tools/gh/team.go @@ -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...) + }) + + }) +}