2018-03-17 18:26:18 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
# This class includes overriding Kernel#format method
|
|
|
|
# what makes impossible to use it here
|
|
|
|
# rubocop:disable Style/FormatString
|
2018-03-17 18:26:18 +05:30
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
class MarkdownText
|
2021-06-08 01:23:25 +05:30
|
|
|
include Gitlab::EncodingHelper
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
# 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.
|
2022-11-25 23:54:43 +05:30
|
|
|
GITHUB_MEDIA_CDN = 'https://user-images.githubusercontent.com'
|
|
|
|
|
|
|
|
ISSUE_REF_MATCHER = '%{github_url}/%{import_source}/issues'
|
|
|
|
PULL_REF_MATCHER = '%{github_url}/%{import_source}/pull'
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
class << self
|
2023-04-23 21:23:45 +05:30
|
|
|
def format(...)
|
|
|
|
new(...).to_s
|
2022-08-13 15:12:31 +05:30
|
|
|
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
|
|
|
|
|
|
|
|
# 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
|
2022-11-25 23:54:43 +05:30
|
|
|
|
|
|
|
def fetch_attachments(text)
|
|
|
|
attachments = []
|
|
|
|
doc = CommonMarker.render_doc(text)
|
|
|
|
|
|
|
|
doc.walk do |node|
|
|
|
|
attachment = extract_attachment(node)
|
|
|
|
attachments << attachment if attachment
|
|
|
|
end
|
|
|
|
attachments
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def extract_attachment(node)
|
|
|
|
::Gitlab::GithubImport::Markdown::Attachment.from_markdown(node)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
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-09-30 23:02:18 +05:30
|
|
|
@text = text.to_s
|
2018-03-17 18:26:18 +05:30
|
|
|
@author = author
|
|
|
|
@exists = exists
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
2021-06-08 01:23:25 +05:30
|
|
|
# Gitlab::EncodingHelper#clean remove `null` chars from the string
|
|
|
|
clean(format)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :text, :author, :exists
|
|
|
|
|
|
|
|
def format
|
|
|
|
if author&.login.present? && !exists
|
2018-03-17 18:26:18 +05:30
|
|
|
"*Created by: #{author.login}*\n\n#{text}"
|
2021-06-08 01:23:25 +05:30
|
|
|
else
|
|
|
|
text
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-10-11 01:57:18 +05:30
|
|
|
# rubocop:enable Style/FormatString
|