diff --git a/github/actions_cache.go b/github/actions_cache.go index 41e777f96e2..d10e77dbd90 100644 --- a/github/actions_cache.go +++ b/github/actions_cache.go @@ -56,7 +56,7 @@ type TotalCacheUsage struct { TotalActiveCachesCount int `json:"total_active_caches_count"` } -// ActionsCacheRetentionLimit represents the GitHub Actions cache retention limit for an organization. +// ActionsCacheRetentionLimit represents the GitHub Actions cache retention limit for a repository, organization, or enterprise. // // GitHub API docs: https://docs.github.com/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-retention-limit-for-an-organization type ActionsCacheRetentionLimit struct { @@ -65,7 +65,7 @@ type ActionsCacheRetentionLimit struct { MaxCacheRetentionDays *int `json:"max_cache_retention_days,omitempty"` } -// ActionsCacheStorageLimit represents the GitHub Actions cache storage limit for an organization. +// ActionsCacheStorageLimit represents the GitHub Actions cache storage limit for a repository, organization, or enterprise. // // GitHub API docs: https://docs.github.com/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-storage-limit-for-an-organization type ActionsCacheStorageLimit struct { @@ -349,3 +349,171 @@ func (s *ActionsService) SetCacheStorageLimitForOrg(ctx context.Context, org str return s.client.Do(req, nil) } + +// GetCacheRetentionLimitForRepo gets the GitHub Actions cache retention limit for a repository. +// This determines how long caches will be retained for, if not manually removed or evicted due to size constraints. +// +// Permissions: OAuth tokens and personal access tokens (classic) need the admin:repository scope to use this endpoint. +// +// GitHub API docs: https://docs.github.com/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-retention-limit-for-a-repository +// +//meta:operation GET /repos/{owner}/{repo}/actions/cache/retention-limit +func (s *ActionsService) GetCacheRetentionLimitForRepo(ctx context.Context, owner, repo string) (*ActionsCacheRetentionLimit, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/actions/cache/retention-limit", owner, repo) + req, err := s.client.NewRequest(ctx, "GET", u, nil) + if err != nil { + return nil, nil, err + } + + var limit *ActionsCacheRetentionLimit + res, err := s.client.Do(req, &limit) + if err != nil { + return nil, res, err + } + + return limit, res, nil +} + +// SetCacheRetentionLimitForRepo sets the GitHub Actions cache retention limit for a repository. +// This determines how long caches will be retained for, if not manually removed or evicted due to size constraints. +// +// Permissions: OAuth tokens and personal access tokens (classic) need the admin:repository scope to use this endpoint. +// +// GitHub API docs: https://docs.github.com/rest/actions/cache?apiVersion=2022-11-28#set-github-actions-cache-retention-limit-for-a-repository +// +//meta:operation PUT /repos/{owner}/{repo}/actions/cache/retention-limit +func (s *ActionsService) SetCacheRetentionLimitForRepo(ctx context.Context, owner, repo string, body ActionsCacheRetentionLimit) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/actions/cache/retention-limit", owner, repo) + req, err := s.client.NewRequest(ctx, "PUT", u, body) + if err != nil { + return nil, err + } + + return s.client.Do(req, nil) +} + +// GetCacheStorageLimitForRepo gets the GitHub Actions cache storage limit for a repository. +// This determines the maximum size of caches that can be stored before eviction occurs. +// +// Permissions: OAuth tokens and personal access tokens (classic) need the admin:repository scope to use this endpoint. +// +// GitHub API docs: https://docs.github.com/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-storage-limit-for-a-repository +// +//meta:operation GET /repos/{owner}/{repo}/actions/cache/storage-limit +func (s *ActionsService) GetCacheStorageLimitForRepo(ctx context.Context, owner, repo string) (*ActionsCacheStorageLimit, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/actions/cache/storage-limit", owner, repo) + req, err := s.client.NewRequest(ctx, "GET", u, nil) + if err != nil { + return nil, nil, err + } + + var limit *ActionsCacheStorageLimit + res, err := s.client.Do(req, &limit) + if err != nil { + return nil, res, err + } + + return limit, res, nil +} + +// SetCacheStorageLimitForRepo sets the GitHub Actions cache storage limit for a repository. +// This determines the maximum size of caches that can be stored before eviction occurs. +// +// Permissions: OAuth tokens and personal access tokens (classic) need the admin:repository scope to use this endpoint. +// +// GitHub API docs: https://docs.github.com/rest/actions/cache?apiVersion=2022-11-28#set-github-actions-cache-storage-limit-for-a-repository +// +//meta:operation PUT /repos/{owner}/{repo}/actions/cache/storage-limit +func (s *ActionsService) SetCacheStorageLimitForRepo(ctx context.Context, owner, repo string, body ActionsCacheStorageLimit) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/actions/cache/storage-limit", owner, repo) + req, err := s.client.NewRequest(ctx, "PUT", u, body) + if err != nil { + return nil, err + } + + return s.client.Do(req, nil) +} + +// GetCacheRetentionLimitForEnterprise gets the GitHub Actions cache retention limit for an enterprise. +// Organizations and repositories in the enterprise may not set a higher cache retention limit. +// +// Permissions: OAuth tokens and personal access tokens (classic) need the admin:enterprise scope to use this endpoint. +// +// GitHub API docs: https://docs.github.com/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-retention-limit-for-an-enterprise +// +//meta:operation GET /enterprises/{enterprise}/actions/cache/retention-limit +func (s *ActionsService) GetCacheRetentionLimitForEnterprise(ctx context.Context, enterprise string) (*ActionsCacheRetentionLimit, *Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/cache/retention-limit", enterprise) + req, err := s.client.NewRequest(ctx, "GET", u, nil) + if err != nil { + return nil, nil, err + } + + var limit *ActionsCacheRetentionLimit + res, err := s.client.Do(req, &limit) + if err != nil { + return nil, res, err + } + + return limit, res, nil +} + +// SetCacheRetentionLimitForEnterprise sets the GitHub Actions cache retention limit for an enterprise. +// Organizations and repositories in the enterprise may not set a higher cache retention limit. +// +// Permissions: OAuth tokens and personal access tokens (classic) need the admin:enterprise scope to use this endpoint. +// +// GitHub API docs: https://docs.github.com/rest/actions/cache?apiVersion=2022-11-28#set-github-actions-cache-retention-limit-for-an-enterprise +// +//meta:operation PUT /enterprises/{enterprise}/actions/cache/retention-limit +func (s *ActionsService) SetCacheRetentionLimitForEnterprise(ctx context.Context, enterprise string, body ActionsCacheRetentionLimit) (*Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/cache/retention-limit", enterprise) + req, err := s.client.NewRequest(ctx, "PUT", u, body) + if err != nil { + return nil, err + } + + return s.client.Do(req, nil) +} + +// GetCacheStorageLimitForEnterprise gets the GitHub Actions cache storage limit for an enterprise. +// Organizations and repositories in the enterprise may not set a higher cache storage limit. +// +// Permissions: OAuth tokens and personal access tokens (classic) need the admin:enterprise scope to use this endpoint. +// +// GitHub API docs: https://docs.github.com/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-storage-limit-for-an-enterprise +// +//meta:operation GET /enterprises/{enterprise}/actions/cache/storage-limit +func (s *ActionsService) GetCacheStorageLimitForEnterprise(ctx context.Context, enterprise string) (*ActionsCacheStorageLimit, *Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/cache/storage-limit", enterprise) + req, err := s.client.NewRequest(ctx, "GET", u, nil) + if err != nil { + return nil, nil, err + } + + var limit *ActionsCacheStorageLimit + res, err := s.client.Do(req, &limit) + if err != nil { + return nil, res, err + } + + return limit, res, nil +} + +// SetCacheStorageLimitForEnterprise sets the GitHub Actions cache storage limit for an enterprise. +// Organizations and repositories in the enterprise may not set a higher cache storage limit. +// +// Permissions: OAuth tokens and personal access tokens (classic) need the admin:enterprise scope to use this endpoint. +// +// GitHub API docs: https://docs.github.com/rest/actions/cache?apiVersion=2022-11-28#set-github-actions-cache-storage-limit-for-an-enterprise +// +//meta:operation PUT /enterprises/{enterprise}/actions/cache/storage-limit +func (s *ActionsService) SetCacheStorageLimitForEnterprise(ctx context.Context, enterprise string, body ActionsCacheStorageLimit) (*Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/cache/storage-limit", enterprise) + req, err := s.client.NewRequest(ctx, "PUT", u, body) + if err != nil { + return nil, err + } + + return s.client.Do(req, nil) +} diff --git a/github/actions_cache_test.go b/github/actions_cache_test.go index 94909f07c7c..938d2e83bde 100644 --- a/github/actions_cache_test.go +++ b/github/actions_cache_test.go @@ -729,3 +729,271 @@ func TestActionsService_SetCacheStorageLimitForOrg_invalidOrganization(t *testin _, err := client.Actions.SetCacheStorageLimitForOrg(ctx, "%", ActionsCacheStorageLimit{}) testURLParseError(t, err) } + +func TestActionsService_GetCacheRetentionLimitForRepo(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + mux.HandleFunc("/repos/o/r/actions/cache/retention-limit", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"max_cache_retention_days":14}`) + }) + + ctx := t.Context() + limit, _, err := client.Actions.GetCacheRetentionLimitForRepo(ctx, "o", "r") + if err != nil { + t.Errorf("Actions.GetCacheRetentionLimitForRepo returned error: %v", err) + } + + want := &ActionsCacheRetentionLimit{MaxCacheRetentionDays: new(14)} + if !cmp.Equal(limit, want) { + t.Errorf("Actions.GetCacheRetentionLimitForRepo returned %+v, want %+v", limit, want) + } + + const methodName = "GetCacheRetentionLimitForRepo" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetCacheRetentionLimitForRepo(ctx, "\n", "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetCacheRetentionLimitForRepo(ctx, "o", "r") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_SetCacheRetentionLimitForRepo(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + input := ActionsCacheRetentionLimit{MaxCacheRetentionDays: new(14)} + + mux.HandleFunc("/repos/o/r/actions/cache/retention-limit", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + testJSONBody(t, r, input) + w.WriteHeader(http.StatusNoContent) + }) + + ctx := t.Context() + resp, err := client.Actions.SetCacheRetentionLimitForRepo(ctx, "o", "r", input) + if err != nil { + t.Errorf("Actions.SetCacheRetentionLimitForRepo returned error: %v", err) + } + if got, want := resp.StatusCode, http.StatusNoContent; got != want { + t.Errorf("Actions.SetCacheRetentionLimitForRepo return status %v, want %v", got, want) + } + + const methodName = "SetCacheRetentionLimitForRepo" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.SetCacheRetentionLimitForRepo(ctx, "\n", "\n", input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.SetCacheRetentionLimitForRepo(ctx, "o", "r", input) + }) +} + +func TestActionsService_GetCacheStorageLimitForRepo(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + mux.HandleFunc("/repos/o/r/actions/cache/storage-limit", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"max_cache_size_gb":10}`) + }) + + ctx := t.Context() + limit, _, err := client.Actions.GetCacheStorageLimitForRepo(ctx, "o", "r") + if err != nil { + t.Errorf("Actions.GetCacheStorageLimitForRepo returned error: %v", err) + } + + want := &ActionsCacheStorageLimit{MaxCacheSizeGB: new(10)} + if !cmp.Equal(limit, want) { + t.Errorf("Actions.GetCacheStorageLimitForRepo returned %+v, want %+v", limit, want) + } + + const methodName = "GetCacheStorageLimitForRepo" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetCacheStorageLimitForRepo(ctx, "\n", "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetCacheStorageLimitForRepo(ctx, "o", "r") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_SetCacheStorageLimitForRepo(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + input := ActionsCacheStorageLimit{MaxCacheSizeGB: new(10)} + + mux.HandleFunc("/repos/o/r/actions/cache/storage-limit", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + testJSONBody(t, r, input) + w.WriteHeader(http.StatusNoContent) + }) + + ctx := t.Context() + resp, err := client.Actions.SetCacheStorageLimitForRepo(ctx, "o", "r", input) + if err != nil { + t.Errorf("Actions.SetCacheStorageLimitForRepo returned error: %v", err) + } + if got, want := resp.StatusCode, http.StatusNoContent; got != want { + t.Errorf("Actions.SetCacheStorageLimitForRepo return status %v, want %v", got, want) + } + + const methodName = "SetCacheStorageLimitForRepo" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.SetCacheStorageLimitForRepo(ctx, "\n", "\n", input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.SetCacheStorageLimitForRepo(ctx, "o", "r", input) + }) +} + +func TestActionsService_GetCacheRetentionLimitForEnterprise(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + mux.HandleFunc("/enterprises/e/actions/cache/retention-limit", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"max_cache_retention_days":14}`) + }) + + ctx := t.Context() + limit, _, err := client.Actions.GetCacheRetentionLimitForEnterprise(ctx, "e") + if err != nil { + t.Errorf("Actions.GetCacheRetentionLimitForEnterprise returned error: %v", err) + } + + want := &ActionsCacheRetentionLimit{MaxCacheRetentionDays: new(14)} + if !cmp.Equal(limit, want) { + t.Errorf("Actions.GetCacheRetentionLimitForEnterprise returned %+v, want %+v", limit, want) + } + + const methodName = "GetCacheRetentionLimitForEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetCacheRetentionLimitForEnterprise(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetCacheRetentionLimitForEnterprise(ctx, "e") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_SetCacheRetentionLimitForEnterprise(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + input := ActionsCacheRetentionLimit{MaxCacheRetentionDays: new(14)} + + mux.HandleFunc("/enterprises/e/actions/cache/retention-limit", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + testJSONBody(t, r, input) + w.WriteHeader(http.StatusNoContent) + }) + + ctx := t.Context() + resp, err := client.Actions.SetCacheRetentionLimitForEnterprise(ctx, "e", input) + if err != nil { + t.Errorf("Actions.SetCacheRetentionLimitForEnterprise returned error: %v", err) + } + if got, want := resp.StatusCode, http.StatusNoContent; got != want { + t.Errorf("Actions.SetCacheRetentionLimitForEnterprise return status %v, want %v", got, want) + } + + const methodName = "SetCacheRetentionLimitForEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.SetCacheRetentionLimitForEnterprise(ctx, "\n", input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.SetCacheRetentionLimitForEnterprise(ctx, "e", input) + }) +} + +func TestActionsService_GetCacheStorageLimitForEnterprise(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + mux.HandleFunc("/enterprises/e/actions/cache/storage-limit", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"max_cache_size_gb":10}`) + }) + + ctx := t.Context() + limit, _, err := client.Actions.GetCacheStorageLimitForEnterprise(ctx, "e") + if err != nil { + t.Errorf("Actions.GetCacheStorageLimitForEnterprise returned error: %v", err) + } + + want := &ActionsCacheStorageLimit{MaxCacheSizeGB: new(10)} + if !cmp.Equal(limit, want) { + t.Errorf("Actions.GetCacheStorageLimitForEnterprise returned %+v, want %+v", limit, want) + } + + const methodName = "GetCacheStorageLimitForEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetCacheStorageLimitForEnterprise(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetCacheStorageLimitForEnterprise(ctx, "e") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_SetCacheStorageLimitForEnterprise(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + input := ActionsCacheStorageLimit{MaxCacheSizeGB: new(10)} + + mux.HandleFunc("/enterprises/e/actions/cache/storage-limit", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + testJSONBody(t, r, input) + w.WriteHeader(http.StatusNoContent) + }) + + ctx := t.Context() + resp, err := client.Actions.SetCacheStorageLimitForEnterprise(ctx, "e", input) + if err != nil { + t.Errorf("Actions.SetCacheStorageLimitForEnterprise returned error: %v", err) + } + if got, want := resp.StatusCode, http.StatusNoContent; got != want { + t.Errorf("Actions.SetCacheStorageLimitForEnterprise return status %v, want %v", got, want) + } + + const methodName = "SetCacheStorageLimitForEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.SetCacheStorageLimitForEnterprise(ctx, "\n", input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.SetCacheStorageLimitForEnterprise(ctx, "e", input) + }) +}