Skip to content

Commit

Permalink
Dashboard: Redirects for old edit & view panel urls (#25653)
Browse files Browse the repository at this point in the history
  • Loading branch information
torkelo authored and aknuds1 committed Jun 29, 2020
1 parent bdb64f1 commit a680287
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/api/api.go
Expand Up @@ -18,6 +18,7 @@ func (hs *HTTPServer) registerRoutes() {
reqSnapshotPublicModeOrSignedIn := middleware.SnapshotPublicModeOrSignedIn()
redirectFromLegacyDashboardURL := middleware.RedirectFromLegacyDashboardURL()
redirectFromLegacyDashboardSoloURL := middleware.RedirectFromLegacyDashboardSoloURL()
redirectFromLegacyPanelEditURL := middleware.RedirectFromLegacyPanelEditURL()
quota := middleware.Quota(hs.QuotaService)
bind := binding.Bind

Expand Down Expand Up @@ -65,8 +66,8 @@ func (hs *HTTPServer) registerRoutes() {
r.Get("/plugins/:id/page/:page", reqSignedIn, hs.Index)
r.Get("/a/:id/*", reqSignedIn, hs.Index) // App Root Page

r.Get("/d/:uid/:slug", reqSignedIn, hs.Index)
r.Get("/d/:uid", reqSignedIn, hs.Index)
r.Get("/d/:uid/:slug", reqSignedIn, redirectFromLegacyPanelEditURL, hs.Index)
r.Get("/d/:uid", reqSignedIn, redirectFromLegacyPanelEditURL, hs.Index)
r.Get("/dashboard/db/:slug", reqSignedIn, redirectFromLegacyDashboardURL, hs.Index)
r.Get("/dashboard/script/*", reqSignedIn, hs.Index)
r.Get("/dashboard-solo/snapshot/*", hs.Index)
Expand Down
27 changes: 27 additions & 0 deletions pkg/middleware/dashboard_redirect.go
Expand Up @@ -34,6 +34,33 @@ func RedirectFromLegacyDashboardURL() macaron.Handler {
}
}

// In Grafana v7.0 we changed panel edit & view query parameters.
// This middleware tries to detect those old url parameters and direct to the new url query params
func RedirectFromLegacyPanelEditURL() macaron.Handler {
return func(c *models.ReqContext) {
queryParams := c.Req.URL.Query()

panelId, hasPanelId := queryParams["panelId"]
_, hasFullscreen := queryParams["fullscreen"]
_, hasEdit := queryParams["edit"]

if hasPanelId && hasFullscreen {
delete(queryParams, "panelId")
delete(queryParams, "fullscreen")
delete(queryParams, "edit")

if hasEdit {
queryParams["editPanel"] = panelId
} else {
queryParams["viewPanel"] = panelId
}

newURL := setting.ToAbsUrl(fmt.Sprintf("%s?%s", strings.TrimPrefix(c.Req.URL.Path, "/"), queryParams.Encode()))
c.Redirect(newURL, 301)
}
}
}

func RedirectFromLegacyDashboardSoloURL() macaron.Handler {
return func(c *models.ReqContext) {
slug := c.Params("slug")
Expand Down
17 changes: 17 additions & 0 deletions pkg/middleware/dashboard_redirect_test.go
Expand Up @@ -55,4 +55,21 @@ func TestMiddlewareDashboardRedirect(t *testing.T) {
})
})
})

Convey("Given the dashboard legacy edit panel middleware", t, func() {
bus.ClearBusHandlers()

middlewareScenario(t, "GET dashboard by legacy edit url", func(sc *scenarioContext) {
sc.m.Get("/d/:uid/:slug", RedirectFromLegacyPanelEditURL(), sc.defaultHandler)

sc.fakeReqWithParams("GET", "/d/asd/dash?orgId=1&panelId=12&fullscreen&edit", map[string]string{}).exec()

Convey("Should redirect to new dashboard edit url with a 301 Moved Permanently", func() {
So(sc.resp.Code, ShouldEqual, 301)
redirectURL, _ := sc.resp.Result().Location()
So(redirectURL.String(), ShouldEqual, "/d/asd/d/asd/dash?editPanel=12&orgId=1")
})
})
})

}

0 comments on commit a680287

Please sign in to comment.