Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding some better error checking in

  • Loading branch information...
commit 251de9bc712056cd311fb41698d0356fcff5603e 1 parent d1c3acc
@qrush qrush authored
View
5 lib/enforcer.rb
@@ -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|
View
16 lib/repository.rb
@@ -2,6 +2,14 @@ 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
@@ -9,8 +17,12 @@ def initialize(account, api_key, project)
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
View
12 test/enforcer_test.rb
@@ -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'
View
9 test/repository_test.rb
@@ -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"]
View
9 test/test_helper.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.