2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Projects::StaticSiteEditorController < Projects::ApplicationController
|
|
|
|
include ExtractsPath
|
|
|
|
include CreatesCommit
|
|
|
|
|
|
|
|
layout 'fullscreen'
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
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-22 19:07:51 +05:30
|
|
|
prepend_before_action :authenticate_user!, only: [:show]
|
|
|
|
before_action :assign_ref_and_path, only: [:show]
|
|
|
|
before_action :authorize_edit_tree!, only: [:show]
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
feature_category :static_site_editor
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
def index
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def show
|
2020-11-24 15:15:51 +05:30
|
|
|
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?
|
2021-01-03 14:25:43 +05:30
|
|
|
Gitlab::UsageDataCounters::StaticSiteEditorCounter.increment_views_count
|
|
|
|
|
|
|
|
@data = serialize_necessary_payload_values_to_json(service_response.payload)
|
2020-11-24 15:15:51 +05:30
|
|
|
else
|
2021-01-03 14:25:43 +05:30
|
|
|
# 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-11-24 15:15:51 +05:30
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
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
|
2021-01-29 00:20:46 +05:30
|
|
|
elsif value.nil?
|
|
|
|
''
|
2021-01-03 14:25:43 +05:30
|
|
|
else
|
|
|
|
value.to_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def assign_ref_and_path
|
2020-11-24 15:15:51 +05:30
|
|
|
@ref, @path = extract_ref(params.fetch(:id))
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
render_404 if @ref.blank? || @path.blank?
|
|
|
|
end
|
|
|
|
end
|