git_helper 1.1.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +17 -13
  3. data/README.md +28 -32
  4. data/Rakefile +1 -37
  5. data/bin/git-helper +10 -28
  6. data/lib/git_helper.rb +3 -5
  7. data/lib/git_helper/change_remote.rb +53 -40
  8. data/lib/git_helper/checkout_default.rb +1 -1
  9. data/lib/git_helper/clean_branches.rb +1 -4
  10. data/lib/git_helper/code_request.rb +95 -0
  11. data/lib/git_helper/empty_commit.rb +1 -1
  12. data/lib/git_helper/forget_local_commits.rb +7 -0
  13. data/lib/git_helper/gitlab_client.rb +0 -1
  14. data/lib/git_helper/highline_cli.rb +72 -6
  15. data/lib/git_helper/local_code.rb +124 -0
  16. data/lib/git_helper/merge_request.rb +59 -97
  17. data/lib/git_helper/new_branch.rb +2 -11
  18. data/lib/git_helper/octokit_client.rb +0 -1
  19. data/lib/git_helper/pull_request.rb +47 -81
  20. data/lib/git_helper/version.rb +1 -1
  21. data/spec/git_helper/change_remote_spec.rb +173 -0
  22. data/spec/git_helper/checkout_default_spec.rb +19 -0
  23. data/spec/git_helper/clean_branches_spec.rb +19 -0
  24. data/spec/git_helper/code_request_spec.rb +259 -0
  25. data/spec/git_helper/empty_commit_spec.rb +19 -0
  26. data/spec/git_helper/forget_local_commits_spec.rb +19 -0
  27. data/spec/git_helper/git_config_reader_spec.rb +60 -0
  28. data/spec/git_helper/gitlab_client_spec.rb +26 -0
  29. data/spec/git_helper/highline_cli_spec.rb +215 -0
  30. data/spec/git_helper/local_code_spec.rb +231 -0
  31. data/spec/git_helper/merge_request_spec.rb +234 -0
  32. data/spec/git_helper/new_branch_spec.rb +44 -0
  33. data/spec/git_helper/octokit_client_spec.rb +26 -0
  34. data/spec/git_helper/pull_request_spec.rb +246 -0
  35. data/spec/spec_helper.rb +0 -7
  36. metadata +40 -23
@@ -1,18 +1,9 @@
1
- require_relative './highline_cli.rb'
2
-
3
1
  module GitHelper
4
2
  class NewBranch
5
3
  def execute(new_branch_name = nil)
6
- branch_name = new_branch_name || cli.ask('New branch name?')
4
+ branch_name = new_branch_name || GitHelper::HighlineCli.new.new_branch_name
7
5
  puts "Attempting to create a new branch: #{branch_name}"
8
- system("git pull")
9
- system("git branch --no-track #{branch_name}")
10
- system("git checkout #{branch_name}")
11
- system("git push --set-upstream origin #{branch_name}")
12
- end
13
-
14
- private def cli
15
- @cli ||= GitHelper::HighlineCli.new
6
+ GitHelper::LocalCode.new.new_branch(branch_name)
16
7
  end
17
8
  end
18
9
  end
@@ -1,5 +1,4 @@
1
1
  require 'octokit'
2
- require_relative './git_config_reader.rb'
3
2
 
4
3
  module GitHelper
5
4
  class OctokitClient
@@ -1,13 +1,19 @@
1
- require_relative './octokit_client.rb'
2
- require_relative './highline_cli.rb'
3
-
4
1
  module GitHelper
5
2
  class GitHubPullRequest
6
- def create
3
+ attr_accessor :local_repo, :local_branch, :local_code, :cli, :base_branch, :new_pr_title
4
+
5
+ def initialize(options)
6
+ @local_repo = options[:local_project]
7
+ @local_branch = options[:local_branch]
8
+ @local_code = options[:local_code]
9
+ @cli = options[:cli]
10
+ end
11
+
12
+ def create(options)
13
+ @base_branch = options[:base_branch]
14
+ @new_pr_title = options[:new_title]
15
+
7
16
  begin
8
- # Ask these questions right away
9
- base_branch
10
- new_pr_title
11
17
  new_pr_body
12
18
 
13
19
  puts "Creating pull request: #{new_pr_title}"
@@ -30,12 +36,11 @@ module GitHelper
30
36
 
31
37
  def merge
32
38
  begin
33
- # Ask these questions right away
34
39
  pr_id
35
40
  merge_method
36
41
 
37
42
  puts "Merging pull request: #{pr_id}"
38
- merge = octokit_client.merge_pull_request(local_repo, pr_id, existing_pr_title, { merge_method: merge_method })
43
+ merge = octokit_client.merge_pull_request(local_repo, pr_id, existing_pr.title, { merge_method: merge_method })
39
44
  puts "Pull request successfully merged: #{merge.sha}"
40
45
  rescue Octokit::UnprocessableEntity => e
41
46
  puts 'Could not merge pull request:'
@@ -66,100 +71,61 @@ module GitHelper
66
71
  end
67
72
  end
68
73
 
69
- private def local_repo
70
- # Get the repository by looking in the remote URLs for the full repository name
71
- remotes = `git remote -v`
72
- return remotes.scan(/\S[\s]*[\S]+.com[\S]{1}([\S]*).git/).first.first
74
+ private def new_pr_body
75
+ @new_pr_body ||= template_name_to_apply ? local_code.read_template(template_name_to_apply) : ''
73
76
  end
74
77
 
75
- private def local_branch
76
- # Get the current branch by looking in the list of branches for the *
77
- branches = `git branch`
78
- return branches.scan(/\*\s([\S]*)/).first.first
79
- end
78
+ private def template_name_to_apply
79
+ return @template_name_to_apply if @template_name_to_apply
80
+ @template_name_to_apply = nil
80
81
 
81
- private def read_template
82
- if pr_template_options.count == 1
83
- apply_template?(pr_template_options.first) ? File.open(pr_template_options.first).read : ''
84
- else
85
- template_file_name_to_apply = template_to_apply
86
- template_file_name_to_apply == "None" ? '' : File.open(template_file_name_to_apply).read
82
+ unless pr_template_options.empty?
83
+ if pr_template_options.count == 1
84
+ apply_single_template = cli.apply_template?(pr_template_options.first, 'pull')
85
+ @template_name_to_apply = pr_template_options.first if apply_single_template
86
+ else
87
+ response = cli.template_to_apply(pr_template_options, 'pull')
88
+ @template_name_to_apply = response unless response == 'None'
89
+ end
87
90
  end
88
- end
89
-
90
- private def merge_options
91
- [ 'merge', 'squash', 'rebase' ]
92
- end
93
91
 
94
- private def pr_id
95
- @pr_id ||= cli.ask('Pull Request ID?')
96
- end
97
-
98
- private def existing_pr_title
99
- @existing_pr_title ||= octokit_client.pull_request(local_repo, pr_id).title
92
+ @template_name_to_apply
100
93
  end
101
94
 
102
- private def new_pr_title
103
- @new_pr_title ||= accept_autogenerated_title? ? autogenerated_title : cli.ask('Title?')
104
- end
105
-
106
- private def new_pr_body
107
- @new_pr_body ||= pr_template_options.empty? ? '' : read_template
108
- end
109
-
110
- private def base_branch
111
- @base_branch ||= base_branch_default? ? default_branch : cli.ask('Base branch?')
112
- end
113
-
114
- private def autogenerated_title
115
- @autogenerated_title ||= local_branch.split('_')[0..-1].join(' ').capitalize
95
+ private def pr_template_options
96
+ @pr_template_options ||= local_code.template_options({
97
+ nested_directory_name: 'PULL_REQUEST_TEMPLATE',
98
+ non_nested_file_name: 'pull_request_template'
99
+ })
116
100
  end
117
101
 
118
- private def default_branch
119
- @default_branch ||= octokit_client.repository(local_repo).default_branch
102
+ private def pr_id
103
+ @pr_id ||= cli.code_request_id('Pull')
120
104
  end
121
105
 
122
106
  private def merge_method
123
- return @merge_method if @merge_method
124
- index = cli.ask_options("Merge method?", merge_options)
125
- @merge_method = merge_options[index]
107
+ @merge_method ||= merge_options.length == 1 ? merge_options.first : cli.merge_method(merge_options)
126
108
  end
127
109
 
128
- private def template_to_apply
129
- return @template_to_apply if @template_to_apply
130
- complete_options = pr_template_options << 'None'
131
- index = cli.ask_options("Which pull request template should be applied?", complete_options)
132
- @template_to_apply = complete_options[index]
133
- end
134
-
135
- private def pr_template_options
136
- return @pr_template_options if @pr_template_options
137
- nested_templates = Dir.glob(File.join("**/PULL_REQUEST_TEMPLATE", "*.md"), File::FNM_DOTMATCH | File::FNM_CASEFOLD)
138
- non_nested_templates = Dir.glob(File.join("**", "pull_request_template.md"), File::FNM_DOTMATCH | File::FNM_CASEFOLD)
139
- @pr_template_options = nested_templates.concat(non_nested_templates)
140
- end
141
-
142
- private def base_branch_default?
143
- answer = cli.ask("Is '#{default_branch}' the correct base branch for your new pull request? (y/n)")
144
- !!(answer =~ /^y/i)
110
+ private def merge_options
111
+ return @merge_options if @merge_options
112
+ merge_options = []
113
+ merge_options << 'merge' if existing_project.allow_merge_commit
114
+ merge_options << 'squash' if existing_project.allow_squash_merge
115
+ merge_options << 'rebase' if existing_project.allow_rebase_merge
116
+ @merge_options = merge_options
145
117
  end
146
118
 
147
- private def accept_autogenerated_title?
148
- answer = cli.ask("Accept the autogenerated pull request title '#{autogenerated_title}'? (y/n)")
149
- !!(answer =~ /^y/i)
119
+ private def existing_project
120
+ @existing_project ||= octokit_client.repository(local_repo)
150
121
  end
151
122
 
152
- private def apply_template?(template_file_name)
153
- answer = cli.ask("Apply the pull request template from #{template_file_name}? (y/n)")
154
- !!(answer =~ /^y/i)
123
+ private def existing_pr
124
+ @existing_pr ||= octokit_client.pull_request(local_repo, pr_id)
155
125
  end
156
126
 
157
127
  private def octokit_client
158
128
  @octokit_client ||= GitHelper::OctokitClient.new.client
159
129
  end
160
-
161
- private def cli
162
- @cli ||= GitHelper::HighlineCli.new
163
- end
164
130
  end
165
131
  end
@@ -1,3 +1,3 @@
1
1
  module GitHelper
2
- VERSION = '1.1.1'
2
+ VERSION = '2.0.1'
3
3
  end
@@ -0,0 +1,173 @@
1
+ require 'spec_helper'
2
+ require 'git_helper'
3
+
4
+ describe GitHelper::ChangeRemote do
5
+ let(:remote1) { 'git@github.com:github-username-old/project-1.git' }
6
+ let(:local_code) do
7
+ double(:local_code,
8
+ remotes: [remote1],
9
+ remote_name: 'origin',
10
+ ssh_remote?: true,
11
+ https_remote?: false,
12
+ remote_project: 'project-1',
13
+ remote_source: 'github.com',
14
+ change_remote: true
15
+ )
16
+ end
17
+ let(:cli) { double(:highline_cli, process_directory_remotes?: true) }
18
+ let(:old_owner) { 'github-username-old' }
19
+ let(:new_owner) { 'github-username-new' }
20
+ let(:directory_entries) { [ '.', '..', 'project-1', 'project-2', 'project-3' ] }
21
+
22
+ subject { GitHelper::ChangeRemote.new(old_owner, new_owner) }
23
+
24
+ before do
25
+ allow(GitHelper::HighlineCli).to receive(:new).and_return(cli)
26
+ allow(GitHelper::LocalCode).to receive(:new).and_return(local_code)
27
+ end
28
+
29
+ describe '#execute' do
30
+ before do
31
+ allow(Dir).to receive(:pwd).and_return('/Users/firstname/lastname/path/to/project')
32
+ allow(Dir).to receive(:entries).and_return(directory_entries)
33
+ allow(File).to receive(:join).and_return('/Users/firstname/lastname/path/to/project/project-1')
34
+ allow(File).to receive(:directory?).and_return(true)
35
+ allow(subject).to receive(:process_dir)
36
+ end
37
+
38
+ it 'should call to process at least one directory' do
39
+ expect(subject).to receive(:process_dir).at_least(:once)
40
+ subject.execute
41
+ end
42
+
43
+ it 'should definitely look in the file structure' do
44
+ expect(Dir).to receive(:pwd)
45
+ expect(Dir).to receive(:entries)
46
+ expect(File).to receive(:join)
47
+ expect(File).to receive(:directory?)
48
+ subject.execute
49
+ end
50
+ end
51
+
52
+ describe '#process_dir' do
53
+ before do
54
+ allow(Dir).to receive(:chdir).and_return(nil)
55
+ allow(File).to receive(:exist?).and_return(true)
56
+ allow(subject).to receive(:process_git_repository)
57
+ end
58
+
59
+ it 'should definitely look in the file structure' do
60
+ expect(Dir).to receive(:chdir)
61
+ expect(File).to receive(:exist?)
62
+ subject.send(:process_dir, '/Users/firstname/lastname/path/to/project', '/Users/firstname/lastname/path/to/project/project-1')
63
+ end
64
+
65
+ context 'when the user says to process the directory' do
66
+ it 'should call to process the git repository at least once' do
67
+ expect(subject).to receive(:process_git_repository).at_least(:once)
68
+ subject.send(:process_dir, '/Users/firstname/lastname/path/to/project', '/Users/firstname/lastname/path/to/project/project-1')
69
+ end
70
+ end
71
+
72
+ context 'when the user says not to process the directory' do
73
+ let(:cli) { double(:highline_cli, process_directory_remotes?: false) }
74
+
75
+ it 'should not call to process the directory' do
76
+ expect(subject).not_to receive(:process_git_repository)
77
+ subject.send(:process_dir, '/Users/firstname/lastname/path/to/project', '/Users/firstname/lastname/path/to/project/project-1')
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '#process_git_repository' do
83
+ before do
84
+ allow(subject).to receive(:process_remote).and_return(nil)
85
+ end
86
+
87
+ it 'should call local_code' do
88
+ expect(GitHelper::LocalCode).to receive(:new)
89
+ subject.send(:process_git_repository)
90
+ end
91
+
92
+ context 'when the remote includes the old owner' do
93
+ it 'should call to process the remote' do
94
+ expect(subject).to receive(:process_remote)
95
+ subject.send(:process_git_repository)
96
+ end
97
+ end
98
+
99
+ context 'when the remote does not include the old owner' do
100
+ let(:remote1) { 'git@github.com:github-username-new/project-1.git' }
101
+
102
+ it 'should not call to process the remote' do
103
+ expect(subject).not_to receive(:process_remote)
104
+ subject.send(:process_git_repository)
105
+ end
106
+ end
107
+ end
108
+
109
+ describe '#process_remote' do
110
+ it 'should always get the remote name' do
111
+ expect(local_code).to receive(:remote_name)
112
+ subject.send(:process_remote, remote1)
113
+ end
114
+
115
+ it 'should always attempt to change the remote' do
116
+ expect(local_code).to receive(:change_remote)
117
+ subject.send(:process_remote, remote1)
118
+ end
119
+
120
+ it 'should attempt to get the remote repo exactly once' do
121
+ expect(local_code).to receive(:remote_project).exactly(:once)
122
+ subject.send(:process_remote, remote1)
123
+ end
124
+
125
+ it 'should attempt to get the remote source exactly once' do
126
+ expect(local_code).to receive(:remote_source).exactly(:once)
127
+ subject.send(:process_remote, remote1)
128
+ end
129
+
130
+ it 'should ask if the remote is SSH' do
131
+ expect(local_code).to receive(:ssh_remote?)
132
+ subject.send(:process_remote, remote1)
133
+ end
134
+
135
+ context 'https remote' do
136
+ let(:local_code) do
137
+ double(:local_code,
138
+ remotes: [remote1],
139
+ remote_name: 'origin',
140
+ ssh_remote?: false,
141
+ https_remote?: false,
142
+ remote_project: 'project-1',
143
+ remote_source: 'github.com',
144
+ change_remote: true
145
+ )
146
+ end
147
+
148
+ it 'should ask if the remote is SSH' do
149
+ expect(local_code).to receive(:ssh_remote?)
150
+ subject.send(:process_remote, remote1)
151
+ end
152
+
153
+ it 'should ask if the remote is https' do
154
+ expect(local_code).to receive(:https_remote?)
155
+ subject.send(:process_remote, remote1)
156
+ end
157
+ end
158
+ end
159
+
160
+ describe '#local_code' do
161
+ it 'should create a new local code instance' do
162
+ expect(GitHelper::LocalCode).to receive(:new)
163
+ subject.send(:local_code)
164
+ end
165
+ end
166
+
167
+ describe '#cli' do
168
+ it 'should create a new highline CLI instance' do
169
+ expect(GitHelper::HighlineCli).to receive(:new)
170
+ subject.send(:cli)
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'git_helper'
3
+
4
+ describe GitHelper::CheckoutDefault do
5
+ let(:local_code) { double(:local_code, checkout_default: :done) }
6
+
7
+ subject { GitHelper::CheckoutDefault.new }
8
+
9
+ it 'should call GitHelper::LocalCode' do
10
+ expect(GitHelper::LocalCode).to receive(:new).and_return(local_code)
11
+ subject.execute
12
+ end
13
+
14
+ it 'should call the checkout_default method from the local code class' do
15
+ allow(GitHelper::LocalCode).to receive(:new).and_return(local_code)
16
+ expect(local_code).to receive(:checkout_default)
17
+ subject.execute
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require 'git_helper'
3
+
4
+ describe GitHelper::CleanBranches do
5
+ let(:local_code) { double(:local_code, clean_branches: :commit) }
6
+
7
+ subject { GitHelper::CleanBranches.new }
8
+
9
+ it 'should call GitHelper::LocalCode' do
10
+ expect(GitHelper::LocalCode).to receive(:new).and_return(local_code)
11
+ subject.execute
12
+ end
13
+
14
+ it 'should call the clean_branches method from the local code class' do
15
+ allow(GitHelper::LocalCode).to receive(:new).and_return(local_code)
16
+ expect(local_code).to receive(:clean_branches)
17
+ subject.execute
18
+ end
19
+ end
@@ -0,0 +1,259 @@
1
+ require 'spec_helper'
2
+ require 'git_helper'
3
+
4
+ describe GitHelper::CodeRequest do
5
+ let(:highline_cli) { double(:highline_cli) }
6
+ let(:local_code) { double(:local_code, project_name: 'name', branch: 'branch') }
7
+ let(:process_project) { double(:process_project, create: :created, merge: :merged) }
8
+
9
+ subject { GitHelper::CodeRequest.new }
10
+
11
+ before do
12
+ allow(GitHelper::LocalCode).to receive(:new).and_return(local_code)
13
+ allow(GitHelper::HighlineCli).to receive(:new).and_return(highline_cli)
14
+ end
15
+
16
+ describe '#create' do
17
+ before do
18
+ allow(subject).to receive(:base_branch).and_return('base')
19
+ allow(subject).to receive(:new_code_request_title).and_return('Title')
20
+ end
21
+
22
+ it 'should call to process the project' do
23
+ expect(subject).to receive(:process_project).and_return(process_project)
24
+ subject.create
25
+ end
26
+
27
+ it 'should call create' do
28
+ allow(subject).to receive(:process_project).and_return(process_project)
29
+ expect(process_project).to receive(:create)
30
+ subject.create
31
+ end
32
+
33
+ it 'should call base_branch and new_code_request_title' do
34
+ expect(subject).to receive(:base_branch).and_return('base')
35
+ expect(subject).to receive(:new_code_request_title).and_return('Title')
36
+ allow(subject).to receive(:process_project).and_return(process_project)
37
+ allow(process_project).to receive(:create)
38
+ subject.create
39
+ end
40
+ end
41
+
42
+ describe '#merge' do
43
+ it 'should call to process the project' do
44
+ expect(subject).to receive(:process_project).and_return(process_project)
45
+ subject.merge
46
+ end
47
+
48
+ it 'should call merge' do
49
+ allow(subject).to receive(:process_project).and_return(process_project)
50
+ expect(process_project).to receive(:merge)
51
+ subject.merge
52
+ end
53
+ end
54
+
55
+ describe '#process_project' do
56
+ it 'should call the local code to see if it is a github or gitlab project' do
57
+ expect(local_code).to receive(:gitlab_project?).and_return(false)
58
+ expect(local_code).to receive(:github_repo?).and_return(true)
59
+ subject.send(:process_project)
60
+ end
61
+
62
+ context 'when github and gitlab remotes are found' do
63
+ it 'should ask for clarification' do
64
+ allow(local_code).to receive(:gitlab_project?).and_return(true)
65
+ allow(local_code).to receive(:github_repo?).and_return(true)
66
+ expect(subject).to receive(:ask_for_clarification)
67
+ subject.send(:process_project)
68
+ end
69
+ end
70
+
71
+ context 'when github' do
72
+ it 'should call the github_pull_request' do
73
+ allow(local_code).to receive(:gitlab_project?).and_return(false)
74
+ allow(local_code).to receive(:github_repo?).and_return(true)
75
+ expect(subject).to receive(:github_pull_request)
76
+ subject.send(:process_project)
77
+ end
78
+ end
79
+
80
+ context 'when gitlab' do
81
+ it 'should call the gitlab_merge_request' do
82
+ allow(local_code).to receive(:gitlab_project?).and_return(true)
83
+ allow(local_code).to receive(:github_repo?).and_return(false)
84
+ expect(subject).to receive(:gitlab_merge_request)
85
+ subject.send(:process_project)
86
+ end
87
+ end
88
+
89
+ context 'when no github or gitlab remotes are found' do
90
+ it 'should raise error' do
91
+ allow(local_code).to receive(:gitlab_project?).and_return(false)
92
+ allow(local_code).to receive(:github_repo?).and_return(false)
93
+ expect(subject).to receive(:exit)
94
+ subject.send(:process_project)
95
+ end
96
+ end
97
+ end
98
+
99
+ describe '#ask_for_clarification' do
100
+ it 'should ask the CLI' do
101
+ expect(highline_cli).to receive(:conflicting_remote_clarification).and_return('github')
102
+ subject.send(:ask_for_clarification)
103
+ end
104
+
105
+ context 'when response is github' do
106
+ it 'should return github_pull_request' do
107
+ allow(highline_cli).to receive(:conflicting_remote_clarification).and_return('github')
108
+ expect(subject).to receive(:github_pull_request)
109
+ subject.send(:ask_for_clarification)
110
+ end
111
+
112
+ it 'should return github_pull_request' do
113
+ allow(highline_cli).to receive(:conflicting_remote_clarification).and_return('Github')
114
+ expect(subject).to receive(:github_pull_request)
115
+ subject.send(:ask_for_clarification)
116
+ end
117
+ end
118
+
119
+ context 'when response is gitlab' do
120
+ it 'should return gitlab_merge_request' do
121
+ allow(highline_cli).to receive(:conflicting_remote_clarification).and_return('gitlab')
122
+ expect(subject).to receive(:gitlab_merge_request)
123
+ subject.send(:ask_for_clarification)
124
+ end
125
+
126
+ it 'should return gitlab_merge_request' do
127
+ allow(highline_cli).to receive(:conflicting_remote_clarification).and_return('Gitlab')
128
+ expect(subject).to receive(:gitlab_merge_request)
129
+ subject.send(:ask_for_clarification)
130
+ end
131
+ end
132
+
133
+ context 'when response is neither' do
134
+ it 'should raise an error' do
135
+ allow(highline_cli).to receive(:conflicting_remote_clarification).and_return('huh?')
136
+ expect(subject).to receive(:exit)
137
+ subject.send(:ask_for_clarification)
138
+ end
139
+ end
140
+ end
141
+
142
+ describe '#github_pull_request' do
143
+ it 'should call the GitHelper::GitHubPullRequest' do
144
+ expect(GitHelper::GitHubPullRequest).to receive(:new)
145
+ subject.send(:github_pull_request)
146
+ end
147
+ end
148
+
149
+ describe '#gitlab_merge_request' do
150
+ it 'should call the GitHelper::GitLabMergeRequest' do
151
+ expect(GitHelper::GitLabMergeRequest).to receive(:new)
152
+ subject.send(:gitlab_merge_request)
153
+ end
154
+ end
155
+
156
+ describe '#local_project' do
157
+ it 'should call the name of the local_code' do
158
+ expect(local_code).to receive(:project_name)
159
+ subject.send(:local_project)
160
+ end
161
+ end
162
+
163
+ describe '#default_branch' do
164
+ it 'should call the name of the local_code' do
165
+ expect(local_code).to receive(:default_branch)
166
+ subject.send(:default_branch)
167
+ end
168
+ end
169
+
170
+ describe '#base_branch' do
171
+ it 'should call the default branch' do
172
+ expect(subject).to receive(:default_branch)
173
+ allow(highline_cli).to receive(:base_branch_default?).at_least(:once)
174
+ allow(highline_cli).to receive(:base_branch).at_least(:once).and_return('base')
175
+ subject.send(:base_branch)
176
+ end
177
+
178
+ it 'should ask the CLI to ask the user' do
179
+ allow(subject).to receive(:default_branch)
180
+ expect(highline_cli).to receive(:base_branch_default?).at_least(:once)
181
+ allow(highline_cli).to receive(:base_branch).at_least(:once).and_return('base')
182
+ subject.send(:base_branch)
183
+ end
184
+
185
+ context 'if the user says no' do
186
+ it "definitely asks for the user's base branch" do
187
+ allow(subject).to receive(:default_branch)
188
+ expect(highline_cli).to receive(:base_branch_default?).at_least(:once).and_return(false)
189
+ expect(highline_cli).to receive(:base_branch).at_least(:once).and_return('base')
190
+ subject.send(:base_branch)
191
+ end
192
+ end
193
+
194
+ context 'if the user says yes' do
195
+ it "does not ask for the user's base branch" do
196
+ allow(subject).to receive(:default_branch)
197
+ expect(highline_cli).to receive(:base_branch_default?).at_least(:once).and_return(true)
198
+ expect(highline_cli).not_to receive(:base_branch)
199
+ subject.send(:base_branch)
200
+ end
201
+ end
202
+ end
203
+
204
+ describe '#autogenerated_title' do
205
+ it 'should generate a title based on the branch' do
206
+ expect(subject).to receive(:local_branch).and_return('branch')
207
+ expect(local_code).to receive(:generate_title)
208
+ subject.send(:autogenerated_title)
209
+ end
210
+ end
211
+
212
+ describe '#new_code_request_title' do
213
+ it 'should call autogenerated title method' do
214
+ expect(subject).to receive(:autogenerated_title)
215
+ allow(highline_cli).to receive(:accept_autogenerated_title?).at_least(:once)
216
+ allow(highline_cli).to receive(:title).at_least(:once).and_return('Title')
217
+ subject.send(:new_code_request_title)
218
+ end
219
+
220
+ it 'should ask the CLI to ask the user' do
221
+ allow(subject).to receive(:autogenerated_title)
222
+ expect(highline_cli).to receive(:accept_autogenerated_title?).at_least(:once)
223
+ allow(highline_cli).to receive(:title).at_least(:once).and_return('Title')
224
+ subject.send(:new_code_request_title)
225
+ end
226
+
227
+ context 'if the user says no' do
228
+ it "definitely asks for the user's title" do
229
+ allow(subject).to receive(:autogenerated_title)
230
+ expect(highline_cli).to receive(:accept_autogenerated_title?).at_least(:once).and_return(false)
231
+ expect(highline_cli).to receive(:title).at_least(:once).and_return('Title')
232
+ subject.send(:new_code_request_title)
233
+ end
234
+ end
235
+
236
+ context 'if the user says yes to original title' do
237
+ it "does not ask for the user's chosen title" do
238
+ allow(subject).to receive(:autogenerated_title)
239
+ expect(highline_cli).to receive(:accept_autogenerated_title?).at_least(:once).and_return(true)
240
+ expect(highline_cli).not_to receive(:title)
241
+ subject.send(:new_code_request_title)
242
+ end
243
+ end
244
+ end
245
+
246
+ describe '#local_code' do
247
+ it 'should call the octokit client' do
248
+ expect(GitHelper::LocalCode).to receive(:new).and_return(local_code)
249
+ subject.send(:local_code)
250
+ end
251
+ end
252
+
253
+ describe '#cli' do
254
+ it 'should call the octokit client' do
255
+ expect(GitHelper::HighlineCli).to receive(:new).and_return(highline_cli)
256
+ subject.send(:cli)
257
+ end
258
+ end
259
+ end