diff options
author | Vinnie Okada <vokada@mrvinn.com> | 2014-10-02 22:26:39 +0400 |
---|---|---|
committer | Vinnie Okada <vokada@mrvinn.com> | 2014-10-03 21:30:20 +0400 |
commit | 2c46c4523fc8aa41cb60e4840af16fdd595f7dd2 (patch) | |
tree | b178ae204f78e40aff7611b70a3a025f6e7befa9 /lib/gitlab/reference_extractor.rb | |
parent | 7edc1439fe11e396bb6327a3f50aca5dfe3c411c (diff) |
Track projects in ReferenceExtractor
Store both the project and identifier of extracted references. This
prevents `ReferenceExtractor` from returning objects in the wrong
project for cross-project references.
Diffstat (limited to 'lib/gitlab/reference_extractor.rb')
-rw-r--r-- | lib/gitlab/reference_extractor.rb | 52 |
1 files changed, 32 insertions, 20 deletions
diff --git a/lib/gitlab/reference_extractor.rb b/lib/gitlab/reference_extractor.rb index d2f02f712ce..99165950aef 100644 --- a/lib/gitlab/reference_extractor.rb +++ b/lib/gitlab/reference_extractor.rb @@ -9,51 +9,63 @@ module Gitlab @users, @issues, @merge_requests, @snippets, @commits = [], [], [], [], [] end - def analyze(string) - parse_references(string.dup) + def analyze(string, project) + parse_references(string.dup, project) end # Given a valid project, resolve the extracted identifiers of the requested type to # model objects. def users_for(project) - users.map do |identifier| - project.users.where(username: identifier).first + users.map do |entry| + project.users.where(username: entry[:id]).first end.reject(&:nil?) end - def issues_for(project) - issues.map do |identifier| - project.issues.where(iid: identifier).first + def issues_for(project = nil) + issues.map do |entry| + if should_lookup?(project, entry[:project]) + entry[:project].issues.where(iid: entry[:id]).first + end end.reject(&:nil?) end - def merge_requests_for(project) - merge_requests.map do |identifier| - project.merge_requests.where(iid: identifier).first + 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 end.reject(&:nil?) end def snippets_for(project) - snippets.map do |identifier| - project.snippets.where(id: identifier).first + snippets.map do |entry| + project.snippets.where(id: entry[:id]).first end.reject(&:nil?) end - def commits_for(project) - repo = project.repository - return [] if repo.nil? - - commits.map do |identifier| - repo.commit(identifier) + 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 end.reject(&:nil?) end private - def reference_link(type, identifier, _, _) + def reference_link(type, identifier, project, _) # Append identifier to the appropriate collection. - send("#{type}s") << identifier + send("#{type}s") << { project: project, id: identifier } + end + + def should_lookup?(project, entry_project) + if entry_project.nil? + false + else + project.nil? || project.id == entry_project.id + end end end end |