Skip to content

Commit 43661dd

Browse files
Paulo Gomesdarkowlzz
authored andcommitted
Enforce effective URL on error messages
Signed-off-by: Paulo Gomes <[email protected]>
1 parent d1a7e5d commit 43661dd

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

pkg/git/libgit2/checkout.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/fluxcd/pkg/version"
3232

3333
"github.com/fluxcd/source-controller/pkg/git"
34+
"github.com/fluxcd/source-controller/pkg/git/libgit2/managed"
3435
)
3536

3637
// CheckoutStrategyForOptions returns the git.CheckoutStrategy for the given
@@ -72,7 +73,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g
7273
CheckoutBranch: c.Branch,
7374
})
7475
if err != nil {
75-
return nil, fmt.Errorf("unable to clone '%s': %w", url, gitutil.LibGit2Error(err))
76+
return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
7677
}
7778
defer repo.Free()
7879
head, err := repo.Head()
@@ -101,7 +102,7 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, opts *git.
101102
},
102103
})
103104
if err != nil {
104-
return nil, fmt.Errorf("unable to clone '%s': %w", url, gitutil.LibGit2Error(err))
105+
return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
105106
}
106107
defer repo.Free()
107108
cc, err := checkoutDetachedDwim(repo, c.Tag)
@@ -125,7 +126,7 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, opts *g
125126
},
126127
})
127128
if err != nil {
128-
return nil, fmt.Errorf("unable to clone '%s': %w", url, gitutil.LibGit2Error(err))
129+
return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
129130
}
130131
defer repo.Free()
131132
oid, err := git2go.NewOid(c.Commit)
@@ -157,7 +158,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, opts *g
157158
},
158159
})
159160
if err != nil {
160-
return nil, fmt.Errorf("unable to clone '%s': %w", url, gitutil.LibGit2Error(err))
161+
return nil, fmt.Errorf("unable to clone '%s': %w", managed.EffectiveURL(url), gitutil.LibGit2Error(err))
161162
}
162163
defer repo.Free()
163164

pkg/git/libgit2/managed/options.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,22 @@ func transportOptions(targetUrl string) (*TransportOptions, bool) {
5454
}
5555
return nil, false
5656
}
57+
58+
// EffectiveURL returns the effective URL for requests.
59+
//
60+
// Given that TransportOptions can allow for the target URL to be overriden
61+
// this returns the same input if Managed Transport is disabled or if no TargetURL
62+
// is set on TransportOptions.
63+
func EffectiveURL(targetUrl string) string {
64+
if !Enabled() {
65+
return targetUrl
66+
}
67+
68+
if opts, found := transportOptions(targetUrl); found {
69+
if opts.TargetURL != "" {
70+
return opts.TargetURL
71+
}
72+
}
73+
74+
return targetUrl
75+
}

0 commit comments

Comments
 (0)