forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.go
More file actions
97 lines (83 loc) · 2.19 KB
/
shared.go
File metadata and controls
97 lines (83 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package shared
import (
"fmt"
"net/url"
"time"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmdutil"
)
var CacheFields = []string{
"createdAt",
"id",
"key",
"lastAccessedAt",
"ref",
"sizeInBytes",
"version",
}
type Cache struct {
CreatedAt time.Time `json:"created_at"`
Id int `json:"id"`
Key string `json:"key"`
LastAccessedAt time.Time `json:"last_accessed_at"`
Ref string `json:"ref"`
SizeInBytes int64 `json:"size_in_bytes"`
Version string `json:"version"`
}
type CachePayload struct {
ActionsCaches []Cache `json:"actions_caches"`
TotalCount int `json:"total_count"`
}
type GetCachesOptions struct {
Limit int
Order string
Sort string
Key string
Ref string
}
// Return a list of caches for a repository. Pass a negative limit to request
// all pages from the API until all caches have been fetched.
func GetCaches(client *api.Client, repo ghrepo.Interface, opts GetCachesOptions) (*CachePayload, error) {
path := fmt.Sprintf("repos/%s/actions/caches", ghrepo.FullName(repo))
perPage := 100
if opts.Limit > 0 && opts.Limit < 100 {
perPage = opts.Limit
}
path += fmt.Sprintf("?per_page=%d", perPage)
if opts.Sort != "" {
path += fmt.Sprintf("&sort=%s", opts.Sort)
}
if opts.Order != "" {
path += fmt.Sprintf("&direction=%s", opts.Order)
}
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:
for path != "" {
var response CachePayload
var err error
path, err = client.RESTWithNext(repo.RepoHost(), "GET", path, nil, &response)
if err != nil {
return nil, err
}
if result == nil {
result = &response
} else {
result.ActionsCaches = append(result.ActionsCaches, response.ActionsCaches...)
}
if opts.Limit > 0 && len(result.ActionsCaches) >= opts.Limit {
result.ActionsCaches = result.ActionsCaches[:opts.Limit]
break pagination
}
}
return result, nil
}
func (c *Cache) ExportData(fields []string) map[string]interface{} {
return cmdutil.StructExportData(c, fields)
}