From 326a4e7fc8d54ffe9032140918370ebe64b10891 Mon Sep 17 00:00:00 2001 From: William Bezuidenhout Date: Thu, 24 Apr 2025 16:34:24 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Update=20redirect=20logic=20to=20redirect?= =?UTF-8?q?=20versions=20=E2=89=A5=205.2=20to=20new=20docs=20domain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler.go | 6 +++--- handler_test.go | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/handler.go b/handler.go index 65b8aa6..fe4f058 100644 --- a/handler.go +++ b/handler.go @@ -14,7 +14,7 @@ import ( // versionPattern matches version strings like @5.2, @5.2.0, etc. and captures major and minor version numbers var versionPattern = regexp.MustCompile(`^@(\d+)\.(\d+)(?:\.(\d+))?$`) -// shouldRedirectVersion returns true for versions ≥ 5.3 (format: @major.minor[.patch]) +// shouldRedirectVersion returns true for versions ≥ 5.2 (format: @major.minor[.patch]) func shouldRedirectVersion(version string) bool { matches := versionPattern.FindStringSubmatch(version) if len(matches) < 3 { @@ -27,7 +27,7 @@ func shouldRedirectVersion(version string) bool { return false } - return major > 5 || (major == 5 && minor >= 3) + return major > 5 || (major == 5 && minor >= 2) } // Handler returns an http.Handler that serves the site. @@ -146,7 +146,7 @@ func (s *Site) Handler() http.Handler { contentVersion = r.URL.Path[1 : 1+end] } - // Redirect versions ≥ 5.3 to new docs domain with path preservation + // Redirect versions ≥ 5.2 to new docs domain with path preservation version := "@" + contentVersion if shouldRedirectVersion(version) { newURL := "https://www.sourcegraph.com/docs/" + contentVersion + "/" diff --git a/handler_test.go b/handler_test.go index 937b56b..f0585d5 100644 --- a/handler_test.go +++ b/handler_test.go @@ -272,11 +272,14 @@ func TestSite_Handler(t *testing.T) { checkResponseStatus(t, rr, http.StatusNotFound) }) - t.Run("version 5.2 - no redirect", func(t *testing.T) { + t.Run("version 5.2 - should redirect", func(t *testing.T) { rr := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/@5.2", nil) handler.ServeHTTP(rr, req) - checkResponseStatus(t, rr, http.StatusNotFound) + checkResponseStatus(t, rr, http.StatusPermanentRedirect) + if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/5.2/"; got != want { + t.Errorf("got redirect Location %q, want %q", got, want) + } }) t.Run("version 5.3 - should redirect", func(t *testing.T) { From 0b2cde22e8b0a6f2455b19cd51299080d7cd1b0d Mon Sep 17 00:00:00 2001 From: William Bezuidenhout Date: Thu, 24 Apr 2025 17:09:28 +0200 Subject: [PATCH 2/2] Update redirect URL to use @version format --- handler.go | 4 ++-- handler_test.go | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/handler.go b/handler.go index fe4f058..8ec2dbd 100644 --- a/handler.go +++ b/handler.go @@ -149,9 +149,9 @@ func (s *Site) Handler() http.Handler { // Redirect versions ≥ 5.2 to new docs domain with path preservation version := "@" + contentVersion if shouldRedirectVersion(version) { - newURL := "https://www.sourcegraph.com/docs/" + contentVersion + "/" + newURL := "https://www.sourcegraph.com/docs/@" + contentVersion if urlPath != "" { - newURL += urlPath + newURL += "/" + urlPath } http.Redirect(w, r, newURL, http.StatusPermanentRedirect) return diff --git a/handler_test.go b/handler_test.go index f0585d5..2cfd296 100644 --- a/handler_test.go +++ b/handler_test.go @@ -277,7 +277,7 @@ func TestSite_Handler(t *testing.T) { req, _ := http.NewRequest("GET", "/@5.2", nil) handler.ServeHTTP(rr, req) checkResponseStatus(t, rr, http.StatusPermanentRedirect) - if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/5.2/"; got != want { + if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/@5.2"; got != want { t.Errorf("got redirect Location %q, want %q", got, want) } }) @@ -287,7 +287,7 @@ func TestSite_Handler(t *testing.T) { req, _ := http.NewRequest("GET", "/@5.3", nil) handler.ServeHTTP(rr, req) checkResponseStatus(t, rr, http.StatusPermanentRedirect) - if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/5.3/"; got != want { + if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/@5.3"; got != want { t.Errorf("got redirect Location %q, want %q", got, want) } }) @@ -297,7 +297,7 @@ func TestSite_Handler(t *testing.T) { req, _ := http.NewRequest("GET", "/@5.3/some/path", nil) handler.ServeHTTP(rr, req) checkResponseStatus(t, rr, http.StatusPermanentRedirect) - if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/5.3/some/path"; got != want { + if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/@5.3/some/path"; got != want { t.Errorf("got redirect Location %q, want %q", got, want) } }) @@ -307,7 +307,7 @@ func TestSite_Handler(t *testing.T) { req, _ := http.NewRequest("GET", "/@6.0", nil) handler.ServeHTTP(rr, req) checkResponseStatus(t, rr, http.StatusPermanentRedirect) - if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/6.0/"; got != want { + if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/@6.0"; got != want { t.Errorf("got redirect Location %q, want %q", got, want) } }) @@ -317,7 +317,7 @@ func TestSite_Handler(t *testing.T) { req, _ := http.NewRequest("GET", "/@5.3.1", nil) handler.ServeHTTP(rr, req) checkResponseStatus(t, rr, http.StatusPermanentRedirect) - if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/5.3.1/"; got != want { + if got, want := rr.Header().Get("Location"), "https://www.sourcegraph.com/docs/@5.3.1"; got != want { t.Errorf("got redirect Location %q, want %q", got, want) } })