Class: Fluent::WebhookGithubInput

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

Constant Summary collapse

HMAC_DIGEST =
OpenSSL::Digest.new('sha1')

Instance Method Summary collapse

Instance Method Details

#process(event, payload, guid) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fluent/plugin/in_webhook_github.rb', line 70

def process(event, payload, guid)
  content = case event
  when nil
    nil
  when "issue", "issue_comment"
    {
      :url   => payload["issue"] && payload["issue"]["html_url"],
      :title => payload["issue"] && payload["issue"]["title"],
      :user  => payload["issue"] && payload["issue"]["user"]["login"],
      :body  => payload["comment"] && payload["comment"]["body"],
    }
  when "pull_request"
    {
      :url   => payload["pull_request"] && payload["pull_request"]["html_url"],
      :title => payload["pull_request"] && payload["pull_request"]["title"],
      :user  => payload["pull_request"] && payload["pull_request"]["user"]["login"],
      :body  => payload["pull_request"] && payload["pull_request"]["body"],
    }
  when "pull_request_review_comment"
    {
      :url   => payload["comment"] && payload["comment"]["html_url"],
      :title => payload["pull_request"] && payload["pull_request"]["title"],
      :user  => payload["comment"] && payload["comment"]["user"]["login"],
      :body  => payload["comment"] && payload["comment"]["body"],
    }
  else
    {}
  end

  if content
    content[:origin] = "github"
    content[:event]  = event
    content[:guid]   = guid if guid
    content[:payload] = payload if @with_payload
    $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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fluent/plugin/in_webhook_github.rb', line 33

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

      if req.request_method != "POST"
        res.status = 405
      elsif verify_signature(req)
        payload = JSON.parse(req.body)
        event = req.header["x-github-event"].first
        guid  = req.header["x-github-delivery"].first
        process(event, payload, guid)
        res.status = 204
      else
        res.status = 401
      end
    rescue => e
      $log.error e.inspect
      $log.error e.backtrace.join("\n")
      res.status = 400
    end
  end
  @server.start
end

#shutdownObject



26
27
28
29
# File 'lib/fluent/plugin/in_webhook_github.rb', line 26

def shutdown
  @server.shutdown
  Thread.kill(@thread)
end

#startObject



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

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

#verify_signature(req) ⇒ Object



63
64
65
66
67
68
# File 'lib/fluent/plugin/in_webhook_github.rb', line 63

def verify_signature(req)
  return true unless @secret
  return false unless req.body
  sig = 'sha1='+OpenSSL::HMAC.hexdigest(HMAC_DIGEST, @secret, req.body)
  SecureCompare.compare(sig, req.header["x-hub-signature"].first)
end