pages-server/server/gitea/client_std.go

117 lines
3.2 KiB
Go
Raw Normal View History

2022-07-15 16:53:04 +00:00
//go:build !fasthttp
package gitea
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"code.gitea.io/sdk/gitea"
2022-07-15 16:53:04 +00:00
"codeberg.org/codeberg/pages/server/cache"
)
type Client struct {
2022-07-27 13:39:46 +00:00
sdkClient *gitea.Client
responseCache cache.SetGetKey
followSymlinks bool
supportLFS bool
2022-07-15 16:53:04 +00:00
}
2022-07-27 13:39:46 +00:00
func NewClient(giteaRoot, giteaAPIToken string, respCache cache.SetGetKey, followSymlinks, supportLFS bool) (*Client, error) {
2022-07-15 16:53:04 +00:00
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{
2022-07-27 13:39:46 +00:00
sdkClient: sdk,
responseCache: respCache,
2022-07-15 16:53:04 +00:00
}, err
}
func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) {
2022-07-27 12:57:17 +00:00
rawBytes, resp, err := client.sdkClient.GetFile(targetOwner, targetRepo, ref, resource, client.supportLFS)
2022-07-15 16:53:04 +00:00
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) {
2022-07-27 12:57:17 +00:00
reader, resp, err := client.sdkClient.GetFileReader(targetOwner, targetRepo, ref, resource, client.supportLFS)
2022-07-15 16:53:04 +00:00
if err != nil {
return nil, err
}
switch resp.StatusCode {
case http.StatusOK:
2022-07-27 12:57:17 +00:00
return reader, nil
2022-07-15 16:53:04 +00:00
case http.StatusNotFound:
return nil, ErrorNotFound
default:
return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode)
}
}
2022-07-27 13:39:46 +00:00
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
}
2022-07-15 16:53:04 +00:00
branch, resp, err := client.sdkClient.GetRepoBranch(repoOwner, repoName, branchName)
if err != nil {
2022-07-27 13:39:46 +00:00
if resp != nil && resp.StatusCode == http.StatusNotFound {
return &BranchTimestamp{}, ErrorNotFound
}
return &BranchTimestamp{}, err
2022-07-15 16:53:04 +00:00
}
if resp.StatusCode != http.StatusOK {
2022-07-27 13:39:46 +00:00
return &BranchTimestamp{}, fmt.Errorf("unexpected status code '%d'", resp.StatusCode)
}
stamp := &BranchTimestamp{
Branch: branch.Name,
Timestamp: branch.Commit.Timestamp,
2022-07-15 16:53:04 +00:00
}
2022-07-27 13:39:46 +00:00
client.responseCache.Set(cacheKey, stamp, branchExistenceCacheTimeout)
return stamp, nil
2022-07-15 16:53:04 +00:00
}
func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (string, error) {
2022-07-27 13:39:46 +00:00
cacheKey := fmt.Sprintf("%s/%s/%s", defaultBranchCacheKeyPrefix, repoOwner, repoName)
if branch, ok := client.responseCache.Get(cacheKey); ok && branch != nil {
return branch.(string), nil
}
2022-07-15 16:53:04 +00:00
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)
}
2022-07-27 13:39:46 +00:00
branch := repo.DefaultBranch
client.responseCache.Set(cacheKey, branch, defaultBranchCacheTimeout)
return branch, nil
2022-07-15 16:53:04 +00:00
}