gitea-pages-server/server/upstream/helper.go

37 lines
1.1 KiB
Go
Raw Normal View History

2021-12-05 14:47:33 +01:00
package upstream
import (
"errors"
2021-12-05 14:47:33 +01:00
"github.com/rs/zerolog/log"
2021-12-05 14:47:33 +01:00
"codeberg.org/codeberg/pages/server/gitea"
)
2021-12-05 14:47:33 +01: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)
func GetBranchTimestamp(giteaClient *gitea.Client, owner, repo, branch string) *gitea.BranchTimestamp {
log := log.With().Strs("BranchInfo", []string{owner, repo, branch}).Logger()
if len(branch) == 0 {
2021-12-05 14:47:33 +01: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 14:47:33 +01:00
return nil
}
log.Debug().Msgf("Succesfully fetched default branch %q from Gitea", defaultBranch)
branch = defaultBranch
2021-12-05 14:47:33 +01:00
}
timestamp, err := giteaClient.GiteaGetRepoBranchTimestamp(owner, repo, branch)
if err != nil {
if !errors.Is(err, gitea.ErrorNotFound) {
log.Error().Err(err).Msg("Could not get latest commit's timestamp from branch")
}
return nil
}
log.Debug().Msgf("Succesfully fetched latest commit's timestamp from branch: %#v", timestamp)
return timestamp
2021-12-05 14:47:33 +01:00
}