Skip to content

Commit

Permalink
implemented mirror using gickup and not giteas mirror function (#225)
Browse files Browse the repository at this point in the history
* implemented mirror using gickup and not giteas mirror function

* added "mirror" to gogs

* push to gogs

* mirror is now a struct

* move deprecation notice, no need to spam
  • Loading branch information
cooperspencer authored Apr 15, 2024
1 parent 61d5070 commit 6a293dd
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 15 deletions.
7 changes: 6 additions & 1 deletion conf.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,11 @@ destination:
user: some-name # can be a user or an organization, it must exist on the system
url: http(s)://url-to-gitea
createorg: true # creates an organization if it doesn't exist already, if no user is set it creates an organization with the name of the original author
mirrorinterval: 2h0m0s # interval to pull changes from source repo
mirrorinterval: 2h0m0s # interval to pull changes from source repo, will be removed in one of the next releases
lfs: false # trigger to enable lfs on gitea
mirror:
enabled: true # if set to true, gickup will clone the repository and push it to gitea itself
mirrorinterval: 2h0m0s # interval to pull changes from source repo
visibility:
repositories: private # private, public, default: private
organizations: private # private, limited, public, default: private
Expand All @@ -216,6 +219,8 @@ destination:
user: some-name # can be a user or an organization, it must exist on the system
url: http(s)://url-to-gogs
createorg: true # creates an organization if it doesn't exist already, if no user is set it creates an organization with the name of the original author
mirror:
enabled: true # if set to true, gickup will clone the repository and push it to gogs itself
visibility:
repositories: private # private, public, default: private
gitlab:
Expand Down
96 changes: 88 additions & 8 deletions gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) bool {
sub.Info().
Msgf("mirroring %s to %s", types.Blue(r.Name), d.URL)

mirrorInterval := "8h0m0s"

if d.MirrorInterval != "" {
mirrorInterval = d.MirrorInterval
}

if d.Mirror.MirrorInterval != "" {
mirrorInterval = d.Mirror.MirrorInterval
}

giteaclient, err := gitea.NewClient(d.URL, gitea.SetToken(d.GetToken()))
if err != nil {
sub.Error().Msg(err.Error())
Expand Down Expand Up @@ -106,7 +116,7 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) bool {
Wiki: r.Origin.Wiki,
Private: repovisibility,
Description: r.Description,
MirrorInterval: d.MirrorInterval,
MirrorInterval: mirrorInterval,
LFS: d.LFS,
}

Expand All @@ -121,7 +131,7 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) bool {
Wiki: r.Origin.Wiki,
Private: repovisibility,
Description: r.Description,
MirrorInterval: d.MirrorInterval,
MirrorInterval: mirrorInterval,
LFS: d.LFS,
}
}
Expand All @@ -148,14 +158,16 @@ 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 mirrorInterval != "" {
_, err = time.ParseDuration(mirrorInterval)
if err != nil {
sub.Warn().Msgf("%s is not a valid duration!", mirrorInterval)
mirrorInterval = repo.MirrorInterval
}
}

if d.MirrorInterval != repo.MirrorInterval {
_, _, err := giteaclient.EditRepo(user.UserName, r.Name, gitea.EditRepoOption{MirrorInterval: &d.MirrorInterval})
if mirrorInterval != repo.MirrorInterval {
_, _, err := giteaclient.EditRepo(user.UserName, r.Name, gitea.EditRepoOption{MirrorInterval: &mirrorInterval})
if err != nil {
sub.Error().
Err(err).
Expand Down Expand Up @@ -559,3 +571,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
}
60 changes: 60 additions & 0 deletions gogs/gogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,63 @@ func GetIssues(repo *gogs.Repository, client *gogs.Client, conf types.GenRepo) m
}
return issues
}

// GetOrCreate Get or create a repository
func GetOrCreate(destination types.GenRepo, repo types.Repo) (string, error) {
repovisibility := getRepoVisibility(destination.Visibility.Repositories, repo.Private)
sub = logger.CreateSubLogger("stage", "gogs", "url", destination.URL)

gogsclient := gogs.NewClient(destination.URL, destination.GetToken())

user, err := gogsclient.GetSelfInfo()
if err != nil {
return "", err
}
me := user

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

if destination.User != "" {
user, err = gogsclient.GetUserInfo(destination.User)
if err != nil {
if destination.CreateOrg {
org, err := gogsclient.CreateOrg(gogs.CreateOrgOption{
UserName: destination.User,
})
if err != nil {
return "", err
}
user.ID = org.ID
user.UserName = org.UserName
} else {
return "", err
}
}

}

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

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

return r.CloneURL, nil
}
Loading

0 comments on commit 6a293dd

Please sign in to comment.