blob: 6419851623cefe92b51694a016c1e6f39d9fd6f0 (
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
|
# frozen_string_literal: true
module Gitlab
module GithubImport
module Importer
class ReleaseAttachmentsImporter
attr_reader :release_db_id, :release_description, :project
# release - An instance of `ReleaseAttachments`.
# project - An instance of `Project`.
# client - An instance of `Gitlab::GithubImport::Client`.
def initialize(release_attachments, project, _client = nil)
@release_db_id = release_attachments.release_db_id
@release_description = release_attachments.description
@project = project
end
def execute
attachment_urls = MarkdownText.fetch_attachment_urls(release_description)
new_description = attachment_urls.reduce(release_description) do |description, url|
new_url = download_attachment(url)
description.gsub(url, new_url)
end
Release.find(release_db_id).update_column(:description, new_description)
end
private
# in: github attachment markdown url
# out: gitlab attachment markdown url
def download_attachment(markdown_url)
url = extract_url_from_markdown(markdown_url)
name_prefix = extract_name_from_markdown(markdown_url)
downloader = ::Gitlab::GithubImport::AttachmentsDownloader.new(url)
file = downloader.perform
uploader = UploadService.new(project, file, FileUploader).execute
"#{name_prefix}(#{uploader.to_h[:url]})"
ensure
downloader&.delete
end
# in: "![image-icon](https://user-images.githubusercontent.com/..)"
# out: https://user-images.githubusercontent.com/..
def extract_url_from_markdown(text)
text.match(%r{https://.*\)$}).to_a[0].chop
end
# in: "![image-icon](https://user-images.githubusercontent.com/..)"
# out: ![image-icon]
def extract_name_from_markdown(text)
text.match(%r{^!?\[.*\]}).to_a[0]
end
end
end
end
end
|