//go:build !fasthttp package gitea import ( "fmt" "io" "net/http" "net/url" "strings" "time" "code.gitea.io/sdk/gitea" "codeberg.org/codeberg/pages/server/cache" ) type Client struct { sdkClient *gitea.Client responseCache cache.SetGetKey followSymlinks bool supportLFS bool } func NewClient(giteaRoot, giteaAPIToken string, respCache cache.SetGetKey, followSymlinks, supportLFS bool) (*Client, error) { rootURL, err := url.Parse(giteaRoot) giteaRoot = strings.Trim(rootURL.String(), "/") stdClient := http.Client{Timeout: 10 * time.Second} sdk, err := gitea.NewClient(giteaRoot, gitea.SetHTTPClient(&stdClient), gitea.SetToken(giteaAPIToken)) return &Client{ sdkClient: sdk, responseCache: respCache, }, err } func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) { rawBytes, resp, err := client.sdkClient.GetFile(targetOwner, targetRepo, ref, resource, client.supportLFS) if err != nil { return nil, err } switch resp.StatusCode { case http.StatusOK: return rawBytes, nil case http.StatusNotFound: return nil, ErrorNotFound default: return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode) } } func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource string) (io.ReadCloser, error) { reader, resp, err := client.sdkClient.GetFileReader(targetOwner, targetRepo, ref, resource, client.supportLFS) if err != nil { return nil, err } switch resp.StatusCode { case http.StatusOK: return reader, nil case http.StatusNotFound: return nil, ErrorNotFound default: return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode) } } func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (*BranchTimestamp, error) { cacheKey := fmt.Sprintf("%s/%s/%s/%s", branchTimestampCacheKeyPrefix, repoOwner, repoName, branchName) if stamp, ok := client.responseCache.Get(cacheKey); ok && stamp != nil { return stamp.(*BranchTimestamp), nil } branch, resp, err := client.sdkClient.GetRepoBranch(repoOwner, repoName, branchName) if err != nil { if resp != nil && resp.StatusCode == http.StatusNotFound { return &BranchTimestamp{}, ErrorNotFound } return &BranchTimestamp{}, err } if resp.StatusCode != http.StatusOK { return &BranchTimestamp{}, fmt.Errorf("unexpected status code '%d'", resp.StatusCode) } stamp := &BranchTimestamp{ Branch: branch.Name, Timestamp: branch.Commit.Timestamp, } client.responseCache.Set(cacheKey, stamp, branchExistenceCacheTimeout) return stamp, nil } func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (string, error) { cacheKey := fmt.Sprintf("%s/%s/%s", defaultBranchCacheKeyPrefix, repoOwner, repoName) if branch, ok := client.responseCache.Get(cacheKey); ok && branch != nil { return branch.(string), nil } repo, resp, err := client.sdkClient.GetRepo(repoOwner, repoName) if err != nil { return "", err } if resp.StatusCode != http.StatusOK { return "", fmt.Errorf("unexpected status code '%d'", resp.StatusCode) } branch := repo.DefaultBranch client.responseCache.Set(cacheKey, branch, defaultBranchCacheTimeout) return branch, nil }