Commit 7ff24772 authored by Sean McGivern's avatar Sean McGivern

Add base class for hook builders, and use it for notes and wikis

parent 6e4d67e0
...@@ -202,9 +202,7 @@ class Note < ActiveRecord::Base ...@@ -202,9 +202,7 @@ class Note < ActiveRecord::Base
end end
def hook_attrs def hook_attrs
attributes.merge({ Gitlab::HookData::NoteBuilder.new(self).build
"note" => MarkdownUtils.absolute_image_urls(self.note)
})
end end
def for_commit? def for_commit?
......
...@@ -59,9 +59,7 @@ class WikiPage ...@@ -59,9 +59,7 @@ class WikiPage
attr_accessor :attributes attr_accessor :attributes
def hook_attrs def hook_attrs
attributes.merge({ Gitlab::HookData::WikiPageBuilder.new(self).build
"content" => MarkdownUtils.absolute_image_urls(self.content)
})
end end
def initialize(wiki, page = nil, persisted = false) def initialize(wiki, page = nil, persisted = false)
......
module Gitlab
module HookData
class BaseBuilder
attr_accessor :object
def initialize(object)
@object = object
end
private
def absolute_image_urls(markdown_text)
return markdown_text unless markdown_text.present?
markdown_text.gsub(/!\[(.*?)\]\((.*?)\)/,
"![\\1](#{Settings.gitlab.url}\\2)")
end
end
end
end
module Gitlab module Gitlab
module HookData module HookData
class IssuableBuilder class IssuableBuilder < BaseBuilder
CHANGES_KEYS = %i[previous current].freeze CHANGES_KEYS = %i[previous current].freeze
attr_accessor :issuable alias_method :issuable, :object
def initialize(issuable)
@issuable = issuable
end
def build(user: nil, changes: {}) def build(user: nil, changes: {})
hook_data = { hook_data = {
...@@ -64,6 +60,13 @@ module Gitlab ...@@ -64,6 +60,13 @@ module Gitlab
hash hash
end end
end end
def absolute_image_urls(markdown_text)
return markdown_text unless markdown_text.present?
markdown_text.gsub(/!\[(.*?)\]\((.*?)\)/,
"![\\1](#{Settings.gitlab.url}\\2)")
end
end end
end end
end end
module Gitlab module Gitlab
module HookData module HookData
class IssueBuilder class IssueBuilder < BaseBuilder
SAFE_HOOK_ATTRIBUTES = %i[ SAFE_HOOK_ATTRIBUTES = %i[
assignee_id assignee_id
author_id author_id
...@@ -30,15 +30,11 @@ module Gitlab ...@@ -30,15 +30,11 @@ module Gitlab
total_time_spent total_time_spent
].freeze ].freeze
attr_accessor :issue alias_method :issue, :object
def initialize(issue)
@issue = issue
end
def build def build
attrs = { attrs = {
description: MarkdownUtils.absolute_image_urls(issue.description), description: 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,
......
module Gitlab module Gitlab
module HookData module HookData
class MergeRequestBuilder class MergeRequestBuilder < BaseBuilder
SAFE_HOOK_ATTRIBUTES = %i[ SAFE_HOOK_ATTRIBUTES = %i[
assignee_id assignee_id
author_id author_id
...@@ -35,15 +35,11 @@ module Gitlab ...@@ -35,15 +35,11 @@ module Gitlab
total_time_spent total_time_spent
].freeze ].freeze
attr_accessor :merge_request alias_method :merge_request, :object
def initialize(merge_request)
@merge_request = merge_request
end
def build def build
attrs = { attrs = {
description: MarkdownUtils.absolute_image_urls(merge_request.description), description: absolute_image_urls(merge_request.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,
......
module Gitlab
module HookData
class NoteBuilder < BaseBuilder
SAFE_HOOK_ATTRIBUTES = %i[
attachment
author_id
change_position
commit_id
created_at
discussion_id
id
line_code
note
noteable_id
noteable_type
original_position
position
project_id
resolved_at
resolved_by_id
resolved_by_push
st_diff
system
type
updated_at
updated_by_id
].freeze
alias_method :note, :object
def build
note
.attributes
.with_indifferent_access
.slice(*SAFE_HOOK_ATTRIBUTES)
.merge(
description: absolute_image_urls(note.note),
url: Gitlab::UrlBuilder.build(note)
)
end
end
end
end
module Gitlab
module HookData
class WikiPageBuilder < BaseBuilder
alias_method :wiki_page, :object
def build
wiki_page
.attributes
.merge(
'content' => absolute_image_urls(wiki_page.content)
)
end
end
end
end
# 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)
if markdown_text.present?
markdown_text.gsub(/!\[(.*?)\]\((.*?)\)/, "![\\1](#{Settings.gitlab.url}\\2)")
else
markdown_text
end
end
end
...@@ -829,12 +829,4 @@ describe Note do ...@@ -829,12 +829,4 @@ 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
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