mirror of
https://github.com/wahyd4/gitlabhq.git
synced 2026-08-11 05:36:07 +10:00
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> Conflicts: app/controllers/projects/services_controller.rb app/models/concerns/mentionable.rb app/models/project.rb app/models/user.rb app/views/admin/users/index.html.haml app/views/projects/services/_form.html.haml config/gitlab.yml.example config/initializers/1_settings.rb lib/gitlab/git_access.rb lib/gitlab/ldap/access.rb lib/gitlab/ldap/person.rb lib/gitlab/reference_extractor.rb spec/lib/gitlab/ldap/access_spec.rb
78 lines
2.0 KiB
Ruby
78 lines
2.0 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, 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 |entry|
|
|
project.users.where(username: entry[:id]).first
|
|
end.reject(&:nil?)
|
|
end
|
|
|
|
def issues_for(project = nil)
|
|
if project && project.jira_tracker?
|
|
issues.uniq.map do |jira_identifier|
|
|
JiraIssue.new(jira_identifier)
|
|
end
|
|
else
|
|
issues.map do |entry|
|
|
if should_lookup?(project, entry[:project])
|
|
entry[:project].issues.where(iid: entry[:id]).first
|
|
end
|
|
end.reject(&:nil?)
|
|
end
|
|
end
|
|
|
|
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 |entry|
|
|
project.snippets.where(id: entry[:id]).first
|
|
end.reject(&:nil?)
|
|
end
|
|
|
|
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, project, _)
|
|
# Append identifier to the appropriate collection.
|
|
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
|