Skip to content

Commit d14a48e

Browse files
authored
Merge pull request #36 from travis-ci/kerrizor/set-queue-name-for-events-destined-for-sync
Set queue name for events destined for sync
2 parents 7807659 + 6e2ce5e commit d14a48e

File tree

8 files changed

+105
-25
lines changed

8 files changed

+105
-25
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ gem 'yajl-ruby', '~> 1.3.1'
2929
gem 'unicorn', '~> 4.6.2'
3030

3131
group :development, :test do
32+
gem 'pry'
3233
gem 'rspec', '~> 2.9'
3334
end
3435

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ GEM
1919
avl_tree (1.2.1)
2020
atomic (~> 1.1)
2121
backports (2.4.0)
22+
coderay (1.1.2)
2223
concurrent-ruby (1.0.5)
2324
connection_pool (2.2.1)
2425
crack (0.4.3)
@@ -35,6 +36,7 @@ GEM
3536
concurrent-ruby (~> 1.0)
3637
json (1.8.6)
3738
kgio (2.11.0)
39+
method_source (0.9.0)
3840
metriks (0.9.9.8)
3941
atomic (~> 1.0)
4042
avl_tree (~> 1.2.0)
@@ -44,6 +46,9 @@ GEM
4446
minitest (5.11.1)
4547
multi_json (1.12.1)
4648
multipart-post (2.0.0)
49+
pry (0.11.3)
50+
coderay (~> 1.1.0)
51+
method_source (~> 0.9.0)
4752
public_suffix (2.0.5)
4853
rack (1.6.8)
4954
rack-protection (1.5.3)
@@ -101,6 +106,7 @@ DEPENDENCIES
101106
metriks
102107
metriks-librato_metrics
103108
multi_json
109+
pry
104110
rack-test
105111
rake (~> 0.9.2.2)
106112
redis-namespace

lib/travis/listener/app.rb

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ class App < Sinatra::Base
1717
# see https://github.com/github/github-services/blob/master/lib/services/travis.rb#L1-2
1818
# https://github.com/travis-ci/travis-api/blob/255640fd4f191f1de6951081f0c5848324210fb5/lib/travis/github/services/set_hook.rb#L8
1919
# https://github.com/travis-ci/travis-api/blob/255640fd4f191f1de6951081f0c5848324210fb5/lib/travis/api/v3/github.rb#L41
20-
set :events, %w[push pull_request create delete repository installation installation_repositories]
20+
set :events, %w[
21+
push
22+
pull_request
23+
create
24+
delete
25+
repository
26+
integration_installation
27+
installation_repositories
28+
]
2129

2230
before do
2331
logger.level = 1
@@ -42,6 +50,7 @@ class App < Sinatra::Base
4250
204
4351
else
4452
Metriks.meter('listener.request.no_payload').mark
53+
4554
422
4655
end
4756
else
@@ -82,20 +91,61 @@ def dispatch_event
8291
return unless handle_event?
8392
debug "Event payload for #{uuid}: #{payload.inspect}"
8493

85-
case event_type
86-
when 'push', 'pull_request', 'create', 'delete', 'repository' then gatekeeper_event
87-
when 'installation', 'installation_repositories' then sync_event
94+
if github_pr_event?
95+
gatekeeper_event
96+
elsif github_apps_event?
97+
sync_event
8898
end
8999
end
90100

101+
def github_pr_event?
102+
[
103+
'push',
104+
'pull_request',
105+
'create',
106+
'delete',
107+
'repository',
108+
].include? event_type
109+
end
110+
111+
def github_apps_event?
112+
[
113+
'integration_installation',
114+
'installation_repositories',
115+
].include? event_type
116+
end
117+
91118
def gatekeeper_event
92-
log_event(event_details, uuid: uuid, delivery_guid: delivery_guid, type: event_type, repository: slug)
119+
log_event(
120+
event_details,
121+
uuid: uuid,
122+
delivery_guid: delivery_guid,
123+
type: event_type,
124+
repository: slug
125+
)
126+
127+
Metriks.meter("listener.event.webhook_#{event_type}").mark
128+
93129
Travis::Sidekiq::Gatekeeper.push(Travis.config.gator.queue, data)
94130
end
95131

96132
def sync_event
97-
log_event(event_details, uuid: uuid, delivery_guid: delivery_guid, type: event_type)
98-
Travis::Sidekiq::GithubSync.push(data)
133+
log_event(
134+
event_details,
135+
uuid: uuid,
136+
delivery_guid: delivery_guid,
137+
type: event_type
138+
)
139+
140+
case event_type
141+
when 'integration_installation'
142+
Travis::Sidekiq::GithubSync.gh_app_install(data)
143+
when 'installation_repositories'
144+
Travis::Sidekiq::GithubSync.gh_app_repos(data)
145+
else
146+
logger.info "Unable to find a sync event for event_type: #{event_type}"
147+
false
148+
end
99149
end
100150

101151
def handle_event?
@@ -108,11 +158,11 @@ def log_event(event_details, event_basics)
108158

109159
def data
110160
{
111-
:type => event_type,
112-
:payload => payload,
113-
:uuid => uuid,
114-
:github_guid => delivery_guid,
115-
:github_event => event_type
161+
:type => event_type,
162+
:payload => payload,
163+
:uuid => uuid,
164+
:github_guid => delivery_guid,
165+
:github_event => event_type,
116166
}
117167
end
118168

@@ -159,7 +209,15 @@ def delivery_guid
159209
end
160210

161211
def payload
162-
params[:payload]
212+
if github_pr_event?
213+
params[:payload]
214+
elsif github_apps_event?
215+
begin
216+
@_parsed_json ||= JSON.parse(request.body.read)
217+
rescue JSON::ParserError
218+
nil
219+
end
220+
end
163221
end
164222

165223
def slug

lib/travis/sidekiq.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@ def self.push(queue, *args)
1414
client.push(
1515
'queue' => queue,
1616
'class' => 'Travis::Gatekeeper::Worker',
17-
'args' => args
17+
'args' => args
1818
)
1919
end
2020
end
2121

2222
class GithubSync
2323
def self.gh_app_install(data)
24+
Metriks.meter('listener.event.gh_apps_install').mark
25+
2426
push('sync.gh_apps', :gh_app_install, data)
2527
end
2628

2729
def self.gh_app_repos(data)
30+
Metriks.meter('listener.event.gh_apps_repos').mark
31+
2832
push('sync.gh_apps', :gh_app_repos, data)
2933
end
3034

@@ -38,7 +42,7 @@ def self.push(queue, *args)
3842
client.push(
3943
'queue' => queue,
4044
'class' => 'Travis::GithubSync::Worker',
41-
'args' => args
45+
'args' => args
4246
)
4347
end
4448
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
require 'sidekiq/testing'
1212

13+
require 'pry'
14+
1315
Travis.logger = ::Logger.new(StringIO.new)
1416

1517
Travis::Listener.setup

spec/travis/app_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def create(opts = {})
1616
params = { :payload => (opts[:payload] || payload) }
1717
headers = { 'HTTP_X_GITHUB_EVENT' => 'push', 'HTTP_X_GITHUB_GUID' => 'abc123' }
1818
headers.merge!(opts.delete(:headers) || {})
19+
1920
post(opts[:url] || '/', params, headers)
2021
end
2122

spec/travis/events_spec.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@
1414
before { create }
1515

1616
def create(opts = {})
17-
params = { :payload => (opts[:payload] || payload) }
17+
if ["integration_installation", "installation_repositories"].include? type
18+
params = payload
19+
else
20+
params = { :payload => (opts[:payload] || payload) }
21+
end
22+
1823
headers = { 'HTTP_X_GITHUB_EVENT' => event, 'HTTP_X_GITHUB_GUID' => 'abc123' }
1924
headers.merge!(opts.delete(:headers) || {})
25+
2026
post(opts[:url] || '/', params, headers)
2127
end
2228

2329
shared_examples_for 'queues gatekeeper event' do |&block|
2430
it { expect(gatekeeper_queue).to have_received(:push).with('build_requests', hash_including(type: event)) }
2531
end
2632

27-
shared_examples_for 'queues gh sync event' do |&block|
28-
it { expect(gh_sync_queue).to have_received(:push).with(hash_including(type: event)) }
29-
end
30-
3133
describe 'a push event' do
3234
let(:type) { 'push' }
3335
let(:event) { 'push' }
@@ -88,15 +90,21 @@ def create(opts = {})
8890
include_examples 'queues gatekeeper event'
8991
end
9092

91-
describe 'an installation event' do
92-
let(:type) { 'installation' }
93-
let(:event) { 'installation' }
94-
include_examples 'queues gh sync event'
93+
describe 'an integration_installation event' do
94+
let(:type) { 'integration_installation' }
95+
let(:event) { 'integration_installation' }
96+
97+
it { expect(gh_sync_queue)
98+
.to have_received(:push)
99+
.with('sync.gh_apps', :gh_app_install, hash_including(type: event)) }
95100
end
96101

97102
describe 'an installation_repositories event' do
98103
let(:type) { 'installation_repositories' }
99104
let(:event) { 'installation_repositories' }
100-
include_examples 'queues gh sync event'
105+
106+
it { expect(gh_sync_queue)
107+
.to have_received(:push)
108+
.with('sync.gh_apps', :gh_app_repos, hash_including(type: event)) }
101109
end
102110
end

0 commit comments

Comments
 (0)