Commit 82e456a9 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Merge branch 'rp/improve-redirect-behavior' into 'master'

Retain all query parameters when adding environment parameter to metrics dashboard URL

See merge request gitlab-org/gitlab!40878
parents 4343a81d 81c0454e
...@@ -20,10 +20,11 @@ module Projects ...@@ -20,10 +20,11 @@ module Projects
elsif default_environment elsif default_environment
redirect_to project_metrics_dashboard_path( redirect_to project_metrics_dashboard_path(
project, project,
**permitted_params # Reverse merge the query parameters so that a query parameter named dashboard_path doesn't
.to_h # override the dashboard_path path parameter.
.symbolize_keys **permitted_params.to_h.symbolize_keys
.merge(environment: default_environment) .merge(environment: default_environment.id)
.reverse_merge(request.query_parameters.symbolize_keys)
) )
else else
render 'projects/environments/empty_metrics' render 'projects/environments/empty_metrics'
......
...@@ -25,7 +25,10 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do ...@@ -25,7 +25,10 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
# Use this scope for all new project routes. # Use this scope for all new project routes.
scope '-' do scope '-' do
get 'archive/*id', constraints: { format: Gitlab::PathRegex.archive_formats_regex, id: /.+?/ }, to: 'repositories#archive', as: 'archive' get 'archive/*id', constraints: { format: Gitlab::PathRegex.archive_formats_regex, id: /.+?/ }, to: 'repositories#archive', as: 'archive'
get 'metrics(/:dashboard_path)(/:page)', constraints: { dashboard_path: /.+\.yml/, page: 'panel/new' }, # Since the page parameter can contain slashes (panel/new), use Rails'
# "Route Globbing" syntax (/*page) so that the route helpers do not encode
# the slash character.
get 'metrics(/:dashboard_path)(/*page)', constraints: { dashboard_path: /.+\.yml/, page: 'panel/new' },
to: 'metrics_dashboard#show', as: :metrics_dashboard, format: false to: 'metrics_dashboard#show', as: :metrics_dashboard, format: false
namespace :metrics, module: :metrics do namespace :metrics, module: :metrics do
......
...@@ -25,15 +25,16 @@ RSpec.describe 'Projects::MetricsDashboardController' do ...@@ -25,15 +25,16 @@ RSpec.describe 'Projects::MetricsDashboardController' do
end end
it 'retains existing parameters when redirecting' do it 'retains existing parameters when redirecting' do
get "#{dashboard_route(dashboard_path: '.gitlab/dashboards/dashboard_path.yml')}/panel/new" params = {
dashboard_path: '.gitlab/dashboards/dashboard_path.yml',
expect(response).to redirect_to( page: 'panel/new',
dashboard_route( group: 'System metrics (Kubernetes)',
dashboard_path: '.gitlab/dashboards/dashboard_path.yml', title: 'Memory Usage (Pod average)',
page: 'panel/new', y_label: 'Memory Used per Pod (MB)'
environment: environment }
) send_request(params)
)
expect(response).to redirect_to(dashboard_route(params.merge(environment: environment.id)))
end end
context 'with anonymous user and public dashboard visibility' do context 'with anonymous user and public dashboard visibility' do
...@@ -110,15 +111,13 @@ RSpec.describe 'Projects::MetricsDashboardController' do ...@@ -110,15 +111,13 @@ RSpec.describe 'Projects::MetricsDashboardController' do
describe 'GET :/namespace/:project/-/metrics/:page' do describe 'GET :/namespace/:project/-/metrics/:page' do
it 'returns 200 with path param page' do it 'returns 200 with path param page' do
# send_request(page: 'panel/new') cannot be used because it encodes '/' send_request(page: 'panel/new', environment: environment.id)
get "#{dashboard_route}/panel/new?environment=#{environment.id}"
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
end end
it 'returns 200 with dashboard and path param page' do it 'returns 200 with dashboard and path param page' do
# send_request(page: 'panel/new') cannot be used because it encodes '/' send_request(dashboard_path: 'dashboard.yml', page: 'panel/new', environment: environment.id)
get "#{dashboard_route(dashboard_path: 'dashboard.yml')}/panel/new?environment=#{environment.id}"
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment