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

43 lines
1.1 KiB
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, :author
2014-09-02 18:07:02 +05:30
def initialize(project, current_user = nil, author = nil)
2015-04-26 12:48:37 +05:30
@project = project
@current_user = current_user
@author = author
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
%i(user label milestone merge_request snippet commit commit_range).each do |type|
2015-12-23 02:04:40 +05:30
define_method("#{type}s") do
@references[type] ||= references(type, reference_context)
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
if project && project.jira_tracker?
@references[:external_issue] ||= references(:external_issue, reference_context)
2015-12-23 02:04:40 +05:30
else
@references[:issue] ||= references(:issue, reference_context)
2015-10-24 18:46:33 +05:30
end
2014-09-02 18:07:02 +05:30
end
private
def reference_context
{ project: project, current_user: current_user, author: author }
end
2014-09-02 18:07:02 +05:30
end
end