debian-mirror-gitlab/lib/gitlab/reference_extractor.rb

38 lines
991 B
Ruby
Raw Normal View History

2015-12-23 02:04:40 +05:30
require 'banzai'
2015-09-25 12:07:36 +05:30
2014-09-02 18:07:02 +05:30
module Gitlab
# Extract possible GFM references from an arbitrary String for further processing.
2015-12-23 02:04:40 +05:30
class ReferenceExtractor < Banzai::ReferenceExtractor
attr_accessor :project, :current_user
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
def initialize(project, current_user = nil)
2015-04-26 12:48:37 +05:30
@project = project
@current_user = current_user
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
@references = {}
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
super()
2014-09-02 18:07:02 +05:30
end
2015-12-23 02:04:40 +05:30
def analyze(text, context = {})
super(text, context.merge(project: project))
end
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
%i(user label merge_request snippet commit commit_range).each do |type|
define_method("#{type}s") do
@references[type] ||= references(type, project: project, current_user: current_user)
2015-09-11 14:41:01 +05:30
end
2014-09-02 18:07:02 +05:30
end
2015-12-23 02:04:40 +05:30
def issues
options = { project: project, current_user: current_user }
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
if project && project.jira_tracker?
@references[:external_issue] ||= references(:external_issue, options)
else
@references[:issue] ||= references(:issue, options)
2015-10-24 18:46:33 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
end