Class: Fluent::WebhookGithubInput

Inherits:
Input
  • Object
show all
Includes:
DetachProcessMixin
Defined in:
lib/fluent/plugin/in_webhook_github.rb

Instance Method Summary collapse

Instance Method Details

#process(event, payload) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fluent/plugin/in_webhook_github.rb', line 48

def process(event, payload)
  content = case event
  when "issue", "issue_comment"
    {
      :url => payload["issue"]["html_url"],
      :title => payload["issue"]["title"],
      :user => payload["issue"]["user"]["login"],
      :body => payload["comment"]["body"],
    }
  when "pull_request"
    {
      :url => payload["pull_request"]["html_url"],
      :title => payload["pull_request"]["title"],
      :user => payload["pull_request"]["user"]["login"],
      :body => payload["pull_request"]["body"],
    }
  when "pull_request_review_comment"
    {
      :url => payload["comment"]["html_url"],
      :title => payload["pull_request"]["title"],
      :user => payload["comment"]["user"]["login"],
      :body => payload["comment"]["body"],
    }
  end
  if content
    content[:origin] = "github"
    $log.info "tag: #{@tag.dup}.#{event}, event:#{event}, content:#{content}" 
    Engine.emit("#{@tag.dup}.#{event}", Engine.now, content) if content
  else
    $log.warn "unknown hook received #{event} #{payload.inspect}"
  end
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fluent/plugin/in_webhook_github.rb', line 26

def run
  server = WEBrick::HTTPServer.new(
    :BindAddress => @bind,
    :Port => @port,
  )
  $log.debug "Listen on http://#{@bind}:#{@port}#{@mount}"
  server.mount_proc(@mount) do |req, res|
    begin
      $log.debug req.header
      payload = JSON.parse(req.body || "{}")
      event = req.header["x-github-event"].first
      process(event, payload)
      res.status = 204
    rescue => e
      $log.error e.inspect
      $log.error e.backtrace.join("\n")
      res.status = 400
    end
  end
  server.start
end

#shutdownObject



22
23
24
# File 'lib/fluent/plugin/in_webhook_github.rb', line 22

def shutdown
  Thread.kill(@thread)
end

#startObject



18
19
20
# File 'lib/fluent/plugin/in_webhook_github.rb', line 18

def start
  @thread = Thread.new(&method(:run))
end