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-06 17:08:28 -05:00
|
|
|
attr_accessor :users, :labels, :issues, :merge_requests, :snippets, :commits, :commit_ranges
|
2013-05-30 19:16:49 -04:00
|
|
|
|
|
|
|
include Markdown
|
|
|
|
|
|
|
|
def initialize
|
2015-03-06 17:08:28 -05:00
|
|
|
@users, @labels, @issues, @merge_requests, @snippets, @commits, @commit_ranges =
|
|
|
|
[], [], [], [], [], [], []
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
def analyze(string, project)
|
|
|
|
parse_references(string.dup, project)
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Given a valid project, resolve the extracted identifiers of the requested type to
|
|
|
|
# model objects.
|
|
|
|
|
2014-09-25 18:07:40 -04:00
|
|
|
def users_for(project)
|
2014-10-02 14:26:39 -04:00
|
|
|
users.map do |entry|
|
|
|
|
project.users.where(username: entry[:id]).first
|
2013-05-30 19:16:49 -04:00
|
|
|
end.reject(&:nil?)
|
|
|
|
end
|
|
|
|
|
2015-02-07 06:14:55 -05:00
|
|
|
def labels_for(project = nil)
|
|
|
|
labels.map do |entry|
|
|
|
|
project.labels.where(id: entry[:id]).first
|
|
|
|
end.reject(&:nil?)
|
|
|
|
end
|
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
def issues_for(project = nil)
|
|
|
|
issues.map do |entry|
|
|
|
|
if should_lookup?(project, entry[:project])
|
|
|
|
entry[:project].issues.where(iid: entry[:id]).first
|
|
|
|
end
|
2013-05-30 19:16:49 -04:00
|
|
|
end.reject(&:nil?)
|
|
|
|
end
|
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
def merge_requests_for(project = nil)
|
|
|
|
merge_requests.map do |entry|
|
|
|
|
if should_lookup?(project, entry[:project])
|
|
|
|
entry[:project].merge_requests.where(iid: entry[:id]).first
|
|
|
|
end
|
2013-05-30 19:16:49 -04:00
|
|
|
end.reject(&:nil?)
|
|
|
|
end
|
|
|
|
|
2014-09-25 18:07:40 -04:00
|
|
|
def snippets_for(project)
|
2014-10-02 14:26:39 -04:00
|
|
|
snippets.map do |entry|
|
|
|
|
project.snippets.where(id: entry[:id]).first
|
2013-05-30 19:16:49 -04:00
|
|
|
end.reject(&:nil?)
|
|
|
|
end
|
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
def commits_for(project = nil)
|
|
|
|
commits.map do |entry|
|
|
|
|
repo = entry[:project].repository if entry[:project]
|
|
|
|
if should_lookup?(project, entry[:project])
|
|
|
|
repo.commit(entry[:id]) if repo
|
|
|
|
end
|
2013-05-30 19:16:49 -04:00
|
|
|
end.reject(&:nil?)
|
|
|
|
end
|
|
|
|
|
2015-03-06 17:08:28 -05:00
|
|
|
def commit_ranges_for(project = nil)
|
|
|
|
commit_ranges.map do |entry|
|
|
|
|
repo = entry[:project].repository if entry[:project]
|
|
|
|
if repo && should_lookup?(project, entry[:project])
|
|
|
|
from_id, to_id = entry[:id].split(/\.{2,3}/, 2)
|
|
|
|
[repo.commit(from_id), repo.commit(to_id)]
|
|
|
|
end
|
|
|
|
end.reject(&:nil?)
|
|
|
|
end
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
private
|
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
def reference_link(type, identifier, project, _)
|
2013-05-30 19:16:49 -04:00
|
|
|
# Append identifier to the appropriate collection.
|
2014-10-02 14:26:39 -04:00
|
|
|
send("#{type}s") << { project: project, id: identifier }
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_lookup?(project, entry_project)
|
|
|
|
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
|