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