diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-26 15:08:56 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-26 15:08:56 +0000 |
commit | 17ab40ca089e1aef61a83f77ab6df62a72f6ce06 (patch) | |
tree | 8eb149293eee90ec2750b6ac5e46a111a806424e /app/services/metrics | |
parent | 66d4203791a01fdedf668a78818a229ea2c07aad (diff) | |
download | gitlab-ce-17ab40ca089e1aef61a83f77ab6df62a72f6ce06.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/metrics')
-rw-r--r-- | app/services/metrics/dashboard/update_dashboard_service.rb | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/app/services/metrics/dashboard/update_dashboard_service.rb b/app/services/metrics/dashboard/update_dashboard_service.rb new file mode 100644 index 00000000000..316563ecbe7 --- /dev/null +++ b/app/services/metrics/dashboard/update_dashboard_service.rb @@ -0,0 +1,89 @@ +# frozen_string_literal: true + +# Updates the content of a specified dashboard in .yml file inside `.gitlab/dashboards` +module Metrics + module Dashboard + class UpdateDashboardService < ::BaseService + ALLOWED_FILE_TYPE = '.yml' + USER_DASHBOARDS_DIR = ::Metrics::Dashboard::ProjectDashboardService::DASHBOARD_ROOT + + def execute + catch(:error) do + throw(:error, error(_(%q(You can't commit to this project)), :forbidden)) unless push_authorized? + + result = ::Files::UpdateService.new(project, current_user, dashboard_attrs).execute + throw(:error, result.merge(http_status: :bad_request)) unless result[:status] == :success + + success(result.merge(http_status: :created, dashboard: dashboard_details)) + end + end + + private + + def dashboard_attrs + { + commit_message: params[:commit_message], + file_path: update_dashboard_path, + file_content: update_dashboard_content, + encoding: 'text', + branch_name: branch, + start_branch: repository.branch_exists?(branch) ? branch : project.default_branch + } + end + + def dashboard_details + { + path: update_dashboard_path, + display_name: ::Metrics::Dashboard::ProjectDashboardService.name_for_path(update_dashboard_path), + default: false, + system_dashboard: false + } + end + + def push_authorized? + Gitlab::UserAccess.new(current_user, project: project).can_push_to_branch?(branch) + end + + def branch + @branch ||= begin + throw(:error, error(_('There was an error updating the dashboard, branch name is invalid.'), :bad_request)) unless valid_branch_name? + throw(:error, error(_('There was an error updating the dashboard, branch named: %{branch} already exists.') % { branch: params[:branch] }, :bad_request)) unless new_or_default_branch? + + params[:branch] + end + end + + def new_or_default_branch? + !repository.branch_exists?(params[:branch]) || project.default_branch == params[:branch] + end + + def valid_branch_name? + Gitlab::GitRefValidator.validate(params[:branch]) + end + + def update_dashboard_path + File.join(USER_DASHBOARDS_DIR, file_name) + end + + def target_file_type_valid? + File.extname(params[:file_name]) == ALLOWED_FILE_TYPE + end + + def file_name + @file_name ||= begin + throw(:error, error(_('The file name should have a .yml extension'), :bad_request)) unless target_file_type_valid? + + File.basename(CGI.unescape(params[:file_name])) + end + end + + def update_dashboard_content + ::PerformanceMonitoring::PrometheusDashboard.from_json(params[:file_content]).to_yaml + end + + def repository + @repository ||= project.repository + end + end + end +end |