Skip to content

Commit

Permalink
Merge pull request #8711 from toshimaru/add-cache-ref-optoion-to-cach…
Browse files Browse the repository at this point in the history
…e-list

feat: Add `ref` option to `gh cache list`
  • Loading branch information
williammartin committed Feb 29, 2024
2 parents d727528 + b7ea025 commit 9dc2355
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/cmd/cache/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ListOptions struct {
Order string
Sort string
Key string
Ref string
}

func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
Expand All @@ -51,6 +52,12 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
# List caches that have keys matching a prefix (or that match exactly)
$ gh cache list --key key-prefix
# To list caches for a specific branch, replace <branch-name> with the actual branch name
$ gh cache list --ref refs/heads/<branch-name>
# To list caches for a specific pull request, replace <pr-number> with the actual pull request number
$ gh cache list --ref refs/pull/<pr-number>/merge
`),
Aliases: []string{"ls"},
Args: cobra.NoArgs,
Expand All @@ -74,6 +81,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
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")
cmd.Flags().StringVarP(&opts.Ref, "ref", "r", "", "Filter by ref, formatted as refs/heads/<branch name> or refs/pull/<number>/merge")
cmdutil.AddJSONFlags(cmd, &opts.Exporter, shared.CacheFields)

return cmd
Expand All @@ -92,7 +100,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, Key: opts.Key})
result, err := shared.GetCaches(client, repo, shared.GetCachesOptions{Limit: opts.Limit, Sort: opts.Sort, Order: opts.Order, Key: opts.Key, Ref: opts.Ref})
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("%s Failed to get caches: %w", opts.IO.ColorScheme().FailureIcon(), err)
Expand Down
36 changes: 36 additions & 0 deletions pkg/cmd/cache/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestNewCmdList(t *testing.T) {
Order: "desc",
Sort: "last_accessed_at",
Key: "",
Ref: "",
},
},
{
Expand All @@ -41,6 +42,7 @@ func TestNewCmdList(t *testing.T) {
Order: "desc",
Sort: "last_accessed_at",
Key: "",
Ref: "",
},
},
{
Expand All @@ -56,6 +58,7 @@ func TestNewCmdList(t *testing.T) {
Order: "desc",
Sort: "created_at",
Key: "",
Ref: "",
},
},
{
Expand All @@ -66,6 +69,7 @@ func TestNewCmdList(t *testing.T) {
Order: "asc",
Sort: "last_accessed_at",
Key: "",
Ref: "",
},
},
{
Expand All @@ -76,6 +80,18 @@ func TestNewCmdList(t *testing.T) {
Order: "desc",
Sort: "last_accessed_at",
Key: "cache-key-prefix-",
Ref: "",
},
},
{
name: "with ref",
input: "--ref refs/heads/main",
wants: ListOptions{
Limit: 30,
Order: "desc",
Sort: "last_accessed_at",
Key: "",
Ref: "refs/heads/main",
},
},
}
Expand Down Expand Up @@ -206,6 +222,26 @@ ID KEY SIZE CREATED ACCESSED
wantErr: true,
wantErrMsg: "No caches found in OWNER/REPO",
},
{
name: "only requests caches with the provided ref",
opts: ListOptions{
Ref: "refs/heads/main",
},
stubs: func(reg *httpmock.Registry) {
reg.Register(
func(req *http.Request) bool {
return req.URL.Query().Get("ref") == "refs/heads/main"
},
httpmock.JSONResponse(shared.CachePayload{
ActionsCaches: []shared.Cache{},
TotalCount: 0,
}))
},
// We could put anything here, we're really asserting that the key is passed
// to the API.
wantErr: true,
wantErrMsg: "No caches found in OWNER/REPO",
},
{
name: "displays no results",
stubs: func(reg *httpmock.Registry) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/cache/shared/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type GetCachesOptions struct {
Order string
Sort string
Key string
Ref string
}

// Return a list of caches for a repository. Pass a negative limit to request
Expand All @@ -62,6 +63,9 @@ func GetCaches(client *api.Client, repo ghrepo.Interface, opts GetCachesOptions)
if opts.Key != "" {
path += fmt.Sprintf("&key=%s", url.QueryEscape(opts.Key))
}
if opts.Ref != "" {
path += fmt.Sprintf("&ref=%s", url.QueryEscape(opts.Ref))
}

var result *CachePayload
pagination:
Expand Down

0 comments on commit 9dc2355

Please sign in to comment.