BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
fd8394fa
Commit
fd8394fa
authored
Mar 07, 2016
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move reference unfolder for GFM to separate class
parent
cd0f1945
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
56 additions
and
92 deletions
+56
-92
move_service.rb
app/services/issues/move_service.rb
+2
-5
reference_unfold_filter.rb
lib/banzai/filter/reference_unfold_filter.rb
+0
-42
reference_unfold_pipeline.rb
lib/banzai/pipeline/reference_unfold_pipeline.rb
+0
-21
reference_unfolder.rb
lib/gitlab/gfm/reference_unfolder.rb
+31
-0
reference_extractor.rb
lib/gitlab/reference_extractor.rb
+7
-1
reference_unfolder_spec.rb
spec/lib/gitlab/gfm/reference_unfolder_spec.rb
+4
-23
reference_extractor_spec.rb
spec/lib/gitlab/reference_extractor_spec.rb
+12
-0
No files found.
app/services/issues/move_service.rb
View file @
fd8394fa
...
@@ -83,11 +83,8 @@ module Issues
...
@@ -83,11 +83,8 @@ module Issues
def
rewrite_references
(
noteable
)
def
rewrite_references
(
noteable
)
content
=
noteable_content
(
noteable
).
dup
content
=
noteable_content
(
noteable
).
dup
context
=
{
pipeline: :reference_unfold
,
unfolder
=
Gitlab
::
Gfm
::
ReferenceUnfolder
.
new
(
content
,
@project_old
)
project:
@project_old
,
new_project:
@project_new
}
unfolder
.
unfold
(
@project_new
)
new_content
=
Banzai
.
render_result
(
content
,
context
)
new_content
[
:output
].
to_s
end
end
def
noteable_content
(
noteable
)
def
noteable_content
(
noteable
)
...
...
lib/banzai/filter/reference_unfold_filter.rb
deleted
100644 → 0
View file @
cd0f1945
require
'html/pipeline/filter'
module
Banzai
module
Filter
##
# Filter than unfolds local references.
#
#
class
ReferenceUnfoldFilter
<
HTML
::
Pipeline
::
Filter
def
initialize
(
*
)
super
unless
result
[
:references
].
is_a?
(
Hash
)
raise
StandardError
,
'References not processed!'
end
@text
=
context
[
:text
].
dup
@new_project
=
context
[
:new_project
]
@referables
=
result
[
:references
].
values
.
flatten
end
def
call
@referables
.
each
do
|
referable
|
next
unless
referable
.
respond_to?
(
:to_reference
)
pattern
=
/
#{
Regexp
.
escape
(
referable
.
to_reference
)
}
/
@text
.
gsub!
(
pattern
,
referable
.
to_reference
(
@new_project
))
end
@text
end
private
def
validate
needs
:project
needs
:new_project
needs
:text
end
end
end
end
lib/banzai/pipeline/reference_unfold_pipeline.rb
deleted
100644 → 0
View file @
cd0f1945
module
Banzai
module
Pipeline
class
ReferenceUnfoldPipeline
<
BasePipeline
def
self
.
filters
FullPipeline
.
filters
+
[
Filter
::
ReferenceGathererFilter
,
Filter
::
ReferenceUnfoldFilter
]
end
def
self
.
call
(
text
,
context
=
{})
context
=
context
.
merge
(
text:
text
)
super
end
class
<<
self
alias_method
:to_document
,
:call
alias_method
:to_html
,
:call
end
end
end
end
lib/gitlab/gfm/reference_unfolder.rb
0 → 100644
View file @
fd8394fa
module
Gitlab
module
Gfm
##
# Class than unfolds local references in text.
#
#
class
ReferenceUnfolder
def
initialize
(
text
,
project
)
@text
=
text
@project
=
project
end
def
unfold
(
from_project
)
referables
.
each_with_object
(
@text
.
dup
)
do
|
referable
,
text
|
next
unless
referable
.
respond_to?
(
:to_reference
)
pattern
=
/
#{
Regexp
.
escape
(
referable
.
to_reference
)
}
/
text
.
gsub!
(
pattern
,
referable
.
to_reference
(
from_project
))
end
end
private
def
referables
extractor
=
Gitlab
::
ReferenceExtractor
.
new
(
@project
)
extractor
.
analyze
(
@text
)
extractor
.
all
end
end
end
end
lib/gitlab/reference_extractor.rb
View file @
fd8394fa
module
Gitlab
module
Gitlab
# Extract possible GFM references from an arbitrary String for further processing.
# Extract possible GFM references from an arbitrary String for further processing.
class
ReferenceExtractor
<
Banzai
::
ReferenceExtractor
class
ReferenceExtractor
<
Banzai
::
ReferenceExtractor
REFERABLES
=
%i(user issue label milestone merge_request snippet commit commit_range)
attr_accessor
:project
,
:current_user
,
:author
attr_accessor
:project
,
:current_user
,
:author
def
initialize
(
project
,
current_user
=
nil
,
author
=
nil
)
def
initialize
(
project
,
current_user
=
nil
,
author
=
nil
)
...
@@ -17,7 +18,7 @@ module Gitlab
...
@@ -17,7 +18,7 @@ module Gitlab
super
(
text
,
context
.
merge
(
project:
project
))
super
(
text
,
context
.
merge
(
project:
project
))
end
end
%i(user label milestone merge_request snippet commit commit_range)
.
each
do
|
type
|
REFERABLES
.
each
do
|
type
|
define_method
(
"
#{
type
}
s"
)
do
define_method
(
"
#{
type
}
s"
)
do
@references
[
type
]
||=
references
(
type
,
reference_context
)
@references
[
type
]
||=
references
(
type
,
reference_context
)
end
end
...
@@ -31,6 +32,11 @@ module Gitlab
...
@@ -31,6 +32,11 @@ module Gitlab
end
end
end
end
def
all
REFERABLES
.
each
{
|
referable
|
send
(
referable
.
to_s
.
pluralize
)
}
@references
.
values
.
flatten
end
private
private
def
reference_context
def
reference_context
...
...
spec/lib/
banzai/pipeline/reference_unfold_pipeline
_spec.rb
→
spec/lib/
gitlab/gfm/reference_unfolder
_spec.rb
View file @
fd8394fa
require
'spec_helper'
require
'spec_helper'
describe
Banzai
::
Pipeline
::
ReferenceUnfoldPipeline
do
describe
Gitlab
::
Gfm
::
ReferenceUnfolder
do
let
(
:text
)
{
'some text'
}
let
(
:text
)
{
'some text'
}
let
(
:old_project
)
{
create
(
:project
)
}
let
(
:old_project
)
{
create
(
:project
)
}
let
(
:new_project
)
{
create
(
:project
)
}
let
(
:new_project
)
{
create
(
:project
)
}
let
(
:pipeline_context
)
do
{
project:
old_project
,
new_project:
new_project
}
end
let
(
:result
)
do
described_class
.
to_document
(
text
,
pipeline_context
)
end
context
'invalid initializers'
do
subject
{
->
{
result
}
}
context
'project context is missing'
do
let
(
:pipeline_context
)
{
{
new_project:
new_project
}
}
it
{
is_expected
.
to
raise_error
ArgumentError
,
/Missing context keys/
}
end
context
'new project context is missing'
do
describe
'#unfold'
do
let
(
:pipeline_context
)
{
{
project:
old_project
}
}
subject
{
described_class
.
new
(
text
,
old_project
).
unfold
(
new_project
)
}
it
{
is_expected
.
to
raise_error
ArgumentError
,
/Missing context keys/
}
end
end
context
'multiple issues and merge requests referenced'
do
context
'multiple issues and merge requests referenced'
do
subject
{
result
[
:output
]
}
let!
(
:issue_first
)
{
create
(
:issue
,
project:
old_project
)
}
let!
(
:issue_first
)
{
create
(
:issue
,
project:
old_project
)
}
let!
(
:issue_second
)
{
create
(
:issue
,
project:
old_project
)
}
let!
(
:issue_second
)
{
create
(
:issue
,
project:
old_project
)
}
let!
(
:merge_request
)
{
create
(
:merge_request
,
source_project:
old_project
)
}
let!
(
:merge_request
)
{
create
(
:merge_request
,
source_project:
old_project
)
}
...
@@ -59,4 +39,5 @@ describe Banzai::Pipeline::ReferenceUnfoldPipeline do
...
@@ -59,4 +39,5 @@ describe Banzai::Pipeline::ReferenceUnfoldPipeline do
it
{
is_expected
.
to
include
url
}
it
{
is_expected
.
to
include
url
}
end
end
end
end
end
end
end
spec/lib/gitlab/reference_extractor_spec.rb
View file @
fd8394fa
...
@@ -122,4 +122,16 @@ describe Gitlab::ReferenceExtractor, lib: true do
...
@@ -122,4 +122,16 @@ describe Gitlab::ReferenceExtractor, lib: true do
expect
(
extracted
).
to
match_array
([
issue
])
expect
(
extracted
).
to
match_array
([
issue
])
end
end
end
end
describe
'#all'
do
let
(
:issue
)
{
create
(
:issue
,
project:
project
)
}
let
(
:label
)
{
create
(
:label
,
project:
project
)
}
let
(
:text
)
{
"Ref.
#{
issue
.
to_reference
}
and
#{
label
.
to_reference
}
"
}
before
{
subject
.
analyze
(
text
)
}
it
'returns all referables'
do
expect
(
subject
.
all
).
to
match_array
([
issue
,
label
])
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