Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: filter repositories that are forks #341

Merged
merged 3 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions cmd/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func configurePlatform(cmd *cobra.Command) {
flags.StringSliceP("project", "P", nil, "The name, including owner of a GitLab project in the format \"ownerName/repoName\".")
flags.BoolP("include-subgroups", "", false, "Include GitLab subgroups when using the --group flag.")
flags.BoolP("ssh-auth", "", false, `Use SSH cloning URL instead of HTTPS + token. This requires that a setup with ssh keys that have access to all repos and that the server is already in known_hosts.`)
flags.BoolP("skip-forks", "", false, `Skip repositories which are forks.`)

flags.StringP("platform", "p", "github", "The platform that is used. Available values: github, gitlab, gitea, bitbucket_server.")
_ = cmd.RegisterFlagCompletionFunc("platform", func(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
Expand Down Expand Up @@ -125,6 +126,7 @@ func createGithubClient(flag *flag.FlagSet, verifyFlags bool, readOnly bool) (mu
forkMode, _ := flag.GetBool("fork")
forkOwner, _ := flag.GetString("fork-owner")
sshAuth, _ := flag.GetBool("ssh-auth")
skipForks, _ := flag.GetBool("skip-forks")

if verifyFlags && len(orgs) == 0 && len(users) == 0 && len(repos) == 0 {
return nil, errors.New("no organization, user or repo set")
Expand Down Expand Up @@ -166,6 +168,7 @@ func createGithubClient(flag *flag.FlagSet, verifyFlags bool, readOnly bool) (mu
Users: users,
Repositories: repoRefs,
Topics: topics,
SkipForks: skipForks,
},
MergeTypes: mergeTypes,
ForkMode: forkMode,
Expand All @@ -189,6 +192,7 @@ func createGitlabClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versi
topics, _ := flag.GetStringSlice("topic")
includeSubgroups, _ := flag.GetBool("include-subgroups")
sshAuth, _ := flag.GetBool("ssh-auth")
skipForks, _ := flag.GetBool("skip-forks")

if verifyFlags && len(groups) == 0 && len(users) == 0 && len(projects) == 0 {
return nil, errors.New("no group user or project set")
Expand All @@ -208,10 +212,11 @@ func createGitlabClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versi
}

vc, err := gitlab.New(token, gitBaseURL, gitlab.RepositoryListing{
Groups: groups,
Users: users,
Projects: projRefs,
Topics: topics,
Groups: groups,
Users: users,
Projects: projRefs,
Topics: topics,
SkipForks: skipForks,
}, gitlab.Config{
IncludeSubgroups: includeSubgroups,
SSHAuth: sshAuth,
Expand All @@ -230,6 +235,7 @@ func createGiteaClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versio
repos, _ := flag.GetStringSlice("repo")
topics, _ := flag.GetStringSlice("topic")
sshAuth, _ := flag.GetBool("ssh-auth")
skipForks, _ := flag.GetBool("skip-forks")

if verifyFlags && len(orgs) == 0 && len(users) == 0 && len(repos) == 0 {
return nil, errors.New("no organization, user or repository set")
Expand Down Expand Up @@ -262,6 +268,7 @@ func createGiteaClient(flag *flag.FlagSet, verifyFlags bool) (multigitter.Versio
Users: users,
Repositories: repoRefs,
Topics: topics,
SkipForks: skipForks,
}, mergeTypes, sshAuth)
if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions internal/scm/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type RepositoryListing struct {
Users []string
Repositories []RepositoryReference
Topics []string
SkipForks bool
}

// RepositoryReference contains information to be able to reference a repository
Expand Down Expand Up @@ -100,6 +101,11 @@ func (g *Gitea) GetRepositories(ctx context.Context) ([]scm.Repository, error) {
for _, repo := range allRepos {
log := log.WithField("repo", repo.FullName)

if g.SkipForks && repo.Fork {
log.Debug("Skipping repository since it's a fork")
continue
}

if len(g.Topics) != 0 {
topics, err := g.getRepoTopics(ctx, repo)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/scm/gitea/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (g *Gitea) convertRepository(repo *gitea.Repository) (repository, error) {
name: repo.Name,
ownerName: repo.Owner.UserName,
defaultBranch: repo.DefaultBranch,
fork: repo.Fork,
}, nil
}

Expand All @@ -34,6 +35,7 @@ type repository struct {
name string
ownerName string
defaultBranch string
fork bool
}

func (r repository) CloneURL() string {
Expand All @@ -47,3 +49,7 @@ func (r repository) DefaultBranch() string {
func (r repository) FullName() string {
return fmt.Sprintf("%s/%s", r.ownerName, r.name)
}

func (r repository) Fork() bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is a relic from when an implementation where the filtering was supposed to be done in the multigitter package instead? I would have been fine with that (only drawback is that there are potential that SCMs allow the filtering to be done at API level). But now this should be dead code?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same is true for the field in the repository struct?

return r.fork
}
4 changes: 4 additions & 0 deletions internal/scm/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type RepositoryListing struct {
Users []string
Repositories []RepositoryReference
Topics []string
SkipForks bool
}

// RepositoryReference contains information to be able to reference a repository
Expand Down Expand Up @@ -158,6 +159,9 @@ func (g *Github) GetRepositories(ctx context.Context) ([]scm.Repository, error)
case len(g.Topics) != 0 && !scm.RepoContainsTopic(r.Topics, g.Topics):
log.Debug("Skipping repository since it does not match repository topics")
continue
case g.SkipForks && r.GetFork():
log.Debug("Skipping repository since it's a fork")
continue
}

if g.checkPermissions {
Expand Down
29 changes: 29 additions & 0 deletions internal/scm/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func Test_GetRepositories(t *testing.T) {
"site_admin": false
},
"html_url": "https://github.com/test-org/test1",
"fork": false,
"archived": false,
"disabled": false,
"default_branch": "master",
Expand All @@ -80,6 +81,7 @@ func Test_GetRepositories(t *testing.T) {
"site_admin": false
},
"html_url": "https://github.com/test-org/test1",
"fork": false,
"archived": false,
"disabled": false,
"default_branch": "master",
Expand All @@ -106,6 +108,7 @@ func Test_GetRepositories(t *testing.T) {
"site_admin": false
},
"html_url": "https://github.com/lindell/test2",
"fork": true,
"archived": false,
"disabled": false,
"default_branch": "main",
Expand Down Expand Up @@ -229,4 +232,30 @@ func Test_GetRepositories(t *testing.T) {
assert.Equal(t, "lindell/test2", repos[1].FullName())
}
}

// Forks
{
gh, err := github.New(github.Config{
TransportMiddleware: transport.Wrapper,
RepoListing: github.RepositoryListing{
Organizations: []string{"test-org"},
Users: []string{"test-user"},
Repositories: []github.RepositoryReference{
{
OwnerName: "test-org",
Name: "test1",
},
},
SkipForks: true,
},
MergeTypes: []scm.MergeType{scm.MergeTypeMerge},
})
require.NoError(t, err)

repos, err := gh.GetRepositories(context.Background())
assert.NoError(t, err)
if assert.Len(t, repos, 1) {
assert.Equal(t, "test-org/test1", repos[0].FullName())
}
}
}
13 changes: 9 additions & 4 deletions internal/scm/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ type Gitlab struct {

// RepositoryListing contains information about which repositories that should be fetched
type RepositoryListing struct {
Groups []string
Users []string
Projects []ProjectReference
Topics []string
Groups []string
Users []string
Projects []ProjectReference
Topics []string
SkipForks bool
}

// Config includes extra config parameters for the GitLab client
Expand Down Expand Up @@ -96,6 +97,10 @@ func (g *Gitlab) GetRepositories(ctx context.Context) ([]scm.Repository, error)
log.Debug("Skipping repository since it does not match repository topics")
continue
}
if g.SkipForks && project.ForkedFromProject != nil {
log.Debug("Skipping repository since it's a fork")
continue
}

p, err := g.convertProject(project)
if err != nil {
Expand Down