git_helper 3.3.0 → 3.6.6

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 (46) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -0
  3. data/Gemfile.lock +58 -64
  4. data/Guardfile +5 -3
  5. data/LICENSE.md +29 -0
  6. data/README.md +42 -34
  7. data/Rakefile +2 -0
  8. data/bin/git-helper +18 -13
  9. data/lib/git_helper/change_remote.rb +15 -6
  10. data/lib/git_helper/checkout_default.rb +2 -0
  11. data/lib/git_helper/clean_branches.rb +2 -0
  12. data/lib/git_helper/code_request.rb +33 -19
  13. data/lib/git_helper/empty_commit.rb +2 -0
  14. data/lib/git_helper/forget_local_commits.rb +2 -0
  15. data/lib/git_helper/git_config_reader.rb +11 -5
  16. data/lib/git_helper/github_client.rb +70 -0
  17. data/lib/git_helper/gitlab_client.rb +48 -2
  18. data/lib/git_helper/local_code.rb +24 -14
  19. data/lib/git_helper/merge_request.rb +98 -70
  20. data/lib/git_helper/new_branch.rb +3 -1
  21. data/lib/git_helper/pull_request.rb +91 -73
  22. data/lib/git_helper/setup.rb +62 -39
  23. data/lib/git_helper/version.rb +3 -1
  24. data/lib/git_helper.rb +7 -4
  25. data/renovate.json +6 -0
  26. metadata +34 -80
  27. data/LICENSE +0 -22
  28. data/lib/git_helper/highline_cli.rb +0 -33
  29. data/lib/git_helper/octokit_client.rb +0 -11
  30. data/plugins.zip +0 -0
  31. data/spec/git_helper/change_remote_spec.rb +0 -176
  32. data/spec/git_helper/checkout_default_spec.rb +0 -19
  33. data/spec/git_helper/clean_branches_spec.rb +0 -19
  34. data/spec/git_helper/code_request_spec.rb +0 -263
  35. data/spec/git_helper/empty_commit_spec.rb +0 -19
  36. data/spec/git_helper/forget_local_commits_spec.rb +0 -19
  37. data/spec/git_helper/git_config_reader_spec.rb +0 -62
  38. data/spec/git_helper/gitlab_client_spec.rb +0 -26
  39. data/spec/git_helper/highline_cli_spec.rb +0 -51
  40. data/spec/git_helper/local_code_spec.rb +0 -268
  41. data/spec/git_helper/merge_request_spec.rb +0 -251
  42. data/spec/git_helper/new_branch_spec.rb +0 -45
  43. data/spec/git_helper/octokit_client_spec.rb +0 -26
  44. data/spec/git_helper/pull_request_spec.rb +0 -254
  45. data/spec/git_helper/setup_spec.rb +0 -170
  46. data/spec/spec_helper.rb +0 -47
@@ -1,10 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GitHelper
2
4
  class CodeRequest
3
5
  def create
4
- process_project.create({
5
- base_branch: base_branch,
6
- new_title: new_code_request_title
7
- })
6
+ process_project.create(
7
+ {
8
+ base_branch: base_branch,
9
+ new_title: new_code_request_title
10
+ }
11
+ )
8
12
  end
9
13
 
10
14
  def merge
@@ -23,8 +27,14 @@ module GitHelper
23
27
  end
24
28
  end
25
29
 
30
+ # rubocop:disable Metrics/MethodLength
26
31
  private def ask_for_clarification
27
- resp = cli.ask('Found git remotes for both GitHub and GitLab. Would you like to proceed with GitLab or GitHub? (github/gitlab)').downcase
32
+ resp = highline.ask(
33
+ 'Found git remotes for both GitHub and GitLab. Would you like to proceed ' \
34
+ 'with GitLab or GitHub? (github/gitlab)',
35
+ { required: true }
36
+ ).downcase
37
+
28
38
  if resp.include?('hub')
29
39
  github_pull_request
30
40
  elsif resp.include?('lab')
@@ -34,6 +44,7 @@ module GitHelper
34
44
  exit
35
45
  end
36
46
  end
47
+ # rubocop:enable Metrics/MethodLength
37
48
 
38
49
  private def github_pull_request
39
50
  @github_pull_request ||= GitHelper::GitHubPullRequest.new(options)
@@ -48,7 +59,7 @@ module GitHelper
48
59
  local_project: local_project,
49
60
  local_branch: local_branch,
50
61
  local_code: local_code,
51
- cli: cli
62
+ highline: highline
52
63
  }
53
64
  end
54
65
 
@@ -61,11 +72,12 @@ module GitHelper
61
72
  end
62
73
 
63
74
  private def base_branch
64
- @base_branch ||= if cli.ask_yes_no("Is '#{default_branch}' the correct base branch for your new code request? (y/n)")
65
- default_branch
66
- else
67
- cli.ask('Base branch?')
68
- end
75
+ @base_branch ||=
76
+ if highline.ask_yes_no("Is '#{default_branch}' the correct base branch for your new code request? (Y/n)")
77
+ default_branch
78
+ else
79
+ highline.ask('Base branch?', { required: true })
80
+ end
69
81
  end
70
82
 
71
83
  private def local_branch
@@ -77,16 +89,18 @@ module GitHelper
77
89
  end
78
90
 
79
91
  private def new_code_request_title
80
- @new_code_request_title ||= if autogenerated_title &&
81
- cli.ask_yes_no("Accept the autogenerated code request title '#{autogenerated_title}'? (y/n)")
82
- autogenerated_title
83
- else
84
- cli.ask('Title?')
85
- end
92
+ @new_code_request_title ||=
93
+ if autogenerated_title && highline.ask_yes_no(
94
+ "Accept the autogenerated code request title '#{autogenerated_title}'? (Y/n)"
95
+ )
96
+ autogenerated_title
97
+ else
98
+ highline.ask('Title?', { required: true })
99
+ end
86
100
  end
87
101
 
88
- private def cli
89
- @cli ||= GitHelper::HighlineCli.new
102
+ private def highline
103
+ @highline ||= HighlineWrapper.new
90
104
  end
91
105
 
92
106
  private def local_code
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GitHelper
2
4
  class EmptyCommit
3
5
  def execute
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GitHelper
2
4
  class ForgetLocalCommits
3
5
  def execute
@@ -1,23 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GitHelper
2
4
  class GitConfigReader
3
5
  def gitlab_token
4
6
  config_file[:gitlab_token]
5
7
  end
6
8
 
9
+ def gitlab_user
10
+ config_file[:gitlab_user]
11
+ end
12
+
7
13
  def github_token
8
14
  config_file[:github_token]
9
15
  end
10
16
 
17
+ def github_user
18
+ config_file[:github_user]
19
+ end
20
+
11
21
  def git_config_file_path
12
- Dir.pwd.scan(/\A\/[\w]*\/[\w]*\//).first << git_config_file
22
+ "#{Dir.home}/.git_helper/config.yml"
13
23
  end
14
24
 
15
25
  private def config_file
16
26
  YAML.load_file(git_config_file_path)
17
27
  end
18
-
19
- private def git_config_file
20
- '.git_helper/config.yml'
21
- end
22
28
  end
23
29
  end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitHelper
4
+ class GitHubClient
5
+ def repository(repo_name)
6
+ run(repo_name.split('/').first, nil, "/repos/#{repo_name}")
7
+ end
8
+
9
+ def pull_request(repo_name, pull_request_id)
10
+ run(repo_name.split('/').first, nil, "/repos/#{repo_name}/pulls/#{pull_request_id}")
11
+ end
12
+
13
+ def create_pull_request(repo_name, options)
14
+ opts_as_string = format_options(options)
15
+ run(
16
+ repo_name.split('/').first,
17
+ 'POST',
18
+ "/repos/#{repo_name}/pulls",
19
+ opts_as_string
20
+ )
21
+ end
22
+
23
+ def merge_pull_request(repo_name, pull_request_id, options)
24
+ opts_as_string = format_options(options)
25
+ run(
26
+ repo_name.split('/').first,
27
+ 'PUT',
28
+ "/repos/#{repo_name}/pulls/#{pull_request_id}/merge",
29
+ opts_as_string
30
+ )
31
+ end
32
+
33
+ private def format_options(options)
34
+ opts_as_string = ''.dup
35
+ options.each do |key, value|
36
+ next if value == ''
37
+
38
+ opts_as_string << "#{key.to_json}:#{value.to_json.gsub("'", "'\\\\''")},"
39
+ end
40
+ opts_as_string = opts_as_string.reverse.sub(',', '').reverse
41
+ opts_as_string.empty? ? '' : "{#{opts_as_string}}"
42
+ end
43
+
44
+ # rubocop:disable Layout/LineLength
45
+ private def run(username, request_type, curl_url, options = '')
46
+ OpenStruct.new(
47
+ JSON.parse(
48
+ if request_type
49
+ `curl -s -u #{username}:#{github_token} -H "Accept: application/vnd.github.v3+json" -X #{request_type} -d '#{options}' "#{github_endpoint}#{curl_url}"`
50
+ else
51
+ `curl -s -u #{username}:#{github_token} -H "Accept: application/vnd.github.v3+json" "#{github_endpoint}#{curl_url}"`
52
+ end
53
+ )
54
+ )
55
+ end
56
+ # rubocop:enable Layout/LineLength
57
+
58
+ private def github_token
59
+ git_config_reader.github_token
60
+ end
61
+
62
+ private def git_config_reader
63
+ @git_config_reader ||= GitHelper::GitConfigReader.new
64
+ end
65
+
66
+ private def github_endpoint
67
+ 'https://api.github.com'
68
+ end
69
+ end
70
+ end
@@ -1,7 +1,53 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GitHelper
2
4
  class GitLabClient
3
- def client
4
- Gitlab.client(endpoint: gitlab_endpoint, private_token: git_config_reader.gitlab_token)
5
+ def project(project_name)
6
+ run('GET', "/projects/#{url_encode(project_name)}")
7
+ end
8
+
9
+ def merge_request(project_name, merge_request_id)
10
+ run('GET', "/projects/#{url_encode(project_name)}/merge_requests/#{merge_request_id}")
11
+ end
12
+
13
+ def create_merge_request(project_name, options)
14
+ opts_as_string = format_options(options)
15
+ run('POST', "/projects/#{url_encode(project_name)}/merge_requests#{opts_as_string}")
16
+ end
17
+
18
+ def accept_merge_request(project_name, merge_request_id, options)
19
+ opts_as_string = format_options(options)
20
+ run(
21
+ 'PUT',
22
+ "/projects/#{url_encode(project_name)}/merge_requests/#{merge_request_id}/merge#{opts_as_string}"
23
+ )
24
+ end
25
+
26
+ private def format_options(options)
27
+ opts_as_string = ''.dup
28
+ options.each do |key, value|
29
+ next if value == ''
30
+
31
+ opts_as_string << "#{key}=#{url_encode(value.to_s)}&"
32
+ end
33
+ opts_as_string = opts_as_string.reverse.sub('&', '').reverse
34
+ opts_as_string.empty? ? '' : "?#{opts_as_string}"
35
+ end
36
+
37
+ private def run(request_type, curl_url)
38
+ OpenStruct.new(
39
+ JSON.parse(
40
+ `curl -s -X #{request_type} -H "PRIVATE-TOKEN: #{gitlab_token}" "#{gitlab_endpoint}#{curl_url}"`
41
+ )
42
+ )
43
+ end
44
+
45
+ private def url_encode(string)
46
+ string.b.gsub(/[^a-zA-Z0-9_\-.~]/n) { |m| format('%%%<val>02X', val: m.unpack1('C')) }
47
+ end
48
+
49
+ private def gitlab_token
50
+ git_config_reader.gitlab_token
5
51
  end
6
52
 
7
53
  private def git_config_reader
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GitHelper
2
4
  class LocalCode
3
5
  def checkout_default
@@ -44,47 +46,49 @@ module GitHelper
44
46
  end
45
47
 
46
48
  def https_remote?(remote)
47
- remote.scan(/(https:\/\/)/).any?
49
+ remote.scan(%r{(https://)}).any?
48
50
  end
49
51
 
50
52
  def remote_project(remote)
51
53
  if https_remote?(remote)
52
- remote.scan(/https:\/\/[\S]+\/([\S]*).git/).first.first
54
+ remote.scan(%r{https://\S+/(\S*).git}).first.first
53
55
  elsif ssh_remote?(remote)
54
- remote.scan(/\/([\S]*).git/).first.first
56
+ remote.scan(%r{/(\S*).git}).first.first
55
57
  end
56
58
  end
57
59
 
58
60
  def remote_source(remote)
59
61
  if https_remote?(remote)
60
- remote.scan(/https:\/\/([a-zA-z.]+)\//).first.first
62
+ remote.scan(%r{https://([a-zA-z.]+)/}).first.first
61
63
  elsif ssh_remote?(remote)
62
64
  remote.scan(/git@([a-zA-z.]+):/).first.first
63
65
  end
64
66
  end
65
67
 
66
68
  def github_repo?
67
- remotes.select { |remote| remote.include?('github') }.any?
69
+ remotes.any? { |remote| remote.include?('github') }
68
70
  end
69
71
 
70
72
  def gitlab_project?
71
- remotes.select { |remote| remote.include?('gitlab') }.any?
73
+ remotes.any? { |remote| remote.include?('gitlab') }
72
74
  end
73
75
 
74
76
  def project_name
75
77
  # Get the repo/project name by looking in the remote URLs for the full name
76
- `git remote -v`.scan(/\S[\s]*[\S]+.com[\S]{1}([\S]*).git/).first.first
78
+ `git remote -v`.scan(/\S\s*\S+.com\S{1}(\S*).git/).first.first
77
79
  end
78
80
 
79
81
  def branch
80
82
  # Get the current branch by looking in the list of branches for the *
81
- `git branch`.scan(/\*\s([\S]*)/).first.first
83
+ `git branch`.scan(/\*\s(\S*)/).first.first
82
84
  end
83
85
 
84
86
  def default_branch
85
87
  `git symbolic-ref refs/remotes/origin/HEAD | sed "s@^refs/remotes/origin/@@" | tr -d "\n"`
86
88
  end
87
89
 
90
+ # rubocop:disable Metrics/AbcSize
91
+ # rubocop:disable Metrics/MethodLength
88
92
  def template_options(identifiers)
89
93
  nested_templates = Dir.glob(
90
94
  File.join("#{identifiers[:template_directory]}/#{identifiers[:nested_directory_name]}", '*.md'),
@@ -100,11 +104,15 @@ module GitHelper
100
104
  )
101
105
  nested_templates.concat(non_nested_templates).concat(root_templates).uniq
102
106
  end
107
+ # rubocop:enable Metrics/MethodLength
108
+ # rubocop:enable Metrics/AbcSize
103
109
 
104
110
  def read_template(file_name)
105
- File.open(file_name).read
111
+ File.read(file_name)
106
112
  end
107
113
 
114
+ # rubocop:disable Metrics/AbcSize
115
+ # rubocop:disable Metrics/MethodLength
108
116
  def generate_title(local_branch)
109
117
  branch_arr = local_branch.split(local_branch.include?('_') ? '_' : '-')
110
118
 
@@ -112,17 +120,19 @@ module GitHelper
112
120
 
113
121
  if branch_arr.length == 1
114
122
  branch_arr.first.capitalize
115
- elsif branch_arr[0].scan(/([\w]+)/).any? && branch_arr[1].scan(/([\d]+)/).any? # branch includes jira_123 at beginning
123
+ elsif branch_arr[0].scan(/(\w+)/).any? && branch_arr[1].scan(/(\d+)/).any? # branch includes jira_123 at beginning
116
124
  issue = "#{branch_arr[0].upcase}-#{branch_arr[1]}"
117
- description = branch_arr[2..-1].join(' ')
125
+ description = branch_arr[2..].join(' ')
118
126
  "#{issue} #{description.capitalize}"
119
- elsif branch_arr[0].scan(/([\w]+-[\d]+)/).any? # branch includes string jira-123 at beginning
127
+ elsif branch_arr[0].scan(/(\w+-\d+)/).any? # branch includes string jira-123 at beginning
120
128
  issue = branch_arr[0].upcase
121
- description = branch_arr[1..-1].join(' ')
129
+ description = branch_arr[1..].join(' ')
122
130
  "#{issue} #{description.capitalize}"
123
131
  else # plain words
124
- branch_arr[0..-1].join(' ').capitalize
132
+ branch_arr[0..].join(' ').capitalize
125
133
  end
126
134
  end
135
+ # rubocop:enable Metrics/MethodLength
136
+ # rubocop:enable Metrics/AbcSize
127
137
  end
128
138
  end
@@ -1,78 +1,82 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GitHelper
2
4
  class GitLabMergeRequest
3
- attr_accessor :local_project, :local_branch, :local_code, :cli, :base_branch, :new_mr_title
5
+ attr_accessor :local_project, :local_branch, :local_code, :highline, :base_branch, :new_mr_title
4
6
 
5
7
  def initialize(options)
6
8
  @local_project = options[:local_project]
7
9
  @local_branch = options[:local_branch]
8
10
  @local_code = options[:local_code]
9
- @cli = options[:cli]
11
+ @highline = options[:highline]
10
12
  end
11
13
 
14
+ # rubocop:disable Metrics/MethodLength
15
+ # rubocop:disable Metrics/AbcSize
12
16
  def create(options)
13
17
  @base_branch = options[:base_branch]
14
18
  @new_mr_title = options[:new_title]
15
19
 
16
- begin
17
- options = {
18
- source_branch: local_branch,
19
- target_branch: base_branch,
20
- squash: squash_merge_request,
21
- remove_source_branch: remove_source_branch,
22
- description: new_mr_body
23
- }
24
-
25
- puts "Creating merge request: #{new_mr_title}"
26
- mr = gitlab_client.create_merge_request(local_project, new_mr_title, options)
27
-
28
- if mr.diff_refs.base_sha == mr.diff_refs.head_sha
29
- puts "Merge request was created, but no commits have been pushed to GitLab: #{mr.web_url}"
30
- else
31
- puts "Merge request successfully created: #{mr.web_url}"
32
- end
33
- rescue Gitlab::Error::Conflict => e
34
- puts 'Could not create merge request:'
35
- puts ' A merge request already exists for this branch'
36
- rescue Exception => e
37
- puts 'Could not create merge request:'
38
- puts e.message
20
+ options = {
21
+ source_branch: local_branch,
22
+ target_branch: base_branch,
23
+ squash: squash_merge_request,
24
+ remove_source_branch: remove_source_branch,
25
+ description: new_mr_body,
26
+ title: new_mr_title
27
+ }
28
+
29
+ puts "Creating merge request: #{new_mr_title}"
30
+ mr = gitlab_client.create_merge_request(local_project, options)
31
+
32
+ if mr.web_url && mr.diff_refs && (mr.diff_refs['base_sha'] == mr.diff_refs['head_sha'])
33
+ puts "Merge request was created, but no commits have been pushed to GitLab: #{mr.web_url}"
34
+ elsif mr.web_url
35
+ puts "Merge request successfully created: #{mr.web_url}"
36
+ else
37
+ raise StandardError, (mr.message.instance_of?(Array) ? mr.message.first : mr.message)
39
38
  end
39
+ rescue StandardError => e
40
+ puts 'Could not create merge request:'
41
+ puts " #{e.message}"
40
42
  end
43
+ # rubocop:enable Metrics/AbcSize
44
+ # rubocop:enable Metrics/MethodLength
41
45
 
46
+ # rubocop:disable Metrics/MethodLength
47
+ # rubocop:disable Metrics/AbcSize
42
48
  def merge
43
- begin
44
- mr_id
45
- options = {
46
- should_remove_source_branch: existing_mr.should_remove_source_branch || existing_mr.force_remove_source_branch,
47
- squash: existing_mr.squash,
48
- squash_commit_message: existing_mr.title
49
- }
50
-
51
- puts "Merging merge request: #{mr_id}"
49
+ mr_id
50
+ options = {
51
+ should_remove_source_branch: existing_mr.should_remove_source_branch || existing_mr.force_remove_source_branch,
52
+ squash: existing_mr.squash,
53
+ squash_commit_message: existing_mr.title
54
+ }
55
+
56
+ puts "Merging merge request: #{mr_id}"
57
+ merge = gitlab_client.accept_merge_request(local_project, mr_id, options)
58
+
59
+ if merge.merge_commit_sha.nil?
60
+ options[:squash] = true
52
61
  merge = gitlab_client.accept_merge_request(local_project, mr_id, options)
62
+ end
53
63
 
54
- if merge.merge_commit_sha.nil?
55
- options[:squash] = true
56
- merge = gitlab_client.accept_merge_request(local_project, mr_id, options)
57
- end
64
+ raise StandardError, merge.message if merge.merge_commit_sha.nil?
58
65
 
59
- if merge.merge_commit_sha.nil?
60
- puts 'Could not merge merge request:'
61
- puts " #{merge.merge_error}"
62
- else
63
- puts "Merge request successfully merged: #{merge.merge_commit_sha}"
64
- end
65
- rescue Gitlab::Error::MethodNotAllowed => e
66
- puts 'Could not merge merge request:'
66
+ puts "Merge request successfully merged: #{merge.merge_commit_sha}"
67
+ rescue StandardError => e
68
+ puts 'Could not merge merge request:'
69
+
70
+ if e.message.include?('404 Not found')
71
+ puts ' Could not a locate a merge request to merge with given ID'
72
+ elsif e.message.include?('405 Method Not Allowed')
67
73
  puts ' The merge request is not mergeable'
68
- rescue Gitlab::Error::NotFound => e
69
- puts 'Could not merge merge request:'
70
- puts " Could not a locate a merge request to merge with ID #{mr_id}"
71
- rescue Exception => e
72
- puts 'Could not merge merge request:'
73
- puts e.message
74
+ else
75
+ puts " #{e.message}"
74
76
  end
75
77
  end
78
+ # rubocop:enable Metrics/AbcSize
79
+ # rubocop:enable Metrics/MethodLength
76
80
 
77
81
  private def new_mr_body
78
82
  @new_mr_body ||= template_name_to_apply ? local_code.read_template(template_name_to_apply) : ''
@@ -80,39 +84,63 @@ module GitHelper
80
84
 
81
85
  private def template_name_to_apply
82
86
  return @template_name_to_apply if @template_name_to_apply
87
+
83
88
  @template_name_to_apply = nil
84
89
 
85
- unless mr_template_options.empty?
86
- if mr_template_options.count == 1
87
- apply_single_template = cli.ask_yes_no("Apply the merge request template from #{mr_template_options.first}? (y/n)")
88
- @template_name_to_apply = mr_template_options.first if apply_single_template
89
- else
90
- response = cli.ask_options("Which merge request template should be applied?", mr_template_options << 'None')
91
- @template_name_to_apply = response unless response == 'None'
92
- end
93
- end
90
+ determine_template unless mr_template_options.empty?
94
91
 
95
92
  @template_name_to_apply
96
93
  end
97
94
 
95
+ # rubocop:disable Metrics/MethodLength
96
+ private def determine_template
97
+ if mr_template_options.count == 1
98
+ apply_single_template = highline.ask_yes_no(
99
+ "Apply the merge request template from #{mr_template_options.first}? (Y/n)"
100
+ )
101
+ @template_name_to_apply = mr_template_options.first if apply_single_template
102
+ else
103
+ response = highline.ask_options(
104
+ 'Which merge request template should be applied?', mr_template_options << 'None'
105
+ )
106
+ @template_name_to_apply = response unless response == 'None'
107
+ end
108
+ end
109
+ # rubocop:enable Metrics/MethodLength
110
+
98
111
  private def mr_template_options
99
- @mr_template_options ||= local_code.template_options({
100
- template_directory: '.gitlab',
101
- nested_directory_name: 'merge_request_templates',
102
- non_nested_file_name: 'merge_request_template'
103
- })
112
+ @mr_template_options ||= local_code.template_options(
113
+ {
114
+ template_directory: '.gitlab',
115
+ nested_directory_name: 'merge_request_templates',
116
+ non_nested_file_name: 'merge_request_template'
117
+ }
118
+ )
104
119
  end
105
120
 
106
121
  private def mr_id
107
- @mr_id ||= cli.ask('Merge Request ID?')
122
+ @mr_id ||= highline.ask('Merge Request ID?', { required: true })
108
123
  end
109
124
 
110
125
  private def squash_merge_request
111
- @squash_merge_request ||= cli.ask_yes_no('Squash merge request? (y/n)')
126
+ return @squash_merge_request if @squash_merge_request
127
+
128
+ @squash_merge_request =
129
+ case existing_project.squash_option
130
+ when 'always', 'default_on'
131
+ true
132
+ when 'never'
133
+ false
134
+ else # 'default_off' or anything else
135
+ highline.ask_yes_no('Squash merge request? (Y/n)')
136
+ end
112
137
  end
113
138
 
114
139
  private def remove_source_branch
115
- @remove_source_branch ||= existing_project.remove_source_branch_after_merge || cli.ask_yes_no('Remove source branch after merging? (y/n)')
140
+ @remove_source_branch ||=
141
+ existing_project.remove_source_branch_after_merge || highline.ask_yes_no(
142
+ 'Remove source branch after merging? (Y/n)'
143
+ )
116
144
  end
117
145
 
118
146
  private def existing_project
@@ -124,7 +152,7 @@ module GitHelper
124
152
  end
125
153
 
126
154
  private def gitlab_client
127
- @gitlab_client ||= GitHelper::GitLabClient.new.client
155
+ @gitlab_client ||= GitHelper::GitLabClient.new
128
156
  end
129
157
  end
130
158
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GitHelper
2
4
  class NewBranch
3
5
  def execute(new_branch_name = nil)
4
- branch_name = new_branch_name || GitHelper::HighlineCli.new.ask('New branch name?')
6
+ branch_name = new_branch_name || HighlineWrapper.new.ask('New branch name?', { required: true })
5
7
  puts "Attempting to create a new branch: #{branch_name}"
6
8
  GitHelper::LocalCode.new.new_branch(branch_name)
7
9
  end