pages-server/server/upstream/helper.go

33 lines
1.1 KiB
Go
Raw Normal View History

2021-12-05 13:47:33 +00:00
package upstream
import (
"github.com/rs/zerolog/log"
2021-12-05 13:47:33 +00:00
"codeberg.org/codeberg/pages/server/gitea"
2021-12-05 13:47:33 +00:00
)
// 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-07-27 13:39:46 +00:00
func GetBranchTimestamp(giteaClient *gitea.Client, owner, repo, branch string) *gitea.BranchTimestamp {
log := log.With().Strs("BranchInfo", []string{owner, repo, branch}).Logger()
2022-09-18 17:47:20 +00:00
if len(branch) == 0 {
2021-12-05 13:47:33 +00:00
// Get default branch
defaultBranch, err := giteaClient.GiteaGetRepoDefaultBranch(owner, repo)
if err != nil {
log.Err(err).Msg("Could't fetch default branch from repository")
2021-12-05 13:47:33 +00:00
return nil
}
2022-11-07 22:01:31 +00:00
log.Debug().Msgf("Succesfully fetched default branch '%s' from Gitea", defaultBranch)
2022-07-27 13:39:46 +00:00
branch = defaultBranch
2021-12-05 13:47:33 +00:00
}
2022-07-27 13:39:46 +00:00
timestamp, err := giteaClient.GiteaGetRepoBranchTimestamp(owner, repo, branch)
if err != nil {
log.Err(err).Msg("Could not get latest commit's timestamp from branch")
2021-12-05 13:47:33 +00:00
return nil
}
log.Debug().Msgf("Succesfully fetched latest commit's timestamp from branch: %#v", timestamp)
2022-07-27 13:39:46 +00:00
return timestamp
2021-12-05 13:47:33 +00:00
}