Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enqueue update_hook if service hook can be detected #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/travis/listener/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def dispatch_event
debug "Event payload for #{uuid}: #{payload.inspect}"

log_event
handle_service_hook

case event_type
when 'installation'
Expand All @@ -112,6 +113,10 @@ def dispatch_event
end
end

def handle_service_hook
Travis::Sidekiq::GithubSync.update_hook(data) if came_from_service_hook?
end

def handle_event?
if accepted_event_excluding_checks? || rerequested_check?
Metriks.meter("listener.handle.accept").mark
Expand Down Expand Up @@ -178,6 +183,10 @@ def delivery_guid
env['HTTP_X_GITHUB_DELIVERY'] || env['HTTP_X_GITHUB_GUID']
end

def came_from_service_hook?
env.has_key?('HTTP_X_GITHUB_GUID')
end

def integration_type
if !params[:payload].blank?
"webhook"
Expand Down
6 changes: 6 additions & 0 deletions lib/travis/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def self.gh_app_member(data)
push('sync', :gh_app_member, data)
end

def self.update_hook(data)
Metriks.meter('listener.event.update_hook').mark

push('sync', :update_hook, data)
end

def self.client
@@client ||= ::Sidekiq::Client.new(
::Sidekiq::RedisConnection.create(Travis.config.redis.to_h)
Expand Down
15 changes: 15 additions & 0 deletions spec/travis/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
let(:payload) { Payloads.load('push') }
let(:redis) { Redis.new }
let(:queue) { Travis::Sidekiq::Gatekeeper }
let(:sync_queue) { Travis::Sidekiq::GithubSync }

before do
authorize(*auth)
allow(queue).to receive(:push)
allow(sync_queue).to receive(:update_hook)
end

def create(opts = {})
Expand All @@ -21,6 +23,7 @@ def create(opts = {})

headers = { 'HTTP_X_GITHUB_EVENT' => 'push', 'HTTP_X_GITHUB_GUID' => 'abc123' }
headers.merge!(opts.delete(:headers) || {})
headers = headers.delete_if { |k, v| v == nil } # allows removal for testing purposes

post(opts[:url] || '/', params, headers)
end
Expand Down Expand Up @@ -111,4 +114,16 @@ def create(opts = {})
last_response.status.should be == 403
end
end

context 'service hook' do
it 'enqueues update_hook if service hook detected' do
create
expect(sync_queue).to have_received(:update_hook).once
end

it 'does not if no service hook detected' do
create headers: { 'HTTP_X_GITHUB_GUID' => nil, 'HTTP_X_GITHUB_DELIVERY' => 'abc123' }
expect(sync_queue).not_to have_received(:update_hook)
end
end
end