Commit dd467d6c authored by Tiago Botelho's avatar Tiago Botelho

Prevents project first environment from being fetched in every project view

parent 50a11a33
......@@ -5,7 +5,6 @@ class Projects::ApplicationController < ApplicationController
skip_before_action :authenticate_user!
before_action :project
before_action :repository
before_action :available_environment
layout 'project'
helper_method :repository, :can_collaborate_with_project?, :user_access
......@@ -33,10 +32,6 @@ class Projects::ApplicationController < ApplicationController
@repository ||= project.repository
end
def available_environment
@available_environment ||= project.environments.with_state(:available).first
end
def authorize_action!(action)
unless can?(current_user, action, project)
return access_denied!
......
......@@ -124,6 +124,19 @@ class Projects::EnvironmentsController < Projects::ApplicationController
render :empty
end
def metrics_redirect
environment = project.environments.with_state(:available).first
path =
if environment
environment_metrics_path(environment)
else
empty_project_environments_path(project)
end
redirect_to path
end
def metrics
# Currently, this acts as a hint to load the metrics details into the cache
# if they aren't there already
......
......@@ -15,7 +15,6 @@ class Projects::GitHttpClientController < Projects::ApplicationController
# Git clients will not know what authenticity token to send along
skip_before_action :verify_authenticity_token
skip_before_action :repository
skip_before_action :available_environment
before_action :authenticate_user
private
......
......@@ -2,7 +2,7 @@ class Projects::UploadsController < Projects::ApplicationController
include UploadsActions
# These will kick you out if you don't have access.
skip_before_action :project, :repository, :available_environment,
skip_before_action :project, :repository,
if: -> { action_name == 'show' && image_or_video? }
before_action :authorize_upload_file!, only: [:create]
......
......@@ -8,7 +8,6 @@ class ProjectsController < Projects::ApplicationController
before_action :redirect_git_extension, only: [:show]
before_action :project, except: [:index, :new, :create]
before_action :repository, except: [:index, :new, :create]
before_action :available_environment, except: [:index, :new, :create]
before_action :assign_ref_vars, only: [:show], if: :repo_exists?
before_action :tree, only: [:show], if: [:repo_exists?, :project_view_files?]
before_action :lfs_blob_ids, only: [:show], if: [:repo_exists?, :project_view_files?]
......
......@@ -4,10 +4,4 @@ module EnvironmentsHelper
endpoint: project_environments_path(@project, format: :json)
}
end
def operations_metrics_path(project, environment)
return environment_metrics_path(environment) if environment
empty_project_environments_path(project)
end
end
......@@ -204,14 +204,14 @@
%ul.sidebar-sub-level-items
= nav_link(controller: [:environments, :clusters, :user, :gcp], html_options: { class: "fly-out-top-item" } ) do
= link_to operations_metrics_path(@project, @available_environment) do
= link_to metrics_project_environments_path(@project) do
%strong.fly-out-top-item-name
= _('Operations')
%li.divider.fly-out-top-item
- if project_nav_tab? :environments
= nav_link(controller: :environments, action: [:metrics, :empty]) do
= link_to operations_metrics_path(@project, @available_environment), title: 'Metrics', class: 'shortcuts-metrics' do
= link_to metrics_project_environments_path(@project), title: 'Metrics', class: 'shortcuts-metrics' do
%span
= _('Metrics')
......
......@@ -235,6 +235,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
end
collection do
get :metrics, action: :metrics_redirect
get :empty
get :folder, path: 'folders/*id', constraints: { format: /(html|json)/ }
end
......
......@@ -287,6 +287,24 @@ describe Projects::EnvironmentsController do
end
end
describe 'GET #metrics_redirect' do
let(:project) { create(:project) }
it 'redirects to environment if it exists' do
environment = create(:environment, name: 'production', project: project)
get :metrics_redirect, environment_params
expect(response).to redirect_to(environment_metrics_path(environment))
end
it 'redirects to empty page if no environment exists' do
get :metrics_redirect, environment_params
expect(response).to redirect_to(empty_project_environments_path(project))
end
end
describe 'GET #metrics' do
before do
allow(controller).to receive(:environment).and_return(environment)
......
require 'spec_helper'
describe EnvironmentsHelper do
include ApplicationHelper
describe 'operations_metrics_path' do
let(:project) { create(:project) }
it 'returns empty metrics path when environment is nil' do
expect(helper.operations_metrics_path(project, nil)).to eq(empty_project_environments_path(project))
end
it 'returns environment metrics path when environment is passed' do
environment = create(:environment, project: project)
expect(helper.operations_metrics_path(project, environment)).to eq(environment_metrics_path(environment))
end
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