2020-11-24 15:15:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module StaticSiteEditor
|
|
|
|
class ConfigService < ::BaseContainerService
|
|
|
|
ValidationError = Class.new(StandardError)
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def initialize(container:, current_user: nil, params: {})
|
|
|
|
super
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
@project = container
|
2021-01-03 14:25:43 +05:30
|
|
|
@repository = project.repository
|
|
|
|
@ref = params.fetch(:ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2020-11-24 15:15:51 +05:30
|
|
|
check_access!
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
file_config = load_file_config!
|
|
|
|
file_data = file_config.to_hash_with_defaults
|
|
|
|
generated_data = load_generated_config.data
|
|
|
|
|
|
|
|
check_for_duplicate_keys!(generated_data, file_data)
|
|
|
|
data = merged_data(generated_data, file_data)
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
ServiceResponse.success(payload: data)
|
|
|
|
rescue ValidationError => e
|
|
|
|
ServiceResponse.error(message: e.message)
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => e
|
2021-01-03 14:25:43 +05:30
|
|
|
Gitlab::ErrorTracking.track_and_raise_exception(e)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
attr_reader :project, :repository, :ref
|
|
|
|
|
|
|
|
def static_site_editor_config_file
|
|
|
|
'.gitlab/static-site-editor.yml'
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
def check_access!
|
|
|
|
unless can?(current_user, :download_code, project)
|
|
|
|
raise ValidationError, 'Insufficient permissions to read configuration'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def load_file_config!
|
|
|
|
yaml = yaml_from_repo.presence || '{}'
|
|
|
|
file_config = Gitlab::StaticSiteEditor::Config::FileConfig.new(yaml)
|
|
|
|
|
|
|
|
unless file_config.valid?
|
|
|
|
raise ValidationError, file_config.errors.first
|
|
|
|
end
|
|
|
|
|
|
|
|
file_config
|
|
|
|
rescue Gitlab::StaticSiteEditor::Config::FileConfig::ConfigError => e
|
|
|
|
raise ValidationError, e.message
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def load_generated_config
|
|
|
|
Gitlab::StaticSiteEditor::Config::GeneratedConfig.new(
|
|
|
|
repository,
|
|
|
|
ref,
|
2020-11-24 15:15:51 +05:30
|
|
|
params.fetch(:path),
|
|
|
|
params[:return_url]
|
2021-01-03 14:25:43 +05:30
|
|
|
)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def check_for_duplicate_keys!(generated_data, file_data)
|
2020-11-24 15:15:51 +05:30
|
|
|
duplicate_keys = generated_data.keys & file_data.keys
|
2021-06-08 01:23:25 +05:30
|
|
|
raise ValidationError, "Duplicate key(s) '#{duplicate_keys}' found." if duplicate_keys.present?
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
def merged_data(generated_data, file_data)
|
|
|
|
generated_data.merge(file_data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def yaml_from_repo
|
|
|
|
repository.blob_data_at(ref, static_site_editor_config_file)
|
|
|
|
rescue GRPC::NotFound
|
|
|
|
# Return nil in the case of a GRPC::NotFound exception, so the default config will be used.
|
|
|
|
# Allow any other unexpected exception will be tracked and re-raised.
|
|
|
|
nil
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|