Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ github:
# (default: [])
repositories:
- ChappIO/git-backup
# (optional) Backup all public repositories of these
# organizations, even if you are not a member.
# Will be merged with the other discovery mechanisms
# and it's result filtered with excluded repos.
# (default: [])
organization:
- golang
# (optional) Exclude this list of repos
# or whole organizations/users
exclude:
Expand Down Expand Up @@ -85,6 +92,13 @@ gitlab:
repositories:
- gnuwget/wget2
- gitlab-org/api/client-go
# (optional) Backup all public projects of these groups
# (including subgroups), even if you are not a member.
# Will be merged with the other discovery mechanisms
# and it's result filtered with excluded repos.
# (default: [])
organization:
- gnuwget
# (optional) Exclude this list of repos
# or whole organizations/users
exclude:
Expand Down
26 changes: 26 additions & 0 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type GithubConfig struct {
Owned *bool `yaml:"owned,omitempty"`
Exclude []string `yaml:"exclude,omitempty"`
Repositories []string `yaml:"repositories,omitempty"`
Organization []string `yaml:"organization,omitempty"`
client *github.Client
}

Expand Down Expand Up @@ -116,6 +117,21 @@ func (c *GithubConfig) getAllRepos() ([]*github.Repository, error) {
// use discovery mechanisms
var err error

for _, org := range c.Organization {
for repos, response, apiErr := c.getOrgRepos(org, 1); true; repos, response, apiErr = c.getOrgRepos(org, response.NextPage) {
if apiErr != nil {
return nil, apiErr
}
for _, repo := range repos {
all[*repo.FullName] = repo
}

if len(repos) == 0 || response.NextPage == 0 {
break
}
}
}

for repos, response, apiErr := c.discoverRepos(1); true; repos, response, apiErr = c.discoverRepos(response.NextPage) {
if apiErr != nil {
err = apiErr
Expand Down Expand Up @@ -175,6 +191,16 @@ func (c *GithubConfig) getRepoList(repos []string) ([]*github.Repository, error)
return ghRepos, nil
}

func (c *GithubConfig) getOrgRepos(org string, page int) ([]*github.Repository, *github.Response, error) {
return c.client.Repositories.ListByOrg(context.Background(), org, &github.RepositoryListByOrgOptions{
Type: "public",
ListOptions: github.ListOptions{
Page: page,
PerPage: 100,
},
})
}

func (c *GithubConfig) discoverRepos(page int) ([]*github.Repository, *github.Response, error) {
affiliations := make([]string, 0)

Expand Down
37 changes: 37 additions & 0 deletions gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type GitLabConfig struct {
Owned *bool `yaml:"owned,omitempty"`
Exclude []string `yaml:"exclude,omitempty"`
Repositories []string `yaml:"repositories,omitempty"`
Organization []string `yaml:"organization,omitempty"`
client *gitlab.Client
}

Expand Down Expand Up @@ -101,6 +102,16 @@ func (g *GitLabConfig) getAllRepos() ([]*gitlab.Project, error) {
}

// use discovery mechanisms
for _, group := range g.Organization {
if repos, err := g.getGroupRepos(group); err != nil {
return nil, err
} else {
for _, repo := range repos {
all[repo.PathWithNamespace] = repo
}
}
}

if *g.Starred {
if repos, err := g.discoverRepos(&gitlab.ListProjectsOptions{Starred: boolPointer(true)}); err != nil {
return nil, err
Expand Down Expand Up @@ -149,6 +160,32 @@ func (g *GitLabConfig) getRepoList(repos []string) ([]*gitlab.Project, error) {

}

func (g *GitLabConfig) getGroupRepos(group string) ([]*gitlab.Project, error) {
opts := &gitlab.ListGroupProjectsOptions{
IncludeSubGroups: boolPointer(true),
Simple: boolPointer(true),
Visibility: gitlab.Ptr(gitlab.PublicVisibility),
}
opts.ListOptions.PerPage = 100

repos := make([]*gitlab.Project, 0, 100)

// paginate
for i := 1; true; i++ {
opts.ListOptions.Page = i
projects, _, err := g.client.Groups.ListGroupProjects(group, opts)
if err != nil {
return nil, err
}

if len(projects) == 0 {
break
}
repos = append(repos, projects...)
}
return repos, nil
}

func (g *GitLabConfig) discoverRepos(opts *gitlab.ListProjectsOptions) ([]*gitlab.Project, error) {

opts.ListOptions.PerPage = 100
Expand Down