summaryrefslogtreecommitdiff
path: root/app/controllers/projects/static_site_editor_controller.rb
blob: 0d9a6f568a1c8e5a25c70881d85a5d6d88305aa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# frozen_string_literal: true

class Projects::StaticSiteEditorController < Projects::ApplicationController
  include ExtractsPath
  include CreatesCommit

  layout 'fullscreen'

  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

  prepend_before_action :authenticate_user!, only: [:show]
  before_action :assign_ref_and_path, only: [:show]
  before_action :authorize_edit_tree!, only: [:show]

  feature_category :static_site_editor

  def index
    render_404
  end

  def show
    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?
      Gitlab::UsageDataCounters::StaticSiteEditorCounter.increment_views_count

      @data = serialize_necessary_payload_values_to_json(service_response.payload)
    else
      # 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)
    end
  end

  private

  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
      elsif value.nil?
        ''
      else
        value.to_json
      end
    end
  end

  def assign_ref_and_path
    @ref, @path = extract_ref(params.fetch(:id))

    render_404 if @ref.blank? || @path.blank?
  end
end