2022-06-24 16:08:15 +05:30
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2022-06-28 14:40:11 +05:30
|
|
|
"math"
|
2022-06-24 16:08:15 +05:30
|
|
|
"net/url"
|
2022-06-28 14:40:11 +05:30
|
|
|
"strconv"
|
2022-06-24 16:08:15 +05:30
|
|
|
|
|
|
|
|
"github.com/actions/gh-actions-cache/types"
|
|
|
|
|
gh "github.com/cli/go-gh"
|
|
|
|
|
"github.com/cli/go-gh/pkg/api"
|
|
|
|
|
ghRepo "github.com/cli/go-gh/pkg/repository"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ArtifactCacheService interface {
|
2022-07-14 02:14:03 +05:30
|
|
|
GetCacheUsage() (float64, error)
|
|
|
|
|
ListCaches(queryParams url.Values) (types.ListApiResponse, error)
|
|
|
|
|
DeleteCaches(queryParams url.Values) (int, error)
|
|
|
|
|
ListAllCaches(queryParams url.Values, key string) ([]types.ActionsCache, error)
|
2022-06-24 16:08:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ArtifactCache struct {
|
|
|
|
|
HttpClient api.RESTClient
|
|
|
|
|
repo ghRepo.Repository
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 02:14:03 +05:30
|
|
|
func NewArtifactCache(repo ghRepo.Repository, command string, version string) (ArtifactCacheService, error) {
|
2022-06-24 16:08:15 +05:30
|
|
|
opts := api.ClientOptions{
|
|
|
|
|
Host: repo.Host(),
|
|
|
|
|
Headers: map[string]string{"User-Agent": fmt.Sprintf("gh-actions-cache/%s/%s", version, command)},
|
|
|
|
|
}
|
|
|
|
|
restClient, err := gh.RESTClient(&opts)
|
|
|
|
|
if err != nil {
|
2022-07-14 02:14:03 +05:30
|
|
|
return nil, err
|
2022-06-24 16:08:15 +05:30
|
|
|
}
|
2022-07-14 02:14:03 +05:30
|
|
|
return &ArtifactCache{HttpClient: restClient, repo: repo}, nil
|
2022-06-24 16:08:15 +05:30
|
|
|
}
|
|
|
|
|
|
2022-07-14 02:14:03 +05:30
|
|
|
func (a *ArtifactCache) GetCacheUsage() (float64, error) {
|
2022-06-24 16:08:15 +05:30
|
|
|
pathComponent := fmt.Sprintf("repos/%s/%s/actions/cache/usage", a.repo.Owner(), a.repo.Name())
|
|
|
|
|
var apiResults types.RepoLevelUsageApiResponse
|
|
|
|
|
err := a.HttpClient.Get(pathComponent, &apiResults)
|
|
|
|
|
if err != nil {
|
2022-07-14 02:14:03 +05:30
|
|
|
return -1, err
|
2022-06-24 16:08:15 +05:30
|
|
|
}
|
|
|
|
|
|
2022-07-14 02:14:03 +05:30
|
|
|
return apiResults.ActiveCacheSizeInBytes, nil
|
2022-06-24 16:08:15 +05:30
|
|
|
}
|
|
|
|
|
|
2022-07-14 02:14:03 +05:30
|
|
|
func (a *ArtifactCache) ListCaches(queryParams url.Values) (types.ListApiResponse, error) {
|
2022-06-24 16:08:15 +05:30
|
|
|
pathComponent := fmt.Sprintf("repos/%s/%s/actions/caches", a.repo.Owner(), a.repo.Name())
|
|
|
|
|
var apiResults types.ListApiResponse
|
|
|
|
|
err := a.HttpClient.Get(pathComponent+"?"+queryParams.Encode(), &apiResults)
|
2022-07-14 02:14:03 +05:30
|
|
|
|
2022-06-24 16:08:15 +05:30
|
|
|
if err != nil {
|
2022-07-14 02:14:03 +05:30
|
|
|
return types.ListApiResponse{}, err
|
2022-06-24 16:08:15 +05:30
|
|
|
}
|
|
|
|
|
|
2022-07-14 02:14:03 +05:30
|
|
|
return apiResults, nil
|
2022-06-24 16:08:15 +05:30
|
|
|
}
|
|
|
|
|
|
2022-07-14 02:14:03 +05:30
|
|
|
func (a *ArtifactCache) DeleteCaches(queryParams url.Values) (int, error) {
|
2022-06-24 16:08:15 +05:30
|
|
|
pathComponent := fmt.Sprintf("repos/%s/%s/actions/caches", a.repo.Owner(), a.repo.Name())
|
|
|
|
|
var apiResults types.DeleteApiResponse
|
|
|
|
|
err := a.HttpClient.Delete(pathComponent+"?"+queryParams.Encode(), &apiResults)
|
|
|
|
|
if err != nil {
|
2022-07-14 02:14:03 +05:30
|
|
|
return 0, err
|
2022-06-24 16:08:15 +05:30
|
|
|
}
|
2022-07-14 02:14:03 +05:30
|
|
|
return apiResults.TotalCount, nil
|
2022-06-24 16:08:15 +05:30
|
|
|
}
|
2022-06-28 14:40:11 +05:30
|
|
|
|
2022-07-14 02:14:03 +05:30
|
|
|
func (a *ArtifactCache) ListAllCaches(queryParams url.Values, key string) ([]types.ActionsCache, error) {
|
2022-06-28 14:40:11 +05:30
|
|
|
var listApiResponse types.ListApiResponse
|
2022-07-14 02:14:03 +05:30
|
|
|
listApiResponse, err := a.ListCaches(queryParams)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-28 14:40:11 +05:30
|
|
|
caches := listApiResponse.ActionsCaches
|
|
|
|
|
totalCaches := listApiResponse.TotalCount
|
|
|
|
|
if totalCaches > 100 {
|
|
|
|
|
for page := 2; page <= int(math.Ceil(float64(listApiResponse.TotalCount)/100)); page++ {
|
|
|
|
|
queryParams.Set("page", strconv.Itoa(page))
|
2022-07-14 02:14:03 +05:30
|
|
|
listApiResponse, err := a.ListCaches(queryParams)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2022-06-28 14:40:11 +05:30
|
|
|
caches = append(caches, listApiResponse.ActionsCaches...)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-14 02:14:03 +05:30
|
|
|
return caches, nil
|
2022-06-28 14:40:11 +05:30
|
|
|
}
|