From 675fd28b0c60df55dae21acab1782032a00b4d38 Mon Sep 17 00:00:00 2001 From: Joe Lombrozo Date: Mon, 18 Sep 2023 12:09:13 -0400 Subject: [PATCH] prevent against nil-based panics --- pkg/affected_apps/argocd_matcher.go | 11 ++++++- pkg/affected_apps/argocd_matcher_test.go | 40 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 pkg/affected_apps/argocd_matcher_test.go diff --git a/pkg/affected_apps/argocd_matcher.go b/pkg/affected_apps/argocd_matcher.go index f02af778..e2a1181b 100644 --- a/pkg/affected_apps/argocd_matcher.go +++ b/pkg/affected_apps/argocd_matcher.go @@ -16,13 +16,22 @@ type ArgocdMatcher struct { func NewArgocdMatcher(vcsToArgoMap pkg.VcsToArgoMap, repo *repo.Repo) *ArgocdMatcher { log.Debug().Msgf("looking for %s repos", repo.CloneURL) repoApps := vcsToArgoMap.GetAppsInRepo(repo.CloneURL) - log.Debug().Msgf("found %d apps", repoApps.Count()) + if repoApps == nil { + log.Debug().Msg("found no apps") + } else { + log.Debug().Msgf("found %d apps", repoApps.Count()) + } + return &ArgocdMatcher{ appsDirectory: repoApps, } } func (a *ArgocdMatcher) AffectedApps(ctx context.Context, changeList []string) (AffectedItems, error) { + if a.appsDirectory == nil { + return AffectedItems{}, nil + } + appsSlice := a.appsDirectory.FindAppsBasedOnChangeList(changeList) return AffectedItems{Applications: appsSlice}, nil } diff --git a/pkg/affected_apps/argocd_matcher_test.go b/pkg/affected_apps/argocd_matcher_test.go new file mode 100644 index 00000000..4ce77298 --- /dev/null +++ b/pkg/affected_apps/argocd_matcher_test.go @@ -0,0 +1,40 @@ +package affected_apps + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + "github.com/zapier/kubechecks/pkg" + repo2 "github.com/zapier/kubechecks/pkg/repo" +) + +func TestCreateNewMatcherWithNilVcsMap(t *testing.T) { + // setup + var ( + vcsMap pkg.VcsToArgoMap + repo repo2.Repo + ) + + // run test + matcher := NewArgocdMatcher(vcsMap, &repo) + + // verify results + require.Nil(t, matcher.appsDirectory) +} + +func TestFindAffectedAppsWithNilAppsDirectory(t *testing.T) { + // setup + var ( + ctx = context.TODO() + changeList = []string{"/go.mod"} + ) + + matcher := ArgocdMatcher{} + items, err := matcher.AffectedApps(ctx, changeList) + + // verify results + require.NoError(t, err) + require.Len(t, items.Applications, 0) + require.Len(t, items.ApplicationSets, 0) +}