diff --git a/lib/heroku/stage.rb b/lib/heroku/stage.rb index e6259fb..5e91d48 100644 --- a/lib/heroku/stage.rb +++ b/lib/heroku/stage.rb @@ -20,7 +20,7 @@ def review_app? private def heroku_pipeline_stage - return ENV.fetch('HEROKU_APP_NAME')[/pr-(\d*)/] if review_app? + return branch || pull_request if review_app? dev_or_test = Rails.env.development? || Rails.env.test? dev_or_test ? '' : ENV.fetch('HEROKU_APP_NAME')[/staging|production/] @@ -28,6 +28,16 @@ def heroku_pipeline_stage raise $!, enable_dyno_metadata_message($!), $!.backtrace end + def branch + app_name = ENV.fetch('HEROKU_APP_NAME') + + return "br-#{app_name.split('-br-')[1]}" if app_name.include? '-br-' + end + + def pull_request + ENV.fetch('HEROKU_APP_NAME')[/pr-(\d*)/] + end + def enable_dyno_metadata_message(exception) <<-USAGE #{exception.message} diff --git a/spec/heroku-stage_spec.rb b/spec/heroku-stage_spec.rb index 4197f91..194633b 100644 --- a/spec/heroku-stage_spec.rb +++ b/spec/heroku-stage_spec.rb @@ -66,19 +66,35 @@ end describe 'on review app' do - before(:each) do - allow(ENV).to receive(:[]).with("HEROKU_PARENT_APP_NAME").and_return("myapp-staging") - allow(ENV).to receive(:fetch).with('HEROKU_APP_NAME').and_return('myapp-staging-pr-766') - end - - it 'get the correct stage' do - expect(Heroku.stage).to eq('pr-766') - end describe '#review_app?' do it 'returns true' do expect(Heroku.review_app?).to be(true) end end + + describe '#heroku_pipeline_stage' do + context 'when app is created from pull request' do + before(:each) do + allow(ENV).to receive(:[]).with("HEROKU_PARENT_APP_NAME").and_return("myapp-staging") + allow(ENV).to receive(:fetch).with('HEROKU_APP_NAME').and_return('myapp-staging-pr-766') + end + + it 'get the correct stage' do + expect(Heroku.stage).to eq('pr-766') + end + end + + context 'when app is created from branch' do + before(:each) do + allow(ENV).to receive(:[]).with("HEROKU_PARENT_APP_NAME").and_return("myapp-staging") + allow(ENV).to receive(:fetch).with('HEROKU_APP_NAME').and_return('myapp-staging-br-new-feature') + end + + it 'get the correct stage' do + expect(Heroku.stage).to eq('br-new-feature') + end + end + end end end