Răsfoiți Sursa

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.
Eric Ripa 7 ani în urmă
părinte
comite
5585e10933
1 a modificat fișierele cu 11 adăugiri și 14 ștergeri
  1. 11 14
      main.go

+ 11 - 14
main.go

@@ -51,6 +51,7 @@ type Repo struct {
 	name       string
 	name       string
 	leaks      []Leak
 	leaks      []Leak
 	repository *git.Repository
 	repository *git.Repository
+	err        error
 }
 }
 
 
 // Owner contains a collection of repos. This could represent an org or user.
 // Owner contains a collection of repos. This could represent an org or user.
@@ -232,6 +233,10 @@ func main() {
 		repos, err = getOwnerRepos()
 		repos, err = getOwnerRepos()
 	}
 	}
 	for _, r := range repos {
 	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)
 		l, err := auditRepo(r.repository)
 		if len(l) == 0 {
 		if len(l) == 0 {
 			log.Infof("no leaks found for repo %s", r.name)
 			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)
 			log.Warnf("leaks found for repo %s", r.name)
 		}
 		}
 		if err != nil {
 		if err != nil {
-			log.Fatal(err)
+			log.Fatalf("error during audit: %s", err)
 		}
 		}
 		leaks = append(leaks, l...)
 		leaks = append(leaks, l...)
 	}
 	}
@@ -249,7 +254,7 @@ func main() {
 	}
 	}
 
 
 	if len(leaks) != 0 {
 	if len(leaks) != 0 {
-		log.Debug("leaks detected")
+		log.Errorf("leaks detected")
 		os.Exit(1)
 		os.Exit(1)
 	}
 	}
 }
 }
@@ -303,14 +308,12 @@ func getRepo() (Repo, error) {
 			})
 			})
 		}
 		}
 	}
 	}
-	if err != nil {
-		return Repo{}, err
-	}
 	return Repo{
 	return Repo{
 		repository: r,
 		repository: r,
 		path:       opts.RepoPath,
 		path:       opts.RepoPath,
 		url:        opts.Repo,
 		url:        opts.Repo,
 		name:       filepath.Base(opts.Repo),
 		name:       filepath.Base(opts.Repo),
+		err:        err,
 	}, nil
 	}, 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{
 			repos = append(repos, Repo{
 				name:       *rDesc.Name,
 				name:       *rDesc.Name,
 				url:        *rDesc.SSHURL,
 				url:        *rDesc.SSHURL,
 				repository: r,
 				repository: r,
+				err:        err,
 			})
 			})
 		}
 		}
 		if resp.NextPage == 0 {
 		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{
 			repos = append(repos, Repo{
 				url:        *rDesc.SSHURL,
 				url:        *rDesc.SSHURL,
 				name:       *rDesc.Name,
 				name:       *rDesc.Name,
 				repository: r,
 				repository: r,
+				err:        err,
 			})
 			})
 		}
 		}
-		if err != nil {
-			return nil, err
-		} else if resp.NextPage == 0 {
+		if resp.NextPage == 0 {
 			break
 			break
 		}
 		}
 		listOpts.Page = resp.NextPage
 		listOpts.Page = resp.NextPage