From d622367738b15fb593255c4725f81317b205218c Mon Sep 17 00:00:00 2001 From: Seow Alex Date: Mon, 24 Apr 2023 03:11:12 +0800 Subject: [PATCH] Backup description and visibility (#146) --- bitbucket/bitbucket.go | 4 ++++ gitea/gitea.go | 38 +++++++++++++++++++++++++--------- github/github.go | 8 +++++++ gitlab/gitlab.go | 47 +++++++++++++++++++++++++++++++++--------- gogs/gogs.go | 18 ++++++++++++++++ onedev/onedev.go | 2 ++ sourcehut/sourcehut.go | 8 +++++++ types/types.go | 2 ++ 8 files changed, 107 insertions(+), 20 deletions(-) diff --git a/bitbucket/bitbucket.go b/bitbucket/bitbucket.go index 1a0a931..1f1eaaf 100644 --- a/bitbucket/bitbucket.go +++ b/bitbucket/bitbucket.go @@ -84,6 +84,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: user, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Is_private, }) continue @@ -103,6 +105,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: user, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Is_private, }) } } diff --git a/gitea/gitea.go b/gitea/gitea.go index f411ff7..1baf013 100644 --- a/gitea/gitea.go +++ b/gitea/gitea.go @@ -22,21 +22,21 @@ func getOrgVisibility(visibility string) gitea.VisibleType { } } -func getRepoVisibility(visibility string) bool { +func getRepoVisibility(visibility string, private bool) bool { switch visibility { case "public": return false case "private": return true default: - return true + return private } } // Backup TODO. func Backup(r types.Repo, d types.GenRepo, dry bool) bool { orgvisibilty := getOrgVisibility(d.Visibility.Organizations) - repovisibility := getRepoVisibility(d.Visibility.Repositories) + repovisibility := getRepoVisibility(d.Visibility.Repositories, r.Private) if d.URL == "" { d.URL = "https://gitea.com/" } @@ -100,13 +100,14 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) bool { repo, _, err := giteaclient.GetRepo(user.UserName, r.Name) if err != nil { opts := gitea.MigrateRepoOption{ - RepoName: r.Name, - RepoOwner: user.UserName, - Mirror: true, - CloneAddr: r.URL, - AuthToken: r.Token, - Wiki: r.Origin.Wiki, - Private: repovisibility, + RepoName: r.Name, + RepoOwner: user.UserName, + Mirror: true, + CloneAddr: r.URL, + AuthToken: r.Token, + Wiki: r.Origin.Wiki, + Private: repovisibility, + Description: r.Description, } if r.Token == "" { @@ -119,6 +120,7 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) bool { AuthPassword: r.Origin.Password, Wiki: r.Origin.Wiki, Private: repovisibility, + Description: r.Description, } } @@ -304,6 +306,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) if r.HasWiki && repo.Wiki && types.StatRemote(r.CloneURL, r.SSHURL, repo) { repos = append(repos, types.Repo{ @@ -315,6 +319,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) } @@ -335,6 +341,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) if r.HasWiki && repo.Wiki && types.StatRemote(r.CloneURL, r.SSHURL, repo) { repos = append(repos, types.Repo{ @@ -346,6 +354,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) } } @@ -438,6 +448,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) if r.HasWiki && repo.Wiki && types.StatRemote(r.CloneURL, r.SSHURL, repo) { repos = append(repos, types.Repo{ @@ -449,6 +461,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) } @@ -469,6 +483,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) if r.HasWiki && repo.Wiki && types.StatRemote(r.CloneURL, r.SSHURL, repo) { repos = append(repos, types.Repo{ @@ -480,6 +496,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) } } diff --git a/github/github.go b/github/github.go index ba8ed47..fec2dec 100644 --- a/github/github.go +++ b/github/github.go @@ -89,6 +89,8 @@ func addWiki(r github.Repository, repo types.GenRepo, token string) types.Repo { Origin: repo, Owner: r.GetOwner().GetLogin(), Hoster: "github.com", + Description: r.GetDescription(), + Private: r.GetPrivate(), } } @@ -256,6 +258,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.GetOwner().GetLogin(), Hoster: "github.com", + Description: r.GetDescription(), + Private: r.GetPrivate(), }) wiki := addWiki(*r, repo, token) if wiki.Name != "" { @@ -285,6 +289,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.GetOwner().GetLogin(), Hoster: "github.com", + Description: r.GetDescription(), + Private: r.GetPrivate(), }) wiki := addWiki(*r, repo, token) if wiki.Name != "" { @@ -301,6 +307,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.GetOwner().GetLogin(), Hoster: "github.com", + Description: r.GetDescription(), + Private: r.GetPrivate(), }) wiki := addWiki(*r, repo, token) if wiki.Name != "" { diff --git a/gitlab/gitlab.go b/gitlab/gitlab.go index 4dbb863..8b313bc 100644 --- a/gitlab/gitlab.go +++ b/gitlab/gitlab.go @@ -67,10 +67,20 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) bool { splittedurl[0], r.Origin.User, r.Origin.Password, splittedurl[1]) } + var visibility gitlab.VisibilityValue + + if r.Private { + visibility = gitlab.PrivateVisibility + } else { + visibility = gitlab.PublicVisibility + } + opts := &gitlab.CreateProjectOptions{ - Mirror: &True, - ImportURL: &r.URL, - Name: &r.Name, + Mirror: &True, + ImportURL: &r.URL, + Name: &r.Name, + Description: &r.Description, + Visibility: gitlab.Visibility(visibility), } _, _, err = gitlabclient.Projects.CreateProject(opts) @@ -226,6 +236,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Namespace.FullPath, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == gitlab.PrivateVisibility, }) } @@ -242,6 +254,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Namespace.FullPath, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == gitlab.PrivateVisibility, }) } } @@ -262,6 +276,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Namespace.FullPath, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == gitlab.PrivateVisibility, }) } @@ -278,6 +294,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Namespace.FullPath, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == gitlab.PrivateVisibility, }) } } @@ -384,6 +402,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: k, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == gitlab.PrivateVisibility, }) } @@ -400,6 +420,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: k, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == gitlab.PrivateVisibility, }) } } @@ -424,6 +446,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: k, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == gitlab.PrivateVisibility, }) } @@ -432,13 +456,16 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { httpURLToRepo := types.DotGitRx.ReplaceAllString(r.HTTPURLToRepo, ".wiki.git") sshURLToRepo := types.DotGitRx.ReplaceAllString(r.SSHURLToRepo, ".wiki.git") repos = append(repos, types.Repo{ - Name: r.Path + ".wiki", - URL: httpURLToRepo, - SSHURL: sshURLToRepo, - Token: token, Defaultbranch: r.DefaultBranch, - Origin: repo, - Owner: k, - Hoster: types.GetHost(repo.URL), + Name: r.Path + ".wiki", + URL: httpURLToRepo, + SSHURL: sshURLToRepo, + Token: token, + Defaultbranch: r.DefaultBranch, + Origin: repo, + Owner: k, + Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == gitlab.PrivateVisibility, }) } } diff --git a/gogs/gogs.go b/gogs/gogs.go index 086af61..ac75213 100644 --- a/gogs/gogs.go +++ b/gogs/gogs.go @@ -81,6 +81,7 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) bool { CloneAddr: r.URL, AuthUsername: r.Token, Private: repovisibility, + Description: r.Description, } if r.Token == "" { @@ -92,6 +93,7 @@ func Backup(r types.Repo, d types.GenRepo, dry bool) bool { AuthUsername: r.Origin.User, AuthPassword: r.Origin.Password, Private: repovisibility, + Description: r.Description, } } @@ -195,6 +197,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) if repo.Wiki { repos = append(repos, types.Repo{ @@ -206,6 +210,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) } @@ -226,6 +232,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) if repo.Wiki { repos = append(repos, types.Repo{ @@ -237,6 +245,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) } } @@ -313,6 +323,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) if repo.Wiki { repos = append(repos, types.Repo{ @@ -324,6 +336,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) } @@ -344,6 +358,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) if repo.Wiki { @@ -356,6 +372,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.UserName, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Private, }) } } diff --git a/onedev/onedev.go b/onedev/onedev.go index 8e234c9..ca10a80 100644 --- a/onedev/onedev.go +++ b/onedev/onedev.go @@ -138,6 +138,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: repo.User, Hoster: types.GetHost(repo.URL), + Description: r.Description, }) } @@ -204,6 +205,7 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: org, Hoster: types.GetHost(repo.URL), + Description: r.Description, }) } } diff --git a/sourcehut/sourcehut.go b/sourcehut/sourcehut.go index 3e4ea3c..7de5e88 100644 --- a/sourcehut/sourcehut.go +++ b/sourcehut/sourcehut.go @@ -228,6 +228,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.Name, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == "private", }) if repo.Wiki { repos = append(repos, types.Repo{ @@ -239,6 +241,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.Name, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == "private", }) } @@ -259,6 +263,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.Name, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == "private", }) if repo.Wiki { refs, err := getRefs(apiURL, fmt.Sprintf("%s-docs", r.Name), token) @@ -283,6 +289,8 @@ func Get(conf *types.Conf) ([]types.Repo, bool) { Origin: repo, Owner: r.Owner.Name, Hoster: types.GetHost(repo.URL), + Description: r.Description, + Private: r.Visibility == "private", }) } } diff --git a/types/types.go b/types/types.go index 54befcc..dc90a23 100644 --- a/types/types.go +++ b/types/types.go @@ -338,6 +338,8 @@ type Repo struct { Origin GenRepo Owner string Hoster string + Description string + Private bool } // Site TODO.