Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
7ff24772
Commit
7ff24772
authored
Jun 20, 2018
by
Sean McGivern
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add base class for hook builders, and use it for notes and wikis
parent
6e4d67e0
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
95 additions
and
45 deletions
+95
-45
app/models/note.rb
app/models/note.rb
+1
-3
app/models/wiki_page.rb
app/models/wiki_page.rb
+1
-3
lib/gitlab/hook_data/base_builder.rb
lib/gitlab/hook_data/base_builder.rb
+20
-0
lib/gitlab/hook_data/issuable_builder.rb
lib/gitlab/hook_data/issuable_builder.rb
+9
-6
lib/gitlab/hook_data/issue_builder.rb
lib/gitlab/hook_data/issue_builder.rb
+3
-7
lib/gitlab/hook_data/merge_request_builder.rb
lib/gitlab/hook_data/merge_request_builder.rb
+3
-7
lib/gitlab/hook_data/note_builder.rb
lib/gitlab/hook_data/note_builder.rb
+43
-0
lib/gitlab/hook_data/wiki_page_builder.rb
lib/gitlab/hook_data/wiki_page_builder.rb
+15
-0
lib/markdown_utils.rb
lib/markdown_utils.rb
+0
-11
spec/models/note_spec.rb
spec/models/note_spec.rb
+0
-8
No files found.
app/models/note.rb
View file @
7ff24772
...
...
@@ -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?
...
...
app/models/wiki_page.rb
View file @
7ff24772
...
...
@@ -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
)
...
...
lib/gitlab/hook_data/base_builder.rb
0 → 100644
View file @
7ff24772
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
lib/gitlab/hook_data/issuable_builder.rb
View file @
7ff24772
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
lib/gitlab/hook_data/issue_builder.rb
View file @
7ff24772
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
,
...
...
lib/gitlab/hook_data/merge_request_builder.rb
View file @
7ff24772
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
,
...
...
lib/gitlab/hook_data/note_builder.rb
0 → 100644
View file @
7ff24772
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
lib/gitlab/hook_data/wiki_page_builder.rb
0 → 100644
View file @
7ff24772
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
lib/markdown_utils.rb
deleted
100644 → 0
View file @
6e4d67e0
# 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
spec/models/note_spec.rb
View file @
7ff24772
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment