From 5585e109330aa6fa6bf7078ea4bf015daab29cff Mon Sep 17 00:00:00 2001 From: Eric Ripa Date: Thu, 9 Aug 2018 10:06:10 -0700 Subject: [PATCH] Change error handling in getRepo functions Instead of returning an empty list of repositories and an error if any cloning operation fails, simply store the error in the Repo instance and always return all repos. Then later on check for errors during the audit step. By having this approach we don't prematurely exit the scanning if there are problems during cloning, such as if a repository is empty and fails to clone. --- main.go | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index eb675856..a99cd11b 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,7 @@ type Repo struct { name string leaks []Leak repository *git.Repository + err error } // Owner contains a collection of repos. This could represent an org or user. @@ -232,6 +233,10 @@ func main() { repos, err = getOwnerRepos() } for _, r := range repos { + if r.err != nil { + log.Warnf("skipping audit for repo %s due to cloning error: %s", r.name, r.err) + continue + } l, err := auditRepo(r.repository) if len(l) == 0 { log.Infof("no leaks found for repo %s", r.name) @@ -239,7 +244,7 @@ func main() { log.Warnf("leaks found for repo %s", r.name) } if err != nil { - log.Fatal(err) + log.Fatalf("error during audit: %s", err) } leaks = append(leaks, l...) } @@ -249,7 +254,7 @@ func main() { } if len(leaks) != 0 { - log.Debug("leaks detected") + log.Errorf("leaks detected") os.Exit(1) } } @@ -303,14 +308,12 @@ func getRepo() (Repo, error) { }) } } - if err != nil { - return Repo{}, err - } return Repo{ repository: r, path: opts.RepoPath, url: opts.Repo, name: filepath.Base(opts.Repo), + err: err, }, nil } @@ -590,13 +593,11 @@ func getUserGithubRepos(ctx context.Context, listOpts *github.RepositoryListOpti }) } } - if err != nil { - return repos, fmt.Errorf("problem cloning %s -- %v", *rDesc.Name, err) - } repos = append(repos, Repo{ name: *rDesc.Name, url: *rDesc.SSHURL, repository: r, + err: err, }) } if resp.NextPage == 0 { @@ -654,18 +655,14 @@ func getOrgGithubRepos(ctx context.Context, listOpts *github.RepositoryListByOrg }) } } - if err != nil { - return nil, err - } repos = append(repos, Repo{ url: *rDesc.SSHURL, name: *rDesc.Name, repository: r, + err: err, }) } - if err != nil { - return nil, err - } else if resp.NextPage == 0 { + if resp.NextPage == 0 { break } listOpts.Page = resp.NextPage