Commit 28cdd8aa authored by Ryan Cobb's avatar Ryan Cobb

Add can_edit to metric dashboard endpoint

parent 359c524e
......@@ -35,15 +35,23 @@ module MetricsDashboard
def all_dashboards
dashboards = dashboard_finder.find_all_paths(project_for_dashboard)
dashboards.map do |dashboard|
dashboard[:edit_path] = edit_path(dashboard)
dashboard[:can_edit] = can_edit?(dashboard)
dashboard[:project_blob_path] = project_blob_path(dashboard)
dashboard
end
end
def edit_path(dashboard)
def can_edit?(dashboard)
return false unless project_for_dashboard
return false if dashboard[:system_dashboard]
return false unless can_collaborate_with_project?(project_for_dashboard, ref: project_for_dashboard.default_branch)
true
end
def project_blob_path(dashboard)
return unless project_for_dashboard
return if dashboard[:system_dashboard]
return unless can_collaborate_with_project?(project_for_dashboard, ref: project_for_dashboard.default_branch)
Gitlab::Routing.url_helpers.project_blob_path(project_for_dashboard, File.join(project_for_dashboard.default_branch, dashboard[:path]))
end
......
......@@ -67,10 +67,18 @@ describe MetricsDashboard do
end
context 'in all_dashboard list' do
let(:system_dashboard) { json_response['all_dashboards'][0] }
let(:project_dashboard) { json_response['all_dashboards'][1] }
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
context 'when a user can collaborate on project' do
it 'includes edit_path only for project dashboards' do
expect(json_response['all_dashboards'][0]['edit_path']).to be_nil
expect(json_response['all_dashboards'][1]['edit_path']).to eq("/#{project.namespace.path}/#{project.name}/blob/master/.gitlab/dashboards/test.yml")
it 'sets can_edit to true for project dashboards' do
expect(system_dashboard['can_edit']).to eq(false)
expect(project_dashboard['can_edit']).to eq(true)
end
end
......@@ -79,8 +87,9 @@ describe MetricsDashboard do
allow(controller).to receive(:can_collaborate_with_project?).and_return(false)
end
it 'does not include edit_path for project dashboards' do
expect(json_response['all_dashboards'][1]['edit_path']).to be_nil
it 'sets can_edit to false for project dashboards' do
expect(system_dashboard['can_edit']).to eq(false)
expect(project_dashboard['can_edit']).to eq(false)
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