require 'highline' module GitHelper class HighlineCli def new_branch_name ask('New branch name?') end def title ask('Title?') end def base_branch ask('Base branch?') end def merge_request_id ask('Merge Request ID?') end def pull_request_id ask('Pull Request ID?') end def accept_autogenerated_title?(autogenerated_title) return false unless autogenerated_title answer = ask("Accept the autogenerated merge request title '#{autogenerated_title}'? (y/n)") !!(answer =~ /^y/i) end def base_branch_default?(default_branch) answer = ask("Is '#{default_branch}' the correct base branch for your new merge request? (y/n)") !!(answer =~ /^y/i) end def squash_merge_request? answer = ask('Squash merge request? (y/n)') !!(answer =~ /^y/i) end def remove_source_branch? answer = ask('Remove source branch after merging? (y/n)') !!(answer =~ /^y/i) end def merge_method merge_options = [ 'merge', 'squash', 'rebase' ] index = ask_options("Merge method?", merge_options) merge_options[index] end def apply_template?(template_file_name) answer = ask("Apply the merge request template from #{template_file_name}? (y/n)") !!(answer =~ /^y/i) end def template_to_apply(template_options, request_type) complete_options = template_options << 'None' index = ask_options("Which #{request_type} request template should be applied?", complete_options) complete_options[index] end ####################### ### GENERAL METHODS ### ####################### private def ask(prompt) highline_client.ask(prompt) do |conf| conf.readline = true end.to_s end private def ask_options(prompt, choices) choices_as_string_options = '' choices.each { |choice| choices_as_string_options << "#{choices.index(choice) + 1}. #{choice}\n" } compiled_prompt = "#{prompt}\n#{choices_as_string_options.strip}" highline_client.ask(compiled_prompt) do |conf| conf.readline = true end.to_i - 1 end private def highline_client @highline_client ||= HighLine.new end end end