Dashboard: Redirects for old edit & view panel urls (#25653)
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user