2021-12-05 13:47:33 +00:00
|
|
|
package upstream
|
|
|
|
|
|
|
|
import (
|
2022-11-11 14:27:18 +00:00
|
|
|
"errors"
|
2022-11-12 16:50:08 +00:00
|
|
|
"fmt"
|
2022-11-11 14:27:18 +00:00
|
|
|
|
2022-09-18 14:13:27 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-12-05 13:47:33 +00:00
|
|
|
|
2022-06-11 21:02:06 +00:00
|
|
|
"codeberg.org/codeberg/pages/server/gitea"
|
2021-12-05 13:47:33 +00:00
|
|
|
)
|
|
|
|
|
2022-11-12 16:50:08 +00:00
|
|
|
// GetBranchTimestamp finds the default branch (if branch is "") and save branch and it's last modification time to Options
|
|
|
|
func (o *Options) GetBranchTimestamp(giteaClient *gitea.Client) (bool, error) {
|
|
|
|
log := log.With().Strs("BranchInfo", []string{o.TargetOwner, o.TargetRepo, o.TargetBranch}).Logger()
|
2022-09-18 17:47:20 +00:00
|
|
|
|
2022-11-12 16:50:08 +00:00
|
|
|
if len(o.TargetBranch) == 0 {
|
2021-12-05 13:47:33 +00:00
|
|
|
// Get default branch
|
2022-11-12 16:50:08 +00:00
|
|
|
defaultBranch, err := giteaClient.GiteaGetRepoDefaultBranch(o.TargetOwner, o.TargetRepo)
|
2022-04-20 21:42:01 +00:00
|
|
|
if err != nil {
|
2022-09-18 17:50:41 +00:00
|
|
|
log.Err(err).Msg("Could't fetch default branch from repository")
|
2022-11-12 16:50:08 +00:00
|
|
|
return false, err
|
2021-12-05 13:47:33 +00:00
|
|
|
}
|
2022-11-11 23:21:45 +00:00
|
|
|
log.Debug().Msgf("Succesfully fetched default branch %q from Gitea", defaultBranch)
|
2022-11-12 16:50:08 +00:00
|
|
|
o.TargetBranch = defaultBranch
|
2021-12-05 13:47:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 16:50:08 +00:00
|
|
|
timestamp, err := giteaClient.GiteaGetRepoBranchTimestamp(o.TargetOwner, o.TargetRepo, o.TargetBranch)
|
2022-04-20 21:42:01 +00:00
|
|
|
if err != nil {
|
2022-11-11 14:27:18 +00:00
|
|
|
if !errors.Is(err, gitea.ErrorNotFound) {
|
|
|
|
log.Error().Err(err).Msg("Could not get latest commit's timestamp from branch")
|
|
|
|
}
|
2022-11-12 16:50:08 +00:00
|
|
|
return false, err
|
2021-12-05 13:47:33 +00:00
|
|
|
}
|
2022-11-12 16:50:08 +00:00
|
|
|
|
|
|
|
if timestamp == nil || timestamp.Branch == "" {
|
|
|
|
return false, fmt.Errorf("empty response")
|
|
|
|
}
|
|
|
|
|
2022-09-18 17:50:41 +00:00
|
|
|
log.Debug().Msgf("Succesfully fetched latest commit's timestamp from branch: %#v", timestamp)
|
2022-11-12 16:50:08 +00:00
|
|
|
o.BranchTimestamp = timestamp.Timestamp
|
|
|
|
o.TargetBranch = timestamp.Branch
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Options) ContentWebLink(giteaClient *gitea.Client) string {
|
|
|
|
return giteaClient.ContentWebLink(o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath) + "; rel=\"canonical\""
|
2021-12-05 13:47:33 +00:00
|
|
|
}
|