2020-04-06 14:09:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Projects::StaticSiteEditorController < Projects::ApplicationController
|
2020-04-13 08:10:03 -04:00
|
|
|
include ExtractsPath
|
2020-04-15 05:09:46 -04:00
|
|
|
include CreatesCommit
|
|
|
|
|
2020-04-06 14:09:37 -04:00
|
|
|
layout 'fullscreen'
|
|
|
|
|
2020-10-27 14:08:59 -04:00
|
|
|
content_security_policy do |policy|
|
|
|
|
next if policy.directives.blank?
|
|
|
|
|
|
|
|
frame_src_values = Array.wrap(policy.directives['frame-src']) | ['https://www.youtube.com']
|
|
|
|
policy.frame_src(*frame_src_values)
|
|
|
|
end
|
|
|
|
|
2020-04-06 14:09:37 -04:00
|
|
|
prepend_before_action :authenticate_user!, only: [:show]
|
2020-04-13 08:10:03 -04:00
|
|
|
before_action :assign_ref_and_path, only: [:show]
|
2020-04-15 05:09:46 -04:00
|
|
|
before_action :authorize_edit_tree!, only: [:show]
|
2020-04-06 14:09:37 -04:00
|
|
|
|
2020-10-08 14:08:32 -04:00
|
|
|
feature_category :static_site_editor
|
|
|
|
|
2020-04-06 14:09:37 -04:00
|
|
|
def show
|
2020-09-15 17:09:35 -04:00
|
|
|
service_response = ::StaticSiteEditor::ConfigService.new(
|
|
|
|
container: project,
|
|
|
|
current_user: current_user,
|
|
|
|
params: {
|
|
|
|
ref: @ref,
|
|
|
|
path: @path,
|
|
|
|
return_url: params[:return_url]
|
|
|
|
}
|
|
|
|
).execute
|
|
|
|
|
|
|
|
if service_response.success?
|
2020-10-13 14:08:58 -04:00
|
|
|
Gitlab::UsageDataCounters::StaticSiteEditorCounter.increment_views_count
|
|
|
|
|
2020-10-02 20:08:46 -04:00
|
|
|
@data = serialize_necessary_payload_values_to_json(service_response.payload)
|
2020-09-15 17:09:35 -04:00
|
|
|
else
|
2020-09-29 02:09:45 -04:00
|
|
|
# TODO: For now, if the service returns any error, the user is redirected
|
|
|
|
# to the root project page with the error message displayed as an alert.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/213285#note_414808004
|
|
|
|
# for discussion of plans to handle this via a page owned by the Static Site Editor.
|
|
|
|
flash[:alert] = service_response.message
|
|
|
|
redirect_to project_path(project)
|
2020-09-15 17:09:35 -04:00
|
|
|
end
|
2020-04-13 08:10:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-10-02 20:08:46 -04:00
|
|
|
def serialize_necessary_payload_values_to_json(payload)
|
|
|
|
# This will convert booleans, Array-like and Hash-like objects to JSON
|
|
|
|
payload.transform_values do |value|
|
|
|
|
if value.is_a?(String) || value.is_a?(Integer)
|
|
|
|
value
|
2020-10-22 05:08:26 -04:00
|
|
|
elsif value.nil?
|
|
|
|
''
|
2020-10-02 20:08:46 -04:00
|
|
|
else
|
|
|
|
value.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-13 08:10:03 -04:00
|
|
|
def assign_ref_and_path
|
2020-09-15 17:09:35 -04:00
|
|
|
@ref, @path = extract_ref(params.fetch(:id))
|
2020-04-13 08:10:03 -04:00
|
|
|
|
|
|
|
render_404 if @ref.blank? || @path.blank?
|
2020-04-06 14:09:37 -04:00
|
|
|
end
|
|
|
|
end
|