1 // Copyright 2022 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 ) 12 13 // ActionsCache represents a GitHub action cache. 14 // 15 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#about-the-cache-api 16 type ActionsCache struct { 17 ID *int64 `json:"id,omitempty" url:"-"` 18 Ref *string `json:"ref,omitempty" url:"ref"` 19 Key *string `json:"key,omitempty" url:"key"` 20 Version *string `json:"version,omitempty" url:"-"` 21 LastAccessedAt *Timestamp `json:"last_accessed_at,omitempty" url:"-"` 22 CreatedAt *Timestamp `json:"created_at,omitempty" url:"-"` 23 SizeInBytes *int64 `json:"size_in_bytes,omitempty" url:"-"` 24 } 25 26 // ActionsCacheList represents a list of GitHub actions Cache. 27 // 28 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#list-github-actions-caches-for-a-repository 29 type ActionsCacheList struct { 30 TotalCount int `json:"total_count"` 31 ActionsCaches []*ActionsCache `json:"actions_caches,omitempty"` 32 } 33 34 // ActionsCacheUsage represents a GitHub Actions Cache Usage object. 35 // 36 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-a-repository 37 type ActionsCacheUsage struct { 38 FullName string `json:"full_name"` 39 ActiveCachesSizeInBytes int64 `json:"active_caches_size_in_bytes"` 40 ActiveCachesCount int `json:"active_caches_count"` 41 } 42 43 // ActionsCacheUsageList represents a list of repositories with GitHub Actions cache usage for an organization. 44 // 45 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-a-repository 46 type ActionsCacheUsageList struct { 47 TotalCount int `json:"total_count"` 48 RepoCacheUsage []*ActionsCacheUsage `json:"repository_cache_usages,omitempty"` 49 } 50 51 // TotalCacheUsage represents total GitHub actions cache usage of an organization or enterprise. 52 // 53 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-an-enterprise 54 type TotalCacheUsage struct { 55 TotalActiveCachesUsageSizeInBytes int64 `json:"total_active_caches_size_in_bytes"` 56 TotalActiveCachesCount int `json:"total_active_caches_count"` 57 } 58 59 // ActionsCacheListOptions represents a list of all possible optional Query parameters for ListCaches method. 60 // 61 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#list-github-actions-caches-for-a-repository 62 type ActionsCacheListOptions struct { 63 ListOptions 64 // The Git reference for the results you want to list. 65 // The ref for a branch can be formatted either as refs/heads/<branch name> 66 // or simply <branch name>. To reference a pull request use refs/pull/<number>/merge 67 Ref *string `url:"ref,omitempty"` 68 Key *string `url:"key,omitempty"` 69 // Can be one of: "created_at", "last_accessed_at", "size_in_bytes". Default: "last_accessed_at" 70 Sort *string `url:"sort,omitempty"` 71 // Can be one of: "asc", "desc" Default: desc 72 Direction *string `url:"direction,omitempty"` 73 } 74 75 // ListCaches lists the GitHub Actions caches for a repository. 76 // You must authenticate using an access token with the repo scope to use this endpoint. 77 // 78 // Permissions: must have the actions:read permission to use this endpoint. 79 // 80 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#list-github-actions-caches-for-a-repository 81 func (s *ActionsService) ListCaches(ctx context.Context, owner, repo string, opts *ActionsCacheListOptions) (*ActionsCacheList, *Response, error) { 82 u := fmt.Sprintf("repos/%v/%v/actions/caches", owner, repo) 83 u, err := addOptions(u, opts) 84 if err != nil { 85 return nil, nil, err 86 } 87 88 req, err := s.client.NewRequest("GET", u, nil) 89 if err != nil { 90 return nil, nil, err 91 } 92 93 actionCacheList := new(ActionsCacheList) 94 resp, err := s.client.Do(ctx, req, actionCacheList) 95 if err != nil { 96 return nil, resp, err 97 } 98 99 return actionCacheList, resp, nil 100 } 101 102 // DeleteCachesByKey deletes one or more GitHub Actions caches for a repository, using a complete cache key. 103 // By default, all caches that match the provided key are deleted, but you can optionally provide 104 // a Git ref to restrict deletions to caches that match both the provided key and the Git ref. 105 // The ref for a branch can be formatted either as "refs/heads/<branch name>" or simply "<branch name>". 106 // To reference a pull request use "refs/pull/<number>/merge". If you don't want to use ref just pass nil in parameter. 107 // 108 // Permissions: You must authenticate using an access token with the repo scope to use this endpoint. GitHub Apps must have the actions:write permission to use this endpoint. 109 // 110 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#delete-github-actions-caches-for-a-repository-using-a-cache-key 111 func (s *ActionsService) DeleteCachesByKey(ctx context.Context, owner, repo, key string, ref *string) (*Response, error) { 112 u := fmt.Sprintf("repos/%v/%v/actions/caches", owner, repo) 113 u, err := addOptions(u, ActionsCache{Key: &key, Ref: ref}) 114 if err != nil { 115 return nil, err 116 } 117 118 req, err := s.client.NewRequest("DELETE", u, nil) 119 if err != nil { 120 return nil, err 121 } 122 123 return s.client.Do(ctx, req, nil) 124 } 125 126 // DeleteCachesByID deletes a GitHub Actions cache for a repository, using a cache ID. 127 // 128 // Permissions: You must authenticate using an access token with the repo scope to use this endpoint. GitHub Apps must have the actions:write permission to use this endpoint. 129 // 130 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#delete-a-github-actions-cache-for-a-repository-using-a-cache-id 131 func (s *ActionsService) DeleteCachesByID(ctx context.Context, owner, repo string, cacheID int64) (*Response, error) { 132 u := fmt.Sprintf("repos/%v/%v/actions/caches/%v", owner, repo, cacheID) 133 req, err := s.client.NewRequest("DELETE", u, nil) 134 if err != nil { 135 return nil, err 136 } 137 138 return s.client.Do(ctx, req, nil) 139 } 140 141 // GetCacheUsageForRepo gets GitHub Actions cache usage for a repository. The data fetched using this API is refreshed approximately every 5 minutes, 142 // so values returned from this endpoint may take at least 5 minutes to get updated. 143 // 144 // Permissions: Anyone with read access to the repository can use this endpoint. If the repository is private, you must use an 145 // access token with the repo scope. GitHub Apps must have the actions:read permission to use this endpoint. 146 // 147 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-a-repository 148 func (s *ActionsService) GetCacheUsageForRepo(ctx context.Context, owner, repo string) (*ActionsCacheUsage, *Response, error) { 149 u := fmt.Sprintf("repos/%v/%v/actions/cache/usage", owner, repo) 150 req, err := s.client.NewRequest("GET", u, nil) 151 if err != nil { 152 return nil, nil, err 153 } 154 155 cacheUsage := new(ActionsCacheUsage) 156 res, err := s.client.Do(ctx, req, cacheUsage) 157 if err != nil { 158 return nil, res, err 159 } 160 161 return cacheUsage, res, err 162 } 163 164 // ListCacheUsageByRepoForOrg lists repositories and their GitHub Actions cache usage for an organization. The data fetched using this API is 165 // refreshed approximately every 5 minutes, so values returned from this endpoint may take at least 5 minutes to get updated. 166 // 167 // Permissions: You must authenticate using an access token with the read:org scope to use this endpoint. 168 // GitHub Apps must have the organization_admistration:read permission to use this endpoint. 169 // 170 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#list-repositories-with-github-actions-cache-usage-for-an-organization 171 func (s *ActionsService) ListCacheUsageByRepoForOrg(ctx context.Context, org string, opts *ListOptions) (*ActionsCacheUsageList, *Response, error) { 172 u := fmt.Sprintf("orgs/%v/actions/cache/usage-by-repository", org) 173 u, err := addOptions(u, opts) 174 if err != nil { 175 return nil, nil, err 176 } 177 178 req, err := s.client.NewRequest("GET", u, nil) 179 if err != nil { 180 return nil, nil, err 181 } 182 183 cacheUsage := new(ActionsCacheUsageList) 184 res, err := s.client.Do(ctx, req, cacheUsage) 185 if err != nil { 186 return nil, res, err 187 } 188 189 return cacheUsage, res, err 190 } 191 192 // GetTotalCacheUsageForOrg gets the total GitHub Actions cache usage for an organization. The data fetched using this API is refreshed approximately every 193 // 5 minutes, so values returned from this endpoint may take at least 5 minutes to get updated. 194 // 195 // Permissions: You must authenticate using an access token with the read:org scope to use this endpoint. 196 // GitHub Apps must have the organization_admistration:read permission to use this endpoint. 197 // 198 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-an-organization 199 func (s *ActionsService) GetTotalCacheUsageForOrg(ctx context.Context, org string) (*TotalCacheUsage, *Response, error) { 200 u := fmt.Sprintf("orgs/%v/actions/cache/usage", org) 201 req, err := s.client.NewRequest("GET", u, nil) 202 if err != nil { 203 return nil, nil, err 204 } 205 206 cacheUsage := new(TotalCacheUsage) 207 res, err := s.client.Do(ctx, req, cacheUsage) 208 if err != nil { 209 return nil, res, err 210 } 211 212 return cacheUsage, res, err 213 } 214 215 // GetTotalCacheUsageForEnterprise gets the total GitHub Actions cache usage for an enterprise. The data fetched using this API is refreshed approximately every 5 minutes, 216 // so values returned from this endpoint may take at least 5 minutes to get updated. 217 // 218 // Permissions: You must authenticate using an access token with the "admin:enterprise" scope to use this endpoint. 219 // 220 // GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-an-enterprise 221 func (s *ActionsService) GetTotalCacheUsageForEnterprise(ctx context.Context, enterprise string) (*TotalCacheUsage, *Response, error) { 222 u := fmt.Sprintf("enterprises/%v/actions/cache/usage", enterprise) 223 req, err := s.client.NewRequest("GET", u, nil) 224 if err != nil { 225 return nil, nil, err 226 } 227 228 cacheUsage := new(TotalCacheUsage) 229 res, err := s.client.Do(ctx, req, cacheUsage) 230 if err != nil { 231 return nil, res, err 232 } 233 234 return cacheUsage, res, err 235 } 236