Commit 1e527d1e authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'mwam/214582-add-starred-dashboard-to-metrics-dashboard-endpoint' into 'master'

Add starred dashboard to metrics dashboard endpoint

See merge request gitlab-org/gitlab!31059
parents 1a018bb5 c6c16db2
......@@ -35,10 +35,10 @@ module MetricsDashboard
private
def all_dashboards
dashboards = dashboard_finder.find_all_paths(project_for_dashboard)
dashboards.map do |dashboard|
amend_dashboard(dashboard)
end
dashboard_finder
.find_all_paths(project_for_dashboard)
.map(&method(:amend_dashboard))
.sort_by { |dashboard| [dashboard[:starred] ? 0 : 1, dashboard[:display_name].downcase] }
end
def amend_dashboard(dashboard)
......@@ -46,6 +46,8 @@ module MetricsDashboard
dashboard[:can_edit] = project_dashboard ? can_edit?(dashboard) : false
dashboard[:project_blob_path] = project_dashboard ? dashboard_project_blob_path(dashboard) : nil
dashboard[:starred] = starred_dashboards.include?(dashboard[:path])
dashboard[:user_starred_path] = nil # placeholder attribute until API endpoint will be merged https://gitlab.com/gitlab-org/gitlab/-/merge_requests/31316
dashboard
end
......@@ -73,6 +75,20 @@ module MetricsDashboard
::Gitlab::Metrics::Dashboard::Finder
end
def starred_dashboards
@starred_dashboards ||= begin
if project_for_dashboard.present?
::Metrics::UsersStarredDashboardsFinder
.new(user: current_user, project: project_for_dashboard)
.execute
.map(&:dashboard_path)
.to_set
else
Set.new
end
end
end
# Project is not defined for group and admin level clusters.
def project_for_dashboard
defined?(project) ? project : nil
......
---
title: Display metrics dashboards starred by user at the top of dashboard select field.
merge_request: 31059
author:
type: added
......@@ -114,6 +114,35 @@ describe MetricsDashboard do
end
end
end
context 'starred dashboards' do
let_it_be(:dashboard_yml) { fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml') }
let_it_be(:dashboards) do
{
'.gitlab/dashboards/test.yml' => dashboard_yml,
'.gitlab/dashboards/anomaly.yml' => dashboard_yml,
'.gitlab/dashboards/errors.yml' => dashboard_yml
}
end
let_it_be(:project) { create(:project, :custom_repo, files: dashboards) }
before do
create(:metrics_users_starred_dashboard, user: user, project: project, dashboard_path: '.gitlab/dashboards/errors.yml')
create(:metrics_users_starred_dashboard, user: user, project: project, dashboard_path: '.gitlab/dashboards/test.yml')
end
it 'adds starred dashboard information and sorts the list' do
all_dashboards = json_response['all_dashboards'].map { |dashboard| dashboard.slice('display_name', 'starred', 'user_starred_path') }
expected_response = [
{ "display_name" => "errors.yml", "starred" => true, 'user_starred_path' => nil },
{ "display_name" => "test.yml", "starred" => true, 'user_starred_path' => nil },
{ "display_name" => "anomaly.yml", "starred" => false, 'user_starred_path' => nil },
{ "display_name" => "Default", "starred" => false, 'user_starred_path' => nil }
]
expect(all_dashboards).to eql expected_response
end
end
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