Skip to content

Commit

Permalink
Drop else statements no longer needed
Browse files Browse the repository at this point in the history
Now that the earlier error is returned, the else is not needed and can
be dropped, reducing the indentation of the happy path [1, 2].

[1] https://maelvls.dev/go-happy-line-of-sight/
[2] https://medium.com/@matryer/line-of-sight-in-code-186dd7cdea88
  • Loading branch information
hnnsgstfssn committed Oct 28, 2024
1 parent eecb6b7 commit 195488c
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,37 +220,34 @@ func handleChangedPREvent(ctx context.Context, mainGithubClientPair GhClientPair
c, err := getComponentConfig(ghPrClientDetails, componentPath, ghPrClientDetails.Ref)
if err != nil {
return fmt.Errorf("get component (%s) config: %w", componentPath, err)
} else {
if c.DisableArgoCDDiff {
componentsToDiff[componentPath] = false
ghPrClientDetails.PrLogger.Debugf("ArgoCD diff disabled for %s\n", componentPath)
} else {
componentsToDiff[componentPath] = true
}
}
componentsToDiff[componentPath] = true
if c.DisableArgoCDDiff {
componentsToDiff[componentPath] = false
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)
} else {
ghPrClientDetails.PrLogger.Debugf("Successfully got ArgoCD diff(comparing live objects against objects rendered form git ref %s)", ghPrClientDetails.Ref)
if !hasComponentDiffErrors && !hasComponentDiff {
ghPrClientDetails.PrLogger.Debugf("ArgoCD diff is empty, this PR will not change cluster state\n")
prLables, resp, err := ghPrClientDetails.GhClientPair.v3Client.Issues.AddLabelsToIssue(ghPrClientDetails.Ctx, ghPrClientDetails.Owner, ghPrClientDetails.Repo, *eventPayload.PullRequest.Number, []string{"noop"})
prom.InstrumentGhCall(resp)
}
ghPrClientDetails.PrLogger.Debugf("Successfully got ArgoCD diff(comparing live objects against objects rendered form git ref %s)", ghPrClientDetails.Ref)
if !hasComponentDiffErrors && !hasComponentDiff {
ghPrClientDetails.PrLogger.Debugf("ArgoCD diff is empty, this PR will not change cluster state\n")
prLables, resp, err := ghPrClientDetails.GhClientPair.v3Client.Issues.AddLabelsToIssue(ghPrClientDetails.Ctx, ghPrClientDetails.Owner, ghPrClientDetails.Repo, *eventPayload.PullRequest.Number, []string{"noop"})
prom.InstrumentGhCall(resp)
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Could not label GitHub PR: err=%s\n%v\n", err, resp)
} else {
ghPrClientDetails.PrLogger.Debugf("PR %v labeled\n%+v", *eventPayload.PullRequest.Number, prLables)
}
// If the PR is a promotion PR and the diff is empty, we can auto-merge it
// "len(componentPathList) > 0" validates we are not auto-merging a PR that we failed to understand which apps it affects
if DoesPrHasLabel(eventPayload.PullRequest.Labels, "promotion") && config.Argocd.AutoMergeNoDiffPRs && len(componentPathList) > 0 {
ghPrClientDetails.PrLogger.Infof("Auto-merging (no diff) PR %d", *eventPayload.PullRequest.Number)
err := MergePr(ghPrClientDetails, eventPayload.PullRequest.Number)
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Could not label GitHub PR: err=%s\n%v\n", err, resp)
} else {
ghPrClientDetails.PrLogger.Debugf("PR %v labeled\n%+v", *eventPayload.PullRequest.Number, prLables)
}
// If the PR is a promotion PR and the diff is empty, we can auto-merge it
// "len(componentPathList) > 0" validates we are not auto-merging a PR that we failed to understand which apps it affects
if DoesPrHasLabel(eventPayload.PullRequest.Labels, "promotion") && config.Argocd.AutoMergeNoDiffPRs && len(componentPathList) > 0 {
ghPrClientDetails.PrLogger.Infof("Auto-merging (no diff) PR %d", *eventPayload.PullRequest.Number)
err := MergePr(ghPrClientDetails, eventPayload.PullRequest.Number)
if err != nil {
return fmt.Errorf("PR auto merge: %w", err)
}
return fmt.Errorf("PR auto merge: %w", err)
}
}
}
Expand Down

0 comments on commit 195488c

Please sign in to comment.