summaryrefslogtreecommitdiff
path: root/lib/gitlab/static_site_editor/config.rb
blob: c931cdecbeb5f020bc71a3931bd8e8074447bd2c (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
# frozen_string_literal: true

module Gitlab
  module StaticSiteEditor
    class Config
      SUPPORTED_EXTENSIONS = %w[.md].freeze

      def initialize(repository, ref, file_path, return_url)
        @repository = repository
        @ref = ref
        @file_path = file_path
        @return_url = return_url
        @commit_id = repository.commit(ref)&.id if ref
      end

      def payload
        {
          branch: ref,
          path: file_path,
          commit_id: commit_id,
          project_id: project.id,
          project: project.path,
          namespace: project.namespace.path,
          return_url: return_url,
          is_supported_content: supported_content?.to_s,
          base_url: Gitlab::Routing.url_helpers.project_show_sse_path(project, full_path)
        }
      end

      private

      attr_reader :repository, :ref, :file_path, :return_url, :commit_id

      delegate :project, to: :repository

      def supported_content?
        master_branch? && extension_supported? && file_exists?
      end

      def master_branch?
        ref == 'master'
      end

      def extension_supported?
        File.extname(file_path).in?(SUPPORTED_EXTENSIONS)
      end

      def file_exists?
        commit_id.present? && repository.blob_at(commit_id, file_path).present?
      end

      def full_path
        "#{ref}/#{file_path}"
      end
    end
  end
end