From b492f0f86ea0f0b4e954e1b7ed0b84d08d784272 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 3 Apr 2015 18:03:15 +0200 Subject: Refactor ReferenceExtractor. --- lib/gitlab/reference_extractor.rb | 57 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/lib/gitlab/reference_extractor.rb b/lib/gitlab/reference_extractor.rb index 2f38c1dcc89..1a9a346d16d 100644 --- a/lib/gitlab/reference_extractor.rb +++ b/lib/gitlab/reference_extractor.rb @@ -8,8 +8,6 @@ module Gitlab def initialize(project, current_user = nil) @project = project @current_user = current_user - - @references = Hash.new { [] } end def can?(user, action, subject) @@ -23,6 +21,7 @@ module Gitlab text.gsub!(%r{
.*?
|.*?}m) { |match| '' } text.gsub!(%r{^```.*?^```}m) { |match| '' } + @references = Hash.new { |hash, type| hash[type] = [] } parse_references(text) end @@ -30,66 +29,66 @@ module Gitlab # model objects. def users - references[:users].map do |entry| - if entry[:id] == "all" + references[:user].uniq.map do |project, identifier| + if identifier == "all" project.team.members.flatten - elsif namespace = Namespace.find_by(path: entry[:id]) + elsif namespace = Namespace.find_by(path: identifier) if namespace.is_a?(Group) namespace.users else namespace.owner end end - end.flatten.compact + end.flatten.compact.uniq end def labels - references[:labels].map do |entry| - project.labels.where(id: entry[:id]).first - end.compact + references[:label].uniq.map do |project, identifier| + project.labels.where(id: identifier).first + end.compact.uniq end def issues - references[:issues].map do |entry| - if entry[:project].default_issues_tracker? - entry[:project].issues.where(iid: entry[:id]).first + references[:issue].uniq.map do |project, identifier| + if project.default_issues_tracker? + project.issues.where(iid: identifier).first end - end.compact + end.compact.uniq end def merge_requests - references[:merge_requests].map do |entry| - entry[:project].merge_requests.where(iid: entry[:id]).first - end.compact + references[:merge_request].uniq.map do |project, identifier| + project.merge_requests.where(iid: identifier).first + end.compact.uniq end def snippets - references[:snippets].map do |entry| - project.snippets.where(id: entry[:id]).first - end.compact + references[:snippet].uniq.map do |project, identifier| + project.snippets.where(id: identifier).first + end.compact.uniq end def commits - references[:commits].map do |entry| - repo = entry[:project].repository - repo.commit(entry[:id]) if repo - end.compact + references[:commit].uniq.map do |project, identifier| + repo = project.repository + repo.commit(identifier) if repo + end.compact.uniq end def commit_ranges - references[:commit_ranges].map do |entry| - repo = entry[:project].repository if entry[:project] + references[:commit_range].uniq.map do |project, identifier| + repo = project.repository if repo - from_id, to_id = entry[:id].split(/\.{2,3}/, 2) + from_id, to_id = identifier.split(/\.{2,3}/, 2) [repo.commit(from_id), repo.commit(to_id)] end - end.compact + end.compact.uniq end private - def reference_link(type, identifier, project, user, _) - references[type] << { project: project, id: identifier } + def reference_link(type, identifier, project, _) + references[type] << [project, identifier] end end end -- cgit v1.2.1