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:38:22 -04:00
|
|
|
attr_accessor :project, :current_user, :references
|
2013-05-30 19:16:49 -04:00
|
|
|
|
2015-03-27 06:38:22 -04:00
|
|
|
def initialize(project, current_user = nil)
|
2015-03-27 06:37:45 -04:00
|
|
|
@project = project
|
2015-03-27 07:58:23 -04:00
|
|
|
@current_user = current_user
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:38:22 -04:00
|
|
|
def can?(user, action, subject)
|
|
|
|
Ability.abilities.allowed?(user, action, subject)
|
|
|
|
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
|
2015-04-21 05:45:03 -04:00
|
|
|
text.gsub!(/^```.*?^```/m, '')
|
|
|
|
text.gsub!(/[^`]`[^`]*?`[^`]/, '')
|
2015-03-03 14:06:17 -05:00
|
|
|
|
2015-04-03 12:03:15 -04:00
|
|
|
@references = Hash.new { |hash, type| hash[type] = [] }
|
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
|
2015-04-03 12:03:15 -04:00
|
|
|
references[:user].uniq.map do |project, identifier|
|
|
|
|
if identifier == "all"
|
2015-03-27 06:45:06 -04:00
|
|
|
project.team.members.flatten
|
2015-04-03 12:03:15 -04:00
|
|
|
elsif namespace = Namespace.find_by(path: identifier)
|
2015-03-27 06:45:06 -04:00
|
|
|
if namespace.is_a?(Group)
|
2015-04-14 17:32:51 -04:00
|
|
|
namespace.users if can?(current_user, :read_group, namespace)
|
2015-03-27 06:45:06 -04:00
|
|
|
else
|
|
|
|
namespace.owner
|
|
|
|
end
|
|
|
|
end
|
2015-04-03 12:03:15 -04:00
|
|
|
end.flatten.compact.uniq
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def labels
|
2015-04-03 12:03:15 -04:00
|
|
|
references[:label].uniq.map do |project, identifier|
|
|
|
|
project.labels.where(id: identifier).first
|
|
|
|
end.compact.uniq
|
2015-02-07 06:14:55 -05:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def issues
|
2015-04-03 12:03:15 -04:00
|
|
|
references[:issue].uniq.map do |project, identifier|
|
|
|
|
if project.default_issues_tracker?
|
|
|
|
project.issues.where(iid: identifier).first
|
2014-10-02 14:26:39 -04:00
|
|
|
end
|
2015-04-03 12:03:15 -04:00
|
|
|
end.compact.uniq
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def merge_requests
|
2015-04-03 12:03:15 -04:00
|
|
|
references[:merge_request].uniq.map do |project, identifier|
|
|
|
|
project.merge_requests.where(iid: identifier).first
|
|
|
|
end.compact.uniq
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def snippets
|
2015-04-03 12:03:15 -04:00
|
|
|
references[:snippet].uniq.map do |project, identifier|
|
|
|
|
project.snippets.where(id: identifier).first
|
|
|
|
end.compact.uniq
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def commits
|
2015-04-03 12:03:15 -04:00
|
|
|
references[:commit].uniq.map do |project, identifier|
|
|
|
|
repo = project.repository
|
|
|
|
repo.commit(identifier) if repo
|
|
|
|
end.compact.uniq
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-03-27 06:37:45 -04:00
|
|
|
def commit_ranges
|
2015-04-03 12:03:15 -04:00
|
|
|
references[:commit_range].uniq.map do |project, identifier|
|
|
|
|
repo = project.repository
|
2015-03-27 07:58:23 -04:00
|
|
|
if repo
|
2015-04-03 12:03:15 -04:00
|
|
|
from_id, to_id = identifier.split(/\.{2,3}/, 2)
|
2015-03-06 17:08:28 -05:00
|
|
|
[repo.commit(from_id), repo.commit(to_id)]
|
|
|
|
end
|
2015-04-03 12:03:15 -04:00
|
|
|
end.compact.uniq
|
2015-03-06 17:08:28 -05:00
|
|
|
end
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
private
|
|
|
|
|
2015-04-13 15:45:30 -04:00
|
|
|
NAME_STR = Gitlab::Regex::NAMESPACE_REGEX_STR
|
|
|
|
PROJ_STR = "(?<project>#{NAME_STR}/#{NAME_STR})"
|
|
|
|
|
|
|
|
REFERENCE_PATTERN = %r{
|
|
|
|
(?<prefix>\W)? # Prefix
|
|
|
|
( # Reference
|
|
|
|
@(?<user>#{NAME_STR}) # User name
|
|
|
|
|~(?<label>\d+) # Label ID
|
|
|
|
|(?<issue>([A-Z\-]+-)\d+) # JIRA Issue ID
|
|
|
|
|#{PROJ_STR}?\#(?<issue>([a-zA-Z\-]+-)?\d+) # Issue ID
|
|
|
|
|#{PROJ_STR}?!(?<merge_request>\d+) # MR ID
|
|
|
|
|\$(?<snippet>\d+) # Snippet ID
|
|
|
|
|(#{PROJ_STR}@)?(?<commit_range>[\h]{6,40}\.{2,3}[\h]{6,40}) # Commit range
|
|
|
|
|(#{PROJ_STR}@)?(?<commit>[\h]{6,40}) # Commit ID
|
|
|
|
)
|
|
|
|
(?<suffix>\W)? # Suffix
|
|
|
|
}x.freeze
|
|
|
|
|
|
|
|
TYPES = %i(user issue label merge_request snippet commit commit_range).freeze
|
|
|
|
|
|
|
|
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 &
|
|
|
|
def html_entity?(prefix, suffix)
|
|
|
|
prefix && suffix && prefix[0] == '&' && suffix[-1] == ';'
|
|
|
|
end
|
|
|
|
|
2014-10-02 14:26:39 -04:00
|
|
|
def reference_link(type, identifier, project, _)
|
2015-04-03 12:03:15 -04:00
|
|
|
references[type] << [project, identifier]
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|