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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
b1ef1aa5
Commit
b1ef1aa5
authored
Mar 27, 2015
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Slightly refactor ReferenceExtractor.
parent
254698d6
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
43 deletions
+41
-43
app/models/concerns/mentionable.rb
app/models/concerns/mentionable.rb
+3
-5
lib/gitlab/closing_issue_extractor.rb
lib/gitlab/closing_issue_extractor.rb
+3
-3
lib/gitlab/reference_extractor.rb
lib/gitlab/reference_extractor.rb
+35
-35
No files found.
app/models/concerns/mentionable.rb
View file @
b1ef1aa5
...
@@ -65,12 +65,10 @@ module Mentionable
...
@@ -65,12 +65,10 @@ module Mentionable
# Extract GFM references to other Mentionables from this Mentionable. Always excludes its #local_reference.
# Extract GFM references to other Mentionables from this Mentionable. Always excludes its #local_reference.
def
references
(
p
=
project
,
text
=
mentionable_text
)
def
references
(
p
=
project
,
text
=
mentionable_text
)
return
[]
if
text
.
blank?
return
[]
if
text
.
blank?
ext
=
Gitlab
::
ReferenceExtractor
.
new
ext
=
Gitlab
::
ReferenceExtractor
.
new
(
p
)
ext
.
analyze
(
text
,
p
)
ext
.
analyze
(
text
)
(
ext
.
issues_for
(
p
)
+
(
ext
.
issues
+
ext
.
merge_requests
+
ext
.
commits
).
uniq
-
[
local_reference
]
ext
.
merge_requests_for
(
p
)
+
ext
.
commits_for
(
p
)).
uniq
-
[
local_reference
]
end
end
# Create a cross-reference Note for each GFM reference to another Mentionable found in +mentionable_text+.
# Create a cross-reference Note for each GFM reference to another Mentionable found in +mentionable_text+.
...
...
lib/gitlab/closing_issue_extractor.rb
View file @
b1ef1aa5
...
@@ -9,9 +9,9 @@ module Gitlab
...
@@ -9,9 +9,9 @@ module Gitlab
md
=
message
.
scan
(
ISSUE_CLOSING_REGEX
)
md
=
message
.
scan
(
ISSUE_CLOSING_REGEX
)
md
.
each
do
|
ref
|
md
.
each
do
|
ref
|
extractor
=
Gitlab
::
ReferenceExtractor
.
new
extractor
=
Gitlab
::
ReferenceExtractor
.
new
(
project
)
extractor
.
analyze
(
ref
[
0
]
,
project
)
extractor
.
analyze
(
ref
[
0
])
issues
+=
extractor
.
issues
_for
(
project
)
issues
+=
extractor
.
issues
end
end
end
end
...
...
lib/gitlab/reference_extractor.rb
View file @
b1ef1aa5
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
class
ReferenceExtractor
attr_accessor
:
users
,
:labels
,
:issues
,
:merge_requests
,
:snippets
,
:commits
,
:commit_rang
es
attr_accessor
:
project
,
:referenc
es
include
Markdown
include
Markdown
def
initialize
def
initialize
(
project
)
@users
,
@labels
,
@issues
,
@merge_requests
,
@snippets
,
@commits
,
@commit_ranges
=
@project
=
project
[],
[],
[],
[],
[],
[],
[]
@references
=
Hash
.
new
{
[]
}
end
end
def
analyze
(
string
,
projec
t
)
def
analyze
(
tex
t
)
text
=
string
.
dup
text
=
text
.
dup
# Remove preformatted/code blocks so that references are not included
# Remove preformatted/code blocks so that references are not included
text
.
gsub!
(
%r{<pre>.*?</pre>|<code>.*?</code>}m
)
{
|
match
|
''
}
text
.
gsub!
(
%r{<pre>.*?</pre>|<code>.*?</code>}m
)
{
|
match
|
''
}
text
.
gsub!
(
%r{^```.*?^```}m
)
{
|
match
|
''
}
text
.
gsub!
(
%r{^```.*?^```}m
)
{
|
match
|
''
}
parse_references
(
text
,
project
)
parse_references
(
text
)
end
end
# Given a valid project, resolve the extracted identifiers of the requested type to
# Given a valid project, resolve the extracted identifiers of the requested type to
# model objects.
# model objects.
def
users
_for
(
project
)
def
users
users
.
map
do
|
entry
|
references
[
:users
]
.
map
do
|
entry
|
project
.
users
.
where
(
username:
entry
[
:id
]).
first
project
.
users
.
where
(
username:
entry
[
:id
]).
first
end
.
reject
(
&
:nil?
)
end
.
compact
end
end
def
labels
_for
(
project
=
nil
)
def
labels
labels
.
map
do
|
entry
|
references
[
:labels
]
.
map
do
|
entry
|
project
.
labels
.
where
(
id:
entry
[
:id
]).
first
project
.
labels
.
where
(
id:
entry
[
:id
]).
first
end
.
reject
(
&
:nil?
)
end
.
compact
end
end
def
issues
_for
(
project
=
nil
)
def
issues
issues
.
map
do
|
entry
|
references
[
:issues
]
.
map
do
|
entry
|
if
should_lookup?
(
project
,
entry
[
:project
])
if
should_lookup?
(
entry
[
:project
])
entry
[
:project
].
issues
.
where
(
iid:
entry
[
:id
]).
first
entry
[
:project
].
issues
.
where
(
iid:
entry
[
:id
]).
first
end
end
end
.
reject
(
&
:nil?
)
end
.
compact
end
end
def
merge_requests
_for
(
project
=
nil
)
def
merge_requests
merge_requests
.
map
do
|
entry
|
references
[
:merge_requests
]
.
map
do
|
entry
|
if
should_lookup?
(
project
,
entry
[
:project
])
if
should_lookup?
(
entry
[
:project
])
entry
[
:project
].
merge_requests
.
where
(
iid:
entry
[
:id
]).
first
entry
[
:project
].
merge_requests
.
where
(
iid:
entry
[
:id
]).
first
end
end
end
.
reject
(
&
:nil?
)
end
.
compact
end
end
def
snippets
_for
(
project
)
def
snippets
snippets
.
map
do
|
entry
|
references
[
:snippets
]
.
map
do
|
entry
|
project
.
snippets
.
where
(
id:
entry
[
:id
]).
first
project
.
snippets
.
where
(
id:
entry
[
:id
]).
first
end
.
reject
(
&
:nil?
)
end
.
compact
end
end
def
commits
_for
(
project
=
nil
)
def
commits
commits
.
map
do
|
entry
|
references
[
:commits
]
.
map
do
|
entry
|
repo
=
entry
[
:project
].
repository
if
entry
[
:project
]
repo
=
entry
[
:project
].
repository
if
entry
[
:project
]
if
should_lookup?
(
project
,
entry
[
:project
])
if
should_lookup?
(
entry
[
:project
])
repo
.
commit
(
entry
[
:id
])
if
repo
repo
.
commit
(
entry
[
:id
])
if
repo
end
end
end
.
reject
(
&
:nil?
)
end
.
compact
end
end
def
commit_ranges
_for
(
project
=
nil
)
def
commit_ranges
commit_ranges
.
map
do
|
entry
|
references
[
:commit_ranges
]
.
map
do
|
entry
|
repo
=
entry
[
:project
].
repository
if
entry
[
:project
]
repo
=
entry
[
:project
].
repository
if
entry
[
:project
]
if
repo
&&
should_lookup?
(
project
,
entry
[
:project
])
if
repo
&&
should_lookup?
(
entry
[
:project
])
from_id
,
to_id
=
entry
[
:id
].
split
(
/\.{2,3}/
,
2
)
from_id
,
to_id
=
entry
[
:id
].
split
(
/\.{2,3}/
,
2
)
[
repo
.
commit
(
from_id
),
repo
.
commit
(
to_id
)]
[
repo
.
commit
(
from_id
),
repo
.
commit
(
to_id
)]
end
end
end
.
reject
(
&
:nil?
)
end
.
compact
end
end
private
private
def
reference_link
(
type
,
identifier
,
project
,
_
)
def
reference_link
(
type
,
identifier
,
project
,
_
)
# Append identifier to the appropriate collection.
references
[
type
]
<<
{
project:
project
,
id:
identifier
}
send
(
"
#{
type
}
s"
)
<<
{
project:
project
,
id:
identifier
}
end
end
def
should_lookup?
(
project
,
entry_project
)
def
should_lookup?
(
entry_project
)
if
entry_project
.
nil?
if
entry_project
.
nil?
false
false
else
else
...
...
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