debian-mirror-gitlab/lib/gitlab/hook_data/base_builder.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
1.6 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-11-18 11:00:15 +05:30
module Gitlab
module HookData
class BaseBuilder
attr_accessor :object
2023-06-09 08:11:10 +05:30
MARKDOWN_SIMPLE_IMAGE =
"#{::Gitlab::Regex.markdown_code_or_html_blocks_untrusted}" \
'|' \
'(?P<image>' \
'!' \
'\[(?P<title>[^\n]*?)\]' \
'\((?P<url>(?P<https>(https?://|//)?)[^\n]+?)\)' \
')'.freeze
2018-11-18 11:00:15 +05:30
def initialize(object)
@object = object
end
private
2021-03-11 19:13:27 +05:30
def event_data(event)
event_name = "#{object.class.name.downcase}_#{event}"
{ event_name: event_name }
end
2021-02-22 17:27:13 +05:30
def timestamps_data
{
created_at: object.created_at&.xmlschema,
updated_at: object.updated_at&.xmlschema
}
end
2018-11-18 11:00:15 +05:30
def absolute_image_urls(markdown_text)
return markdown_text unless markdown_text.present?
2023-06-09 08:11:10 +05:30
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]
2018-12-13 13:39:08 +05:30
url = "#{uploads_prefix}#{url}" if url.start_with?('/uploads')
2018-11-18 11:00:15 +05:30
url = "/#{url}" unless url.start_with?('/')
2023-06-09 08:11:10 +05:30
"![#{match[:title]}](#{Gitlab.config.gitlab.url}#{url})"
2018-11-18 11:00:15 +05:30
else
2023-06-09 08:11:10 +05:30
match.to_s
2018-11-18 11:00:15 +05:30
end
end
end
2018-12-13 13:39:08 +05:30
def uploads_prefix
project&.full_path || ''
end
def project
return unless object.respond_to?(:project)
object.project
end
2018-11-18 11:00:15 +05:30
end
end
end