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
|
2016-01-14 18:37:52 +05:30
|
|
|
attr_accessor :project, :current_user, :author
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
def initialize(project, current_user = nil, author = nil)
|
2015-04-26 12:48:37 +05:30
|
|
|
@project = project
|
|
|
|
@current_user = current_user
|
2016-01-14 18:37:52 +05:30
|
|
|
@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
|
|
|
|
2016-01-14 18:37:52 +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
|
2016-01-14 18:37:52 +05:30
|
|
|
@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?
|
2016-01-14 18:37:52 +05:30
|
|
|
@references[:external_issue] ||= references(:external_issue, reference_context)
|
2015-12-23 02:04:40 +05:30
|
|
|
else
|
2016-01-14 18:37:52 +05:30
|
|
|
@references[:issue] ||= references(:issue, reference_context)
|
2015-10-24 18:46:33 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def reference_context
|
|
|
|
{ project: project, current_user: current_user, author: author }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|