Welcome to mirror list, hosted at ThFree Co, Russian Federation.

base_builder.rb « hook_data « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4a81f6b8a0efeb2ab22839b8533eabe056785e39 (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
# frozen_string_literal: true

module Gitlab
  module HookData
    class BaseBuilder
      attr_accessor :object

      MARKDOWN_SIMPLE_IMAGE =
        "#{::Gitlab::Regex.markdown_code_or_html_blocks_untrusted}" \
        '|' \
        '(?P<image>' \
        '!' \
        '\[(?P<title>[^\n]*?)\]' \
        '\((?P<url>(?P<https>(https?://|//)?)[^\n]+?)\)' \
        ')'.freeze

      def initialize(object)
        @object = object
      end

      private

      def event_data(event)
        event_name =  "#{object.class.name.downcase}_#{event}"

        { event_name: event_name }
      end

      def timestamps_data
        {
          created_at: object.created_at&.xmlschema,
          updated_at: object.updated_at&.xmlschema
        }
      end

      def absolute_image_urls(markdown_text)
        return markdown_text unless markdown_text.present?

        regex = Gitlab::UntrustedRegexp.new(MARKDOWN_SIMPLE_IMAGE, multiline: false)
        return markdown_text unless regex.match?(markdown_text)

        regex.replace_gsub(markdown_text) do |match|
          if match[:image] && !match[:https]
            url = match[:url]
            url = "#{uploads_prefix}#{url}" if url.start_with?('/uploads')
            url = "/#{url}" unless url.start_with?('/')

            "![#{match[:title]}](#{Gitlab.config.gitlab.url}#{url})"
          else
            match.to_s
          end
        end
      end

      def uploads_prefix
        project&.full_path || ''
      end

      def project
        return unless object.respond_to?(:project)

        object.project
      end
    end
  end
end