Skip to content

Commit f79bd83

Browse files
authored
Find admin service via VCS
1 parent 6cfccc5 commit f79bd83

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

lib/travis/remote_vcs/client.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ def http_options
2222
{ ssl: Travis.config.ssl.to_h }
2323
end
2424

25-
def request(method, name)
25+
def request(method, name, data_in_body = true)
2626
resp = connection.send(method) { |req| yield(req) }
27-
Travis.logger.info "#{self.class.name} #{name} response status: #{resp.status}"
28-
if resp.success?
29-
resp.body.present? ? JSON.parse(resp.body)['data'] : true
30-
else
31-
raise ResponseError, "#{self.class.name} #{name} request unexpected response: #{resp.body} #{resp.status}"
32-
end
27+
28+
raise ResponseError, "#{self.class.name} #{name} request unexpected response: #{resp.body} #{resp.status}" unless resp.success?
29+
return true unless resp.body.present?
30+
31+
parsed_response = JSON.parse(resp.body)
32+
data_in_body ? parsed_response['data'] : parsed_response
3333
end
3434
end
3535
end

lib/travis/remote_vcs/repository.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ def delete_key(repository_id:, user_id:, id:)
4141
rescue ResponseError
4242
false
4343
end
44+
45+
def show(repository_id:, admin_id: nil)
46+
request(:get, __method__, false) do |req|
47+
req.url "repos/#{repository_id}"
48+
req.params['admin_id'] = admin_id
49+
end
50+
rescue ResponseError
51+
nil
52+
end
4453
end
4554
end
4655
end

lib/travis/services/find_admin.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def candidates
3434

3535
def validate(user)
3636
Timeout.timeout(2) do
37-
data = Github.authenticated(user) { repository_data }
37+
data = repository_data(user)
3838
if data['permissions'] && data['permissions']['admin']
3939
user
4040
else
@@ -43,10 +43,7 @@ def validate(user)
4343
false
4444
end
4545
end
46-
rescue Timeout::Error => error
47-
handle_error(user, error)
48-
false
49-
rescue GH::Error => error
46+
rescue error
5047
handle_error(user, error)
5148
false
5249
end
@@ -56,7 +53,7 @@ def handle_error(user, error)
5653
case status
5754
when 401
5855
error "[github-admin] token for #{user.login} no longer valid"
59-
user.update_attributes!(:github_oauth_token => "")
56+
# user.update_attributes!(:github_oauth_token => "")
6057
when 404
6158
info "[github-admin] #{user.login} no longer has any access to #{repository.slug}"
6259
update(user, {})
@@ -66,10 +63,11 @@ def handle_error(user, error)
6663
end
6764

6865
# TODO should this not be memoized?
69-
def repository_data
70-
data = GH["repos/#{repository.slug}"]
71-
info "[github-admin] could not retrieve data for #{repository.slug}" unless data
72-
data || { 'permissions' => {} }
66+
def repository_data(user)
67+
Travis::RemoteVCS::Repository.new.show(
68+
repository_id: repository.id,
69+
admin_id: user.id
70+
)
7371
end
7472

7573
def update(user, permissions)

spec/lib/services/find_admin_spec.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,13 @@
99
end
1010

1111
describe 'given a user has admin access to a repository (as seen by github)' do
12-
before :each do
13-
allow(GH).to receive(:[]).with("repos/#{repository.slug}").and_return('permissions' => { 'admin' => true })
14-
end
15-
1612
it 'returns that user' do
1713
expect(result).to eq(user)
1814
end
1915
end
2016

2117
describe 'given a user does not have access to a repository' do
2218
before :each do
23-
allow(GH).to receive(:[]).with("repos/#{repository.slug}").and_return('permissions' => { 'admin' => false })
2419
allow(user).to receive(:update_attributes!)
2520
end
2621

@@ -74,15 +69,14 @@ def ignore_exception(&block)
7469
before :each do
7570
Travis::Notification.publishers.replace([publisher])
7671
allow(User).to receive(:with_permissions).with(repository_id: repository.id, admin: true).and_return [user]
77-
allow(GH).to receive(:[]).with("repos/#{repository.slug}").and_return('permissions' => { 'admin' => true })
7872
service.run
7973
end
8074

8175
it 'publishes a event' do
8276
expect(event).to publish_instrumentation_event(
8377
event: 'travis.services.find_admin.run:completed',
8478
message: 'Travis::Services::FindAdmin#run:completed for svenfuchs/minimal: svenfuchs',
85-
result: user,
79+
result: user
8680
)
8781
end
8882
end

0 commit comments

Comments
 (0)