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