Skip to content

Commit

Permalink
implemented mirror using gickup and not giteas mirror function
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperspencer committed Apr 12, 2024
1 parent 61d5070 commit a659ca3
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 7 deletions.
81 changes: 77 additions & 4 deletions gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func getRepoVisibility(visibility string, private bool) bool {

// Backup TODO.
func Backup(r types.Repo, d types.GenRepo, dry bool) bool {
if d.MirrorInterval == "" {
d.MirrorInterval = "8h0m0s"
}
orgvisibilty := getOrgVisibility(d.Visibility.Organizations)
repovisibility := getRepoVisibility(d.Visibility.Repositories, r.Private)
if d.URL == "" {
Expand Down Expand Up @@ -148,10 +151,12 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) bool {
return true
}

_, err = time.ParseDuration(d.MirrorInterval)
if err != nil {
sub.Warn().Msgf("%s is not a valid duration!", d.MirrorInterval)
d.MirrorInterval = repo.MirrorInterval
if d.MirrorInterval != "" {
_, err = time.ParseDuration(d.MirrorInterval)
if err != nil {
sub.Warn().Msgf("%s is not a valid duration!", d.MirrorInterval)
d.MirrorInterval = repo.MirrorInterval
}
}

if d.MirrorInterval != repo.MirrorInterval {
Expand Down Expand Up @@ -559,3 +564,71 @@ func GetIssues(repo *gitea.Repository, client *gitea.Client, conf types.GenRepo)
}
return issues
}

// GetOrCreate Get or create a repository
func GetOrCreate(destination types.GenRepo, repo types.Repo) (string, error) {
orgvisibilty := getOrgVisibility(destination.Visibility.Organizations)
repovisibility := getRepoVisibility(destination.Visibility.Repositories, repo.Private)
if destination.URL == "" {
destination.URL = "https://gitea.com/"
}
sub = logger.CreateSubLogger("stage", "gitea", "url", destination.URL)

giteaclient, err := gitea.NewClient(destination.URL, gitea.SetToken(destination.GetToken()))
if err != nil {
return "", err
}

user, _, err := giteaclient.GetMyUserInfo()
if err != nil {
return "", err
}
me := user

if destination.User == "" && destination.CreateOrg {
destination.User = repo.Owner
}

if destination.User != "" {
user, _, err = giteaclient.GetUserInfo(destination.User)
if err != nil {
if destination.CreateOrg {
org, _, err := giteaclient.CreateOrg(gitea.CreateOrgOption{
Name: destination.User,
Visibility: orgvisibilty,
})
if err != nil {
return "", err
}
user.ID = org.ID
user.UserName = org.UserName
} else {
return "", err
}
}

}

r, _, err := giteaclient.GetRepo(user.UserName, repo.Name)
if err != nil {
opts := gitea.CreateRepoOption{
Name: repo.Name,
Private: repovisibility,
Description: repo.Description,
}

if me.UserName == user.UserName {
r, _, err = giteaclient.CreateRepo(opts)
if err != nil {
return "", err
}
} else {
r, _, err = giteaclient.CreateOrgRepo(user.UserName, opts)
if err != nil {
return "", err
}
}
}

return r.CloneURL, nil
}
83 changes: 80 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,85 @@ func backup(repos []types.Repo, conf *types.Conf) {
if !strings.HasSuffix(r.Name, ".wiki") {
repotime := time.Now()
status := 0
if gitea.Backup(r, d, cli.Dry) {
prometheus.RepoTime.WithLabelValues(r.Hoster, r.Name, r.Owner, "gitea", d.URL).Set(time.Since(repotime).Seconds())
status = 1
if d.SelfMirror {
if !strings.HasSuffix(r.Name, ".wiki") {
repotime := time.Now()
status := 0

log.Info().
Str("stage", "gitea").
Str("url", d.URL).
Msgf("mirroring %s to %s", types.Blue(r.Name), d.URL)

if !cli.Dry {
tempdir, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("gitea-%x", repotime))
if err != nil {
log.Error().
Str("stage", "tempclone").
Str("url", r.URL).
Msg(err.Error())
continue
}

defer os.RemoveAll(tempdir)
temprepo, err := local.TempClone(r, tempdir)
if err != nil {
if err == git.NoErrAlreadyUpToDate {
log.Info().
Str("stage", "gitea").
Str("url", r.URL).
Msg(err.Error())
} else {
log.Error().
Str("stage", "tempclone").
Str("url", r.URL).
Str("git", "clone").
Msg(err.Error())
os.RemoveAll(tempdir)
continue
}
}

cloneurl, err := gitea.GetOrCreate(d, r)
if err != nil {
log.Error().
Str("stage", "gitea").
Str("url", r.URL).
Msg(err.Error())
os.RemoveAll(tempdir)
continue
}

err = local.CreateRemotePush(temprepo, d, cloneurl, r.Origin.LFS)
if err != nil {
if err == git.NoErrAlreadyUpToDate {
log.Info().
Str("stage", "gitea").
Str("url", r.URL).
Msg(err.Error())
} else {
log.Error().
Str("stage", "gitea").
Str("url", r.URL).
Str("git", "push").
Msg(err.Error())
os.RemoveAll(tempdir)
continue
}
}

prometheus.RepoTime.WithLabelValues(r.Hoster, r.Name, r.Owner, "gitea", d.URL).Set(time.Since(repotime).Seconds())
status = 1

prometheus.RepoSuccess.WithLabelValues(r.Hoster, r.Name, r.Owner, "gitea", d.URL).Set(float64(status))
prometheus.DestinationBackupsComplete.WithLabelValues("gitea").Inc()
}
}
} else {
if gitea.Backup(r, d, cli.Dry) {
prometheus.RepoTime.WithLabelValues(r.Hoster, r.Name, r.Owner, "gitea", d.URL).Set(time.Since(repotime).Seconds())
status = 1
}
}

prometheus.RepoSuccess.WithLabelValues(r.Hoster, r.Name, r.Owner, "gitea", d.URL).Set(float64(status))
Expand Down Expand Up @@ -609,6 +685,7 @@ func main() {
return
}

zerolog.SetGlobalLevel(zerolog.InfoLevel)
if cli.Quiet {
zerolog.SetGlobalLevel(zerolog.WarnLevel)
}
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ type GenRepo struct {
Contributed bool `yaml:"contributed"`
MirrorInterval string `yaml:"mirrorinterval"`
LFS bool `yaml:"lfs"`
SelfMirror bool `yaml:"selfmirror"`
}

// Visibility struct
Expand Down

0 comments on commit a659ca3

Please sign in to comment.