Skip to content

Commit

Permalink
Move InitArgoClients out of server.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvarts committed Dec 4, 2024
1 parent 380eb67 commit 594a039
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
7 changes: 0 additions & 7 deletions cmd/telefonistka/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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())
Expand Down
19 changes: 12 additions & 7 deletions internal/pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {

Check failure on line 556 in internal/pkg/argocd/argocd.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary leading newline (whitespace)

// init argoclients
argoClients, err := InitArgoClients()
if err != nil {
log.Fatalf("error initializing argo clients: %v", err)
}

hasComponentDiff = false
hasComponentDiffErrors = false

Expand Down
17 changes: 10 additions & 7 deletions internal/pkg/argocd/argocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 594a039

Please sign in to comment.