Sha256: 1f791e34441f0ec96121886b4f12d44d7786a5642e3c1d767ebfd88498b76a2b

Contents?: true

Size: 1.47 KB

Versions: 7

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Rodbot
  class Plugins
    class GithubWebhook
      module App

        class Routes < ::App
          route do |r|
            r.post '' do
              r.halt 200 if request.env['HTTP_X_GITHUB_EVENT'] == 'ping'
              r.halt 400 unless request.env['HTTP_X_GITHUB_EVENT'] == 'workflow_run'
              r.halt 401 unless authorized? request
              json = JSON.parse(request.body.read)
              project = json.dig('repository', 'full_name')
              status = json.dig('workflow_run', 'status')
              status = json.dig('workflow_run', 'conclusion') if status == 'completed'
              Rodbot.say [emoji_for(status), project, status.gsub('_', ' ')].join(' ')
              r.halt 200
            end
          end

          private

          def authorized?(request)
            Rodbot.config(:plugin, :github_webhook, :secret_tokens).to_s.split(':').any? do |secret|
              signature = 'sha256=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), secret, request.body.read)
              request.body.rewind
              ::Rack::Utils.secure_compare(signature, request.env['HTTP_X_HUB_SIGNATURE_256'])
            end
          end

          def emoji_for(status)
            case status
              when 'requested' then '🟡'
              when 'success' then '🟢'
              when 'failure' then '🔴'
              else '⚪️'
            end
          end

        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rodbot-0.3.3 lib/rodbot/plugins/github_webhook/app.rb
rodbot-0.3.2 lib/rodbot/plugins/github_webhook/app.rb
rodbot-0.3.1 lib/rodbot/plugins/github_webhook/app.rb
rodbot-0.3.0 lib/rodbot/plugins/github_webhook/app.rb
rodbot-0.2.0 lib/rodbot/plugins/github_webhook/app.rb
rodbot-0.1.1 lib/rodbot/plugins/github_webhook/app.rb
rodbot-0.1.0 lib/rodbot/plugins/github_webhook/app.rb