2017-10-13 12:50:36 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-08-30 23:10:23 -04:00
|
|
|
# This class includes overriding Kernel#format method
|
|
|
|
# what makes impossible to use it here
|
|
|
|
# rubocop:disable Style/FormatString
|
2017-10-13 12:50:36 -04:00
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
class MarkdownText
|
2021-05-17 05:10:26 -04:00
|
|
|
include Gitlab::EncodingHelper
|
2017-10-13 12:50:36 -04:00
|
|
|
|
2022-07-18 20:09:42 -04:00
|
|
|
ISSUE_REF_MATCHER = '%{github_url}/%{import_source}/issues'
|
|
|
|
PULL_REF_MATCHER = '%{github_url}/%{import_source}/pull'
|
|
|
|
|
2022-08-30 23:10:23 -04:00
|
|
|
MEDIA_TYPES = %w[gif jpeg jpg mov mp4 png svg webm].freeze
|
|
|
|
DOC_TYPES = %w[
|
|
|
|
csv docx fodg fodp fods fodt gz log md odf odg odp ods
|
|
|
|
odt pdf pptx tgz txt xls xlsx zip
|
|
|
|
].freeze
|
|
|
|
ALL_TYPES = (MEDIA_TYPES + DOC_TYPES).freeze
|
|
|
|
|
|
|
|
# On github.com we have base url for docs and CDN url for media.
|
|
|
|
# On github EE as far as we can know there is no CDN urls and media is placed on base url.
|
|
|
|
# To no escape the escaping symbol we use single quotes instead of double with interpolation.
|
|
|
|
# rubocop:disable Style/StringConcatenation
|
|
|
|
CDN_URL_MATCHER = '(!\[.+\]\(%{github_media_cdn}/\d+/(\w|-)+\.(' + MEDIA_TYPES.join('|') + ')\))'
|
|
|
|
BASE_URL_MATCHER = '(\[.+\]\(%{github_url}/.+/.+/files/\d+/.+\.(' + ALL_TYPES.join('|') + ')\))'
|
|
|
|
# rubocop:enable Style/StringConcatenation
|
|
|
|
|
2022-07-18 20:09:42 -04:00
|
|
|
class << self
|
|
|
|
def format(*args)
|
|
|
|
new(*args).to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# Links like `https://domain.github.com/<namespace>/<project>/pull/<iid>` needs to be converted
|
|
|
|
def convert_ref_links(text, project)
|
|
|
|
matcher_options = { github_url: github_url, import_source: project.import_source }
|
|
|
|
issue_ref_matcher = ISSUE_REF_MATCHER % matcher_options
|
|
|
|
pull_ref_matcher = PULL_REF_MATCHER % matcher_options
|
|
|
|
|
|
|
|
url_helpers = Rails.application.routes.url_helpers
|
|
|
|
text.gsub(issue_ref_matcher, url_helpers.project_issues_url(project))
|
|
|
|
.gsub(pull_ref_matcher, url_helpers.project_merge_requests_url(project))
|
|
|
|
end
|
|
|
|
|
2022-08-30 23:10:23 -04:00
|
|
|
def fetch_attachment_urls(text)
|
|
|
|
cdn_url_matcher = CDN_URL_MATCHER % { github_media_cdn: Regexp.escape(github_media_cdn) }
|
|
|
|
doc_url_matcher = BASE_URL_MATCHER % { github_url: Regexp.escape(github_url) }
|
|
|
|
|
|
|
|
text.scan(Regexp.new(cdn_url_matcher)).map(&:first) +
|
|
|
|
text.scan(Regexp.new(doc_url_matcher)).map(&:first)
|
|
|
|
end
|
|
|
|
|
2022-07-18 20:09:42 -04:00
|
|
|
private
|
|
|
|
|
2022-08-30 23:10:23 -04:00
|
|
|
def github_media_cdn
|
|
|
|
'https://user-images.githubusercontent.com'
|
|
|
|
end
|
|
|
|
|
2022-07-18 20:09:42 -04:00
|
|
|
# Returns github domain without slash in the end
|
|
|
|
def github_url
|
|
|
|
oauth_config = Gitlab::Auth::OAuth::Provider.config_for('github') || {}
|
|
|
|
url = oauth_config['url'].presence || 'https://github.com'
|
|
|
|
url = url.chop if url.end_with?('/')
|
|
|
|
url
|
|
|
|
end
|
2017-10-13 12:50:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# text - The Markdown text as a String.
|
|
|
|
# author - An instance of `Gitlab::GithubImport::Representation::User`
|
|
|
|
# exists - Boolean that indicates the user exists in the GitLab database.
|
|
|
|
def initialize(text, author, exists = false)
|
2021-06-22 15:11:50 -04:00
|
|
|
@text = text.to_s
|
2017-10-13 12:50:36 -04:00
|
|
|
@author = author
|
|
|
|
@exists = exists
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
2021-05-17 05:10:26 -04:00
|
|
|
# Gitlab::EncodingHelper#clean remove `null` chars from the string
|
|
|
|
clean(format)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :text, :author, :exists
|
|
|
|
|
|
|
|
def format
|
2021-05-11 14:10:36 -04:00
|
|
|
if author&.login.present? && !exists
|
2017-10-13 12:50:36 -04:00
|
|
|
"*Created by: #{author.login}*\n\n#{text}"
|
2021-05-11 14:10:36 -04:00
|
|
|
else
|
|
|
|
text
|
2017-10-13 12:50:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-08-30 23:10:23 -04:00
|
|
|
# rubocop:enable Style/FormatString
|