| 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/rest/actions/cache#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/rest/actions/cache#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/rest/actions/cache#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/rest/actions/cache#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/rest/actions/cache#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/rest/actions/cache#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/rest/actions/cache#list-github-actions-caches-for-a-repository |
| 81 |
// |
| 82 |
//meta:operation GET /repos/{owner}/{repo}/actions/caches |
| 83 |
func (s *ActionsService) ListCaches(ctx context.Context, owner, repo string, opts *ActionsCacheListOptions) (*ActionsCacheList, *Response, error) { |
| 84 |
u := fmt.Sprintf("repos/%v/%v/actions/caches", owner, repo) |
| 85 |
u, err := addOptions(u, opts) |
| 86 |
if err != nil { |
| 87 |
return nil, nil, err |
| 88 |
} |
| 89 |
|
| 90 |
req, err := s.client.NewRequest("GET", u, nil) |
| 91 |
if err != nil { |
| 92 |
return nil, nil, err |
| 93 |
} |
| 94 |
|
| 95 |
actionCacheList := new(ActionsCacheList) |
| 96 |
resp, err := s.client.Do(ctx, req, actionCacheList) |
| 97 |
if err != nil { |
| 98 |
return nil, resp, err |
| 99 |
} |
| 100 |
|
| 101 |
return actionCacheList, resp, nil |
| 102 |
} |
| 103 |
|
| 104 |
// DeleteCachesByKey deletes one or more GitHub Actions caches for a repository, using a complete cache key. |
| 105 |
// By default, all caches that match the provided key are deleted, but you can optionally provide |
| 106 |
// a Git ref to restrict deletions to caches that match both the provided key and the Git ref. |
| 107 |
// The ref for a branch can be formatted either as "refs/heads/<branch name>" or simply "<branch name>". |
| 108 |
// To reference a pull request use "refs/pull/<number>/merge". If you don't want to use ref just pass nil in parameter. |
| 109 |
// |
| 110 |
// 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. |
| 111 |
// |
| 112 |
// GitHub API docs: https://docs.github.com/rest/actions/cache#delete-github-actions-caches-for-a-repository-using-a-cache-key |
| 113 |
// |
| 114 |
//meta:operation DELETE /repos/{owner}/{repo}/actions/caches |
| 115 |
func (s *ActionsService) DeleteCachesByKey(ctx context.Context, owner, repo, key string, ref *string) (*Response, error) { |
| 116 |
u := fmt.Sprintf("repos/%v/%v/actions/caches", owner, repo) |
| 117 |
u, err := addOptions(u, ActionsCache{Key: &key, Ref: ref}) |
| 118 |
if err != nil { |
| 119 |
return nil, err |
| 120 |
} |
| 121 |
|
| 122 |
req, err := s.client.NewRequest("DELETE", u, nil) |
| 123 |
if err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
|
| 127 |
return s.client.Do(ctx, req, nil) |
| 128 |
} |
| 129 |
|
| 130 |
// DeleteCachesByID deletes a GitHub Actions cache for a repository, using a cache ID. |
| 131 |
// |
| 132 |
// 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. |
| 133 |
// |
| 134 |
// GitHub API docs: https://docs.github.com/rest/actions/cache#delete-a-github-actions-cache-for-a-repository-using-a-cache-id |
| 135 |
// |
| 136 |
//meta:operation DELETE /repos/{owner}/{repo}/actions/caches/{cache_id} |
| 137 |
func (s *ActionsService) DeleteCachesByID(ctx context.Context, owner, repo string, cacheID int64) (*Response, error) { |
| 138 |
u := fmt.Sprintf("repos/%v/%v/actions/caches/%v", owner, repo, cacheID) |
| 139 |
req, err := s.client.NewRequest("DELETE", u, nil) |
| 140 |
if err != nil { |
| 141 |
return nil, err |
| 142 |
} |
| 143 |
|
| 144 |
return s.client.Do(ctx, req, nil) |
| 145 |
} |
| 146 |
|
| 147 |
// GetCacheUsageForRepo gets GitHub Actions cache usage for a repository. The data fetched using this API is refreshed approximately every 5 minutes, |
| 148 |
// so values returned from this endpoint may take at least 5 minutes to get updated. |
| 149 |
// |
| 150 |
// Permissions: Anyone with read access to the repository can use this endpoint. If the repository is private, you must use an |
| 151 |
// access token with the repo scope. GitHub Apps must have the actions:read permission to use this endpoint. |
| 152 |
// |
| 153 |
// GitHub API docs: https://docs.github.com/rest/actions/cache#get-github-actions-cache-usage-for-a-repository |
| 154 |
// |
| 155 |
//meta:operation GET /repos/{owner}/{repo}/actions/cache/usage |
| 156 |
func (s *ActionsService) GetCacheUsageForRepo(ctx context.Context, owner, repo string) (*ActionsCacheUsage, *Response, error) { |
| 157 |
u := fmt.Sprintf("repos/%v/%v/actions/cache/usage", owner, repo) |
| 158 |
req, err := s.client.NewRequest("GET", u, nil) |
| 159 |
if err != nil { |
| 160 |
return nil, nil, err |
| 161 |
} |
| 162 |
|
| 163 |
cacheUsage := new(ActionsCacheUsage) |
| 164 |
res, err := s.client.Do(ctx, req, cacheUsage) |
| 165 |
if err != nil { |
| 166 |
return nil, res, err |
| 167 |
} |
| 168 |
|
| 169 |
return cacheUsage, res, err |
| 170 |
} |
| 171 |
|
| 172 |
// ListCacheUsageByRepoForOrg lists repositories and their GitHub Actions cache usage for an organization. The data fetched using this API is |
| 173 |
// refreshed approximately every 5 minutes, so values returned from this endpoint may take at least 5 minutes to get updated. |
| 174 |
// |
| 175 |
// Permissions: You must authenticate using an access token with the read:org scope to use this endpoint. |
| 176 |
// GitHub Apps must have the organization_admistration:read permission to use this endpoint. |
| 177 |
// |
| 178 |
// GitHub API docs: https://docs.github.com/rest/actions/cache#list-repositories-with-github-actions-cache-usage-for-an-organization |
| 179 |
// |
| 180 |
//meta:operation GET /orgs/{org}/actions/cache/usage-by-repository |
| 181 |
func (s *ActionsService) ListCacheUsageByRepoForOrg(ctx context.Context, org string, opts *ListOptions) (*ActionsCacheUsageList, *Response, error) { |
| 182 |
u := fmt.Sprintf("orgs/%v/actions/cache/usage-by-repository", org) |
| 183 |
u, err := addOptions(u, opts) |
| 184 |
if err != nil { |
| 185 |
return nil, nil, err |
| 186 |
} |
| 187 |
|
| 188 |
req, err := s.client.NewRequest("GET", u, nil) |
| 189 |
if err != nil { |
| 190 |
return nil, nil, err |
| 191 |
} |
| 192 |
|
| 193 |
cacheUsage := new(ActionsCacheUsageList) |
| 194 |
res, err := s.client.Do(ctx, req, cacheUsage) |
| 195 |
if err != nil { |
| 196 |
return nil, res, err |
| 197 |
} |
| 198 |
|
| 199 |
return cacheUsage, res, err |
| 200 |
} |
| 201 |
|
| 202 |
// GetTotalCacheUsageForOrg gets the total GitHub Actions cache usage for an organization. The data fetched using this API is refreshed approximately every |
| 203 |
// 5 minutes, so values returned from this endpoint may take at least 5 minutes to get updated. |
| 204 |
// |
| 205 |
// Permissions: You must authenticate using an access token with the read:org scope to use this endpoint. |
| 206 |
// GitHub Apps must have the organization_admistration:read permission to use this endpoint. |
| 207 |
// |
| 208 |
// GitHub API docs: https://docs.github.com/rest/actions/cache#get-github-actions-cache-usage-for-an-organization |
| 209 |
// |
| 210 |
//meta:operation GET /orgs/{org}/actions/cache/usage |
| 211 |
func (s *ActionsService) GetTotalCacheUsageForOrg(ctx context.Context, org string) (*TotalCacheUsage, *Response, error) { |
| 212 |
u := fmt.Sprintf("orgs/%v/actions/cache/usage", org) |
| 213 |
req, err := s.client.NewRequest("GET", u, nil) |
| 214 |
if err != nil { |
| 215 |
return nil, nil, err |
| 216 |
} |
| 217 |
|
| 218 |
cacheUsage := new(TotalCacheUsage) |
| 219 |
res, err := s.client.Do(ctx, req, cacheUsage) |
| 220 |
if err != nil { |
| 221 |
return nil, res, err |
| 222 |
} |
| 223 |
|
| 224 |
return cacheUsage, res, err |
| 225 |
} |
| 226 |
|
| 227 |
// GetTotalCacheUsageForEnterprise gets the total GitHub Actions cache usage for an enterprise. The data fetched using this API is refreshed approximately every 5 minutes, |
| 228 |
// so values returned from this endpoint may take at least 5 minutes to get updated. |
| 229 |
// |
| 230 |
// Permissions: You must authenticate using an access token with the "admin:enterprise" scope to use this endpoint. |
| 231 |
// |
| 232 |
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/actions/cache#get-github-actions-cache-usage-for-an-enterprise |
| 233 |
// |
| 234 |
//meta:operation GET /enterprises/{enterprise}/actions/cache/usage |
| 235 |
func (s *ActionsService) GetTotalCacheUsageForEnterprise(ctx context.Context, enterprise string) (*TotalCacheUsage, *Response, error) { |
| 236 |
u := fmt.Sprintf("enterprises/%v/actions/cache/usage", enterprise) |
| 237 |
req, err := s.client.NewRequest("GET", u, nil) |
| 238 |
if err != nil { |
| 239 |
return nil, nil, err |
| 240 |
} |
| 241 |
|
| 242 |
cacheUsage := new(TotalCacheUsage) |
| 243 |
res, err := s.client.Do(ctx, req, cacheUsage) |
| 244 |
if err != nil { |
| 245 |
return nil, res, err |
| 246 |
} |
| 247 |
|
| 248 |
return cacheUsage, res, err |
| 249 |
} |