Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Specification of Base Branch #80

Merged
merged 2 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Add
- Support for Ruby 3. ([#79])
- Allow specification of base branch, upone which to base the pull-request
([#80])

### Changed
- Moved CI to GitHub Actions ([#78])
Expand All @@ -17,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#77]: https://github.com/envato/unwrappr/pull/77
[#78]: https://github.com/envato/unwrappr/pull/78
[#79]: https://github.com/envato/unwrappr/pull/79
[#80]: https://github.com/envato/unwrappr/pull/80

## [0.4.0] 2020-04-14
### Changed
Expand Down
17 changes: 13 additions & 4 deletions lib/unwrappr/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ module Unwrappr
class CLI < Clamp::Command
self.default_subcommand = 'all'

option(['-b', '--base'],
'BRANCH',
<<~DESCRIPTION,
the branch upon which to base the pull-request. Omit this option
to use the current branch, or repository's default branch
(typically 'origin/main') on clone.
DESCRIPTION
attribute_name: :base_branch)

option ['-v', '--version'], :flag, 'Show version' do
puts "unwrappr v#{Unwrappr::VERSION}"
exit(0)
Expand All @@ -16,7 +25,7 @@ class CLI < Clamp::Command
subcommand 'all', 'run bundle update, push to github, '\
'create a pr and annotate changes' do
def execute
Unwrappr.run_unwapper_in_pwd
Unwrappr.run_unwapper_in_pwd(base_branch: base_branch)
end
end

Expand Down Expand Up @@ -59,18 +68,18 @@ def execute
)
end

Dir.chdir(repo) { Unwrappr.run_unwapper_in_pwd }
Dir.chdir(repo) { Unwrappr.run_unwapper_in_pwd(base_branch: base_branch) }
end
end
end
end

def self.run_unwapper_in_pwd
def self.run_unwapper_in_pwd(base_branch:)
return unless lockfile_present?

puts "Doing the unwrappr thing in #{Dir.pwd}"

GitCommandRunner.create_branch!
GitCommandRunner.create_branch!(base_branch: base_branch)
BundlerCommandRunner.bundle_update!
GitCommandRunner.commit_and_push_changes!
GitHub::Client.make_pull_request!
Expand Down
8 changes: 4 additions & 4 deletions lib/unwrappr/git_command_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module Unwrappr
# Runs Git commands
module GitCommandRunner
class << self
def create_branch!
def create_branch!(base_branch:)
raise 'Not a git working dir' unless git_dir?
raise 'failed to create branch' unless branch_created?
raise "failed to create branch from '#{base_branch}'" unless branch_created?(base_branch: base_branch)
end

def commit_and_push_changes!
Expand Down Expand Up @@ -50,10 +50,10 @@ def git_dir?
git_wrap { !current_branch_name.empty? }
end

def branch_created?
def branch_created?(base_branch:)
johnsyweb marked this conversation as resolved.
Show resolved Hide resolved
timestamp = Time.now.strftime('%Y%m%d-%H%M').freeze
git_wrap do
git.checkout('origin/master')
git.checkout(base_branch) unless base_branch.nil?
git.branch("auto_bundle_update_#{timestamp}").checkout
end
end
Expand Down
33 changes: 25 additions & 8 deletions spec/lib/unwrappr/git_command_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
end

describe '#create_branch!' do
subject(:create_branch!) { described_class.create_branch! }
subject(:create_branch!) { described_class.create_branch!(base_branch: base_branch) }
let(:base_branch) { 'some_branch' }

before { allow(Time).to receive(:now).and_return(Time.parse('2017-11-01 11:23')) }

Expand All @@ -27,16 +28,32 @@

context 'Given the current directory is a git repo' do
before do
expect(fake_git).to receive(:current_branch).and_return('master')
expect(fake_git).to receive(:checkout).with('origin/master')
expect(fake_git).to receive(:current_branch).and_return('main')
allow(fake_git).to receive(:checkout)
end

it 'checks out a new branch based on origin/master, with a timestamp' do
expect(fake_git).to receive(:branch).with('auto_bundle_update_20171101-1123').and_return(fake_git)
context 'with a named base branch' do
let(:base_branch) { 'origin/main' }

expect(fake_git).to receive(:checkout).with(no_args)
it 'checks out a new branch based on the named branch, with a timestamp' do
expect(fake_git).to receive(:branch).with('auto_bundle_update_20171101-1123').and_return(fake_git)

expect(create_branch!).to be_nil
expect(create_branch!).to be_nil

expect(fake_git).to have_received(:checkout).with('origin/main').once
end
end

context 'without a named base branch' do
let(:base_branch) { nil }

it 'checks out a new branch based on the current branch, with a timestamp' do
expect(fake_git).to receive(:branch).with('auto_bundle_update_20171101-1123').and_return(fake_git)

expect(create_branch!).to be_nil

expect(fake_git).not_to have_received(:checkout).with(nil)
end
end

context 'When there is some failure in creating the branch' do
Expand All @@ -47,7 +64,7 @@
end

specify do
expect { create_branch! }.to raise_error(RuntimeError, 'failed to create branch')
expect { create_branch! }.to raise_error(RuntimeError, "failed to create branch from 'some_branch'")
end
end
end
Expand Down