Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Commit

Permalink
Adding some better error checking in
Browse files Browse the repository at this point in the history
  • Loading branch information
qrush committed Aug 24, 2009
1 parent d1c3acc commit 251de9b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/enforcer.rb
Expand Up @@ -20,6 +20,11 @@ def project(project_name, *collaborators)

existing_collaborators = repo.list

if existing_collaborators.nil?
STDOUT.puts ">> Can't find existing collaborators for this project"
return
end

{ :add => collaborators - existing_collaborators,
:remove => existing_collaborators - collaborators}.each_pair do |action, group|
group.each do |collaborator|
Expand Down
16 changes: 14 additions & 2 deletions lib/repository.rb
Expand Up @@ -2,15 +2,27 @@ class Repository
include HTTParty
base_uri 'http://github.com/api/v2/json/repos'

HTTP_ERRORS = [Timeout::Error,
Errno::EINVAL,
Errno::ECONNRESET,
EOFError,
Net::HTTPBadResponse,
Net::HTTPHeaderSyntaxError,
Net::ProtocolError]

def initialize(account, api_key, project)
@account = account
@project = project
@api_key = api_key
end

def request(method, path)
response = self.class.send(method, path, :body => { :login => @account, :token => @api_key })
response['collaborators']
begin
response = self.class.send(method, path, :body => { :login => @account, :token => @api_key })
response['collaborators']
rescue *HTTP_ERRORS => ex
STDOUT.puts ">> There was a problem contacting GitHub: #{ex}"
end
end

def list
Expand Down
12 changes: 12 additions & 0 deletions test/enforcer_test.rb
Expand Up @@ -16,6 +16,18 @@ class EnforcerTest < Test::Unit::TestCase
@enforcer = Enforcer.new(@account, @api_key)
end

should "totally skip adding/removing collaborators if the existing collaborators can't be found" do
@existing_collaborators = nil

assert_nothing_raised do
@enforcer.project @project, 'chaines'
end

assert_received(@repo) do |subject|
subject.add('chaines').never
end
end

should "add a collaborator to the project" do
@enforcer.project @project, 'chaines'

Expand Down
9 changes: 9 additions & 0 deletions test/repository_test.rb
Expand Up @@ -14,6 +14,15 @@ class RepositoryTest < Test::Unit::TestCase
assert_equal "http://github.com/api/v2/json/repos", Repository.base_uri
end

context "github is down" do
should "not fail" do
stub(Repository).get(anything, anything) { raise TimeoutError }
assert_nothing_raised do
@repo.list
end
end
end

context "listing collaborators" do
setup do
@collaborators = ["qrush", "coreyhaines"]
Expand Down
9 changes: 9 additions & 0 deletions test/test_helper.rb
Expand Up @@ -16,6 +16,15 @@ class Test::Unit::TestCase
include RR::Adapters::TestUnit

def setup
RR.reset
stub(STDOUT).puts
end

def teardown
begin
RR.verify
ensure
RR.reset
end
end
end

0 comments on commit 251de9b

Please sign in to comment.