2021-12-05 13:47:33 +00:00
|
|
|
package upstream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-12-05 14:53:46 +00:00
|
|
|
"codeberg.org/codeberg/pages/server/cache"
|
2021-12-05 13:47:33 +00: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)
|
2022-04-20 21:42:01 +00:00
|
|
|
func GetBranchTimestamp(owner, repo, branch, giteaRoot, giteaAPIToken string, branchTimestampCache cache.SetGetKey) *branchTimestamp {
|
2021-12-05 13:47:33 +00:00
|
|
|
if result, ok := branchTimestampCache.Get(owner + "/" + repo + "/" + branch); ok {
|
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return result.(*branchTimestamp)
|
|
|
|
}
|
2022-04-20 21:42:01 +00:00
|
|
|
result := &branchTimestamp{
|
|
|
|
Branch: branch,
|
|
|
|
}
|
|
|
|
if len(branch) == 0 {
|
2021-12-05 13:47:33 +00:00
|
|
|
// Get default branch
|
2022-04-20 21:42:01 +00:00
|
|
|
defaultBranch, err := giteaGetRepoDefaultBranch(giteaRoot, owner, repo, giteaAPIToken)
|
|
|
|
if err != nil {
|
|
|
|
_ = branchTimestampCache.Set(owner+"/"+repo+"/", nil, defaultBranchCacheTimeout)
|
2021-12-05 13:47:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-04-20 21:42:01 +00:00
|
|
|
result.Branch = defaultBranch
|
2021-12-05 13:47:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 21:42:01 +00:00
|
|
|
timestamp, err := giteaGetRepoBranchTimestamp(giteaRoot, owner, repo, branch, giteaAPIToken)
|
|
|
|
if err != nil {
|
2021-12-05 13:47:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-04-20 21:42:01 +00:00
|
|
|
result.Timestamp = timestamp
|
2021-12-05 15:24:26 +00:00
|
|
|
_ = branchTimestampCache.Set(owner+"/"+repo+"/"+branch, result, branchExistenceCacheTimeout)
|
2021-12-05 13:47:33 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileResponse struct {
|
|
|
|
exists bool
|
|
|
|
mimeType string
|
|
|
|
body []byte
|
|
|
|
}
|