mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-24 22:06:57 +00:00
118 lines
3.3 KiB
Go
118 lines
3.3 KiB
Go
package gitea
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
"github.com/valyala/fastjson"
|
|
)
|
|
|
|
const giteaAPIRepos = "/api/v1/repos/"
|
|
|
|
var ErrorNotFound = errors.New("not found")
|
|
|
|
type Client struct {
|
|
giteaRoot string
|
|
giteaAPIToken string
|
|
}
|
|
|
|
type FileResponse struct {
|
|
Exists bool
|
|
MimeType string
|
|
Body []byte
|
|
}
|
|
|
|
func (f FileResponse) IsEmpty() bool { return len(f.Body) != 0 }
|
|
|
|
func NewClient(giteaRoot, giteaAPIToken string) *Client {
|
|
return &Client{
|
|
giteaRoot: giteaRoot,
|
|
giteaAPIToken: "token " + giteaAPIToken,
|
|
}
|
|
}
|
|
|
|
// TODOs:
|
|
// * handle 404 -> page will show 500 atm
|
|
|
|
func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) {
|
|
req := fasthttp.AcquireRequest()
|
|
|
|
req.SetRequestURI(path.Join(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref)))
|
|
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
|
|
res := fasthttp.AcquireResponse()
|
|
|
|
if err := getFastHTTPClient(10*time.Second).Do(req, res); err != nil {
|
|
return nil, err
|
|
}
|
|
switch res.StatusCode() {
|
|
case fasthttp.StatusOK:
|
|
return res.Body(), nil
|
|
case fasthttp.StatusNotFound:
|
|
return nil, ErrorNotFound
|
|
default:
|
|
return nil, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
|
}
|
|
}
|
|
|
|
func (client *Client) ServeRawContent(uri string) (*fasthttp.Response, error) {
|
|
fastClient := getFastHTTPClient(10 * time.Second)
|
|
|
|
req := fasthttp.AcquireRequest()
|
|
req.SetRequestURI(path.Join(client.giteaRoot, giteaAPIRepos, uri))
|
|
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
|
|
|
|
resp := fasthttp.AcquireResponse()
|
|
resp.SetBodyStream(&strings.Reader{}, -1)
|
|
|
|
if err := fastClient.Do(req, resp); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch resp.StatusCode() {
|
|
case fasthttp.StatusOK:
|
|
return resp, nil
|
|
case fasthttp.StatusNotFound:
|
|
return nil, ErrorNotFound
|
|
default:
|
|
return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode())
|
|
}
|
|
}
|
|
|
|
func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (time.Time, error) {
|
|
fastClient := getFastHTTPClient(5 * time.Second)
|
|
|
|
req := fasthttp.AcquireRequest()
|
|
req.SetRequestURI(path.Join(client.giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName))
|
|
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
|
|
res := fasthttp.AcquireResponse()
|
|
|
|
if err := fastClient.Do(req, res); err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
if res.StatusCode() != fasthttp.StatusOK {
|
|
return time.Time{}, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
|
}
|
|
return time.Parse(time.RFC3339, fastjson.GetString(res.Body(), "commit", "timestamp"))
|
|
}
|
|
|
|
func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (string, error) {
|
|
fastClient := getFastHTTPClient(5 * time.Second)
|
|
|
|
req := fasthttp.AcquireRequest()
|
|
req.SetRequestURI(path.Join(client.giteaRoot, giteaAPIRepos, repoOwner, repoName))
|
|
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
|
|
res := fasthttp.AcquireResponse()
|
|
|
|
if err := fastClient.Do(req, res); err != nil {
|
|
return "", err
|
|
}
|
|
if res.StatusCode() != fasthttp.StatusOK {
|
|
return "", fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
|
}
|
|
return fastjson.GetString(res.Body(), "default_branch"), nil
|
|
}
|