1
1
mirror of https://github.com/cooperspencer/gickup synced 2024-11-08 12:09:18 +01:00

improved gitlab, gitea and gogs organization/group clones

This commit is contained in:
Andreas Wachter 2021-12-14 08:45:13 +01:00
parent 838b233a43
commit 5befa164c5
4 changed files with 99 additions and 13 deletions

@ -57,9 +57,9 @@ func Get(conf *types.Conf) []types.Repo {
opt.PageSize = 50
i := 0
gitearepos := []*gitea.Repository{}
client, err := gitea.NewClient(repo.Url)
for {
opt.Page = i
client, err := gitea.NewClient(repo.Url)
if err != nil {
log.Fatal().Str("stage", "gitea").Str("url", repo.Url).Msg(err.Error())
}
@ -79,6 +79,7 @@ func Get(conf *types.Conf) []types.Repo {
include := types.GetMap(repo.Include)
exclude := types.GetMap(repo.Exclude)
excludeorgs := types.GetMap(repo.ExcludeOrgs)
for _, r := range gitearepos {
if include[r.Name] {
@ -92,6 +93,48 @@ func Get(conf *types.Conf) []types.Repo {
repos = append(repos, types.Repo{Name: r.Name, Url: r.CloneURL, SshUrl: r.SSHURL, Token: repo.Token, Defaultbranch: r.DefaultBranch, Origin: repo})
}
}
orgopt := gitea.ListOptions{Page: 1, PageSize: 50}
orgs := []*gitea.Organization{}
for {
o, _, err := client.ListUserOrgs(repo.User, gitea.ListOrgsOptions{ListOptions: orgopt})
if err != nil {
log.Fatal().Str("stage", "gitea").Str("url", repo.Url).Msg(err.Error())
}
if len(o) == 0 {
break
}
orgs = append(orgs, o...)
orgopt.Page++
}
orgopt.Page = 1
orgrepos := []*gitea.Repository{}
for _, org := range orgs {
if excludeorgs[org.UserName] {
continue
}
o, _, err := client.ListOrgRepos(org.UserName, gitea.ListOrgReposOptions{orgopt})
if err != nil {
log.Fatal().Str("stage", "gitea").Str("url", repo.Url).Msg(err.Error())
}
if len(o) == 0 {
break
}
orgrepos = append(orgrepos, o...)
orgopt.Page++
}
for _, r := range orgrepos {
if include[r.Name] {
repos = append(repos, types.Repo{Name: r.Name, Url: r.CloneURL, SshUrl: r.SSHURL, Token: repo.Token, Defaultbranch: r.DefaultBranch, Origin: repo})
continue
}
if exclude[r.Name] {
continue
}
if len(repo.Include) == 0 {
repos = append(repos, types.Repo{Name: r.Name, Url: r.CloneURL, SshUrl: r.SSHURL, Token: repo.Token, Defaultbranch: r.DefaultBranch, Origin: repo})
}
}
}
return repos
}

@ -92,7 +92,6 @@ func Get(conf *types.Conf) []types.Repo {
}
}
}
include := types.GetMap(repo.Include)
exclude := types.GetMap(repo.Exclude)
@ -108,18 +107,26 @@ func Get(conf *types.Conf) []types.Repo {
repos = append(repos, types.Repo{Name: r.Name, Url: r.HTTPURLToRepo, SshUrl: r.SSHURLToRepo, Token: repo.Token, Defaultbranch: r.DefaultBranch, Origin: repo})
}
}
groups, _, err := client.Groups.ListGroups(&gitlab.ListGroupsOptions{})
if err != nil {
log.Fatal().Str("stage", "gitlab").Str("url", repo.Url).Msg(err.Error())
}
if repo.Token != "" {
groups := []*gitlab.Group{}
i = 1
for {
g, _, err := client.Groups.ListGroups(&gitlab.ListGroupsOptions{ListOptions: gitlab.ListOptions{Page: i, PerPage: 50}})
if err != nil {
log.Fatal().Str("stage", "gitlab").Str("url", repo.Url).Msg(err.Error())
}
if len(g) == 0 {
break
}
groups = append(groups, g...)
i++
}
visibilities := []gitlab.VisibilityValue{gitlab.PrivateVisibility, gitlab.PublicVisibility, gitlab.InternalVisibility}
for _, visibility := range visibilities {
gopt := &gitlab.ListGroupProjectsOptions{Visibility: gitlab.Visibility(visibility)}
gopt.PerPage = 50
i = 0
gopt := &gitlab.ListGroupProjectsOptions{}
for _, group := range groups {
i = 1
gopt.PerPage = 50
gopt.Page = i
for {
projects, _, err := client.Groups.ListGroupProjects(group.ID, gopt)
if err != nil {

@ -51,6 +51,7 @@ func Get(conf *types.Conf) []types.Repo {
include := types.GetMap(repo.Include)
exclude := types.GetMap(repo.Exclude)
excludeorgs := types.GetMap(repo.ExcludeOrgs)
for _, r := range gogsrepos {
if include[r.Name] {
@ -64,6 +65,37 @@ func Get(conf *types.Conf) []types.Repo {
repos = append(repos, types.Repo{Name: r.Name, Url: r.CloneURL, SshUrl: r.SSHURL, Token: repo.Token, Defaultbranch: r.DefaultBranch, Origin: repo})
}
}
orgs, err := client.ListUserOrgs(repo.User)
if err != nil {
log.Fatal().Str("stage", "gogs").Str("url", repo.Url).Msg(err.Error())
}
orgrepos := []*gogs.Repository{}
for _, org := range orgs {
if excludeorgs[org.UserName] {
continue
}
o, err := client.ListOrgRepos(org.UserName)
if err != nil {
log.Fatal().Str("stage", "gogs").Str("url", repo.Url).Msg(err.Error())
}
if len(o) == 0 {
break
}
orgrepos = append(orgrepos, o...)
}
for _, r := range orgrepos {
if include[r.Name] {
repos = append(repos, types.Repo{Name: r.Name, Url: r.CloneURL, SshUrl: r.SSHURL, Token: repo.Token, Defaultbranch: r.DefaultBranch, Origin: repo})
continue
}
if exclude[r.Name] {
continue
}
if len(repo.Include) == 0 {
repos = append(repos, types.Repo{Name: r.Name, Url: r.CloneURL, SshUrl: r.SSHURL, Token: repo.Token, Defaultbranch: r.DefaultBranch, Origin: repo})
}
}
}
return repos
}

@ -109,7 +109,8 @@ func PlaysForever() {
}
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "2006-01-02T15:04:05Z07:00"})
timeformat := "2006-01-02T15:04:05Z07:00"
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: timeformat})
kong.Parse(&cli, kong.Name("gickup"), kong.Description("a tool to backup all your favorite repos"))
@ -122,6 +123,9 @@ func main() {
log.Info().Str("file", cli.Configfile).Msgf("Reading %s", types.Green(cli.Configfile))
conf := ReadConfigfile(cli.Configfile)
if conf.Log.Timeformat == "" {
conf.Log.Timeformat = timeformat
}
log.Logger = logger.CreateLogger(conf.Log)