diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 01:45:44 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-09-19 01:45:44 +0000 |
commit | 85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch) | |
tree | 9160f299afd8c80c038f08e1545be119f5e3f1e1 /app/services/static_site_editor/config_service.rb | |
parent | 15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff) | |
download | gitlab-ce-85dc423f7090da0a52c73eb66faf22ddb20efff9.tar.gz |
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'app/services/static_site_editor/config_service.rb')
-rw-r--r-- | app/services/static_site_editor/config_service.rb | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/app/services/static_site_editor/config_service.rb b/app/services/static_site_editor/config_service.rb new file mode 100644 index 00000000000..987ee071976 --- /dev/null +++ b/app/services/static_site_editor/config_service.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module StaticSiteEditor + class ConfigService < ::BaseContainerService + ValidationError = Class.new(StandardError) + + def execute + @project = container + check_access! + + ServiceResponse.success(payload: data) + rescue ValidationError => e + ServiceResponse.error(message: e.message) + end + + private + + attr_reader :project + + def check_access! + unless can?(current_user, :download_code, project) + raise ValidationError, 'Insufficient permissions to read configuration' + end + end + + def data + check_for_duplicate_keys! + generated_data.merge(file_data) + end + + def generated_data + @generated_data ||= Gitlab::StaticSiteEditor::Config::GeneratedConfig.new( + project.repository, + params.fetch(:ref), + params.fetch(:path), + params[:return_url] + ).data + end + + def file_data + @file_data ||= Gitlab::StaticSiteEditor::Config::FileConfig.new.data + end + + def check_for_duplicate_keys! + duplicate_keys = generated_data.keys & file_data.keys + raise ValidationError.new("Duplicate key(s) '#{duplicate_keys}' found.") if duplicate_keys.present? + end + end +end |