diff options
author | Sean McGivern <sean@gitlab.com> | 2018-06-20 14:53:19 +0100 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2018-06-20 15:52:16 +0100 |
commit | 7ff24772b63691f857a1ee1d113dcbdc3b01f064 (patch) | |
tree | 3ed303b327f2968f1984d301affe9781932c3671 /lib/gitlab/hook_data | |
parent | 6e4d67e099cfe15bc5daf5014fd74d9ceb1cb3f3 (diff) | |
download | gitlab-ce-7ff24772b63691f857a1ee1d113dcbdc3b01f064.tar.gz |
Add base class for hook builders, and use it for notes and wikis
Diffstat (limited to 'lib/gitlab/hook_data')
-rw-r--r-- | lib/gitlab/hook_data/base_builder.rb | 20 | ||||
-rw-r--r-- | lib/gitlab/hook_data/issuable_builder.rb | 15 | ||||
-rw-r--r-- | lib/gitlab/hook_data/issue_builder.rb | 10 | ||||
-rw-r--r-- | lib/gitlab/hook_data/merge_request_builder.rb | 10 | ||||
-rw-r--r-- | lib/gitlab/hook_data/note_builder.rb | 43 | ||||
-rw-r--r-- | lib/gitlab/hook_data/wiki_page_builder.rb | 15 |
6 files changed, 93 insertions, 20 deletions
diff --git a/lib/gitlab/hook_data/base_builder.rb b/lib/gitlab/hook_data/base_builder.rb new file mode 100644 index 00000000000..1d99e793a03 --- /dev/null +++ b/lib/gitlab/hook_data/base_builder.rb @@ -0,0 +1,20 @@ +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 diff --git a/lib/gitlab/hook_data/issuable_builder.rb b/lib/gitlab/hook_data/issuable_builder.rb index 6ab36676127..ad0d6f6352b 100644 --- a/lib/gitlab/hook_data/issuable_builder.rb +++ b/lib/gitlab/hook_data/issuable_builder.rb @@ -1,13 +1,9 @@ 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 diff --git a/lib/gitlab/hook_data/issue_builder.rb b/lib/gitlab/hook_data/issue_builder.rb index 55dc84a60d4..0d71c748dc6 100644 --- a/lib/gitlab/hook_data/issue_builder.rb +++ b/lib/gitlab/hook_data/issue_builder.rb @@ -1,6 +1,6 @@ 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, diff --git a/lib/gitlab/hook_data/merge_request_builder.rb b/lib/gitlab/hook_data/merge_request_builder.rb index 6e5ef09de9e..dfbed0597ed 100644 --- a/lib/gitlab/hook_data/merge_request_builder.rb +++ b/lib/gitlab/hook_data/merge_request_builder.rb @@ -1,6 +1,6 @@ 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, diff --git a/lib/gitlab/hook_data/note_builder.rb b/lib/gitlab/hook_data/note_builder.rb new file mode 100644 index 00000000000..81873e345d5 --- /dev/null +++ b/lib/gitlab/hook_data/note_builder.rb @@ -0,0 +1,43 @@ +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 diff --git a/lib/gitlab/hook_data/wiki_page_builder.rb b/lib/gitlab/hook_data/wiki_page_builder.rb new file mode 100644 index 00000000000..59c94a61cf2 --- /dev/null +++ b/lib/gitlab/hook_data/wiki_page_builder.rb @@ -0,0 +1,15 @@ +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 |