Passing absolute image urls in the markdown content in the webhooks

This commit is contained in:
Satish Perala 2016-08-25 22:35:59 +05:30 committed by Sean McGivern
parent b349c01c6a
commit 9170aab92e
9 changed files with 53 additions and 2 deletions

View file

@ -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?

View file

@ -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)

View file

@ -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,

View file

@ -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,

8
lib/markdown_utils.rb Normal file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)