Commit 94663bd6 authored by rpereira2's avatar rpereira2

Redirect to default environment when no env in params

When there is no environment in query params, redirect to the metrics
dashboard page of the default environment, rather than opaquely using
the default environment on the backend without updating the URL.

This will ensure that the query param always displays the environment
being used.
parent 62d07844
...@@ -6,6 +6,8 @@ module Projects ...@@ -6,6 +6,8 @@ module Projects
# app/controllers/projects/environments_controller.rb # app/controllers/projects/environments_controller.rb
# See https://gitlab.com/gitlab-org/gitlab/-/issues/226002 for more details. # See https://gitlab.com/gitlab-org/gitlab/-/issues/226002 for more details.
include Gitlab::Utils::StrongMemoize
before_action :authorize_metrics_dashboard! before_action :authorize_metrics_dashboard!
before_action do before_action do
push_frontend_feature_flag(:prometheus_computed_alerts) push_frontend_feature_flag(:prometheus_computed_alerts)
...@@ -15,6 +17,8 @@ module Projects ...@@ -15,6 +17,8 @@ module Projects
def show def show
if environment if environment
render 'projects/environments/metrics' render 'projects/environments/metrics'
elsif default_environment
redirect_to project_metrics_dashboard_path(project, environment: default_environment)
else else
render 'projects/environments/empty_metrics' render 'projects/environments/empty_metrics'
end end
...@@ -23,12 +27,15 @@ module Projects ...@@ -23,12 +27,15 @@ module Projects
private private
def environment def environment
@environment ||= strong_memoize(:environment) do
if params[:environment] project.environments.find(params[:environment]) if params[:environment]
project.environments.find(params[:environment]) end
else end
project.default_environment
end def default_environment
strong_memoize(:default_environment) do
project.default_environment
end
end end
end end
end end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe 'metrics dashboard page' do RSpec.describe 'Projects::MetricsDashboardController' do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:environment) { create(:environment, project: project) } let_it_be(:environment) { create(:environment, project: project) }
let_it_be(:environment2) { create(:environment, project: project) } let_it_be(:environment2) { create(:environment, project: project) }
...@@ -16,12 +16,12 @@ RSpec.describe 'metrics dashboard page' do ...@@ -16,12 +16,12 @@ RSpec.describe 'metrics dashboard page' do
describe 'GET /:namespace/:project/-/metrics' do describe 'GET /:namespace/:project/-/metrics' do
it 'returns 200' do it 'returns 200' do
send_request send_request
expect(response).to have_gitlab_http_status(:ok) expect(response).to redirect_to(dashboard_route(environment: environment))
end end
it 'assigns environment' do it 'assigns default_environment' do
send_request send_request
expect(assigns(:environment).id).to eq(environment.id) expect(assigns(:default_environment).id).to eq(environment.id)
end end
context 'with anonymous user and public dashboard visibility' do context 'with anonymous user and public dashboard visibility' do
...@@ -64,12 +64,12 @@ RSpec.describe 'metrics dashboard page' do ...@@ -64,12 +64,12 @@ RSpec.describe 'metrics dashboard page' do
let(:dashboard_path) { '.gitlab/dashboards/dashboard_path.yml' } let(:dashboard_path) { '.gitlab/dashboards/dashboard_path.yml' }
it 'returns 200' do it 'returns 200' do
send_request(dashboard_path: dashboard_path) send_request(dashboard_path: dashboard_path, environment: environment.id)
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
end end
it 'assigns environment' do it 'assigns environment' do
send_request(dashboard_path: dashboard_path) send_request(dashboard_path: dashboard_path, environment: environment.id)
expect(assigns(:environment).id).to eq(environment.id) expect(assigns(:environment).id).to eq(environment.id)
end end
end end
...@@ -98,14 +98,14 @@ RSpec.describe 'metrics dashboard page' do ...@@ -98,14 +98,14 @@ RSpec.describe 'metrics dashboard page' 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') cannot be used because it encodes '/'
get "#{dashboard_route}/panel/new" 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(page: 'panel/new') cannot be used because it encodes '/'
get "#{dashboard_route(dashboard_path: 'dashboard.yml')}/panel/new" 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