From ae0e5da7ac127494b99238cdb3de7ccd4408e4db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 26 Apr 2024 09:03:01 +0200 Subject: [PATCH] Fix path html_url etc. path logic for modules with major versions in path Updates #454 --- pkg/buildcmd/build.go | 2 +- pkg/client/client.go | 18 +++++++++++++++--- pkg/client/client_test.go | 9 +++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pkg/buildcmd/build.go b/pkg/buildcmd/build.go index cea8a41..2bcb5de 100644 --- a/pkg/buildcmd/build.go +++ b/pkg/buildcmd/build.go @@ -393,7 +393,7 @@ func (t *theme) toFrontMatter() map[string]interface{} { htmlURL = t.ghRepo.HTMLURL } else { // Gitlab etc., assume the path is the base of the URL. - htmlURL = fmt.Sprintf("https://%s", t.m.Path) + htmlURL = fmt.Sprintf("https://%s", t.m.PathRepo()) } var expiryDate time.Time diff --git a/pkg/client/client.go b/pkg/client/client.go index 700fdbf..ed7adef 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -12,6 +12,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "runtime" "strings" "sync" @@ -327,10 +328,15 @@ type Module struct { Meta map[string]interface{} `json:"meta"` } +func (m Module) PathWithoutVersion() string { + return PathWithoutVersion(m.Path) +} + // PathRepo returns the root path to the repository. func (m Module) PathRepo() string { slashCount := 0 - idx := strings.IndexFunc(m.Path, func(r rune) bool { + p := m.PathWithoutVersion() + idx := strings.IndexFunc(p, func(r rune) bool { if r == '/' { slashCount++ } @@ -338,10 +344,10 @@ func (m Module) PathRepo() string { }) if slashCount < 3 { - return m.Path + return p } - return m.Path[:idx] + return p[:idx] } // HugoVersion holds Hugo binary version requirements for a module. @@ -353,6 +359,12 @@ type HugoVersion struct { type ModulesMap map[string]Module +var pathVersionRe = regexp.MustCompile(`/v\d+$`) + +func PathWithoutVersion(s string) string { + return pathVersionRe.ReplaceAllString(s, "") +} + func setEnvVar(vars *[]string, key, value string) { for i := range *vars { if strings.HasPrefix((*vars)[i], key+"=") { diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 19a2a6e..5e3a118 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -15,5 +15,14 @@ func TestModule(t *testing.T) { c.Assert(Module{Path: "github.com/a/b"}.PathRepo(), qt.Equals, "github.com/a/b") c.Assert(Module{Path: "github.com/a"}.PathRepo(), qt.Equals, "github.com/a") c.Assert(Module{Path: "github.com"}.PathRepo(), qt.Equals, "github.com") + c.Assert(Module{Path: "github.com/a/v2"}.PathRepo(), qt.Equals, "github.com/a") }) } + +func TestPathWithoutVersion(t *testing.T) { + c := qt.New(t) + + c.Assert(PathWithoutVersion("github.com/gohugoio/hugo/v3"), qt.Equals, "github.com/gohugoio/hugo") + c.Assert(PathWithoutVersion("github.com/gohugoio/hugo/v2"), qt.Equals, "github.com/gohugoio/hugo") + c.Assert(PathWithoutVersion("github.com/gohugoio/hugo"), qt.Equals, "github.com/gohugoio/hugo") +}