diff options
author | Satish Perala <satish@cybrilla.com> | 2016-08-25 20:05:59 +0300 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2018-06-20 16:13:23 +0300 |
commit | 9170aab92e616a6f6d3ddfc4cf8326cba0e4a1a8 (patch) | |
tree | ad2c13c7521fedb74f89693dd4561e92f92167db | |
parent | b349c01c6a71ac1f486b8ee86ce96ef48ac04ed8 (diff) |
Passing absolute image urls in the markdown content in the webhooks
-rw-r--r-- | app/models/note.rb | 4 | ||||
-rw-r--r-- | app/models/wiki_page.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/hook_data/issue_builder.rb | 1 | ||||
-rw-r--r-- | lib/gitlab/hook_data/merge_request_builder.rb | 1 | ||||
-rw-r--r-- | lib/markdown_utils.rb | 8 | ||||
-rw-r--r-- | spec/lib/gitlab/hook_data/issue_builder_spec.rb | 9 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 8 | ||||
-rw-r--r-- | spec/models/note_spec.rb | 8 | ||||
-rw-r--r-- | spec/models/wiki_page_spec.rb | 12 |
9 files changed, 53 insertions, 2 deletions
diff --git a/app/models/note.rb b/app/models/note.rb index 41c04ae0571..3893fe25fb7 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -202,7 +202,9 @@ class Note < ActiveRecord::Base end def hook_attrs - attributes + attributes.merge({ + "note" => MarkdownUtils.absolute_image_urls(self.note) + }) end def for_commit? diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index cde79b95062..5f8552212ad 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -59,7 +59,9 @@ class WikiPage attr_accessor :attributes def hook_attrs - attributes + attributes.merge({ + "content" => MarkdownUtils.absolute_image_urls(self.content) + }) end def initialize(wiki, page = nil, persisted = false) diff --git a/lib/gitlab/hook_data/issue_builder.rb b/lib/gitlab/hook_data/issue_builder.rb index f9b1a3caf5e..55dc84a60d4 100644 --- a/lib/gitlab/hook_data/issue_builder.rb +++ b/lib/gitlab/hook_data/issue_builder.rb @@ -38,6 +38,7 @@ module Gitlab def build attrs = { + description: MarkdownUtils.absolute_image_urls(issue.description), url: Gitlab::UrlBuilder.build(issue), total_time_spent: issue.total_time_spent, human_total_time_spent: issue.human_total_time_spent, diff --git a/lib/gitlab/hook_data/merge_request_builder.rb b/lib/gitlab/hook_data/merge_request_builder.rb index aff786864f2..2dfc0435848 100644 --- a/lib/gitlab/hook_data/merge_request_builder.rb +++ b/lib/gitlab/hook_data/merge_request_builder.rb @@ -43,6 +43,7 @@ module Gitlab def build attrs = { + description: MarkdownUtils.absolute_image_urls(issue.description), url: Gitlab::UrlBuilder.build(merge_request), source: merge_request.source_project.try(:hook_attrs), target: merge_request.target_project.hook_attrs, diff --git a/lib/markdown_utils.rb b/lib/markdown_utils.rb new file mode 100644 index 00000000000..a5cf035018b --- /dev/null +++ b/lib/markdown_utils.rb @@ -0,0 +1,8 @@ +# Class to have all utility functions related to markdown +class MarkdownUtils + + # Convert image urls in the markdown text to absolute urls + def self.absolute_image_urls(markdown_text) + markdown_text.gsub(/!\[(.*?)\]\((.*?)\)/, "![\\1](#{Settings.gitlab.url}\\2)") + end +end diff --git a/spec/lib/gitlab/hook_data/issue_builder_spec.rb b/spec/lib/gitlab/hook_data/issue_builder_spec.rb index 506b2c0be20..0e4a1d345f4 100644 --- a/spec/lib/gitlab/hook_data/issue_builder_spec.rb +++ b/spec/lib/gitlab/hook_data/issue_builder_spec.rb @@ -40,5 +40,14 @@ describe Gitlab::HookData::IssueBuilder do expect(data).to include(:human_total_time_spent) expect(data).to include(:assignee_ids) end + + context 'when the issue has an image in the description' do + let(:issue_with_description) { create(:issue, description: 'test![Issue_Image](/uploads/abc/Issue_Image.png)') } + let(:builder) { described_class.new(issue_with_description) } + + it 'adds absolute urls for images in the description' do + expect(data[:description]).to eq("test![Issue_Image](#{Settings.gitlab.url}/uploads/abc/Issue_Image.png)") + end + end end end diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index 3f028b3bd5c..da302b5457e 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -2357,4 +2357,12 @@ describe MergeRequest do end end end + + describe '#hook_attrs' do + let(:mr_with_description) { create(:merge_request, description: 'test![Mr_Image](/uploads/abc/Mr_Image.png)') } + + it 'adds absolute urls for images in the description' do + expect(mr_with_description.hook_attrs['description']).to eq("test![Mr_Image](#{Settings.gitlab.url}/uploads/abc/Mr_Image.png)") + end + end end diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb index 6a6c71e6c82..41d84ded1b2 100644 --- a/spec/models/note_spec.rb +++ b/spec/models/note_spec.rb @@ -829,4 +829,12 @@ describe Note do note.destroy! end end + + describe '#hook_attrs' do + let(:note) { create(:note, note: 'test![Note_Image](/uploads/abc/Note_Image.png)') } + + it 'adds absolute urls for images in the description' do + expect(note.hook_attrs['note']).to eq("test![Note_Image](#{Settings.gitlab.url}/uploads/abc/Note_Image.png)") + end + end end diff --git a/spec/models/wiki_page_spec.rb b/spec/models/wiki_page_spec.rb index 1c765ceac2f..c00c628b172 100644 --- a/spec/models/wiki_page_spec.rb +++ b/spec/models/wiki_page_spec.rb @@ -554,6 +554,18 @@ describe WikiPage do end end + describe '#hook_attrs' do + before do + create_page("test page", "test![WikiPage_Image](/uploads/abc/WikiPage_Image.png)") + @page = wiki.wiki.paged("test page") + @wiki_page = WikiPage.new(wiki, @page, true) + end + + it 'adds absolute urls for images in the content' do + expect(@wiki_page.hook_attrs['content']).to eq("test![WikiPage_Image](#{Settings.gitlab.url}/uploads/abc/WikiPage_Image.png)") + end + end + private def remove_temp_repo(path) |