# frozen_string_literal: true module GitHelper class CodeRequest def create process_project.create( { base_branch: base_branch, new_title: new_code_request_title } ) end def merge process_project.merge end private def process_project if local_code.github_repo? # GitHub remotes are found # If GitLab remotes are also found, ask for clarification, else proceed with GitHub local_code.gitlab_project? ? ask_for_clarification : github_pull_request elsif local_code.gitlab_project? # Only GitLab remotes are found gitlab_merge_request else # Neither GitHub nor GitLab remotes are found puts 'Could not locate GitHub or GitLab remote URLs.' exit end end # rubocop:disable Metrics/MethodLength private def ask_for_clarification resp = cli.ask( 'Found git remotes for both GitHub and GitLab. Would you like to proceed ' \ 'with GitLab or GitHub? (github/gitlab)' ).downcase if resp.include?('hub') github_pull_request elsif resp.include?('lab') gitlab_merge_request else puts 'The answer we received was not parseable.' exit end end # rubocop:enable Metrics/MethodLength private def github_pull_request @github_pull_request ||= GitHelper::GitHubPullRequest.new(options) end private def gitlab_merge_request @gitlab_merge_request ||= GitHelper::GitLabMergeRequest.new(options) end private def options { local_project: local_project, local_branch: local_branch, local_code: local_code, cli: cli } end private def local_project @local_project ||= local_code.project_name end private def default_branch @default_branch ||= local_code.default_branch end private def base_branch @base_branch ||= if cli.ask_yes_no("Is '#{default_branch}' the correct base branch for your new code request? (y/n)") default_branch else cli.ask('Base branch?') end end private def local_branch @local_branch ||= local_code.branch end private def autogenerated_title @autogenerated_title ||= local_code.generate_title(local_branch) end private def new_code_request_title @new_code_request_title ||= if autogenerated_title && cli.ask_yes_no( "Accept the autogenerated code request title '#{autogenerated_title}'? (y/n)" ) autogenerated_title else cli.ask('Title?') end end private def cli @cli ||= GitHelper::HighlineCli.new end private def local_code @local_code ||= GitHelper::LocalCode.new end end end