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
end
def hook_attrs
attributes.merge({
"note" => MarkdownUtils.absolute_image_urls(self.note)
})
Gitlab::HookData::NoteBuilder.new(self).build
end
def for_commit?
......
......@@ -59,9 +59,7 @@ class WikiPage
attr_accessor :attributes
def hook_attrs
attributes.merge({
"content" => MarkdownUtils.absolute_image_urls(self.content)
})
Gitlab::HookData::WikiPageBuilder.new(self).build
end
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 HookData
class IssuableBuilder
class IssuableBuilder < BaseBuilder
CHANGES_KEYS = %i[previous current].freeze
attr_accessor :issuable
def initialize(issuable)
@issuable = issuable
end
alias_method :issuable, :object
def build(user: nil, changes: {})
hook_data = {
......@@ -64,6 +60,13 @@ module Gitlab
hash
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
module Gitlab
module HookData
class IssueBuilder
class IssueBuilder < BaseBuilder
SAFE_HOOK_ATTRIBUTES = %i[
assignee_id
author_id
......@@ -30,15 +30,11 @@ module Gitlab
total_time_spent
].freeze
attr_accessor :issue
def initialize(issue)
@issue = issue
end
alias_method :issue, :object
def build
attrs = {
description: MarkdownUtils.absolute_image_urls(issue.description),
description: 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,
......
module Gitlab
module HookData
class MergeRequestBuilder
class MergeRequestBuilder < BaseBuilder
SAFE_HOOK_ATTRIBUTES = %i[
assignee_id
author_id
......@@ -35,15 +35,11 @@ module Gitlab
total_time_spent
].freeze
attr_accessor :merge_request
def initialize(merge_request)
@merge_request = merge_request
end
alias_method :merge_request, :object
def build
attrs = {
description: MarkdownUtils.absolute_image_urls(merge_request.description),
description: absolute_image_urls(merge_request.description),
url: Gitlab::UrlBuilder.build(merge_request),
source: merge_request.source_project.try(: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
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
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