From 43661dd15eefd1b6c989e22fcf610506c3774d65 Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Tue, 15 Mar 2022 16:02:35 +0000 Subject: [PATCH] Enforce effective URL on error messages Signed-off-by: Paulo Gomes --- pkg/git/libgit2/checkout.go | 9 +++++---- pkg/git/libgit2/managed/options.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/git/libgit2/checkout.go b/pkg/git/libgit2/checkout.go index 6732aeb1..8e1e5cad 100644 --- a/pkg/git/libgit2/checkout.go +++ b/pkg/git/libgit2/checkout.go @@ -31,6 +31,7 @@ import ( "github.com/fluxcd/pkg/version" "github.com/fluxcd/source-controller/pkg/git" + "github.com/fluxcd/source-controller/pkg/git/libgit2/managed" ) // CheckoutStrategyForOptions returns the git.CheckoutStrategy for the given @@ -72,7 +73,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g CheckoutBranch: c.Branch, }) if err != nil { - return nil, fmt.Errorf("unable to clone '%s': %w", url, gitutil.LibGit2Error(err)) + return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err)) } defer repo.Free() head, err := repo.Head() @@ -101,7 +102,7 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, opts *git. }, }) if err != nil { - return nil, fmt.Errorf("unable to clone '%s': %w", url, gitutil.LibGit2Error(err)) + return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err)) } defer repo.Free() cc, err := checkoutDetachedDwim(repo, c.Tag) @@ -125,7 +126,7 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, opts *g }, }) if err != nil { - return nil, fmt.Errorf("unable to clone '%s': %w", url, gitutil.LibGit2Error(err)) + return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err)) } defer repo.Free() oid, err := git2go.NewOid(c.Commit) @@ -157,7 +158,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, opts *g }, }) if err != nil { - return nil, fmt.Errorf("unable to clone '%s': %w", url, gitutil.LibGit2Error(err)) + return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err)) } defer repo.Free() diff --git a/pkg/git/libgit2/managed/options.go b/pkg/git/libgit2/managed/options.go index 13ef0812..d4d346ad 100644 --- a/pkg/git/libgit2/managed/options.go +++ b/pkg/git/libgit2/managed/options.go @@ -54,3 +54,22 @@ func transportOptions(targetUrl string) (*TransportOptions, bool) { } return nil, false } + +// EffectiveURL returns the effective URL for requests. +// +// Given that TransportOptions can allow for the target URL to be overriden +// this returns the same input if Managed Transport is disabled or if no TargetURL +// is set on TransportOptions. +func EffectiveURL(targetUrl string) string { + if !Enabled() { + return targetUrl + } + + if opts, found := transportOptions(targetUrl); found { + if opts.TargetURL != "" { + return opts.TargetURL + } + } + + return targetUrl +}