Skip to content

Commit

Permalink
Parse git remote name to determine Heroku app
Browse files Browse the repository at this point in the history
Several reports have matched #72, which was affecting the `restore`
command for some users. The cause turned out to be local folder names
not matching the name of the Heroku app. Now that Heroku tracks Git
remotes, we can take advantage of the Git remote to correctly identify
the app name even the local folder name doesn't match.

While this requirement has existed for the entirety of Parity's
existence, it stopped being a problem for most commands once Heroku
introduced remotes. I inadvertently reintroduced this requirement in
ad2c21a when we started automatically
bypassing confirmation arguments for restores to non-production
environments.

Fix #72.
  • Loading branch information
geoffharcourt committed Jan 22, 2016
1 parent 95ac59c commit 1d3ae15
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ source 'https://rubygems.org'

gemspec

gem 'rake'
gem 'rspec'
gem "git"
gem "rake"
gem "rspec"
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ Parity expects:
* A `production` remote pointing to the production Heroku app.
* There is a `config/database.yml` file that can be parsed as YAML for
`['development']['database']`.
* The Heroku apps are named like `app-staging` and `app-production`
where `app` is equal to `basename $PWD`.

Customization
-------------
Expand Down
1 change: 1 addition & 0 deletions lib/parity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "parity/version"
require "parity/environment"
require "parity/usage"
require "git"
require "open3"
require "pathname"
require "uri"
6 changes: 5 additions & 1 deletion lib/parity/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ def raw_redis_url
)[0].strip
end

def git_remote
Git.init.remote(environment).url
end

def heroku_app_name
[basename, environment].join('-')
git_remote.split(":").last.split(".").first
end

def basename
Expand Down
31 changes: 31 additions & 0 deletions spec/parity/environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,26 @@
expect(Kernel).to have_received(:system).with(heroku_backup)
end

it "correctly connects to the right Heroku app when the local folder's name does not match the Heroku app's name" do
backup = double("backup", restore: nil)
stub_git_remote(base_name: "parity-integration", environment: "staging")
allow(Parity::Backup).to receive(:new).and_return(backup)

Parity::Environment.new("staging", ["restore", "production"]).run

expect(Parity::Backup).
to have_received(:new).
with(
from: "production",
to: "staging",
additional_args: "--confirm parity-integration-staging",
)
expect(backup).to have_received(:restore)
end

it "restores backups from production to staging" do
backup = double("backup", restore: nil)
stub_git_remote(environment: "staging")
allow(Parity::Backup).to receive(:new).and_return(backup)

Parity::Environment.new("staging", ["restore", "production"]).run
Expand All @@ -49,6 +67,7 @@

it "restores using restore-from" do
backup = double("backup", restore: nil)
stub_git_remote(environment: "staging")
allow(Parity::Backup).to receive(:new).and_return(backup)

Parity::Environment.new("staging", ["restore-from", "production"]).run
Expand All @@ -65,6 +84,7 @@

it "passes the confirm argument when restoring to a non-prod environment" do
backup = double("backup", restore: nil)
stub_git_remote(environment: "staging")
allow(Parity::Backup).to receive(:new).and_return(backup)

Parity::Environment.new("staging", ["restore", "production"]).run
Expand Down Expand Up @@ -102,6 +122,7 @@

it "does not allow restoring backups into production" do
backup = double("backup", restore: nil)
stub_git_remote
allow(Parity::Backup).to receive(:new).and_return(backup)
allow($stdout).to receive(:puts)

Expand Down Expand Up @@ -323,4 +344,14 @@ def stub_migration_path_check(result)

path_stub
end

def stub_git_remote(base_name: "parity", environment: "staging")
git_remote = double(
"Git::Remote",
url: "[email protected]:#{base_name}-#{environment}.git",
)
git = double("Git")
allow(git).to receive(:remote).with("staging").and_return(git_remote)
allow(Git).to receive(:init).and_return(git)
end
end

0 comments on commit 1d3ae15

Please sign in to comment.