Commit 0365b2c1 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch '35217-add_edit_path_to_all_dashboards' into 'master'

Add can_edit and project_blob_path to metrics_dashboard endpoint

Closes #35217

See merge request gitlab-org/gitlab!19663
parents c8645e52 49cdcec4
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
# to the parameters specified by the controller. # to the parameters specified by the controller.
module MetricsDashboard module MetricsDashboard
include RenderServiceResults include RenderServiceResults
include ChecksCollaboration
extend ActiveSupport::Concern extend ActiveSupport::Concern
def metrics_dashboard def metrics_dashboard
...@@ -14,7 +16,7 @@ module MetricsDashboard ...@@ -14,7 +16,7 @@ module MetricsDashboard
) )
if include_all_dashboards? && result if include_all_dashboards? && result
result[:all_dashboards] = dashboard_finder.find_all_paths(project_for_dashboard) result[:all_dashboards] = all_dashboards
end end
respond_to do |format| respond_to do |format|
...@@ -30,6 +32,30 @@ module MetricsDashboard ...@@ -30,6 +32,30 @@ module MetricsDashboard
private private
def all_dashboards
dashboards = dashboard_finder.find_all_paths(project_for_dashboard)
dashboards.map do |dashboard|
amend_dashboard(dashboard)
end
end
def amend_dashboard(dashboard)
project_dashboard = project_for_dashboard && !dashboard[:system_dashboard]
dashboard[:can_edit] = project_dashboard ? can_edit?(dashboard) : false
dashboard[:project_blob_path] = project_dashboard ? dashboard_project_blob_path(dashboard) : nil
dashboard
end
def dashboard_project_blob_path(dashboard)
project_blob_path(project_for_dashboard, File.join(project_for_dashboard.default_branch, dashboard.fetch(:path, "")))
end
def can_edit?(dashboard)
can_collaborate_with_project?(project_for_dashboard, ref: project_for_dashboard.default_branch)
end
# Override in class to provide arguments to the finder. # Override in class to provide arguments to the finder.
def metrics_dashboard_params def metrics_dashboard_params
{} {}
......
...@@ -16,7 +16,8 @@ module Metrics ...@@ -16,7 +16,8 @@ module Metrics
{ {
path: filepath, path: filepath,
display_name: name_for_path(filepath), display_name: name_for_path(filepath),
default: false default: false,
system_dashboard: false
} }
end end
end end
......
...@@ -20,7 +20,8 @@ module Metrics ...@@ -20,7 +20,8 @@ module Metrics
[{ [{
path: SYSTEM_DASHBOARD_PATH, path: SYSTEM_DASHBOARD_PATH,
display_name: SYSTEM_DASHBOARD_NAME, display_name: SYSTEM_DASHBOARD_NAME,
default: true default: true,
system_dashboard: true
}] }]
end end
......
---
title: Add can_edit and project_blob_path to metrics_dashboard endpoint
merge_request: 19663
author:
type: added
...@@ -3,9 +3,11 @@ ...@@ -3,9 +3,11 @@
require 'spec_helper' require 'spec_helper'
describe MetricsDashboard do describe MetricsDashboard do
include MetricsDashboardHelpers
describe 'GET #metrics_dashboard' do describe 'GET #metrics_dashboard' do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :repository) } let_it_be(:project) { project_with_dashboard('.gitlab/dashboards/test.yml') }
let_it_be(:environment) { create(:environment, project: project) } let_it_be(:environment) { create(:environment, project: project) }
before do before do
...@@ -63,6 +65,36 @@ describe MetricsDashboard do ...@@ -63,6 +65,36 @@ describe MetricsDashboard do
expect(json_response['dashboard']['dashboard']).to eq('Environment metrics') expect(json_response['dashboard']['dashboard']).to eq('Environment metrics')
expect(json_response).to have_key('all_dashboards') expect(json_response).to have_key('all_dashboards')
end end
context 'in all_dashboard list' do
let(:system_dashboard) { json_response['all_dashboards'].find { |dashboard| dashboard["system_dashboard"] == true } }
let(:project_dashboard) { json_response['all_dashboards'].find { |dashboard| dashboard["system_dashboard"] == false } }
it 'includes project_blob_path only for project dashboards' do
expect(system_dashboard['project_blob_path']).to be_nil
expect(project_dashboard['project_blob_path']).to eq("/#{project.namespace.path}/#{project.name}/blob/master/.gitlab/dashboards/test.yml")
end
describe 'project permissions' do
using RSpec::Parameterized::TableSyntax
where(:can_collaborate, :system_can_edit, :project_can_edit) do
false | false | false
true | false | true
end
with_them do
before do
allow(controller).to receive(:can_collaborate_with_project?).and_return(can_collaborate)
end
it "sets can_edit appropriately" do
expect(system_dashboard["can_edit"]).to eq(system_can_edit)
expect(project_dashboard["can_edit"]).to eq(project_can_edit)
end
end
end
end
end end
end end
end end
......
...@@ -136,7 +136,7 @@ describe Gitlab::Metrics::Dashboard::Finder, :use_clean_rails_memory_store_cachi ...@@ -136,7 +136,7 @@ describe Gitlab::Metrics::Dashboard::Finder, :use_clean_rails_memory_store_cachi
describe '.find_all_paths' do describe '.find_all_paths' do
let(:all_dashboard_paths) { described_class.find_all_paths(project) } let(:all_dashboard_paths) { described_class.find_all_paths(project) }
let(:system_dashboard) { { path: system_dashboard_path, display_name: 'Default', default: true } } let(:system_dashboard) { { path: system_dashboard_path, display_name: 'Default', default: true, system_dashboard: true } }
it 'includes only the system dashboard by default' do it 'includes only the system dashboard by default' do
expect(all_dashboard_paths).to eq([system_dashboard]) expect(all_dashboard_paths).to eq([system_dashboard])
...@@ -147,7 +147,7 @@ describe Gitlab::Metrics::Dashboard::Finder, :use_clean_rails_memory_store_cachi ...@@ -147,7 +147,7 @@ describe Gitlab::Metrics::Dashboard::Finder, :use_clean_rails_memory_store_cachi
let(:project) { project_with_dashboard(dashboard_path) } let(:project) { project_with_dashboard(dashboard_path) }
it 'includes system and project dashboards' do it 'includes system and project dashboards' do
project_dashboard = { path: dashboard_path, display_name: 'test.yml', default: false } project_dashboard = { path: dashboard_path, display_name: 'test.yml', default: false, system_dashboard: false }
expect(all_dashboard_paths).to contain_exactly(system_dashboard, project_dashboard) expect(all_dashboard_paths).to contain_exactly(system_dashboard, project_dashboard)
end end
......
...@@ -80,7 +80,8 @@ describe Metrics::Dashboard::ProjectDashboardService, :use_clean_rails_memory_st ...@@ -80,7 +80,8 @@ describe Metrics::Dashboard::ProjectDashboardService, :use_clean_rails_memory_st
[{ [{
path: dashboard_path, path: dashboard_path,
display_name: 'test.yml', display_name: 'test.yml',
default: false default: false,
system_dashboard: false
}] }]
) )
end end
......
...@@ -44,7 +44,8 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto ...@@ -44,7 +44,8 @@ describe Metrics::Dashboard::SystemDashboardService, :use_clean_rails_memory_sto
[{ [{
path: described_class::SYSTEM_DASHBOARD_PATH, path: described_class::SYSTEM_DASHBOARD_PATH,
display_name: described_class::SYSTEM_DASHBOARD_NAME, display_name: described_class::SYSTEM_DASHBOARD_NAME,
default: true default: true,
system_dashboard: true
}] }]
) )
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