class Fluent::WebhookGithubInput

Constants

HMAC_DIGEST

Public Instance Methods

process(event, payload, guid) click to toggle source
# 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
run() click to toggle source
# 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
shutdown() click to toggle source
# File lib/fluent/plugin/in_webhook_github.rb, line 26
def shutdown
  @server.shutdown
  Thread.kill(@thread)
end
start() click to toggle source
# File lib/fluent/plugin/in_webhook_github.rb, line 22
def start
  @thread = Thread.new(&method(:run))
end
verify_signature(req) click to toggle source
# 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