Skip to content

Commit

Permalink
roll back ActiveResource for Unfuddle; can't get new repos associated…
Browse files Browse the repository at this point in the history
… to projects
  • Loading branch information
Mark Cornick committed Jun 1, 2009
1 parent c184eaa commit 9c0586d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 40 deletions.
9 changes: 8 additions & 1 deletion lib/provisional/exceptions.reek
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ NestedIterators:

FeatureEnvy:
exclude:
- create_repository
- xml_payload

UtilityFunction:
exclude:
- xml_payload

LongMethod:
max_statements: 10
37 changes: 23 additions & 14 deletions lib/provisional/unfuddle_common.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
require 'rubygems'
require 'active_resource'

module Unfuddle
class Repository < ActiveResource::Base
end
end
require 'net/http'
require 'builder'

def ensure_required_options(options)
%w(username password domain id).each do |opt|
raise ArgumentError, "#{opt} must be specified" unless options[opt]
end
end

def xml_payload(options)
xml = Builder::XmlMarkup.new
xml.repository do
xml.abbreviation options['name']
xml.title options['name']
xml.system options['scm']
xml.projects do
xml.project(:id => options['id'])
end
end
return xml.target!
end

def create_repository(options)
begin
Unfuddle::Repository.site = "http://#{options['domain']}.unfuddle.com/api/v1"
Unfuddle::Repository.user = options['username']
Unfuddle::Repository.password = options['password']
Unfuddle::Repository.create :abbreviation => options['name'],
:title => options['name'],
:system => options['scm'] == 'unfuddle_svn' ? 'svn' : 'git',
:projects => [{:id => options['id']}]
http = Net::HTTP.new("#{options['domain']}.unfuddle.com", 80)
request = Net::HTTP::Post.new('/api/v1/repositories.xml', 'Content-Type' => 'application/xml')
request.basic_auth(options['username'], options['password'])
request.body = xml_payload(options)
response, data = http.request(request)
unless response.code == "201"
raise RuntimeError, "Repository not created on Unfuddle due to HTTP error: #{response.code}"
end
rescue
raise RuntimeError, "Repository not created on Unfuddle due to exception: #{$!}"
end
Expand Down
12 changes: 0 additions & 12 deletions reeks.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
(in /Users/mcornick/Sites/provisional)
"lib/provisional/rails_application.rb" -- 1 warnings:
Provisional::RailsApplication#initialize doesn't depend on instance state (Utility Function)

"lib/provisional/scm/beanstalk.rb" -- 1 warnings:
Provisional::SCM::Beanstalk#init has approx 6 statements (Long Method)

"lib/provisional/scm/git.rb" -- 1 warnings:
Provisional::SCM::Git#checkin has approx 6 statements (Long Method)

"lib/provisional/scm/github.rb" -- 1 warnings:
Provisional::SCM::Github#checkin has approx 7 statements (Long Method)

"lib/provisional/scm/svn.rb" -- 1 warnings:
Provisional::SCM::Svn#generate_rails has approx 8 statements (Long Method)
55 changes: 42 additions & 13 deletions test/unit/unfuddle_common_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,55 @@ def test_ensure_required_options_should_raise_if_any_option_missing
end
end

def test_xml_payload
options = {'name' => 'name', 'scm' => 'git', 'id' => 1}
expected = '<repository><abbreviation>name</abbreviation><title>name</title><system>git</system>'
expected += '<projects><project id="1"/></projects></repository>'
assert_equal expected, xml_payload(options)
end

def test_checkin
options = {'username' => 'username', 'password' => 'password', 'domain' => 'domain',
'name' => 'name', 'scm' => 'unfuddle', 'id' => 'id'}
Unfuddle::Repository.expects(:create).with(:abbreviation => 'name',
:title => 'name',
:system => 'git',
:projects => [{:id => 'id'}])
options = {'username' => 'username', 'password' => 'password', 'domain' => 'domain'}
request_stub = stub()
request_stub.expects(:basic_auth).with('username', 'password')
request_stub.expects(:body=)
response_stub = stub(:code => '201')
http_stub = stub()
http_stub.expects(:request).with(request_stub).returns(response_stub, nil)

self.expects(:xml_payload).returns("")

Net::HTTP::Post.expects(:new).with('/api/v1/repositories.xml', 'Content-Type' => 'application/xml').returns(request_stub)
Net::HTTP.expects(:new).with("domain.unfuddle.com", 80).returns(http_stub)

create_repository(options)
end

def test_checkin_should_fail_if_unfuddle_api_call_fails
options = {'username' => 'username', 'password' => 'password', 'domain' => 'domain',
'name' => 'name', 'scm' => 'unfuddle', 'id' => 'id'}
Unfuddle::Repository.expects(:create).with(:abbreviation => 'name',
:title => 'name',
:system => 'git',
:projects => [{:id => 'id'}]).raises(RuntimeError)
options = {'username' => 'username', 'password' => 'password', 'domain' => 'domain'}
request_stub = stub()
request_stub.expects(:basic_auth).with('username', 'password')
request_stub.expects(:body=)
response_stub = stub(:code => '500')
http_stub = stub()
http_stub.expects(:request).with(request_stub).returns(response_stub, nil)

self.expects(:xml_payload).returns("")

Net::HTTP::Post.expects(:new).with('/api/v1/repositories.xml', 'Content-Type' => 'application/xml').returns(request_stub)
Net::HTTP.expects(:new).with("domain.unfuddle.com", 80).returns(http_stub)

assert_raise RuntimeError do
create_repository(options)
end
end

def test_checkin_should_fail_if_any_step_raises_any_exception
options = {'domain' => 'domain'}
Net::HTTP.expects(:new).with("domain.unfuddle.com", 80).raises(Net::HTTPNotFound)

assert_raise RuntimeError do
create_repository(options)
end
end
end
end

0 comments on commit 9c0586d

Please sign in to comment.