Class: Fluent::WebhookGithubInput
- Inherits:
-
Input
- Object
- Input
- Fluent::WebhookGithubInput
- Includes:
- DetachProcessMixin
- Defined in:
- lib/fluent/plugin/in_webhook_github.rb
Constant Summary collapse
- HMAC_DIGEST =
OpenSSL::Digest.new('sha1')
Instance Method Summary collapse
- #process(event, payload) ⇒ Object
- #run ⇒ Object
- #shutdown ⇒ Object
- #start ⇒ Object
- #verify_signature(req) ⇒ Object
Instance Method Details
#process(event, payload) ⇒ Object
66 67 68 69 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 |
# File 'lib/fluent/plugin/in_webhook_github.rb', line 66 def process(event, payload) content = case event 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[: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 |
#run ⇒ Object
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 |
# 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 verify_signature(req) payload = JSON.parse(req.body || "{}") event = req.header["x-github-event"].first process(event, payload) 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 |
#shutdown ⇒ Object
26 27 28 29 |
# File 'lib/fluent/plugin/in_webhook_github.rb', line 26 def shutdown @server.shutdown Thread.kill(@thread) end |
#start ⇒ Object
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
60 61 62 63 64 |
# File 'lib/fluent/plugin/in_webhook_github.rb', line 60 def verify_signature(req) return true unless @secret sig = 'sha1='+OpenSSL::HMAC.hexdigest(HMAC_DIGEST, @secret, req.body) SecureCompare.compare(sig, req.header["x-hub-signature"].first) end |