Commit 8f8a8ab3 authored by Robert Speicher's avatar Robert Speicher

Refactor ReferenceExtractor to use pipeline filters

parent a6defd15
...@@ -8,151 +8,68 @@ module Gitlab ...@@ -8,151 +8,68 @@ module Gitlab
@current_user = current_user @current_user = current_user
end end
def can?(user, action, subject)
Ability.abilities.allowed?(user, action, subject)
end
def analyze(text) def analyze(text)
text = text.dup @_text = text.dup
# Remove preformatted/code blocks so that references are not included
text.gsub!(/^```.*?^```/m, '')
text.gsub!(/[^`]`[^`]*?`[^`]/, '')
@references = Hash.new { |hash, type| hash[type] = [] }
parse_references(text)
end end
# Given a valid project, resolve the extracted identifiers of the requested type to
# model objects.
def users def users
references[:user].uniq.map do |project, identifier| result = pipeline_result(:user)
if identifier == "all" result[:references][:user].flatten.compact.uniq
project.team.members.flatten
elsif namespace = Namespace.find_by(path: identifier)
if namespace.is_a?(Group)
namespace.users if can?(current_user, :read_group, namespace)
else
namespace.owner
end
end
end.flatten.compact.uniq
end end
def labels def labels
references[:label].uniq.map do |project, identifier| result = pipeline_result(:label)
project.labels.where(id: identifier).first result[:references][:label].compact.uniq
end.compact.uniq
end end
def issues def issues
references[:issue].uniq.map do |project, identifier| # TODO (rspeicher): What about external issues?
if project.default_issues_tracker?
project.issues.where(iid: identifier).first result = pipeline_result(:issue)
end result[:references][:issue].compact.uniq
end.compact.uniq
end end
def merge_requests def merge_requests
references[:merge_request].uniq.map do |project, identifier| result = pipeline_result(:merge_request)
project.merge_requests.where(iid: identifier).first result[:references][:merge_request].compact.uniq
end.compact.uniq
end end
def snippets def snippets
references[:snippet].uniq.map do |project, identifier| result = pipeline_result(:snippet)
project.snippets.where(id: identifier).first result[:references][:snippet].compact.uniq
end.compact.uniq
end end
def commits def commits
references[:commit].uniq.map do |project, identifier| result = pipeline_result(:commit)
repo = project.repository result[:references][:commit].compact.uniq
repo.commit(identifier) if repo
end.compact.uniq
end end
def commit_ranges def commit_ranges
references[:commit_range].uniq.map do |project, identifier| result = pipeline_result(:commit_range)
repo = project.repository result[:references][:commit_range].compact.uniq
if repo
from_id, to_id = identifier.split(/\.{2,3}/, 2)
[repo.commit(from_id), repo.commit(to_id)]
end
end.compact.uniq
end end
private private
NAME_STR = Gitlab::Regex::NAMESPACE_REGEX_STR # Instantiate and call HTML::Pipeline with a single reference filter type,
PROJ_STR = "(?<project>#{NAME_STR}/#{NAME_STR})" # returning the result
#
REFERENCE_PATTERN = %r{ # filter_type - Symbol reference type (e.g., :commit, :issue, etc.)
(?<prefix>\W)? # Prefix #
( # Reference # Returns the results Hash
@(?<user>#{NAME_STR}) # User name def pipeline_result(filter_type)
|~(?<label>\d+) # Label ID klass = filter_type.to_s.camelize + 'ReferenceFilter'
|(?<issue>([A-Z\-]+-)\d+) # JIRA Issue ID filter = "Gitlab::Markdown::#{klass}".constantize
|#{PROJ_STR}?\#(?<issue>([a-zA-Z\-]+-)?\d+) # Issue ID
|#{PROJ_STR}?!(?<merge_request>\d+) # MR ID context = {
|\$(?<snippet>\d+) # Snippet ID project: project,
|(#{PROJ_STR}@)?(?<commit_range>[\h]{6,40}\.{2,3}[\h]{6,40}) # Commit range current_user: current_user,
|(#{PROJ_STR}@)?(?<commit>[\h]{6,40}) # Commit ID # We don't actually care about the links generated
) only_path: true
(?<suffix>\W)? # Suffix }
}x.freeze
pipeline = HTML::Pipeline.new([filter], context)
TYPES = %i(user issue label merge_request snippet commit commit_range).freeze pipeline.call(@_text)
def parse_references(text, project = @project)
# parse reference links
text.gsub!(REFERENCE_PATTERN) do |match|
type = TYPES.detect { |t| $~[t].present? }
actual_project = project
project_prefix = nil
project_path = $LAST_MATCH_INFO[:project]
if project_path
actual_project = ::Project.find_with_namespace(project_path)
actual_project = nil unless can?(current_user, :read_project, actual_project)
project_prefix = project_path
end
parse_result($LAST_MATCH_INFO, type,
actual_project, project_prefix) || match
end
end
# Called from #parse_references. Attempts to build a gitlab reference
# link. Returns nil if +type+ is nil, if the match string is an HTML
# entity, if the reference is invalid, or if the matched text includes an
# invalid project path.
def parse_result(match_info, type, project, project_prefix)
prefix = match_info[:prefix]
suffix = match_info[:suffix]
return nil if html_entity?(prefix, suffix) || type.nil?
return nil if project.nil? && !project_prefix.nil?
identifier = match_info[type]
ref_link = reference_link(type, identifier, project, project_prefix)
if ref_link
"#{prefix}#{ref_link}#{suffix}"
else
nil
end
end
# Return true if the +prefix+ and +suffix+ indicate that the matched string
# is an HTML entity like &amp;
def html_entity?(prefix, suffix)
prefix && suffix && prefix[0] == '&' && suffix[-1] == ';'
end
def reference_link(type, identifier, project, _)
references[type] << [project, identifier]
end end
end end
end end
...@@ -4,80 +4,6 @@ describe Gitlab::ReferenceExtractor do ...@@ -4,80 +4,6 @@ describe Gitlab::ReferenceExtractor do
let(:project) { create(:project) } let(:project) { create(:project) }
subject { Gitlab::ReferenceExtractor.new(project, project.creator) } subject { Gitlab::ReferenceExtractor.new(project, project.creator) }
it 'extracts username references' do
subject.analyze('this contains a @user reference')
expect(subject.references[:user]).to eq([[project, 'user']])
end
it 'extracts issue references' do
subject.analyze('this one talks about issue #1234')
expect(subject.references[:issue]).to eq([[project, '1234']])
end
it 'extracts JIRA issue references' do
subject.analyze('this one talks about issue JIRA-1234')
expect(subject.references[:issue]).to eq([[project, 'JIRA-1234']])
end
it 'extracts merge request references' do
subject.analyze("and here's !43, a merge request")
expect(subject.references[:merge_request]).to eq([[project, '43']])
end
it 'extracts snippet ids' do
subject.analyze('snippets like $12 get extracted as well')
expect(subject.references[:snippet]).to eq([[project, '12']])
end
it 'extracts commit shas' do
subject.analyze('commit shas 98cf0ae3 are pulled out as Strings')
expect(subject.references[:commit]).to eq([[project, '98cf0ae3']])
end
it 'extracts commit ranges' do
subject.analyze('here you go, a commit range: 98cf0ae3...98cf0ae4')
expect(subject.references[:commit_range]).to eq([[project, '98cf0ae3...98cf0ae4']])
end
it 'extracts multiple references and preserves their order' do
subject.analyze('@me and @you both care about this')
expect(subject.references[:user]).to eq([
[project, 'me'],
[project, 'you']
])
end
it 'leaves the original note unmodified' do
text = 'issue #123 is just the worst, @user'
subject.analyze(text)
expect(text).to eq('issue #123 is just the worst, @user')
end
it 'extracts no references for <pre>..</pre> blocks' do
subject.analyze("<pre>def puts '#1 issue'\nend\n</pre>```")
expect(subject.issues).to be_blank
end
it 'extracts no references for <code>..</code> blocks' do
subject.analyze("<code>def puts '!1 request'\nend\n</code>```")
expect(subject.merge_requests).to be_blank
end
it 'extracts no references for code blocks with language' do
subject.analyze("this code:\n```ruby\ndef puts '#1 issue'\nend\n```")
expect(subject.issues).to be_blank
end
it 'extracts issue references for invalid code blocks' do
subject.analyze('test: ```this one talks about issue #1234```')
expect(subject.references[:issue]).to eq([[project, '1234']])
end
it 'handles all possible kinds of references' do
accessors = described_class::TYPES.map { |t| "#{t}s".to_sym }
expect(subject).to respond_to(*accessors)
end
it 'accesses valid user objects' do it 'accesses valid user objects' do
@u_foo = create(:user, username: 'foo') @u_foo = create(:user, username: 'foo')
@u_bar = create(:user, username: 'bar') @u_bar = create(:user, username: 'bar')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment