dashboard: fix redirect of legacy dashboard url's

Redirect of /dashboard/db/:slug to /d/:uid/:slug and
/dashboard-solo/db/:slug to /d-solo/:uid/:slug now includes querystring parameters.

Fixes #10752
pull/10754/head
Marcus Efraimsson 7 years ago
parent a906dd8157
commit 32054ad9a6
  1. 3
      pkg/middleware/dashboard_redirect.go
  2. 6
      pkg/middleware/dashboard_redirect_test.go

@ -1,6 +1,7 @@
package middleware package middleware
import ( import (
"fmt"
"strings" "strings"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
@ -24,6 +25,7 @@ func RedirectFromLegacyDashboardUrl() macaron.Handler {
if slug != "" { if slug != "" {
if url, err := getDashboardUrlBySlug(c.OrgId, slug); err == nil { if url, err := getDashboardUrlBySlug(c.OrgId, slug); err == nil {
url = fmt.Sprintf("%s?%s", url, c.Req.URL.RawQuery)
c.Redirect(url, 301) c.Redirect(url, 301)
return return
} }
@ -38,6 +40,7 @@ func RedirectFromLegacyDashboardSoloUrl() macaron.Handler {
if slug != "" { if slug != "" {
if url, err := getDashboardUrlBySlug(c.OrgId, slug); err == nil { if url, err := getDashboardUrlBySlug(c.OrgId, slug); err == nil {
url = strings.Replace(url, "/d/", "/d-solo/", 1) url = strings.Replace(url, "/d/", "/d-solo/", 1)
url = fmt.Sprintf("%s?%s", url, c.Req.URL.RawQuery)
c.Redirect(url, 301) c.Redirect(url, 301)
return return
} }

@ -30,19 +30,20 @@ func TestMiddlewareDashboardRedirect(t *testing.T) {
middlewareScenario("GET dashboard by legacy url", func(sc *scenarioContext) { middlewareScenario("GET dashboard by legacy url", func(sc *scenarioContext) {
sc.m.Get("/dashboard/db/:slug", redirectFromLegacyDashboardUrl, sc.defaultHandler) sc.m.Get("/dashboard/db/:slug", redirectFromLegacyDashboardUrl, sc.defaultHandler)
sc.fakeReqWithParams("GET", "/dashboard/db/dash", map[string]string{}).exec() sc.fakeReqWithParams("GET", "/dashboard/db/dash?orgId=1&panelId=2", map[string]string{}).exec()
Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() { Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() {
So(sc.resp.Code, ShouldEqual, 301) So(sc.resp.Code, ShouldEqual, 301)
redirectUrl, _ := sc.resp.Result().Location() redirectUrl, _ := sc.resp.Result().Location()
So(redirectUrl.Path, ShouldEqual, m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug)) So(redirectUrl.Path, ShouldEqual, m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug))
So(len(redirectUrl.Query()), ShouldEqual, 2)
}) })
}) })
middlewareScenario("GET dashboard solo by legacy url", func(sc *scenarioContext) { middlewareScenario("GET dashboard solo by legacy url", func(sc *scenarioContext) {
sc.m.Get("/dashboard-solo/db/:slug", redirectFromLegacyDashboardSoloUrl, sc.defaultHandler) sc.m.Get("/dashboard-solo/db/:slug", redirectFromLegacyDashboardSoloUrl, sc.defaultHandler)
sc.fakeReqWithParams("GET", "/dashboard-solo/db/dash", map[string]string{}).exec() sc.fakeReqWithParams("GET", "/dashboard-solo/db/dash?orgId=1&panelId=2", map[string]string{}).exec()
Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() { Convey("Should redirect to new dashboard url with a 301 Moved Permanently", func() {
So(sc.resp.Code, ShouldEqual, 301) So(sc.resp.Code, ShouldEqual, 301)
@ -50,6 +51,7 @@ func TestMiddlewareDashboardRedirect(t *testing.T) {
expectedUrl := m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug) expectedUrl := m.GetDashboardUrl(fakeDash.Uid, fakeDash.Slug)
expectedUrl = strings.Replace(expectedUrl, "/d/", "/d-solo/", 1) expectedUrl = strings.Replace(expectedUrl, "/d/", "/d-solo/", 1)
So(redirectUrl.Path, ShouldEqual, expectedUrl) So(redirectUrl.Path, ShouldEqual, expectedUrl)
So(len(redirectUrl.Query()), ShouldEqual, 2)
}) })
}) })
}) })

Loading…
Cancel
Save