2013-05-30 19:16:49 -04:00
|
|
|
module Gitlab
|
|
|
|
# Extract possible GFM references from an arbitrary String for further processing.
|
|
|
|
class ReferenceExtractor
|
2015-03-27 06:37:45 -04:00
|
|
|
attr_accessor :project, :references
|
2013-05-30 19:16:49 -04:00
|
|
|
|
|
|
|
include Markdown
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
|
|
|
|
|
|
|
@references = Hash.new { [] }
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def analyze(text)
|
|
|
|
text = text.dup
|
2015-03-03 14:06:17 -05:00
|
|
|
|
|
|
|
# Remove preformatted/code blocks so that references are not included
|
|
|
|
text.gsub!(%r{<pre>.*?</pre>|<code>.*?</code>}m) { |match| '' }
|
|
|
|
text.gsub!(%r{^```.*?^```}m) { |match| '' }
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
parse_references(text)
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Given a valid project, resolve the extracted identifiers of the requested type to
|
|
|
|
# model objects.
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def users
|
|
|
|
references[:users].map do |entry|
|
2014-10-02 14:26:39 -04:00
|
|
|
project.users.where(username: entry[:id]).first
|
2015-03-27 06:37:45 -04:00
|
|
|
end.compact
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def labels
|
|
|
|
references[:labels].map do |entry|
|
2015-02-07 06:14:55 -05:00
|
|
|
project.labels.where(id: entry[:id]).first
|
2015-03-27 06:37:45 -04:00
|
|
|
end.compact
|
2015-02-07 06:14:55 -05:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def issues
|
|
|
|
references[:issues].map do |entry|
|
|
|
|
if should_lookup?(entry[:project])
|
2014-10-02 14:26:39 -04:00
|
|
|
entry[:project].issues.where(iid: entry[:id]).first
|
|
|
|
end
|
2015-03-27 06:37:45 -04:00
|
|
|
end.compact
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def merge_requests
|
|
|
|
references[:merge_requests].map do |entry|
|
|
|
|
if should_lookup?(entry[:project])
|
2014-10-02 14:26:39 -04:00
|
|
|
entry[:project].merge_requests.where(iid: entry[:id]).first
|
|
|
|
end
|
2015-03-27 06:37:45 -04:00
|
|
|
end.compact
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def snippets
|
|
|
|
references[:snippets].map do |entry|
|
2014-10-02 14:26:39 -04:00
|
|
|
project.snippets.where(id: entry[:id]).first
|
2015-03-27 06:37:45 -04:00
|
|
|
end.compact
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def commits
|
|
|
|
references[:commits].map do |entry|
|
2014-10-02 14:26:39 -04:00
|
|
|
repo = entry[:project].repository if entry[:project]
|
2015-03-27 06:37:45 -04:00
|
|
|
if should_lookup?(entry[:project])
|
2014-10-02 14:26:39 -04:00
|
|
|
repo.commit(entry[:id]) if repo
|
|
|
|
end
|
2015-03-27 06:37:45 -04:00
|
|
|
end.compact
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def commit_ranges
|
|
|
|
references[:commit_ranges].map do |entry|
|
2015-03-06 17:08:28 -05:00
|
|
|
repo = entry[:project].repository if entry[:project]
|
2015-03-27 06:37:45 -04:00
|
|
|
if repo && should_lookup?(entry[:project])
|
2015-03-06 17:08:28 -05:00
|
|
|
from_id, to_id = entry[:id].split(/\.{2,3}/, 2)
|
|
|
|
[repo.commit(from_id), repo.commit(to_id)]
|
|
|
|
end
|
2015-03-27 06:37:45 -04:00
|
|
|
end.compact
|
2015-03-06 17:08:28 -05:00
|
|
|
end
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
private
|
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
def reference_link(type, identifier, project, _)
|
2015-03-27 06:37:45 -04:00
|
|
|
references[type] << { project: project, id: identifier }
|
2014-10-02 14:26:39 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def should_lookup?(entry_project)
|
2014-10-02 14:26:39 -04:00
|
|
|
if entry_project.nil?
|
|
|
|
false
|
|
|
|
else
|
2015-02-02 18:11:19 -05:00
|
|
|
project.nil? || entry_project.default_issues_tracker?
|
2014-10-02 14:26:39 -04:00
|
|
|
end
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|