diff --git a/github/actions_cache.go b/github/actions_cache.go new file mode 100644 index 0000000000..9592d9ab62 --- /dev/null +++ b/github/actions_cache.go @@ -0,0 +1,235 @@ +// Copyright 2022 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "fmt" +) + +// ActionsCache represents a GitHub action cache. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#about-the-cache-api +type ActionsCache struct { + ID *int64 `json:"id,omitempty" url:"-"` + Ref *string `json:"ref,omitempty" url:"ref"` + Key *string `json:"key,omitempty" url:"key"` + Version *string `json:"version,omitempty" url:"-"` + LastAccessedAt *Timestamp `json:"last_accessed_at,omitempty" url:"-"` + CreatedAt *Timestamp `json:"created_at,omitempty" url:"-"` + SizeInBytes *int64 `json:"size_in_bytes,omitempty" url:"-"` +} + +// ActionsCacheList represents a list of GitHub actions Cache. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#list-github-actions-caches-for-a-repository +type ActionsCacheList struct { + TotalCount int `json:"total_count"` + ActionsCaches []*ActionsCache `json:"actions_caches,omitempty"` +} + +// ActionsCacheUsage represents a GitHub Actions Cache Usage object. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-a-repository +type ActionsCacheUsage struct { + FullName string `json:"full_name"` + ActiveCachesSizeInBytes int64 `json:"active_caches_size_in_bytes"` + ActiveCachesCount int `json:"active_caches_count"` +} + +// ActionsCacheUsageList represents a list of repositories with GitHub Actions cache usage for an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-a-repository +type ActionsCacheUsageList struct { + TotalCount int `json:"total_count"` + RepoCacheUsage []*ActionsCacheUsage `json:"repository_cache_usages,omitempty"` +} + +// TotalCacheUsage represents total GitHub actions cache usage of an organization or enterprise. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-an-enterprise +type TotalCacheUsage struct { + TotalActiveCachesUsageSizeInBytes int64 `json:"total_active_caches_size_in_bytes"` + TotalActiveCachesCount int `json:"total_active_caches_count"` +} + +// ActionsCacheListOptions represents a list of all possible optional Query parameters for ListCaches method. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#list-github-actions-caches-for-a-repository +type ActionsCacheListOptions struct { + ListOptions + // The Git reference for the results you want to list. + // The ref for a branch can be formatted either as refs/heads/ + // or simply . To reference a pull request use refs/pull//merge + Ref *string `url:"ref,omitempty"` + Key *string `url:"key,omitempty"` + // Can be one of: "created_at", "last_accessed_at", "size_in_bytes". Default: "last_accessed_at" + Sort *string `url:"sort,omitempty"` + // Can be one of: "asc", "desc" Default: desc + Direction *string `url:"direction,omitempty"` +} + +// ListCaches lists the GitHub Actions caches for a repository. +// You must authenticate using an access token with the repo scope to use this endpoint. +// +// Permissions: must have the actions:read permission to use this endpoint. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#list-github-actions-caches-for-a-repository +func (s *ActionsService) ListCaches(ctx context.Context, owner, repo string, opts *ActionsCacheListOptions) (*ActionsCacheList, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/actions/caches", owner, repo) + u, err := addOptions(u, opts) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + actionCacheList := new(ActionsCacheList) + resp, err := s.client.Do(ctx, req, actionCacheList) + if err != nil { + return nil, resp, err + } + + return actionCacheList, resp, nil +} + +// DeleteCachesByKey deletes one or more GitHub Actions caches for a repository, using a complete cache key. +// By default, all caches that match the provided key are deleted, but you can optionally provide +// a Git ref to restrict deletions to caches that match both the provided key and the Git ref. +// The ref for a branch can be formatted either as "refs/heads/" or simply "". +// To reference a pull request use "refs/pull//merge". If you don't want to use ref just pass nil in parameter. +// +// 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. +// +// 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 +func (s *ActionsService) DeleteCachesByKey(ctx context.Context, owner, repo, key string, ref *string) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/actions/caches", owner, repo) + u, err := addOptions(u, ActionsCache{Key: &key, Ref: ref}) + if err != nil { + return nil, err + } + + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// DeleteCachesByID deletes a GitHub Actions cache for a repository, using a cache ID. +// +// 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. +// +// 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 +func (s *ActionsService) DeleteCachesByID(ctx context.Context, owner, repo string, cacheID int64) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/actions/caches/%v", owner, repo, cacheID) + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// GetCacheUsageForRepo gets GitHub Actions cache usage for a repository. The data fetched using this API is refreshed approximately every 5 minutes, +// so values returned from this endpoint may take at least 5 minutes to get updated. +// +// Permissions: Anyone with read access to the repository can use this endpoint. If the repository is private, you must use an +// access token with the repo scope. GitHub Apps must have the actions:read permission to use this endpoint. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-a-repository +func (s *ActionsService) GetCacheUsageForRepo(ctx context.Context, owner, repo string) (*ActionsCacheUsage, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/actions/cache/usage", owner, repo) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + cacheUsage := new(ActionsCacheUsage) + res, err := s.client.Do(ctx, req, cacheUsage) + if err != nil { + return nil, res, err + } + + return cacheUsage, res, err +} + +// ListCacheUsageByRepoForOrg lists repositories and their GitHub Actions cache usage for an organization. The data fetched using this API is +// refreshed approximately every 5 minutes, so values returned from this endpoint may take at least 5 minutes to get updated. +// +// Permissions: You must authenticate using an access token with the read:org scope to use this endpoint. +// GitHub Apps must have the organization_admistration:read permission to use this endpoint. +// +// 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 +func (s *ActionsService) ListCacheUsageByRepoForOrg(ctx context.Context, org string, opts *ListOptions) (*ActionsCacheUsageList, *Response, error) { + u := fmt.Sprintf("orgs/%v/actions/cache/usage-by-repository", org) + u, err := addOptions(u, opts) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + cacheUsage := new(ActionsCacheUsageList) + res, err := s.client.Do(ctx, req, cacheUsage) + if err != nil { + return nil, res, err + } + + return cacheUsage, res, err +} + +// GetTotalCacheUsageForOrg gets the total GitHub Actions cache usage for an organization. The data fetched using this API is refreshed approximately every +// 5 minutes, so values returned from this endpoint may take at least 5 minutes to get updated. +// +// Permissions: You must authenticate using an access token with the read:org scope to use this endpoint. +// GitHub Apps must have the organization_admistration:read permission to use this endpoint. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-an-organization +func (s *ActionsService) GetTotalCacheUsageForOrg(ctx context.Context, org string) (*TotalCacheUsage, *Response, error) { + u := fmt.Sprintf("orgs/%v/actions/cache/usage", org) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + cacheUsage := new(TotalCacheUsage) + res, err := s.client.Do(ctx, req, cacheUsage) + if err != nil { + return nil, res, err + } + + return cacheUsage, res, err +} + +// GetTotalCacheUsageForEnterprise gets the total GitHub Actions cache usage for an enterprise. The data fetched using this API is refreshed approximately every 5 minutes, +// so values returned from this endpoint may take at least 5 minutes to get updated. +// +// Permissions: You must authenticate using an access token with the "admin:enterprise" scope to use this endpoint. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/cache?apiVersion=2022-11-28#get-github-actions-cache-usage-for-an-enterprise +func (s *ActionsService) GetTotalCacheUsageForEnterprise(ctx context.Context, enterprise string) (*TotalCacheUsage, *Response, error) { + u := fmt.Sprintf("enterprises/%v/actions/cache/usage", enterprise) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + cacheUsage := new(TotalCacheUsage) + res, err := s.client.Do(ctx, req, cacheUsage) + if err != nil { + return nil, res, err + } + + return cacheUsage, res, err +} diff --git a/github/actions_cache_test.go b/github/actions_cache_test.go new file mode 100644 index 0000000000..b72d5cb828 --- /dev/null +++ b/github/actions_cache_test.go @@ -0,0 +1,517 @@ +// Copyright 2022 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestActionsService_ListCaches(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/caches", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"page": "2"}) + fmt.Fprint(w, + `{ + "total_count":1, + "actions_caches":[{"id":1}] + }`, + ) + }) + + opts := &ActionsCacheListOptions{ListOptions: ListOptions{Page: 2}} + ctx := context.Background() + cacheList, _, err := client.Actions.ListCaches(ctx, "o", "r", opts) + if err != nil { + t.Errorf("Actions.ListCaches returned error: %v", err) + } + + want := &ActionsCacheList{TotalCount: 1, ActionsCaches: []*ActionsCache{{ID: Int64(1)}}} + if !cmp.Equal(cacheList, want) { + t.Errorf("Actions.ListCaches returned %+v, want %+v", cacheList, want) + } + + const methodName = "ListCaches" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.ListCaches(ctx, "\n", "\n", opts) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.ListCaches(ctx, "o", "r", opts) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_ListCaches_invalidOwner(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, _, err := client.Actions.ListCaches(ctx, "%", "r", nil) + testURLParseError(t, err) +} + +func TestActionsService_ListCaches_invalidRepo(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, _, err := client.Actions.ListCaches(ctx, "o", "%", nil) + testURLParseError(t, err) +} + +func TestActionsService_ListCaches_notFound(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/caches", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.WriteHeader(http.StatusNotFound) + }) + + ctx := context.Background() + caches, resp, err := client.Actions.ListCaches(ctx, "o", "r", nil) + if err == nil { + t.Errorf("Expected HTTP 404 response") + } + if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { + t.Errorf("Actions.ListCaches return status %d, want %d", got, want) + } + if caches != nil { + t.Errorf("Actions.ListCaches return %+v, want nil", caches) + } +} + +func TestActionsService_DeleteCachesByKey(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/caches", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + testFormValues(t, r, values{"key": "1", "ref": "main"}) + }) + + ctx := context.Background() + _, err := client.Actions.DeleteCachesByKey(ctx, "o", "r", "1", String("main")) + if err != nil { + t.Errorf("Actions.DeleteCachesByKey return error: %v", err) + } + + const methodName = "DeleteCachesByKey" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.DeleteCachesByKey(ctx, "\n", "\n", "\n", String("\n")) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.DeleteCachesByKey(ctx, "o", "r", "1", String("main")) + }) +} + +func TestActionsService_DeleteCachesByKey_invalidOwner(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, err := client.Actions.DeleteCachesByKey(ctx, "%", "r", "1", String("main")) + testURLParseError(t, err) +} + +func TestActionsService_DeleteCachesByKey_invalidRepo(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, err := client.Actions.DeleteCachesByKey(ctx, "o", "%", "1", String("main")) + testURLParseError(t, err) +} +func TestActionsService_DeleteCachesByKey_notFound(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/artifacts/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.WriteHeader(http.StatusNotFound) + }) + + ctx := context.Background() + resp, err := client.Actions.DeleteCachesByKey(ctx, "o", "r", "1", String("main")) + if err == nil { + t.Errorf("Expected HTTP 404 response") + } + if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { + t.Errorf("Actions.DeleteCachesByKey return status %d, want %d", got, want) + } +} + +func TestActionsService_DeleteCachesByID(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/caches/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + }) + + ctx := context.Background() + _, err := client.Actions.DeleteCachesByID(ctx, "o", "r", 1) + if err != nil { + t.Errorf("Actions.DeleteCachesByID return error: %v", err) + } + + const methodName = "DeleteCachesByID" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.DeleteCachesByID(ctx, "\n", "\n", 0) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.DeleteCachesByID(ctx, "o", "r", 1) + }) +} + +func TestActionsService_DeleteCachesByID_invalidOwner(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, err := client.Actions.DeleteCachesByID(ctx, "%", "r", 1) + testURLParseError(t, err) +} + +func TestActionsService_DeleteCachesByID_invalidRepo(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, err := client.Actions.DeleteCachesByID(ctx, "o", "%", 1) + testURLParseError(t, err) +} + +func TestActionsService_DeleteCachesByID_notFound(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("repos/o/r/actions/caches/1", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + w.WriteHeader(http.StatusNotFound) + }) + + ctx := context.Background() + resp, err := client.Actions.DeleteCachesByID(ctx, "o", "r", 1) + if err == nil { + t.Errorf("Expected HTTP 404 response") + } + if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { + t.Errorf("Actions.DeleteCachesByID return status %d, want %d", got, want) + } +} + +func TestActionsService_GetCacheUsageForRepo(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, + `{ + "full_name":"test-cache", + "active_caches_size_in_bytes":1000, + "active_caches_count":1 + }`, + ) + }) + + ctx := context.Background() + cacheUse, _, err := client.Actions.GetCacheUsageForRepo(ctx, "o", "r") + if err != nil { + t.Errorf("Actions.GetCacheUsageForRepo returned error: %v", err) + } + + want := &ActionsCacheUsage{FullName: "test-cache", ActiveCachesSizeInBytes: 1000, ActiveCachesCount: 1} + if !cmp.Equal(cacheUse, want) { + t.Errorf("Actions.GetCacheUsageForRepo returned %+v, want %+v", cacheUse, want) + } + + const methodName = "GetCacheUsageForRepo" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetCacheUsageForRepo(ctx, "\n", "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetCacheUsageForRepo(ctx, "o", "r") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_GetCacheUsageForRepo_invalidOwner(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, _, err := client.Actions.GetCacheUsageForRepo(ctx, "%", "r") + testURLParseError(t, err) +} + +func TestActionsService_GetCacheUsageForRepo_invalidRepo(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, _, err := client.Actions.GetCacheUsageForRepo(ctx, "o", "%") + testURLParseError(t, err) +} + +func TestActionsService_GetCacheUsageForRepo_notFound(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.WriteHeader(http.StatusNotFound) + }) + + ctx := context.Background() + caches, resp, err := client.Actions.GetCacheUsageForRepo(ctx, "o", "r") + if err == nil { + t.Errorf("Expected HTTP 404 response") + } + if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { + t.Errorf("Actions.GetCacheUsageForRepo return status %d, want %d", got, want) + } + if caches != nil { + t.Errorf("Actions.GetCacheUsageForRepo return %+v, want nil", caches) + } +} + +func TestActionsService_ListCacheUsageByRepoForOrg(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/cache/usage-by-repository", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"page": "2", "per_page": "1"}) + fmt.Fprint(w, + `{ + "total_count":1, + "repository_cache_usages":[{"full_name":"test-cache","active_caches_size_in_bytes":1000,"active_caches_count":1}] + }`, + ) + }) + + opts := &ListOptions{PerPage: 1, Page: 2} + ctx := context.Background() + cacheList, _, err := client.Actions.ListCacheUsageByRepoForOrg(ctx, "o", opts) + if err != nil { + t.Errorf("Actions.ListCacheUsageByRepoForOrg returned error: %v", err) + } + + want := &ActionsCacheUsageList{TotalCount: 1, RepoCacheUsage: []*ActionsCacheUsage{{FullName: "test-cache", ActiveCachesSizeInBytes: 1000, ActiveCachesCount: 1}}} + if !cmp.Equal(cacheList, want) { + t.Errorf("Actions.ListCacheUsageByRepoForOrg returned %+v, want %+v", cacheList, want) + } + + const methodName = "ListCacheUsageByRepoForOrg" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.ListCacheUsageByRepoForOrg(ctx, "\n", opts) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.ListCacheUsageByRepoForOrg(ctx, "o", opts) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_ListCacheUsageByRepoForOrg_invalidOrganization(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, _, err := client.Actions.ListCacheUsageByRepoForOrg(ctx, "%", nil) + testURLParseError(t, err) +} + +func TestActionsService_ListCacheUsageByRepoForOrg_notFound(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/cache/usage-by-repository", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.WriteHeader(http.StatusNotFound) + }) + + ctx := context.Background() + caches, resp, err := client.Actions.ListCacheUsageByRepoForOrg(ctx, "o", nil) + if err == nil { + t.Errorf("Expected HTTP 404 response") + } + if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { + t.Errorf("Actions.ListCacheUsageByRepoForOrg return status %d, want %d", got, want) + } + if caches != nil { + t.Errorf("Actions.ListCacheUsageByRepoForOrg return %+v, want nil", caches) + } +} + +func TestActionsService_GetCacheUsageForOrg(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, + `{ + "total_active_caches_size_in_bytes":1000, + "total_active_caches_count":1 + }`, + ) + }) + + ctx := context.Background() + cache, _, err := client.Actions.GetTotalCacheUsageForOrg(ctx, "o") + if err != nil { + t.Errorf("Actions.GetTotalCacheUsageForOrg returned error: %v", err) + } + + want := &TotalCacheUsage{TotalActiveCachesUsageSizeInBytes: 1000, TotalActiveCachesCount: 1} + if !cmp.Equal(cache, want) { + t.Errorf("Actions.GetTotalCacheUsageForOrg returned %+v, want %+v", cache, want) + } + + const methodName = "GetTotalCacheUsageForOrg" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetTotalCacheUsageForOrg(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetTotalCacheUsageForOrg(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_GetCacheUsageForOrg_invalidOrganization(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, _, err := client.Actions.GetTotalCacheUsageForOrg(ctx, "%") + testURLParseError(t, err) +} + +func TestActionsService_GetCacheUsageForOrg_notFound(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.WriteHeader(http.StatusNotFound) + }) + + ctx := context.Background() + caches, resp, err := client.Actions.GetTotalCacheUsageForOrg(ctx, "o") + if err == nil { + t.Errorf("Expected HTTP 404 response") + } + if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { + t.Errorf("Actions.GetTotalCacheUsageForOrg return status %d, want %d", got, want) + } + if caches != nil { + t.Errorf("Actions.GetTotalCacheUsageForOrg return %+v, want nil", caches) + } +} + +func TestActionsService_GetCacheUsageForEnterprise(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/enterprises/e/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, + `{ + "total_active_caches_size_in_bytes":1000, + "total_active_caches_count":1 + }`, + ) + }) + + ctx := context.Background() + cache, _, err := client.Actions.GetTotalCacheUsageForEnterprise(ctx, "e") + if err != nil { + t.Errorf("Actions.GetTotalCacheUsageForEnterprise returned error: %v", err) + } + + want := &TotalCacheUsage{TotalActiveCachesUsageSizeInBytes: 1000, TotalActiveCachesCount: 1} + if !cmp.Equal(cache, want) { + t.Errorf("Actions.GetTotalCacheUsageForEnterprise returned %+v, want %+v", cache, want) + } + + const methodName = "GetTotalCacheUsageForEnterprise" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetTotalCacheUsageForEnterprise(ctx, "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetTotalCacheUsageForEnterprise(ctx, "e") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_GetCacheUsageForEnterprise_invalidEnterprise(t *testing.T) { + client, _, _, teardown := setup() + defer teardown() + + ctx := context.Background() + _, _, err := client.Actions.GetTotalCacheUsageForEnterprise(ctx, "%") + testURLParseError(t, err) +} + +func TestActionsService_GetCacheUsageForEnterprise_notFound(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/enterprises/e/actions/cache/usage", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + w.WriteHeader(http.StatusNotFound) + }) + + ctx := context.Background() + caches, resp, err := client.Actions.GetTotalCacheUsageForEnterprise(ctx, "o") + if err == nil { + t.Errorf("Expected HTTP 404 response") + } + if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want { + t.Errorf("Actions.GetTotalCacheUsageForEnterprise return status %d, want %d", got, want) + } + if caches != nil { + t.Errorf("Actions.GetTotalCacheUsageForEnterprise return %+v, want nil", caches) + } +} diff --git a/github/github-accessors.go b/github/github-accessors.go index 422d8bc0d3..9ed960fb77 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -38,6 +38,94 @@ func (a *ActionsAllowed) GetVerifiedAllowed() bool { return *a.VerifiedAllowed } +// GetCreatedAt returns the CreatedAt field if it's non-nil, zero value otherwise. +func (a *ActionsCache) GetCreatedAt() Timestamp { + if a == nil || a.CreatedAt == nil { + return Timestamp{} + } + return *a.CreatedAt +} + +// GetID returns the ID field if it's non-nil, zero value otherwise. +func (a *ActionsCache) GetID() int64 { + if a == nil || a.ID == nil { + return 0 + } + return *a.ID +} + +// GetKey returns the Key field if it's non-nil, zero value otherwise. +func (a *ActionsCache) GetKey() string { + if a == nil || a.Key == nil { + return "" + } + return *a.Key +} + +// GetLastAccessedAt returns the LastAccessedAt field if it's non-nil, zero value otherwise. +func (a *ActionsCache) GetLastAccessedAt() Timestamp { + if a == nil || a.LastAccessedAt == nil { + return Timestamp{} + } + return *a.LastAccessedAt +} + +// GetRef returns the Ref field if it's non-nil, zero value otherwise. +func (a *ActionsCache) GetRef() string { + if a == nil || a.Ref == nil { + return "" + } + return *a.Ref +} + +// GetSizeInBytes returns the SizeInBytes field if it's non-nil, zero value otherwise. +func (a *ActionsCache) GetSizeInBytes() int64 { + if a == nil || a.SizeInBytes == nil { + return 0 + } + return *a.SizeInBytes +} + +// GetVersion returns the Version field if it's non-nil, zero value otherwise. +func (a *ActionsCache) GetVersion() string { + if a == nil || a.Version == nil { + return "" + } + return *a.Version +} + +// GetDirection returns the Direction field if it's non-nil, zero value otherwise. +func (a *ActionsCacheListOptions) GetDirection() string { + if a == nil || a.Direction == nil { + return "" + } + return *a.Direction +} + +// GetKey returns the Key field if it's non-nil, zero value otherwise. +func (a *ActionsCacheListOptions) GetKey() string { + if a == nil || a.Key == nil { + return "" + } + return *a.Key +} + +// GetRef returns the Ref field if it's non-nil, zero value otherwise. +func (a *ActionsCacheListOptions) GetRef() string { + if a == nil || a.Ref == nil { + return "" + } + return *a.Ref +} + +// GetSort returns the Sort field if it's non-nil, zero value otherwise. +func (a *ActionsCacheListOptions) GetSort() string { + if a == nil || a.Sort == nil { + return "" + } + return *a.Sort +} + // GetAllowedActions returns the AllowedActions field if it's non-nil, zero value otherwise. func (a *ActionsPermissions) GetAllowedActions() string { if a == nil || a.AllowedActions == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index efa66e7875..17d8504ae7 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -45,6 +45,116 @@ func TestActionsAllowed_GetVerifiedAllowed(tt *testing.T) { a.GetVerifiedAllowed() } +func TestActionsCache_GetCreatedAt(tt *testing.T) { + var zeroValue Timestamp + a := &ActionsCache{CreatedAt: &zeroValue} + a.GetCreatedAt() + a = &ActionsCache{} + a.GetCreatedAt() + a = nil + a.GetCreatedAt() +} + +func TestActionsCache_GetID(tt *testing.T) { + var zeroValue int64 + a := &ActionsCache{ID: &zeroValue} + a.GetID() + a = &ActionsCache{} + a.GetID() + a = nil + a.GetID() +} + +func TestActionsCache_GetKey(tt *testing.T) { + var zeroValue string + a := &ActionsCache{Key: &zeroValue} + a.GetKey() + a = &ActionsCache{} + a.GetKey() + a = nil + a.GetKey() +} + +func TestActionsCache_GetLastAccessedAt(tt *testing.T) { + var zeroValue Timestamp + a := &ActionsCache{LastAccessedAt: &zeroValue} + a.GetLastAccessedAt() + a = &ActionsCache{} + a.GetLastAccessedAt() + a = nil + a.GetLastAccessedAt() +} + +func TestActionsCache_GetRef(tt *testing.T) { + var zeroValue string + a := &ActionsCache{Ref: &zeroValue} + a.GetRef() + a = &ActionsCache{} + a.GetRef() + a = nil + a.GetRef() +} + +func TestActionsCache_GetSizeInBytes(tt *testing.T) { + var zeroValue int64 + a := &ActionsCache{SizeInBytes: &zeroValue} + a.GetSizeInBytes() + a = &ActionsCache{} + a.GetSizeInBytes() + a = nil + a.GetSizeInBytes() +} + +func TestActionsCache_GetVersion(tt *testing.T) { + var zeroValue string + a := &ActionsCache{Version: &zeroValue} + a.GetVersion() + a = &ActionsCache{} + a.GetVersion() + a = nil + a.GetVersion() +} + +func TestActionsCacheListOptions_GetDirection(tt *testing.T) { + var zeroValue string + a := &ActionsCacheListOptions{Direction: &zeroValue} + a.GetDirection() + a = &ActionsCacheListOptions{} + a.GetDirection() + a = nil + a.GetDirection() +} + +func TestActionsCacheListOptions_GetKey(tt *testing.T) { + var zeroValue string + a := &ActionsCacheListOptions{Key: &zeroValue} + a.GetKey() + a = &ActionsCacheListOptions{} + a.GetKey() + a = nil + a.GetKey() +} + +func TestActionsCacheListOptions_GetRef(tt *testing.T) { + var zeroValue string + a := &ActionsCacheListOptions{Ref: &zeroValue} + a.GetRef() + a = &ActionsCacheListOptions{} + a.GetRef() + a = nil + a.GetRef() +} + +func TestActionsCacheListOptions_GetSort(tt *testing.T) { + var zeroValue string + a := &ActionsCacheListOptions{Sort: &zeroValue} + a.GetSort() + a = &ActionsCacheListOptions{} + a.GetSort() + a = nil + a.GetSort() +} + func TestActionsPermissions_GetAllowedActions(tt *testing.T) { var zeroValue string a := &ActionsPermissions{AllowedActions: &zeroValue}