diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
commit | 9f46488805e86b1bc341ea1620b866016c2ce5ed (patch) | |
tree | f9748c7e287041e37d6da49e0a29c9511dc34768 /app/services/metrics/users_starred_dashboards/create_service.rb | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'app/services/metrics/users_starred_dashboards/create_service.rb')
-rw-r--r-- | app/services/metrics/users_starred_dashboards/create_service.rb | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/app/services/metrics/users_starred_dashboards/create_service.rb b/app/services/metrics/users_starred_dashboards/create_service.rb new file mode 100644 index 00000000000..7784ed4eb4e --- /dev/null +++ b/app/services/metrics/users_starred_dashboards/create_service.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +# Create Metrics::UsersStarredDashboard entry for given user based on matched dashboard_path, project +module Metrics + module UsersStarredDashboards + class CreateService < ::BaseService + include Stepable + + steps :authorize_create_action, + :parse_dashboard_path, + :create + + def initialize(user, project, dashboard_path) + @user, @project, @dashboard_path = user, project, dashboard_path + end + + def execute + keys = %i[status message starred_dashboard] + status, message, dashboards = execute_steps.values_at(*keys) + + if status != :success + ServiceResponse.error(message: message) + else + ServiceResponse.success(payload: dashboards) + end + end + + private + + attr_reader :user, :project, :dashboard_path + + def authorize_create_action(_options) + if Ability.allowed?(user, :create_metrics_user_starred_dashboard, project) + success(user: user, project: project) + else + error(s_('Metrics::UsersStarredDashboards|You are not authorized to add star to this dashboard')) + end + end + + def parse_dashboard_path(options) + if dashboard_path_exists? + options[:dashboard_path] = dashboard_path + success(options) + else + error(s_('Metrics::UsersStarredDashboards|Dashboard with requested path can not be found')) + end + end + + def create(options) + starred_dashboard = build_starred_dashboard_from(options) + + if starred_dashboard.save + success(starred_dashboard: starred_dashboard) + else + error(starred_dashboard.errors.messages) + end + end + + def build_starred_dashboard_from(options) + Metrics::UsersStarredDashboard.new( + user: options.fetch(:user), + project: options.fetch(:project), + dashboard_path: options.fetch(:dashboard_path) + ) + end + + def dashboard_path_exists? + Gitlab::Metrics::Dashboard::Finder + .find_all_paths(project) + .any? { |dashboard| dashboard[:path] == dashboard_path } + end + end + end +end |