From b70bdce3c1d1b03b2ab2b63941a2de77c1b27715 Mon Sep 17 00:00:00 2001 From: Sunny Date: Thu, 26 May 2022 00:26:00 +0530 Subject: [PATCH] gitrepo: gitCheckout() return typed errors only gitCheckout() should return typed errors only. This helps prevent error type assertions by the caller to determine how to handle the error. gitCheckout() also sets the appropriate conditions associated with the error, if any. Signed-off-by: Sunny --- controllers/gitrepository_controller.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/controllers/gitrepository_controller.go b/controllers/gitrepository_controller.go index f3c4e5713..de03c2cf2 100644 --- a/controllers/gitrepository_controller.go +++ b/controllers/gitrepository_controller.go @@ -505,12 +505,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, c, err := r.gitCheckout(ctx, obj, repositoryURL, authOpts, dir, optimizedClone) if err != nil { - e := serror.NewGeneric( - fmt.Errorf("failed to checkout and determine revision: %w", err), - sourcev1.GitOperationFailedReason, - ) - conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error()) - return sreconcile.ResultEmpty, e + return sreconcile.ResultEmpty, err } // Assign the commit to the shared commit reference. *commit = *c @@ -544,12 +539,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, // optimization. c, err := r.gitCheckout(ctx, obj, repositoryURL, authOpts, dir, false) if err != nil { - e := serror.NewGeneric( - fmt.Errorf("failed to checkout and determine revision: %w", err), - sourcev1.GitOperationFailedReason, - ) - conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error()) - return sreconcile.ResultEmpty, e + return sreconcile.ResultEmpty, err } *commit = *c } @@ -773,7 +763,16 @@ func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context, // Checkout HEAD of reference in object gitCtx, cancel := context.WithTimeout(ctx, obj.Spec.Timeout.Duration) defer cancel() - return checkoutStrategy.Checkout(gitCtx, dir, repoURL, authOpts) + commit, err := checkoutStrategy.Checkout(gitCtx, dir, repoURL, authOpts) + if err != nil { + e := serror.NewGeneric( + fmt.Errorf("failed to checkout and determine revision: %w", err), + sourcev1.GitOperationFailedReason, + ) + conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error()) + return nil, e + } + return commit, nil } // fetchIncludes fetches artifact metadata of all the included repos.