Sha256: 9fbea8bad20afa00d02dfe3644f860ddcb647951bfbb395393b40548e835857c

Contents?: true

Size: 1.42 KB

Versions: 8

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true
module Shipit
  class WebhooksController < ActionController::Base
    skip_before_action :verify_authenticity_token, raise: false
    before_action :check_if_ping, :drop_unhandled_event, :verify_signature

    respond_to :json

    def create
      params = JSON.parse(request.raw_post)
      Shipit::Webhooks.for_event(event).each { |handler| handler.call(params) }

      head(:ok)
    end

    private

    def drop_unhandled_event
      # Acknowledge, but do nothing
      head(204) unless Shipit::Webhooks.for_event(event).present?
    end

    def verify_signature
      github_app = Shipit.github(organization: repository_owner)
      verified = github_app.verify_webhook_signature(
        request.headers['X-Hub-Signature'],
        request.raw_post
      )
      head(422) unless verified

      Rails.logger.info([
        'WebhookController#verify_signature',
        "event=#{event}",
        "repository_owner=#{repository_owner}",
        "signature=#{request.headers['X-Hub-Signature']}",
        "status=#{status}",
      ].join(' '))
    end

    def check_if_ping
      head(:ok) if event == 'ping'
    end

    def event
      request.headers.fetch('X-Github-Event')
    end

    def repository_owner
      # Fallback to the organization sub-object if repository isn't included in the payload
      params.dig('repository', 'owner', 'login') || params.dig('organization', 'login')
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
shipit-engine-0.39.0 app/controllers/shipit/webhooks_controller.rb
shipit-engine-0.38.0 app/controllers/shipit/webhooks_controller.rb
shipit-engine-0.37.0 app/controllers/shipit/webhooks_controller.rb
shipit-engine-0.36.1 app/controllers/shipit/webhooks_controller.rb
shipit-engine-0.36.0 app/controllers/shipit/webhooks_controller.rb
shipit-engine-0.35.1 app/controllers/shipit/webhooks_controller.rb
shipit-engine-0.35.0 app/controllers/shipit/webhooks_controller.rb
shipit-engine-0.34.0 app/controllers/shipit/webhooks_controller.rb