From b9439d0e07e5d9fdbf526fabb0d54b8e8caebece Mon Sep 17 00:00:00 2001 From: Ali Date: Tue, 20 Dec 2022 18:56:26 +0500 Subject: [PATCH 1/4] implemented github actions --- github/actions_cache.go | 236 +++++++++++++++ github/actions_cache_test.go | 517 ++++++++++++++++++++++++++++++++ github/github-accessors.go | 112 +++++++ github/github-accessors_test.go | 140 +++++++++ 4 files changed, 1005 insertions(+) create mode 100644 github/actions_cache.go create mode 100644 github/actions_cache_test.go diff --git a/github/actions_cache.go b/github/actions_cache.go new file mode 100644 index 0000000000..cefa7539df --- /dev/null +++ b/github/actions_cache.go @@ -0,0 +1,236 @@ +// 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 *int64 `json:"total_count,omitempty"` + 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"` + ActiveCacheUsageSize *int64 `json:"active_caches_size_in_bytes"` + ActiveCachesCount *int64 `json:"active_caches_count"` +} + +// ActionsCacheUsageList represents a list 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 *int64 `json:"total_count"` + RepoCacheUsage []*ActionsCacheUsage `json:"repository_cache_usages"` +} + +// TotalCacheUsage represents total github actions cache usage of an organization or enterprise. +// +type TotalCacheUsage struct { + TotalActiveCacheUsageSize *int64 `json:"total_active_caches_size_in_bytes"` + TotalActiveCachesCount *int64 `json:"total_active_caches_count"` +} + +// ActionsCacheListOptions list 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 { + //The number of results per page (max 100). Default:30 + PerPage int `url:"per_page,omitempty"` + //Page number of the results to fetch.Default:1 + Page int `url:"page,omitempty"` + //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"` +} + +// 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 +} + +// 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) +} + +// 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) +} + +// 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 +} + +// 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 +} + +// 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 +} + +// 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..bfec5eb4c2 --- /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{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: Int64(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: String("test-cache"), ActiveCacheUsageSize: Int64(1000), ActiveCachesCount: Int64(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: Int64(1), RepoCacheUsage: []*ActionsCacheUsage{{FullName: String("test-cache"), ActiveCacheUsageSize: Int64(1000), ActiveCachesCount: Int64(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{TotalActiveCacheUsageSize: Int64(1000), TotalActiveCachesCount: Int64(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{TotalActiveCacheUsageSize: Int64(1000), TotalActiveCachesCount: Int64(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..165bbcf05b 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -38,6 +38,102 @@ 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 +} + +// GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. +func (a *ActionsCacheList) GetTotalCount() int64 { + if a == nil || a.TotalCount == nil { + return 0 + } + return *a.TotalCount +} + +// GetActiveCachesCount returns the ActiveCachesCount field if it's non-nil, zero value otherwise. +func (a *ActionsCacheUsage) GetActiveCachesCount() int64 { + if a == nil || a.ActiveCachesCount == nil { + return 0 + } + return *a.ActiveCachesCount +} + +// GetActiveCacheUsageSize returns the ActiveCacheUsageSize field if it's non-nil, zero value otherwise. +func (a *ActionsCacheUsage) GetActiveCacheUsageSize() int64 { + if a == nil || a.ActiveCacheUsageSize == nil { + return 0 + } + return *a.ActiveCacheUsageSize +} + +// GetFullName returns the FullName field if it's non-nil, zero value otherwise. +func (a *ActionsCacheUsage) GetFullName() string { + if a == nil || a.FullName == nil { + return "" + } + return *a.FullName +} + +// GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. +func (a *ActionsCacheUsageList) GetTotalCount() int64 { + if a == nil || a.TotalCount == nil { + return 0 + } + return *a.TotalCount +} + // GetAllowedActions returns the AllowedActions field if it's non-nil, zero value otherwise. func (a *ActionsPermissions) GetAllowedActions() string { if a == nil || a.AllowedActions == nil { @@ -19350,6 +19446,22 @@ func (t *TopicsSearchResult) GetTotal() int { return *t.Total } +// GetTotalActiveCachesCount returns the TotalActiveCachesCount field if it's non-nil, zero value otherwise. +func (t *TotalCacheUsage) GetTotalActiveCachesCount() int64 { + if t == nil || t.TotalActiveCachesCount == nil { + return 0 + } + return *t.TotalActiveCachesCount +} + +// GetTotalActiveCacheUsageSize returns the TotalActiveCacheUsageSize field if it's non-nil, zero value otherwise. +func (t *TotalCacheUsage) GetTotalActiveCacheUsageSize() int64 { + if t == nil || t.TotalActiveCacheUsageSize == nil { + return 0 + } + return *t.TotalActiveCacheUsageSize +} + // GetCount returns the Count field if it's non-nil, zero value otherwise. func (t *TrafficClones) GetCount() int { if t == nil || t.Count == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index efa66e7875..3c043e9639 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -45,6 +45,126 @@ 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 TestActionsCacheList_GetTotalCount(tt *testing.T) { + var zeroValue int64 + a := &ActionsCacheList{TotalCount: &zeroValue} + a.GetTotalCount() + a = &ActionsCacheList{} + a.GetTotalCount() + a = nil + a.GetTotalCount() +} + +func TestActionsCacheUsage_GetActiveCachesCount(tt *testing.T) { + var zeroValue int64 + a := &ActionsCacheUsage{ActiveCachesCount: &zeroValue} + a.GetActiveCachesCount() + a = &ActionsCacheUsage{} + a.GetActiveCachesCount() + a = nil + a.GetActiveCachesCount() +} + +func TestActionsCacheUsage_GetActiveCacheUsageSize(tt *testing.T) { + var zeroValue int64 + a := &ActionsCacheUsage{ActiveCacheUsageSize: &zeroValue} + a.GetActiveCacheUsageSize() + a = &ActionsCacheUsage{} + a.GetActiveCacheUsageSize() + a = nil + a.GetActiveCacheUsageSize() +} + +func TestActionsCacheUsage_GetFullName(tt *testing.T) { + var zeroValue string + a := &ActionsCacheUsage{FullName: &zeroValue} + a.GetFullName() + a = &ActionsCacheUsage{} + a.GetFullName() + a = nil + a.GetFullName() +} + +func TestActionsCacheUsageList_GetTotalCount(tt *testing.T) { + var zeroValue int64 + a := &ActionsCacheUsageList{TotalCount: &zeroValue} + a.GetTotalCount() + a = &ActionsCacheUsageList{} + a.GetTotalCount() + a = nil + a.GetTotalCount() +} + func TestActionsPermissions_GetAllowedActions(tt *testing.T) { var zeroValue string a := &ActionsPermissions{AllowedActions: &zeroValue} @@ -22535,6 +22655,26 @@ func TestTopicsSearchResult_GetTotal(tt *testing.T) { t.GetTotal() } +func TestTotalCacheUsage_GetTotalActiveCachesCount(tt *testing.T) { + var zeroValue int64 + t := &TotalCacheUsage{TotalActiveCachesCount: &zeroValue} + t.GetTotalActiveCachesCount() + t = &TotalCacheUsage{} + t.GetTotalActiveCachesCount() + t = nil + t.GetTotalActiveCachesCount() +} + +func TestTotalCacheUsage_GetTotalActiveCacheUsageSize(tt *testing.T) { + var zeroValue int64 + t := &TotalCacheUsage{TotalActiveCacheUsageSize: &zeroValue} + t.GetTotalActiveCacheUsageSize() + t = &TotalCacheUsage{} + t.GetTotalActiveCacheUsageSize() + t = nil + t.GetTotalActiveCacheUsageSize() +} + func TestTrafficClones_GetCount(tt *testing.T) { var zeroValue int t := &TrafficClones{Count: &zeroValue} From 524b7f05a35c34cff7cb148c706eb428566786c3 Mon Sep 17 00:00:00 2001 From: Ali Date: Thu, 22 Dec 2022 17:38:14 +0500 Subject: [PATCH 2/4] changes --- github/actions_cache.go | 77 ++++++++++++++++++------------------ github/actions_cache_test.go | 12 +++--- 2 files changed, 44 insertions(+), 45 deletions(-) diff --git a/github/actions_cache.go b/github/actions_cache.go index cefa7539df..2a51104cab 100644 --- a/github/actions_cache.go +++ b/github/actions_cache.go @@ -27,53 +27,52 @@ type ActionsCache struct { // // 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 *int64 `json:"total_count,omitempty"` + 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 +// 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"` - ActiveCacheUsageSize *int64 `json:"active_caches_size_in_bytes"` - ActiveCachesCount *int64 `json:"active_caches_count"` + FullName string `json:"full_name"` + ActiveCachesSizeInBytes int64 `json:"active_caches_size_in_bytes"` + ActiveCachesCount int `json:"active_caches_count"` } -// ActionsCacheUsageList represents a list repositories with GitHub Actions cache usage for an organization. +// 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 +// 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 *int64 `json:"total_count"` - RepoCacheUsage []*ActionsCacheUsage `json:"repository_cache_usages"` + TotalCount int `json:"total_count"` + RepoCacheUsage []*ActionsCacheUsage `json:"repository_cache_usages,omitempty"` } -// TotalCacheUsage represents total github actions cache usage of an organization or enterprise. +// 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 { - TotalActiveCacheUsageSize *int64 `json:"total_active_caches_size_in_bytes"` - TotalActiveCachesCount *int64 `json:"total_active_caches_count"` + TotalActiveCacheUsageSizeInBytes int64 `json:"total_active_caches_size_in_bytes"` + TotalActiveCachesCount int `json:"total_active_caches_count"` } -// ActionsCacheListOptions list all possible optional Query parameters for ListCaches method. +// 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 { - //The number of results per page (max 100). Default:30 - PerPage int `url:"per_page,omitempty"` - //Page number of the results to fetch.Default:1 - Page int `url:"page,omitempty"` - //The Git reference for the results you want to list. + 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 + // 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" + // 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 + // Can be one of: "asc", "desc" Default: desc Direction string `url:"direction,omitempty"` } -// Lists the GitHub Actions caches for a repository. +// 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. @@ -100,7 +99,7 @@ func (s *ActionsService) ListCaches(ctx context.Context, owner, repo string, opt return actionCacheList, resp, nil } -// Deletes one or more GitHub Actions caches for a repository, using a complete cache key. +// 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 "". @@ -115,28 +114,31 @@ func (s *ActionsService) DeleteCachesByKey(ctx context.Context, owner, repo, key 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) } -// Deletes a GitHub Actions cache for a repository, using a cache ID. +// 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) +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) } -// Gets GitHub Actions cache usage for a repository. The data fetched using this API is refreshed approximately every 5 minutes, +// 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 @@ -145,21 +147,21 @@ func (s *ActionsService) DeleteCachesByID(ctx context.Context, owner, repo strin // 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 } -// Lists repositories and their GitHub Actions cache usage for an organization. The data fetched using this API is +// 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. @@ -174,13 +176,12 @@ func (s *ActionsService) ListCacheUsageByRepoForOrg(ctx context.Context, org str } 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 } @@ -188,7 +189,7 @@ func (s *ActionsService) ListCacheUsageByRepoForOrg(ctx context.Context, org str return cacheUsage, res, err } -// Gets the total GitHub Actions cache usage for an organization. The data fetched using this API is refreshed approximately every +// 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. @@ -197,21 +198,21 @@ func (s *ActionsService) ListCacheUsageByRepoForOrg(ctx context.Context, org str // 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 } -// Gets the total GitHub Actions cache usage for an enterprise. The data fetched using this API is refreshed approximately every 5 minutes, +// 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. @@ -219,18 +220,16 @@ func (s *ActionsService) GetTotalCacheUsageForOrg(ctx context.Context, org strin // 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 index bfec5eb4c2..a827b71a1d 100644 --- a/github/actions_cache_test.go +++ b/github/actions_cache_test.go @@ -29,14 +29,14 @@ func TestActionsService_ListCaches(t *testing.T) { ) }) - opts := &ActionsCacheListOptions{Page: 2} + 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: Int64(1), ActionsCaches: []*ActionsCache{{ID: Int64(1)}}} + want := &ActionsCacheList{TotalCount: 1, ActionsCaches: []*ActionsCache{{ID: Int64(1)}}} if !cmp.Equal(cacheList, want) { t.Errorf("Actions.ListCaches returned %+v, want %+v", cacheList, want) } @@ -241,7 +241,7 @@ func TestActionsService_GetCacheUsageForRepo(t *testing.T) { t.Errorf("Actions.GetCacheUsageForRepo returned error: %v", err) } - want := &ActionsCacheUsage{FullName: String("test-cache"), ActiveCacheUsageSize: Int64(1000), ActiveCachesCount: Int64(1)} + want := &ActionsCacheUsage{FullName: "test-cache", ActiveCachesSizeInBytes: 1000, ActiveCachesCount: 1} if !cmp.Equal(cacheUse, want) { t.Errorf("Actions.GetCacheUsageForRepo returned %+v, want %+v", cacheUse, want) } @@ -323,7 +323,7 @@ func TestActionsService_ListCacheUsageByRepoForOrg(t *testing.T) { t.Errorf("Actions.ListCacheUsageByRepoForOrg returned error: %v", err) } - want := &ActionsCacheUsageList{TotalCount: Int64(1), RepoCacheUsage: []*ActionsCacheUsage{{FullName: String("test-cache"), ActiveCacheUsageSize: Int64(1000), ActiveCachesCount: Int64(1)}}} + 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) } @@ -394,7 +394,7 @@ func TestActionsService_GetCacheUsageForOrg(t *testing.T) { t.Errorf("Actions.GetTotalCacheUsageForOrg returned error: %v", err) } - want := &TotalCacheUsage{TotalActiveCacheUsageSize: Int64(1000), TotalActiveCachesCount: Int64(1)} + want := &TotalCacheUsage{TotalActiveCacheUsageSizeInBytes: 1000, TotalActiveCachesCount: 1} if !cmp.Equal(cache, want) { t.Errorf("Actions.GetTotalCacheUsageForOrg returned %+v, want %+v", cache, want) } @@ -465,7 +465,7 @@ func TestActionsService_GetCacheUsageForEnterprise(t *testing.T) { t.Errorf("Actions.GetTotalCacheUsageForEnterprise returned error: %v", err) } - want := &TotalCacheUsage{TotalActiveCacheUsageSize: Int64(1000), TotalActiveCachesCount: Int64(1)} + want := &TotalCacheUsage{TotalActiveCacheUsageSizeInBytes: 1000, TotalActiveCachesCount: 1} if !cmp.Equal(cache, want) { t.Errorf("Actions.GetTotalCacheUsageForEnterprise returned %+v, want %+v", cache, want) } From 49f753e525996b8966b5d86d04710e612666232f Mon Sep 17 00:00:00 2001 From: Ali Date: Thu, 22 Dec 2022 17:44:25 +0500 Subject: [PATCH 3/4] all tests passed --- github/github-accessors.go | 56 -------------------------- github/github-accessors_test.go | 70 --------------------------------- 2 files changed, 126 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index 165bbcf05b..5827782dc9 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -94,46 +94,6 @@ func (a *ActionsCache) GetVersion() string { return *a.Version } -// GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. -func (a *ActionsCacheList) GetTotalCount() int64 { - if a == nil || a.TotalCount == nil { - return 0 - } - return *a.TotalCount -} - -// GetActiveCachesCount returns the ActiveCachesCount field if it's non-nil, zero value otherwise. -func (a *ActionsCacheUsage) GetActiveCachesCount() int64 { - if a == nil || a.ActiveCachesCount == nil { - return 0 - } - return *a.ActiveCachesCount -} - -// GetActiveCacheUsageSize returns the ActiveCacheUsageSize field if it's non-nil, zero value otherwise. -func (a *ActionsCacheUsage) GetActiveCacheUsageSize() int64 { - if a == nil || a.ActiveCacheUsageSize == nil { - return 0 - } - return *a.ActiveCacheUsageSize -} - -// GetFullName returns the FullName field if it's non-nil, zero value otherwise. -func (a *ActionsCacheUsage) GetFullName() string { - if a == nil || a.FullName == nil { - return "" - } - return *a.FullName -} - -// GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. -func (a *ActionsCacheUsageList) GetTotalCount() int64 { - if a == nil || a.TotalCount == nil { - return 0 - } - return *a.TotalCount -} - // GetAllowedActions returns the AllowedActions field if it's non-nil, zero value otherwise. func (a *ActionsPermissions) GetAllowedActions() string { if a == nil || a.AllowedActions == nil { @@ -19446,22 +19406,6 @@ func (t *TopicsSearchResult) GetTotal() int { return *t.Total } -// GetTotalActiveCachesCount returns the TotalActiveCachesCount field if it's non-nil, zero value otherwise. -func (t *TotalCacheUsage) GetTotalActiveCachesCount() int64 { - if t == nil || t.TotalActiveCachesCount == nil { - return 0 - } - return *t.TotalActiveCachesCount -} - -// GetTotalActiveCacheUsageSize returns the TotalActiveCacheUsageSize field if it's non-nil, zero value otherwise. -func (t *TotalCacheUsage) GetTotalActiveCacheUsageSize() int64 { - if t == nil || t.TotalActiveCacheUsageSize == nil { - return 0 - } - return *t.TotalActiveCacheUsageSize -} - // GetCount returns the Count field if it's non-nil, zero value otherwise. func (t *TrafficClones) GetCount() int { if t == nil || t.Count == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 3c043e9639..c30427a83f 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -115,56 +115,6 @@ func TestActionsCache_GetVersion(tt *testing.T) { a.GetVersion() } -func TestActionsCacheList_GetTotalCount(tt *testing.T) { - var zeroValue int64 - a := &ActionsCacheList{TotalCount: &zeroValue} - a.GetTotalCount() - a = &ActionsCacheList{} - a.GetTotalCount() - a = nil - a.GetTotalCount() -} - -func TestActionsCacheUsage_GetActiveCachesCount(tt *testing.T) { - var zeroValue int64 - a := &ActionsCacheUsage{ActiveCachesCount: &zeroValue} - a.GetActiveCachesCount() - a = &ActionsCacheUsage{} - a.GetActiveCachesCount() - a = nil - a.GetActiveCachesCount() -} - -func TestActionsCacheUsage_GetActiveCacheUsageSize(tt *testing.T) { - var zeroValue int64 - a := &ActionsCacheUsage{ActiveCacheUsageSize: &zeroValue} - a.GetActiveCacheUsageSize() - a = &ActionsCacheUsage{} - a.GetActiveCacheUsageSize() - a = nil - a.GetActiveCacheUsageSize() -} - -func TestActionsCacheUsage_GetFullName(tt *testing.T) { - var zeroValue string - a := &ActionsCacheUsage{FullName: &zeroValue} - a.GetFullName() - a = &ActionsCacheUsage{} - a.GetFullName() - a = nil - a.GetFullName() -} - -func TestActionsCacheUsageList_GetTotalCount(tt *testing.T) { - var zeroValue int64 - a := &ActionsCacheUsageList{TotalCount: &zeroValue} - a.GetTotalCount() - a = &ActionsCacheUsageList{} - a.GetTotalCount() - a = nil - a.GetTotalCount() -} - func TestActionsPermissions_GetAllowedActions(tt *testing.T) { var zeroValue string a := &ActionsPermissions{AllowedActions: &zeroValue} @@ -22655,26 +22605,6 @@ func TestTopicsSearchResult_GetTotal(tt *testing.T) { t.GetTotal() } -func TestTotalCacheUsage_GetTotalActiveCachesCount(tt *testing.T) { - var zeroValue int64 - t := &TotalCacheUsage{TotalActiveCachesCount: &zeroValue} - t.GetTotalActiveCachesCount() - t = &TotalCacheUsage{} - t.GetTotalActiveCachesCount() - t = nil - t.GetTotalActiveCachesCount() -} - -func TestTotalCacheUsage_GetTotalActiveCacheUsageSize(tt *testing.T) { - var zeroValue int64 - t := &TotalCacheUsage{TotalActiveCacheUsageSize: &zeroValue} - t.GetTotalActiveCacheUsageSize() - t = &TotalCacheUsage{} - t.GetTotalActiveCacheUsageSize() - t = nil - t.GetTotalActiveCacheUsageSize() -} - func TestTrafficClones_GetCount(tt *testing.T) { var zeroValue int t := &TrafficClones{Count: &zeroValue} From 7e6570ea88a85870a8425744b0491566351958af Mon Sep 17 00:00:00 2001 From: Ali Date: Thu, 29 Dec 2022 01:28:10 +0500 Subject: [PATCH 4/4] asked changes done --- github/actions_cache.go | 12 +++++----- github/actions_cache_test.go | 4 ++-- github/github-accessors.go | 32 ++++++++++++++++++++++++++ github/github-accessors_test.go | 40 +++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 8 deletions(-) diff --git a/github/actions_cache.go b/github/actions_cache.go index 2a51104cab..9592d9ab62 100644 --- a/github/actions_cache.go +++ b/github/actions_cache.go @@ -52,8 +52,8 @@ type ActionsCacheUsageList struct { // // 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 { - TotalActiveCacheUsageSizeInBytes int64 `json:"total_active_caches_size_in_bytes"` - TotalActiveCachesCount int `json:"total_active_caches_count"` + 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. @@ -64,12 +64,12 @@ type ActionsCacheListOptions struct { // 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"` + 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"` + Sort *string `url:"sort,omitempty"` // Can be one of: "asc", "desc" Default: desc - Direction string `url:"direction,omitempty"` + Direction *string `url:"direction,omitempty"` } // ListCaches lists the GitHub Actions caches for a repository. diff --git a/github/actions_cache_test.go b/github/actions_cache_test.go index a827b71a1d..b72d5cb828 100644 --- a/github/actions_cache_test.go +++ b/github/actions_cache_test.go @@ -394,7 +394,7 @@ func TestActionsService_GetCacheUsageForOrg(t *testing.T) { t.Errorf("Actions.GetTotalCacheUsageForOrg returned error: %v", err) } - want := &TotalCacheUsage{TotalActiveCacheUsageSizeInBytes: 1000, TotalActiveCachesCount: 1} + want := &TotalCacheUsage{TotalActiveCachesUsageSizeInBytes: 1000, TotalActiveCachesCount: 1} if !cmp.Equal(cache, want) { t.Errorf("Actions.GetTotalCacheUsageForOrg returned %+v, want %+v", cache, want) } @@ -465,7 +465,7 @@ func TestActionsService_GetCacheUsageForEnterprise(t *testing.T) { t.Errorf("Actions.GetTotalCacheUsageForEnterprise returned error: %v", err) } - want := &TotalCacheUsage{TotalActiveCacheUsageSizeInBytes: 1000, TotalActiveCachesCount: 1} + want := &TotalCacheUsage{TotalActiveCachesUsageSizeInBytes: 1000, TotalActiveCachesCount: 1} if !cmp.Equal(cache, want) { t.Errorf("Actions.GetTotalCacheUsageForEnterprise returned %+v, want %+v", cache, want) } diff --git a/github/github-accessors.go b/github/github-accessors.go index 5827782dc9..9ed960fb77 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -94,6 +94,38 @@ func (a *ActionsCache) GetVersion() string { 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 c30427a83f..17d8504ae7 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -115,6 +115,46 @@ func TestActionsCache_GetVersion(tt *testing.T) { 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}