From 594a039304abadab74c4333506751c01751c82dd Mon Sep 17 00:00:00 2001 From: Arthur Shvarts Date: Wed, 4 Dec 2024 11:28:16 -0500 Subject: [PATCH] Move InitArgoClients out of server.go --- cmd/telefonistka/server.go | 7 ------- internal/pkg/argocd/argocd.go | 19 ++++++++++++------- internal/pkg/argocd/argocd_test.go | 17 ++++++++++------- internal/pkg/githubapi/github.go | 1 + 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/cmd/telefonistka/server.go b/cmd/telefonistka/server.go index e346b346..076b28f8 100644 --- a/cmd/telefonistka/server.go +++ b/cmd/telefonistka/server.go @@ -10,7 +10,6 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/wayfair-incubator/telefonistka/internal/pkg/argocd" "github.com/wayfair-incubator/telefonistka/internal/pkg/githubapi" ) @@ -58,12 +57,6 @@ func serve() { mainGhClientCache, _ := lru.New[string, githubapi.GhClientPair](128) prApproverGhClientCache, _ := lru.New[string, githubapi.GhClientPair](128) - // init argoclients - err := argocd.InitArgoClients() - if err != nil { - log.Fatalf("error initializing argo clients: %v", err) - } - mux := http.NewServeMux() mux.HandleFunc("/webhook", handleWebhook(githubWebhookSecret, mainGhClientCache, prApproverGhClientCache)) mux.Handle("/metrics", promhttp.Handler()) diff --git a/internal/pkg/argocd/argocd.go b/internal/pkg/argocd/argocd.go index 5593f02f..4b1fea9e 100644 --- a/internal/pkg/argocd/argocd.go +++ b/internal/pkg/argocd/argocd.go @@ -33,15 +33,13 @@ import ( // ctxLines is the number of context lines used in application diffs. const ctxLines = 10 -var argoClients argoCdClients // replaced during tests - -func InitArgoClients() error { - var err error - argoClients, err = createArgoCdClients() +// replace in testing +var InitArgoClients = func() (argoCdClients, error) { + argoClients, err := createArgoCdClients() if err != nil { - return fmt.Errorf("error creating ArgoCD clients: %w", err) + return argoCdClients{}, fmt.Errorf("error creating ArgoCD clients: %w", err) } - return nil + return argoClients, nil } type argoCdClients struct { @@ -556,6 +554,13 @@ func generateDiffOfAComponent(ctx context.Context, commentDiff bool, componentPa // GenerateDiffOfChangedComponents generates diff of changed components func GenerateDiffOfChangedComponents(ctx context.Context, componentsToDiff map[string]bool, prBranch string, repo string, useSHALabelForArgoDicovery bool, createTempAppObjectFromNewApps bool) (hasComponentDiff bool, hasComponentDiffErrors bool, diffResults []DiffResult, err error) { + + // init argoclients + argoClients, err := InitArgoClients() + if err != nil { + log.Fatalf("error initializing argo clients: %v", err) + } + hasComponentDiff = false hasComponentDiffErrors = false diff --git a/internal/pkg/argocd/argocd_test.go b/internal/pkg/argocd/argocd_test.go index 798d3a79..9d903a9e 100644 --- a/internal/pkg/argocd/argocd_test.go +++ b/internal/pkg/argocd/argocd_test.go @@ -326,18 +326,21 @@ func TestFetchArgoDiffConcurrently(t *testing.T) { defer mockCtrl.Finish() // Save and restore the original argoClients - saved := argoClients - defer func() { argoClients = saved }() + saved := InitArgoClients + defer func() { InitArgoClients = saved }() // mock the argoClients mockAppServiceClient := mocks.NewMockApplicationServiceClient(mockCtrl) mockSettingsServiceClient := mocks.NewMockSettingsServiceClient(mockCtrl) mockProjectServiceClient := mocks.NewMockProjectServiceClient(mockCtrl) - - argoClients = argoCdClients{ - app: mockAppServiceClient, - setting: mockSettingsServiceClient, - project: mockProjectServiceClient, + // fake InitArgoClients + InitArgoClients = func() (argoCdClients, error) { + argoClients := argoCdClients{ + app: mockAppServiceClient, + setting: mockSettingsServiceClient, + project: mockProjectServiceClient, + } + return argoClients, nil } // slowReply simulates a slow reply from the server diff --git a/internal/pkg/githubapi/github.go b/internal/pkg/githubapi/github.go index f8372997..5d59bf85 100644 --- a/internal/pkg/githubapi/github.go +++ b/internal/pkg/githubapi/github.go @@ -224,6 +224,7 @@ func handleChangedPREvent(ctx context.Context, mainGithubClientPair GhClientPair ghPrClientDetails.PrLogger.Debugf("ArgoCD diff disabled for %s\n", componentPath) } } + hasComponentDiff, hasComponentDiffErrors, diffOfChangedComponents, err := argocd.GenerateDiffOfChangedComponents(ctx, componentsToDiff, ghPrClientDetails.Ref, ghPrClientDetails.RepoURL, config.Argocd.UseSHALabelForAppDiscovery, config.Argocd.CreateTempAppObjectFroNewApps) if err != nil { return fmt.Errorf("getting diff information: %w", err)