Commit 9170aab9 authored by Satish Perala's avatar Satish Perala Committed by Sean McGivern

Passing absolute image urls in the markdown content in the webhooks

parent b349c01c
...@@ -202,7 +202,9 @@ class Note < ActiveRecord::Base ...@@ -202,7 +202,9 @@ class Note < ActiveRecord::Base
end end
def hook_attrs def hook_attrs
attributes attributes.merge({
"note" => MarkdownUtils.absolute_image_urls(self.note)
})
end end
def for_commit? def for_commit?
......
...@@ -59,7 +59,9 @@ class WikiPage ...@@ -59,7 +59,9 @@ class WikiPage
attr_accessor :attributes attr_accessor :attributes
def hook_attrs def hook_attrs
attributes attributes.merge({
"content" => MarkdownUtils.absolute_image_urls(self.content)
})
end end
def initialize(wiki, page = nil, persisted = false) def initialize(wiki, page = nil, persisted = false)
......
...@@ -38,6 +38,7 @@ module Gitlab ...@@ -38,6 +38,7 @@ module Gitlab
def build def build
attrs = { attrs = {
description: MarkdownUtils.absolute_image_urls(issue.description),
url: Gitlab::UrlBuilder.build(issue), url: Gitlab::UrlBuilder.build(issue),
total_time_spent: issue.total_time_spent, total_time_spent: issue.total_time_spent,
human_total_time_spent: issue.human_total_time_spent, human_total_time_spent: issue.human_total_time_spent,
......
...@@ -43,6 +43,7 @@ module Gitlab ...@@ -43,6 +43,7 @@ module Gitlab
def build def build
attrs = { attrs = {
description: MarkdownUtils.absolute_image_urls(issue.description),
url: Gitlab::UrlBuilder.build(merge_request), url: Gitlab::UrlBuilder.build(merge_request),
source: merge_request.source_project.try(:hook_attrs), source: merge_request.source_project.try(:hook_attrs),
target: merge_request.target_project.hook_attrs, target: merge_request.target_project.hook_attrs,
......
# 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
...@@ -40,5 +40,14 @@ describe Gitlab::HookData::IssueBuilder do ...@@ -40,5 +40,14 @@ describe Gitlab::HookData::IssueBuilder do
expect(data).to include(:human_total_time_spent) expect(data).to include(:human_total_time_spent)
expect(data).to include(:assignee_ids) expect(data).to include(:assignee_ids)
end 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
end end
...@@ -2357,4 +2357,12 @@ describe MergeRequest do ...@@ -2357,4 +2357,12 @@ describe MergeRequest do
end end
end 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 end
...@@ -829,4 +829,12 @@ describe Note do ...@@ -829,4 +829,12 @@ describe Note do
note.destroy! note.destroy!
end end
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 end
...@@ -554,6 +554,18 @@ describe WikiPage do ...@@ -554,6 +554,18 @@ describe WikiPage do
end end
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 private
def remove_temp_repo(path) def remove_temp_repo(path)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment