summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-08-26 02:31:57 -0700
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-08-26 02:31:57 -0700
commit551946a34e60195c44f293febaa8a0e77f0a23c7 (patch)
tree5a36c3e0f364fdfb710e01090fc81b9676ea53c4 /lib
parent2b36dee64485062c69779217d4a202e5ca1b67bd (diff)
parentc8a115c0e3a9c8242c2a422572d47a49e0cb2874 (diff)
downloadgitlab-ce-551946a34e60195c44f293febaa8a0e77f0a23c7.tar.gz
Merge pull request #4507 from smashwilson/linking-issues
Linking objects from GFM references
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/reference_extractor.rb59
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/gitlab/reference_extractor.rb b/lib/gitlab/reference_extractor.rb
new file mode 100644
index 00000000000..94b01e808d9
--- /dev/null
+++ b/lib/gitlab/reference_extractor.rb
@@ -0,0 +1,59 @@
+module Gitlab
+ # Extract possible GFM references from an arbitrary String for further processing.
+ class ReferenceExtractor
+ attr_accessor :users, :issues, :merge_requests, :snippets, :commits
+
+ include Markdown
+
+ def initialize
+ @users, @issues, @merge_requests, @snippets, @commits = [], [], [], [], []
+ end
+
+ def analyze string
+ parse_references(string.dup)
+ 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
+ end.reject(&:nil?)
+ end
+
+ def issues_for project
+ issues.map do |identifier|
+ project.issues.where(iid: identifier).first
+ end.reject(&:nil?)
+ end
+
+ def merge_requests_for project
+ merge_requests.map do |identifier|
+ project.merge_requests.where(iid: identifier).first
+ end.reject(&:nil?)
+ end
+
+ def snippets_for project
+ snippets.map do |identifier|
+ project.snippets.where(id: identifier).first
+ end.reject(&:nil?)
+ end
+
+ def commits_for project
+ repo = project.repository
+ return [] if repo.nil?
+
+ commits.map do |identifier|
+ repo.commit(identifier)
+ end.reject(&:nil?)
+ end
+
+ private
+
+ def reference_link type, identifier
+ # Append identifier to the appropriate collection.
+ send("#{type}s") << identifier
+ end
+ end
+end