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

Commit

Permalink
Use the heroku gem's --remote option. Close issue #21.
Browse files Browse the repository at this point in the history
  • Loading branch information
tapajos committed Sep 1, 2011
1 parent 6e39269 commit 9f2addd
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -100,7 +100,7 @@ Yes. To indicate that a particular app is using Cedar, run with the -c flag:
a465afd..bc8932b master -> master
==> - true
==> Pushed master -> heroku-staging
==> + bundle exec heroku rake db:migrate --app staging-sushi
==> + bundle exec heroku rake db:migrate --remote staging-sushi
... Postgres output removed ...
==> - false
==> Migrated staging-sushi
Expand Down
4 changes: 2 additions & 2 deletions features/kumade_executable.feature
Expand Up @@ -27,7 +27,7 @@ Feature: Kumade executable
run git branch deploy
run git push -f pretend-staging deploy:master
==> Pushed deploy:master -> pretend-staging
==> Migrated pretend-staging-app
==> Migrated pretend-staging
run git checkout master && git branch -D deploy
==> Deployed to: pretend-staging
"""
Expand Down Expand Up @@ -58,7 +58,7 @@ Feature: Kumade executable
run git branch deploy
run git push -f pretend-staging deploy:master
==> Pushed deploy:master -> pretend-staging
==> Migrated pretend-staging-app
==> Migrated pretend-staging
run git checkout new_branch && git branch -D deploy
==> Deployed to: pretend-staging
"""
Expand Down
12 changes: 5 additions & 7 deletions lib/kumade/deployer.rb
Expand Up @@ -36,23 +36,21 @@ def sync_heroku
end

def heroku_migrate
app = Kumade::Git.app_for(environment)

heroku("rake db:migrate", app) unless pretending
success("Migrated #{app}")
heroku("rake db:migrate") unless pretending
success("Migrated #{environment}")
end

def post_deploy
git.delete(DEPLOY_BRANCH, @branch)
end

def heroku(command, app)
def heroku(command)
heroku_command = if @cedar
"bundle exec heroku run"
else
"bundle exec heroku"
end
run_or_error("#{heroku_command} #{command} --app #{app}",
run_or_error("#{heroku_command} #{command} --remote #{environment}",
"Failed to run #{command} on Heroku")
end

Expand Down Expand Up @@ -146,7 +144,7 @@ def custom_task?

def ensure_heroku_remote_exists
if git.remote_exists?(environment)
if app_name = Kumade::Git.app_for(environment)
if git.heroku_remote?
success("#{environment} is a Heroku remote")
else
error(%{Cannot deploy: "#{environment}" remote does not point to Heroku})
Expand Down
10 changes: 3 additions & 7 deletions lib/kumade/git.rb
@@ -1,18 +1,14 @@
module Kumade
class Git < Base
attr_reader :environment
def initialize(pretending, environment)
super()
@pretending = pretending
@environment = environment
end

def self.app_for(environment)
heroku_git_url = `git config --get remote.#{environment}.url`.strip
if heroku_git_url =~ /^git@heroku\.com:(.+)\.git$/
$1
else
nil
end
def heroku_remote?
`git config --get remote.#{environment}.url`.strip =~ /^git@heroku\.com:(.+)\.git$/
end

def self.environments
Expand Down
23 changes: 9 additions & 14 deletions spec/kumade/deployer_spec.rb
Expand Up @@ -30,11 +30,10 @@

describe Kumade::Deployer, "#deploy" do
let(:remote_name){ 'staging' }
let(:app_name){ 'kumade-staging' }

before do
subject.stub(:say)
force_add_heroku_remote(remote_name, app_name)
force_add_heroku_remote(remote_name)
end

it "calls the correct methods in order" do
Expand Down Expand Up @@ -361,18 +360,17 @@ class More

describe Kumade::Deployer, "#heroku_migrate" do
let(:environment){ 'staging' }
let(:app_name){ 'sushi' }

before do
subject.stub(:say)
force_add_heroku_remote(environment, app_name)
force_add_heroku_remote(environment)
end

it "runs db:migrate with the correct app" do
subject.stub(:run => true)
subject.should_receive(:heroku).
with("rake db:migrate", app_name)
subject.should_receive(:success).with("Migrated #{app_name}")
with("rake db:migrate")
subject.should_receive(:success).with("Migrated staging")

subject.heroku_migrate
end
Expand All @@ -381,11 +379,10 @@ class More
describe Kumade::Deployer, "#ensure_heroku_remote_exists" do
let(:environment){ 'staging' }
let(:bad_environment){ 'bad' }
let(:staging_app_name) { 'staging-sushi' }

before do
subject.stub(:say)
force_add_heroku_remote(environment, staging_app_name)
force_add_heroku_remote(environment)
`git remote add #{bad_environment} blerg@example.com`
end

Expand Down Expand Up @@ -429,23 +426,21 @@ class More
end

describe Kumade::Deployer, "#heroku" do
let(:app_name){ 'sushi' }

context "when on Cedar" do
subject { Kumade::Deployer.new('staging', false, cedar = true) }

it "runs commands with `run`" do
subject.should_receive(:run_or_error).with("bundle exec heroku run rake --app #{app_name}", //)
subject.heroku("rake", app_name)
subject.should_receive(:run_or_error).with("bundle exec heroku run rake --remote staging", //)
subject.heroku("rake")
end
end

context "when not on Cedar" do
subject { Kumade::Deployer.new('staging', false, cedar = false) }

it "runs commands without `run`" do
subject.should_receive(:run_or_error).with("bundle exec heroku rake --app #{app_name}", //)
subject.heroku("rake", app_name)
subject.should_receive(:run_or_error).with("bundle exec heroku rake --remote staging", //)
subject.heroku("rake")
end
end
end
Expand Down
22 changes: 8 additions & 14 deletions spec/kumade/git_spec.rb
@@ -1,41 +1,35 @@
require 'spec_helper'

describe Kumade::Git, ".app_for" do
describe Kumade::Git, "#heroku_remote?" do
let(:environment){ 'staging' }
let(:app_name){ 'staging_test' }
let(:not_a_heroku_env){ 'fake_heroku' }
let(:not_a_heroku_url){ 'git@github.com:gabebw/kumade.git' }

before do
force_add_heroku_remote(environment, app_name)
force_add_heroku_remote(environment)
`git remote add #{not_a_heroku_env} #{not_a_heroku_url}`
end
after do
remove_remote(environment)
remove_remote(not_a_heroku_env)
end

it "autodetects the Heroku app name" do
Kumade::Git.app_for(environment).should == app_name
end

it "is nil if the app cannot be found" do
Kumade::Git.app_for('xyz').should be_nil

it "should return true when environment remote is a heroku repository" do
Kumade::Git.new(false, environment).heroku_remote?.should be_true
end

it "is nil if the remote is not a Heroku remote" do
Kumade::Git.app_for(not_a_heroku_env).should be_nil
it "should return false when environment remote isn't a heroku repository" do
Kumade::Git.new(false, 'kumade').heroku_remote?.should be_false
end
end

describe Kumade::Git, ".environments" do
let(:environment){ 'staging' }
let(:app_name){ 'staging_test' }
let(:not_a_heroku_env){ 'fake_heroku' }
let(:not_a_heroku_url){ 'git@github.com:gabebw/kumade.git' }

before do
force_add_heroku_remote(environment, app_name)
force_add_heroku_remote(environment)
`git remote add #{not_a_heroku_env} #{not_a_heroku_url}`
end
after do
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -5,9 +5,9 @@
require 'aruba/api'

module GitRemoteHelpers
def force_add_heroku_remote(remote_name, app_name)
def force_add_heroku_remote(remote_name)
remove_remote(remote_name)
`git remote add #{remote_name} git@heroku.com:#{app_name}.git`
`git remote add #{remote_name} git@heroku.com:#{remote_name}.git`
end

def remove_remote(remote_name)
Expand Down

0 comments on commit 9f2addd

Please sign in to comment.