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
Jérome Perrin
gitlab-ce
Commits
f2674c7b
Commit
f2674c7b
authored
Mar 29, 2016
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor uploads rewriter used when moving issue
parent
0b8cefd3
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
27 additions
and
39 deletions
+27
-39
app/services/issues/move_service.rb
app/services/issues/move_service.rb
+6
-17
lib/gitlab/gfm/reference_rewriter.rb
lib/gitlab/gfm/reference_rewriter.rb
+6
-3
lib/gitlab/gfm/uploads_rewriter.rb
lib/gitlab/gfm/uploads_rewriter.rb
+11
-10
spec/lib/gitlab/gfm/uploads_rewriter_spec.rb
spec/lib/gitlab/gfm/uploads_rewriter_spec.rb
+2
-2
spec/services/issues/move_service_spec.rb
spec/services/issues/move_service_spec.rb
+2
-7
No files found.
app/services/issues/move_service.rb
View file @
f2674c7b
...
...
@@ -62,26 +62,15 @@ module Issues
end
def
rewrite_content
(
content
)
rewrite_uploads
(
unfold_references
(
content
)
)
end
def
unfold_references
(
content
)
return
unless
content
rewriter
=
Gitlab
::
Gfm
::
ReferenceRewriter
.
new
(
content
,
@old_project
,
@current_user
)
rewriter
.
rewrite
(
@new_project
)
end
def
rewrite_uploads
(
content
)
return
unless
content
rewriters
=
[
Gitlab
::
Gfm
::
ReferenceRewriter
,
Gitlab
::
Gfm
::
UploadsRewriter
]
rewriter
=
Gitlab
::
Gfm
::
UploadsRewriter
.
new
(
content
,
@old_project
,
@current_user
)
return
content
unless
rewriter
.
has_uploads?
rewriter
.
rewrite
(
@new_project
)
rewriter
s
.
inject
(
content
)
do
|
text
,
klass
|
rewriter
=
klass
.
new
(
text
,
@old_project
,
@current_user
)
rewriter
.
needs_rewrite?
?
rewriter
.
rewrite
(
@new_project
)
:
text
end
end
def
close_issue
...
...
lib/gitlab/gfm/reference_rewriter.rb
View file @
f2674c7b
...
...
@@ -34,16 +34,19 @@ module Gitlab
@source_project
=
source_project
@current_user
=
current_user
@original_html
=
markdown
(
text
)
@pattern
=
Gitlab
::
ReferenceExtractor
.
references_pattern
end
def
rewrite
(
target_project
)
pattern
=
Gitlab
::
ReferenceExtractor
.
references_pattern
@text
.
gsub
(
pattern
)
do
|
reference
|
@text
.
gsub
(
@pattern
)
do
|
reference
|
unfold_reference
(
reference
,
Regexp
.
last_match
,
target_project
)
end
end
def
needs_rewrite?
!
(
@text
=~
@pattern
).
nil?
end
private
def
unfold_reference
(
reference
,
match
,
target_project
)
...
...
lib/gitlab/gfm/uploads_rewriter.rb
View file @
f2674c7b
...
...
@@ -3,8 +3,9 @@ module Gitlab
##
# Class that rewrites markdown links for uploads
#
# Using a pattern defined in `FileUploader` copies files to a new project
# and rewrites all links to uploads in ain a given text.
# Using a pattern defined in `FileUploader` it copies files to a new
# project and rewrites all links to uploads in in a given text.
#
#
class
UploadsRewriter
def
initialize
(
text
,
source_project
,
_current_user
)
...
...
@@ -17,17 +18,17 @@ module Gitlab
return
unless
@text
new_uploader
=
file_uploader
(
target_project
)
@text
.
gsub
(
@pattern
)
do
|
markdown
_link
|
old_
file
=
find_file
(
@source_project
,
$~
[
:secret
],
$~
[
:file
])
return
markdown
_link
unless
old_file
.
exists?
@text
.
gsub
(
@pattern
)
do
|
markdown
|
file
=
find_file
(
@source_project
,
$~
[
:secret
],
$~
[
:file
])
return
markdown
unless
file
.
try
(
:exists?
)
new_uploader
.
store!
(
old_
file
)
new_uploader
.
store!
(
file
)
new_uploader
.
to_h
[
:markdown
]
end
end
def
has_uploads
?
!
(
@text
=~
@pattern
).
nil
?
def
needs_rewrite
?
files
.
any
?
end
def
files
...
...
@@ -46,8 +47,8 @@ module Gitlab
uploader
.
file
end
def
file_uploader
(
*
args
)
uploader
=
FileUploader
.
new
(
*
args
)
def
file_uploader
(
project
,
secret
=
nil
)
uploader
=
FileUploader
.
new
(
project
,
secret
)
uploader
.
define_singleton_method
(
:move_to_store
)
{
false
}
uploader
end
...
...
spec/lib/gitlab/gfm/uploads_rewriter_spec.rb
View file @
f2674c7b
...
...
@@ -33,8 +33,8 @@ describe Gitlab::Gfm::UploadsRewriter do
end
end
describe
'#
has_uploads
?'
do
subject
{
rewriter
.
has_uploads
?
}
describe
'#
needs_rewrite
?'
do
subject
{
rewriter
.
needs_rewrite
?
}
it
{
is_expected
.
to
eq
true
}
end
...
...
spec/services/issues/move_service_spec.rb
View file @
f2674c7b
...
...
@@ -162,12 +162,9 @@ describe Issues::MoveService, services: true do
end
context
'issue description with uploads'
do
let
(
:path
)
{
Rails
.
root
+
'spec/fixtures/rails_sample.jpg'
}
let
(
:file
)
{
fixture_file_upload
(
path
,
'image/jpg'
)
}
let
(
:uploader
)
{
FileUploader
.
new
(
old_project
)
}
let!
(
:store
)
{
uploader
.
store!
(
file
)
}
let
(
:uploader
)
{
build
(
:file_uploader
,
project:
old_project
)
}
let
(
:markdown
)
{
uploader
.
to_h
[
:markdown
]
}
let
(
:description
)
{
"Text and
#{
markdown
}
"
}
let
(
:description
)
{
"Text and
#{
markdown
}
"
}
include_context
'issue move executed'
...
...
@@ -176,8 +173,6 @@ describe Issues::MoveService, services: true do
expect
(
new_issue
.
description
)
.
to
match
(
/Text and
#{
FileUploader
::
MARKDOWN_PATTERN
}
/
)
end
after
{
uploader
.
remove!
}
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