2021-12-05 14:47:33 +01:00
|
|
|
package upstream
|
|
|
|
|
|
|
|
import (
|
2021-12-15 06:17:37 +01:00
|
|
|
"fmt"
|
|
|
|
"path"
|
2021-12-05 14:47:33 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"github.com/valyala/fastjson"
|
2021-12-05 15:53:46 +01:00
|
|
|
|
|
|
|
"codeberg.org/codeberg/pages/server/cache"
|
2021-12-05 14:47:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type branchTimestamp struct {
|
|
|
|
Branch string
|
|
|
|
Timestamp time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBranchTimestamp finds the default branch (if branch is "") and returns the last modification time of the branch
|
|
|
|
// (or nil if the branch doesn't exist)
|
2021-12-15 06:17:37 +01:00
|
|
|
func GetBranchTimestamp(owner, repo, branch, giteaRoot, giteaAPIToken string, branchTimestampCache cache.SetGetKey) *branchTimestamp {
|
2021-12-05 14:47:33 +01:00
|
|
|
if result, ok := branchTimestampCache.Get(owner + "/" + repo + "/" + branch); ok {
|
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return result.(*branchTimestamp)
|
|
|
|
}
|
2021-12-15 06:17:37 +01:00
|
|
|
result := &branchTimestamp{
|
|
|
|
Branch: branch,
|
|
|
|
}
|
|
|
|
if len(branch) == 0 {
|
2021-12-05 14:47:33 +01:00
|
|
|
// Get default branch
|
2021-12-15 06:17:37 +01:00
|
|
|
defaultBranch, err := giteaGetRepoDefaultBranch(giteaRoot, owner, repo, giteaAPIToken)
|
|
|
|
if err != nil {
|
|
|
|
_ = branchTimestampCache.Set(owner+"/"+repo+"/", nil, defaultBranchCacheTimeout)
|
2021-12-05 14:47:33 +01:00
|
|
|
return nil
|
|
|
|
}
|
2021-12-15 06:17:37 +01:00
|
|
|
result.Branch = defaultBranch
|
2021-12-05 14:47:33 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 06:17:37 +01:00
|
|
|
timestamp, err := giteaGetRepoBranchTimestamp(giteaRoot, owner, repo, branch, giteaAPIToken)
|
|
|
|
if err != nil {
|
2021-12-05 14:47:33 +01:00
|
|
|
return nil
|
|
|
|
}
|
2021-12-15 06:17:37 +01:00
|
|
|
result.Timestamp = timestamp
|
2021-12-05 16:24:26 +01:00
|
|
|
_ = branchTimestampCache.Set(owner+"/"+repo+"/"+branch, result, branchExistenceCacheTimeout)
|
2021-12-05 14:47:33 +01:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-12-15 06:17:37 +01:00
|
|
|
func giteaGetRepoBranchTimestamp(giteaRoot, repoOwner, repoName, branchName, giteaAPIToken string) (time.Time, error) {
|
|
|
|
client := getFastHTTPClient(5 * time.Second)
|
|
|
|
|
|
|
|
req := fasthttp.AcquireRequest()
|
2021-12-15 06:21:30 +01:00
|
|
|
req.SetRequestURI(path.Join(giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName))
|
2021-12-15 06:17:37 +01:00
|
|
|
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
|
|
|
|
res := fasthttp.AcquireResponse()
|
|
|
|
|
|
|
|
if err := client.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 giteaGetRepoDefaultBranch(giteaRoot, repoOwner, repoName, giteaAPIToken string) (string, error) {
|
|
|
|
client := getFastHTTPClient(5 * time.Second)
|
|
|
|
|
|
|
|
req := fasthttp.AcquireRequest()
|
2021-12-15 06:21:30 +01:00
|
|
|
req.SetRequestURI(path.Join(giteaRoot, giteaAPIRepos, repoOwner, repoName))
|
2021-12-15 06:17:37 +01:00
|
|
|
req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken)
|
|
|
|
res := fasthttp.AcquireResponse()
|
|
|
|
|
|
|
|
if err := client.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
|
|
|
|
}
|
|
|
|
|
2021-12-05 14:47:33 +01:00
|
|
|
type fileResponse struct {
|
|
|
|
exists bool
|
|
|
|
mimeType string
|
|
|
|
body []byte
|
|
|
|
}
|