Merge pull request #8711 from toshimaru/add-cache-ref-optoion-to-cache-list
feat: Add `ref` option to `gh cache list`
This commit is contained in:
commit
9dc23559c1
3 changed files with 49 additions and 1 deletions
10
pkg/cmd/cache/list/list.go
vendored
10
pkg/cmd/cache/list/list.go
vendored
|
|
@ -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 {
|
||||
|
|
@ -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,
|
||||
|
|
@ -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
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
36
pkg/cmd/cache/list/list_test.go
vendored
36
pkg/cmd/cache/list/list_test.go
vendored
|
|
@ -31,6 +31,7 @@ func TestNewCmdList(t *testing.T) {
|
|||
Order: "desc",
|
||||
Sort: "last_accessed_at",
|
||||
Key: "",
|
||||
Ref: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -41,6 +42,7 @@ func TestNewCmdList(t *testing.T) {
|
|||
Order: "desc",
|
||||
Sort: "last_accessed_at",
|
||||
Key: "",
|
||||
Ref: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -56,6 +58,7 @@ func TestNewCmdList(t *testing.T) {
|
|||
Order: "desc",
|
||||
Sort: "created_at",
|
||||
Key: "",
|
||||
Ref: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -66,6 +69,7 @@ func TestNewCmdList(t *testing.T) {
|
|||
Order: "asc",
|
||||
Sort: "last_accessed_at",
|
||||
Key: "",
|
||||
Ref: "",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -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",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
4
pkg/cmd/cache/shared/shared.go
vendored
4
pkg/cmd/cache/shared/shared.go
vendored
|
|
@ -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
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue