Class: Fluent::Plugin::RedmineOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_redmine.rb

Defined Under Namespace

Classes: Error, TemplateExpander

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fluent/plugin/out_redmine.rb', line 56

def configure(conf)
  super

  @use_ssl = @url.start_with?("https:")

  @subject_expander = TemplateExpander.new(@subject)
  @description_expander = TemplateExpander.new(@description)
  @redmine_uri = URI.parse("#{@url}/issues.json")

  @redmine_request_header = {
    "Content-Type" => "application/json",
    "X-Redmine-API-Key" => @api_key
  }
end

#make_payload(subject, desc, record) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fluent/plugin/out_redmine.rb', line 114

def make_payload(subject, desc, record)
  priority_id = @priority_id_key.nil? ? @priority_id : (record[@priority_id_key] || @priority_id).to_i
  category_id = @category_id_key.nil? ? @category_id : (record[@category_id_key] || @category_id).to_i
  custom_fields = @custom_fields_key.nil? ? @custom_fields : (record[@custom_fields_key] || [])
  issue = {
    project_id: @project_id,
    category_id: category_id,
    subject: subject,
    description: desc
  }
  issue[:tracker_id] = @tracker_id unless @tracker_id.nil?
  issue[:priority_id] = priority_id unless priority_id.nil?
  issue[:custom_fields] = custom_fields unless custom_fields.empty?
  { issue: issue }
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/fluent/plugin/out_redmine.rb', line 71

def multi_workers_ready?
  true
end

#submit_ticket(subject, desc, record) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fluent/plugin/out_redmine.rb', line 89

def submit_ticket(subject, desc, record)
  request = Net::HTTP::Post.new(
    @redmine_uri.request_uri,
    @redmine_request_header
  )
  request.body = JSON.generate(make_payload(subject, desc, record))

  client = Net::HTTP.new(@redmine_uri.host, @redmine_uri.port)
  if @use_ssl
    client.use_ssl = true
    client.verify_mode = OpenSSL::SSL::VERIFY_NONE # TODO support other verify mode
  end
  if @debug_http
    client.set_debug_output($stderr)
  end

  client.start do |http|
    res = http.request(request)
    unless res.code.to_i == 201
      raise Error, "Error: #{res.code}, #{res.body}"
    end
    return res.body
  end
end

#write(chunk) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fluent/plugin/out_redmine.rb', line 75

def write(chunk)
  tag = chunk..tag
  chunk.each do |_time, record|
    subject = @subject_expander.bind(make_record(tag, record))
    desc = @description_expander.bind(make_record(tag, record))
    begin
      submit_ticket(subject, desc, record)
    rescue => e
      log.error "out_redmine: failed to create ticket to #{@redmine_uri}, subject: #{subject}, description: #{desc}, error_class: #{e.class}, error_message: #{e.message}, error_backtrace: #{e.backtrace.first}"
      raise e
    end
  end
end