diff --git a/README.md b/README.md index 940bd773..5613c133 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ Application Options: --org= organization to audit --user= user to audit --pr= pull/merge request url + --exclude-forks audit excludes forks Help Options: -h, --help Show this help message diff --git a/hosts/github.go b/hosts/github.go index 1051293a..997f39e6 100644 --- a/hosts/github.go +++ b/hosts/github.go @@ -72,7 +72,15 @@ func (g *Github) Audit() { _githubRepos, resp, err = g.client.Repositories.ListByOrg(ctx, g.manager.Opts.Organization, &github.RepositoryListByOrgOptions{ListOptions: listOptions}) } - githubRepos = append(githubRepos, _githubRepos...) + + for _, r := range _githubRepos { + if g.manager.Opts.ExcludeForks && r.GetFork() { + log.Debugf("excluding forked repo: %s", *r.Name) + continue + } + githubRepos = append(githubRepos, r) + } + if resp == nil { break diff --git a/hosts/gitlab.go b/hosts/gitlab.go index 8cbb79d8..16843ad5 100644 --- a/hosts/gitlab.go +++ b/hosts/gitlab.go @@ -57,19 +57,26 @@ func (g *Gitlab) Audit() { glOpts := &gitlab.ListProjectsOptions{ ListOptions: listOpts, } - projects, resp, err = g.client.Projects.ListUserProjects(g.manager.Opts.User, glOpts) + _projects, resp, err = g.client.Projects.ListUserProjects(g.manager.Opts.User, glOpts) } else if g.manager.Opts.Organization != "" { glOpts := &gitlab.ListGroupProjectsOptions{ ListOptions: listOpts, } - projects, resp, err = g.client.Groups.ListGroupProjects(g.manager.Opts.Organization, glOpts) + _projects, resp, err = g.client.Groups.ListGroupProjects(g.manager.Opts.Organization, glOpts) } if err != nil { log.Error(err) } - projects = append(projects, _projects...) + for _, p := range _projects { + if g.manager.Opts.ExcludeForks && p.ForkedFromProject != nil { + log.Debugf("excluding forked repo: %s", p.Name) + continue + } + projects = append(projects, p) + } + if resp == nil { break } diff --git a/options/options.go b/options/options.go index 45ba6210..af471c0f 100644 --- a/options/options.go +++ b/options/options.go @@ -57,8 +57,9 @@ type Options struct { Host string `long:"host" description:"git hosting service like gitlab or github. Supported hosts include: Github, Gitlab"` BaseURL string `long:"baseurl" description:"Base URL for API requests. Defaults to the public GitLab or GitHub API, but can be set to a domain endpoint to use with a self hosted server."` Organization string `long:"org" description:"organization to audit"` - User string `long:"user" description:"user to audit"` //work + User string `long:"user" description:"user to audit"` PullRequest string `long:"pr" description:"pull/merge request url"` + ExcludeForks bool `long:"exclude-forks" description:"audit excludes forks"` } // ParseOptions is responsible for parsing options passed in by cli. An Options struct