From eb99827966ebcf45536b5b434a9d2b97dcc1f923 Mon Sep 17 00:00:00 2001 From: Daniel Moran Date: Tue, 29 Jun 2021 14:49:41 -0400 Subject: [PATCH] fix: fix extension-check logic when normalizing github URLs (#158) --- clients/apply/source.go | 2 +- pkg/github/normalize.go | 2 +- pkg/github/normalize_test.go | 8 +++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/clients/apply/source.go b/clients/apply/source.go index 3846c44..212324e 100644 --- a/clients/apply/source.go +++ b/clients/apply/source.go @@ -142,7 +142,7 @@ func SourceFromURL(u *url.URL, encoding TemplateEncoding) TemplateSource { } } - normalized := github.NormalizeURLToContent(u, "yaml", "yml", "jsonnet", "json").String() + normalized := github.NormalizeURLToContent(u, ".yaml", ".yml", ".jsonnet", ".json").String() return TemplateSource{ Name: normalized, diff --git a/pkg/github/normalize.go b/pkg/github/normalize.go index 6732007..715602d 100644 --- a/pkg/github/normalize.go +++ b/pkg/github/normalize.go @@ -37,7 +37,7 @@ func NormalizeURLToContent(u *url.URL, extensions ...string) *url.URL { func extensionMatches(u *url.URL, extensions []string) bool { ext := path.Ext(u.Path) for _, e := range extensions { - if strings.EqualFold(e, "."+ext) { + if strings.EqualFold(ext, e) { return true } } diff --git a/pkg/github/normalize_test.go b/pkg/github/normalize_test.go index 5ca5263..432145e 100644 --- a/pkg/github/normalize_test.go +++ b/pkg/github/normalize_test.go @@ -22,6 +22,12 @@ func TestNormalize(t *testing.T) { in: url.URL{Host: "github.com", Path: "/influxdata/influxdb/blob/master/flags.yml"}, out: url.URL{Host: "raw.githubusercontent.com", Path: "/influxdata/influxdb/master/flags.yml"}, }, + { + name: "github URL with extensions", + in: url.URL{Host: "github.com", Path: "/influxdata/community-templates/blob/master/github/github.yml"}, + exts: []string{".yaml", ".yml", ".jsonnet", ".json"}, + out: url.URL{Host: "raw.githubusercontent.com", Path: "/influxdata/community-templates/master/github/github.yml"}, + }, { name: "other URL", in: url.URL{Host: "google.com", Path: "/fake.yml"}, @@ -30,7 +36,7 @@ func TestNormalize(t *testing.T) { { name: "github URL - wrong extension", in: url.URL{Host: "github.com", Path: "/influxdata/influxdb/blob/master/flags.yml"}, - exts: []string{"json"}, + exts: []string{".json"}, out: url.URL{Host: "github.com", Path: "/influxdata/influxdb/blob/master/flags.yml"}, }, }