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
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
...
@@ -62,26 +62,15 @@ module Issues
end
end
def
rewrite_content
(
content
)
def
rewrite_content
(
content
)
rewrite_uploads
(
unfold_references
(
content
)
)
end
def
unfold_references
(
content
)
return
unless
content
return
unless
content
rewriter
=
Gitlab
::
Gfm
::
ReferenceRewriter
.
new
(
content
,
@old_project
,
rewriters
=
[
Gitlab
::
Gfm
::
ReferenceRewriter
,
@current_user
)
Gitlab
::
Gfm
::
UploadsRewriter
]
rewriter
.
rewrite
(
@new_project
)
end
def
rewrite_uploads
(
content
)
return
unless
content
rewriter
=
Gitlab
::
Gfm
::
UploadsRewriter
.
new
(
content
,
@old_project
,
rewriter
s
.
inject
(
content
)
do
|
text
,
klass
|
@current_user
)
rewriter
=
klass
.
new
(
text
,
@old_project
,
@current_user
)
return
content
unless
rewriter
.
has_uploads?
rewriter
.
needs_rewrite?
?
rewriter
.
rewrite
(
@new_project
)
:
text
rewriter
.
rewrite
(
@new_project
)
end
end
end
def
close_issue
def
close_issue
...
...
lib/gitlab/gfm/reference_rewriter.rb
View file @
f2674c7b
...
@@ -34,16 +34,19 @@ module Gitlab
...
@@ -34,16 +34,19 @@ module Gitlab
@source_project
=
source_project
@source_project
=
source_project
@current_user
=
current_user
@current_user
=
current_user
@original_html
=
markdown
(
text
)
@original_html
=
markdown
(
text
)
@pattern
=
Gitlab
::
ReferenceExtractor
.
references_pattern
end
end
def
rewrite
(
target_project
)
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
)
unfold_reference
(
reference
,
Regexp
.
last_match
,
target_project
)
end
end
end
end
def
needs_rewrite?
!
(
@text
=~
@pattern
).
nil?
end
private
private
def
unfold_reference
(
reference
,
match
,
target_project
)
def
unfold_reference
(
reference
,
match
,
target_project
)
...
...
lib/gitlab/gfm/uploads_rewriter.rb
View file @
f2674c7b
...
@@ -3,8 +3,9 @@ module Gitlab
...
@@ -3,8 +3,9 @@ module Gitlab
##
##
# Class that rewrites markdown links for uploads
# Class that rewrites markdown links for uploads
#
#
# Using a pattern defined in `FileUploader` copies files to a new project
# Using a pattern defined in `FileUploader` it copies files to a new
# and rewrites all links to uploads in ain a given text.
# project and rewrites all links to uploads in in a given text.
#
#
#
class
UploadsRewriter
class
UploadsRewriter
def
initialize
(
text
,
source_project
,
_current_user
)
def
initialize
(
text
,
source_project
,
_current_user
)
...
@@ -17,17 +18,17 @@ module Gitlab
...
@@ -17,17 +18,17 @@ module Gitlab
return
unless
@text
return
unless
@text
new_uploader
=
file_uploader
(
target_project
)
new_uploader
=
file_uploader
(
target_project
)
@text
.
gsub
(
@pattern
)
do
|
markdown
_link
|
@text
.
gsub
(
@pattern
)
do
|
markdown
|
old_
file
=
find_file
(
@source_project
,
$~
[
:secret
],
$~
[
:file
])
file
=
find_file
(
@source_project
,
$~
[
:secret
],
$~
[
:file
])
return
markdown
_link
unless
old_file
.
exists?
return
markdown
unless
file
.
try
(
:exists?
)
new_uploader
.
store!
(
old_
file
)
new_uploader
.
store!
(
file
)
new_uploader
.
to_h
[
:markdown
]
new_uploader
.
to_h
[
:markdown
]
end
end
end
end
def
has_uploads
?
def
needs_rewrite
?
!
(
@text
=~
@pattern
).
nil
?
files
.
any
?
end
end
def
files
def
files
...
@@ -46,8 +47,8 @@ module Gitlab
...
@@ -46,8 +47,8 @@ module Gitlab
uploader
.
file
uploader
.
file
end
end
def
file_uploader
(
*
args
)
def
file_uploader
(
project
,
secret
=
nil
)
uploader
=
FileUploader
.
new
(
*
args
)
uploader
=
FileUploader
.
new
(
project
,
secret
)
uploader
.
define_singleton_method
(
:move_to_store
)
{
false
}
uploader
.
define_singleton_method
(
:move_to_store
)
{
false
}
uploader
uploader
end
end
...
...
spec/lib/gitlab/gfm/uploads_rewriter_spec.rb
View file @
f2674c7b
...
@@ -33,8 +33,8 @@ describe Gitlab::Gfm::UploadsRewriter do
...
@@ -33,8 +33,8 @@ describe Gitlab::Gfm::UploadsRewriter do
end
end
end
end
describe
'#
has_uploads
?'
do
describe
'#
needs_rewrite
?'
do
subject
{
rewriter
.
has_uploads
?
}
subject
{
rewriter
.
needs_rewrite
?
}
it
{
is_expected
.
to
eq
true
}
it
{
is_expected
.
to
eq
true
}
end
end
...
...
spec/services/issues/move_service_spec.rb
View file @
f2674c7b
...
@@ -162,12 +162,9 @@ describe Issues::MoveService, services: true do
...
@@ -162,12 +162,9 @@ describe Issues::MoveService, services: true do
end
end
context
'issue description with uploads'
do
context
'issue description with uploads'
do
let
(
:path
)
{
Rails
.
root
+
'spec/fixtures/rails_sample.jpg'
}
let
(
:uploader
)
{
build
(
:file_uploader
,
project:
old_project
)
}
let
(
:file
)
{
fixture_file_upload
(
path
,
'image/jpg'
)
}
let
(
:uploader
)
{
FileUploader
.
new
(
old_project
)
}
let!
(
:store
)
{
uploader
.
store!
(
file
)
}
let
(
:markdown
)
{
uploader
.
to_h
[
:markdown
]
}
let
(
:markdown
)
{
uploader
.
to_h
[
:markdown
]
}
let
(
:description
)
{
"Text and
#{
markdown
}
"
}
let
(
:description
)
{
"Text and
#{
markdown
}
"
}
include_context
'issue move executed'
include_context
'issue move executed'
...
@@ -176,8 +173,6 @@ describe Issues::MoveService, services: true do
...
@@ -176,8 +173,6 @@ describe Issues::MoveService, services: true do
expect
(
new_issue
.
description
)
expect
(
new_issue
.
description
)
.
to
match
(
/Text and
#{
FileUploader
::
MARKDOWN_PATTERN
}
/
)
.
to
match
(
/Text and
#{
FileUploader
::
MARKDOWN_PATTERN
}
/
)
end
end
after
{
uploader
.
remove!
}
end
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