From 8252e59f17468ae4407246bcd923b78da795daf2 Mon Sep 17 00:00:00 2001 From: toshimaru Date: Mon, 5 Feb 2024 09:34:50 +0900 Subject: [PATCH] feat: Add cache key option to `gh cache list` Add `key` option to `gh cache list` command. ``` -K, --key string Filter by cache key prefix ``` > An explicit key or prefix for identifying the cache ref. https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#list-github-actions-caches-for-a-repository - Add key option related tests to `TestNewCmdList` --- pkg/cmd/cache/list/list.go | 4 +++- pkg/cmd/cache/list/list_test.go | 15 +++++++++++++++ pkg/cmd/cache/shared/shared.go | 5 +++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/cache/list/list.go b/pkg/cmd/cache/list/list.go index 6920c2faa..e9b0d4367 100644 --- a/pkg/cmd/cache/list/list.go +++ b/pkg/cmd/cache/list/list.go @@ -27,6 +27,7 @@ type ListOptions struct { Limit int Order string Sort string + Key string } func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command { @@ -69,6 +70,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman cmd.Flags().IntVarP(&opts.Limit, "limit", "L", 30, "Maximum number of caches to fetch") cmdutil.StringEnumFlag(cmd, &opts.Order, "order", "O", "desc", []string{"asc", "desc"}, "Order of caches returned") cmdutil.StringEnumFlag(cmd, &opts.Sort, "sort", "S", "last_accessed_at", []string{"created_at", "last_accessed_at", "size_in_bytes"}, "Sort fetched caches") + cmd.Flags().StringVarP(&opts.Key, "key", "K", "", "Filter by cache key prefix") cmdutil.AddJSONFlags(cmd, &opts.Exporter, shared.CacheFields) return cmd @@ -87,7 +89,7 @@ func listRun(opts *ListOptions) error { client := api.NewClientFromHTTP(httpClient) opts.IO.StartProgressIndicator() - result, err := shared.GetCaches(client, repo, shared.GetCachesOptions{Limit: opts.Limit, Sort: opts.Sort, Order: opts.Order}) + result, err := shared.GetCaches(client, repo, shared.GetCachesOptions{Limit: opts.Limit, Sort: opts.Sort, Order: opts.Order, Key: opts.Key}) opts.IO.StopProgressIndicator() if err != nil { return fmt.Errorf("%s Failed to get caches: %w", opts.IO.ColorScheme().FailureIcon(), err) diff --git a/pkg/cmd/cache/list/list_test.go b/pkg/cmd/cache/list/list_test.go index 1129297ea..30c248c22 100644 --- a/pkg/cmd/cache/list/list_test.go +++ b/pkg/cmd/cache/list/list_test.go @@ -30,6 +30,7 @@ func TestNewCmdList(t *testing.T) { Limit: 30, Order: "desc", Sort: "last_accessed_at", + Key: "", }, }, { @@ -39,6 +40,7 @@ func TestNewCmdList(t *testing.T) { Limit: 100, Order: "desc", Sort: "last_accessed_at", + Key: "", }, }, { @@ -53,6 +55,7 @@ func TestNewCmdList(t *testing.T) { Limit: 30, Order: "desc", Sort: "created_at", + Key: "", }, }, { @@ -62,6 +65,17 @@ func TestNewCmdList(t *testing.T) { Limit: 30, Order: "asc", Sort: "last_accessed_at", + Key: "", + }, + }, + { + name: "with key", + input: "--key cache-key-prefix-", + wants: ListOptions{ + Limit: 30, + Order: "desc", + Sort: "last_accessed_at", + Key: "cache-key-prefix-", }, }, } @@ -90,6 +104,7 @@ func TestNewCmdList(t *testing.T) { assert.Equal(t, tt.wants.Limit, gotOpts.Limit) assert.Equal(t, tt.wants.Sort, gotOpts.Sort) assert.Equal(t, tt.wants.Order, gotOpts.Order) + assert.Equal(t, tt.wants.Key, gotOpts.Key) }) } } diff --git a/pkg/cmd/cache/shared/shared.go b/pkg/cmd/cache/shared/shared.go index 6dceb186c..315e19520 100644 --- a/pkg/cmd/cache/shared/shared.go +++ b/pkg/cmd/cache/shared/shared.go @@ -2,6 +2,7 @@ package shared import ( "fmt" + "net/url" "time" "github.com/cli/cli/v2/api" @@ -38,6 +39,7 @@ type GetCachesOptions struct { Limit int Order string Sort string + Key string } // Return a list of caches for a repository. Pass a negative limit to request @@ -57,6 +59,9 @@ func GetCaches(client *api.Client, repo ghrepo.Interface, opts GetCachesOptions) if opts.Order != "" { path += fmt.Sprintf("&direction=%s", opts.Order) } + if opts.Key != "" { + path += fmt.Sprintf("&key=%s", url.QueryEscape(opts.Key)) + } var result *CachePayload pagination: