Files
gitlabhq/lib/gitlab/reference_extractor.rb
T
Dmitriy Zaporozhets b165b097f5 Merge branch 'master' of dev.gitlab.org:gitlab/gitlabhq into ce-7-to-ee
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>

Conflicts:
	LICENSE
	VERSION
	app/controllers/omniauth_callbacks_controller.rb
	app/helpers/application_helper.rb
	app/helpers/merge_requests_helper.rb
	app/models/group.rb
	app/models/project.rb
	app/models/project_team.rb
	app/views/admin/groups/edit.html.haml
	app/views/groups/_projects.html.haml
	app/views/groups/edit.html.haml
	db/schema.rb
	doc/install/installation.md
	doc/integration/README.md
	lib/gitlab/git_access.rb
	lib/gitlab/markdown.rb
	spec/helpers/merge_requests_helper.rb
	spec/models/merge_request_spec.rb
2014-06-17 10:53:44 +03:00

66 lines
1.6 KiB
Ruby

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
if project.jira_tracker?
issues.uniq.map do |jira_identifier|
JiraIssue.new(jira_identifier)
end
else
issues.map do |identifier|
project.issues.where(iid: identifier).first
end.reject(&:nil?)
end
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, project)
# Append identifier to the appropriate collection.
send("#{type}s") << identifier
end
end
end