Skip to content

Commit b1cd011

Browse files
committed
Handle 401 and 404 from Github when finding an admin
1 parent 13061d1 commit b1cd011

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

lib/travis/services/find_admin.rb

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ class FindAdmin < Base
1010
include Travis::Logging
1111

1212
register :find_admin
13+
NUM_CANDIDATES = 15
1314

1415
def run
1516
if repository
16-
admin = candidates.first
17-
admin || raise_admin_missing
17+
admin
1818
else
1919
error "[github-admin] repository is nil: #{params.inspect}"
2020
raise Travis::RepositoryMissing, "no repository given"
@@ -29,25 +29,31 @@ def repository
2929
private
3030

3131
def candidates
32-
User.with_github_token.with_permissions(:repository_id => repository.id, :admin => true)
32+
User.with_github_token.with_permissions(:repository_id => repository.id, :admin => true).first(NUM_CANDIDATES)
33+
end
34+
35+
def admin
36+
admin = candidates.detect do |candidate|
37+
is_valid? candidate
38+
end
39+
admin || raise_admin_missing
3340
end
3441

35-
def validate(user)
42+
def is_valid?(admin)
3643
Timeout.timeout(2) do
37-
data = Github.authenticated(user) { repository_data }
44+
data = Github.authenticated(admin) { repository_data }
3845
if data['permissions'] && data['permissions']['admin']
39-
user
46+
true
4047
else
41-
info "[github-admin] #{user.login} no longer has admin access to #{repository.slug}"
42-
update(user, data['permissions'])
48+
revoke_admin_rights admin
4349
false
4450
end
4551
end
4652
rescue Timeout::Error => error
47-
handle_error(user, error)
53+
handle_error(admin, error)
4854
false
4955
rescue GH::Error => error
50-
handle_error(user, error)
56+
handle_error(admin, error)
5157
false
5258
end
5359

@@ -58,8 +64,7 @@ def handle_error(user, error)
5864
error "[github-admin] token for #{user.login} no longer valid"
5965
user.update_attributes!(:github_oauth_token => "")
6066
when 404
61-
info "[github-admin] #{user.login} no longer has any access to #{repository.slug}"
62-
update(user, {})
67+
revoke_admin_rights user
6368
else
6469
error "[github-admin] error retrieving repository info for #{repository.slug} for #{user.login}: #{error.message}"
6570
end
@@ -72,14 +77,16 @@ def repository_data
7277
data || { 'permissions' => {} }
7378
end
7479

75-
def update(user, permissions)
76-
user.update_attributes!(:permissions => permissions)
77-
end
78-
7980
def raise_admin_missing
8081
raise Travis::AdminMissing.new("no admin available for #{repository.slug}")
8182
end
8283

84+
def revoke_admin_rights(user)
85+
info "[github-admin] #{user.login} no longer has admin access to #{repository.slug}"
86+
87+
Permission.where(user_id: user.id, repository_id: repository.id).update_all(admin: false)
88+
end
89+
8390
class Instrument < Notification::Instrument
8491
def run_completed
8592
publish(

lib/travis/testing/stubs.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@ def included(base)
2727
let(:broadcast) { stub_broadcast }
2828
let(:travis_token) { stub_travis_token }
2929
let(:cache) { stub_cache }
30+
let(:permission) { stub_permission }
3031
end
3132
end
3233
end
3334

35+
def stub_permission(attributes = {})
36+
Stubs.stub 'permission', attributes.reverse_merge(
37+
id: 1,
38+
user_id: stub_user,
39+
repository_id: stub_repo
40+
)
41+
end
42+
3443
def stub_repo(attributes = {})
3544
Stubs.stub 'repository', attributes.reverse_merge(
3645
id: 1,
@@ -60,7 +69,7 @@ def stub_repo(attributes = {})
6069
github_id: 549743,
6170
builds_only_with_travis_yml?: false,
6271
settings: stub_settings,
63-
users_with_permission: [],
72+
users_with_permission: stub_user,
6473
default_branch: 'master',
6574
current_build_id: nil
6675
)

spec/travis/services/find_admin_spec.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'pry'
12
require 'spec_helper'
23

34
describe Travis::Services::FindAdmin do
@@ -13,6 +14,8 @@
1314
describe 'given a user has admin access to a repository (as seen by github)' do
1415
before :each do
1516
GH.stubs(:[]).with("repos/#{repository.slug}").returns('permissions' => { 'admin' => true })
17+
stub_permission(user_id: user.id, repository_id: repository.id)
18+
Permission.stub(:update_all)
1619
end
1720

1821
it 'returns that user' do
@@ -23,15 +26,17 @@
2326
describe 'given a user does not have access to a repository' do
2427
before :each do
2528
GH.stubs(:[]).with("repos/#{repository.slug}").returns('permissions' => { 'admin' => false })
26-
user.stubs(:update_attributes!)
29+
30+
Permission.stubs(:where).with(user_id: user.id, repository_id: repository.id).returns(permission)
31+
permission.stubs(:update_all)
2732
end
2833

29-
xit 'raises an exception' do
34+
it 'raises an exception' do
3035
lambda { result }.should raise_error(Travis::AdminMissing, 'no admin available for svenfuchs/minimal')
3136
end
3237

33-
xit 'revokes admin permissions for that user on our side' do
34-
user.expects(:update_attributes!).with(:permissions => { 'admin' => false })
38+
it 'revokes admin permissions for that user on our side' do
39+
permission.expects(:update_all).with(admin: false)
3540
ignore_exception { result }
3641
end
3742
end
@@ -43,7 +48,7 @@
4348
GH.stubs(:[]).with("repos/#{repository.slug}").raises(GH::Error.new(error))
4449
end
4550

46-
xit 'raises an exception' do
51+
it 'raises an exception' do
4752
lambda { result }.should raise_error(Travis::AdminMissing, 'no admin available for svenfuchs/minimal')
4853
end
4954

0 commit comments

Comments
 (0)