module GithubWebhook::Processor

Constants

GITHUB_EVENTS

To fetch list from developer.github.com/v3/activity/events/types run this little JS code in the console:

document.querySelectorAll('.list-style-none li.lh-condensed a').forEach(e => console.log(e.text))
HMAC_DIGEST

Public Instance Methods

create() click to toggle source
# File lib/github_webhook/processor.rb, line 73
def create
  if self.respond_to?(event_method, true)
    self.send event_method, json_body
    head(:ok)
  else
    raise NoMethodError.new("GithubWebhooksController##{event_method} not implemented")
  end
end
github_ping(payload) click to toggle source
# File lib/github_webhook/processor.rb, line 82
def github_ping(payload)
  GithubWebhook.logger && GithubWebhook.logger.info("[GithubWebhook::Processor] Hook ping "\
    "received, hook_id: #{payload[:hook_id]}, #{payload[:zen]}")
end

Private Instance Methods

authenticate_github_request!() click to toggle source
# File lib/github_webhook/processor.rb, line 91
def authenticate_github_request!
  raise UnspecifiedWebhookSecretError.new unless respond_to?(:webhook_secret, true)
  secret = webhook_secret(json_body)

  expected_signature = "sha1=#{OpenSSL::HMAC.hexdigest(HMAC_DIGEST, secret, request_body)}"
  unless ActiveSupport::SecurityUtils.secure_compare(signature_header, expected_signature)
    GithubWebhook.logger && GithubWebhook.logger.warn("[GithubWebhook::Processor] signature "\
      "invalid, actual: #{signature_header}, expected: #{expected_signature}")
    raise SignatureError
  end
end
check_github_event!() click to toggle source
# File lib/github_webhook/processor.rb, line 103
def check_github_event!
  unless GITHUB_EVENTS.include?(request.headers['X-GitHub-Event'])
    raise UnsupportedGithubEventError.new("#{request.headers['X-GitHub-Event']} is not a whitelisted GitHub event. See https://developer.github.com/v3/activity/events/types/")
  end
end
event_method() click to toggle source
# File lib/github_webhook/processor.rb, line 137
def event_method
  @event_method ||= "github_#{request.headers['X-GitHub-Event']}".to_sym
end
json_body() click to toggle source
# File lib/github_webhook/processor.rb, line 116
def json_body
  @json_body ||= (
    content_type = request.headers['Content-Type']
    case content_type
    when 'application/x-www-form-urlencoded'
      require 'rack'
      payload = Rack::Utils.parse_query(request_body)['payload']
    when 'application/json'
      payload = request_body
    else
      raise UnsupportedContentTypeError.new(
        "Content-Type #{content_type} is not supported. Use 'application/x-www-form-urlencoded' or 'application/json")
    end
    ActiveSupport::HashWithIndifferentAccess.new(JSON.load(payload))
  )
end
request_body() click to toggle source
# File lib/github_webhook/processor.rb, line 109
def request_body
  @request_body ||= (
    request.body.rewind
    request.body.read
  )
end
signature_header() click to toggle source
# File lib/github_webhook/processor.rb, line 133
def signature_header
  @signature_header ||= request.headers['X-Hub-Signature']
end