require_relative './octokit_client.rb' require_relative './highline_cli.rb' require_relative './local_code.rb' module GitHelper class GitHubPullRequest def create begin # Ask these questions right away base_branch new_pr_title new_pr_body puts "Creating pull request: #{new_pr_title}" pr = octokit_client.create_pull_request(local_repo, base_branch, local_branch, new_pr_title, new_pr_body) puts "Pull request successfully created: #{pr.html_url}" rescue Octokit::UnprocessableEntity => e puts 'Could not create pull request:' if e.message.include?('pull request already exists') puts ' A pull request already exists for this branch' elsif e.message.include?('No commits between') puts ' No commits have been pushed to GitHub' else puts e.message end rescue Exception => e puts 'Could not create pull request:' puts e.message end end def merge begin # Ask these questions right away pr_id merge_method puts "Merging pull request: #{pr_id}" merge = octokit_client.merge_pull_request(local_repo, pr_id, existing_pr_title, { merge_method: merge_method }) puts "Pull request successfully merged: #{merge.sha}" rescue Octokit::UnprocessableEntity => e puts 'Could not merge pull request:' puts e.message rescue Octokit::NotFound => e puts 'Could not merge pull request:' puts " Could not a locate a pull request to merge with ID #{pr_id}" rescue Octokit::MethodNotAllowed => e puts 'Could not merge pull request:' if e.message.include?('405 - Required status check') puts ' A required status check has not passed' elsif e.message.include?('405 - Base branch was modified') puts ' The base branch has been modified' elsif e.message.include?('405 - Pull Request is not mergeable') puts ' The pull request is not mergeable' elsif e.message.include?('405 - Rebase merges are not allowed on this repository') puts ' Rebase merges are not allowed on this repository' elsif e.message.include?('405 - Merge commits are not allowed on this repository') puts ' Merge commits are not allowed on this repository' elsif e.message.include?('405 - Squash commits are not allowed on this repository') puts ' Squash merges are not allowed on this repository' else puts e.message end rescue Exception => e puts 'Could not merge pull request:' puts e.message end end private def local_repo @local_project ||= local_code.name 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 default_branch @default_branch ||= local_code.default_branch(local_repo, octokit_client, :octokit) end private def pr_template_options @pr_template_options ||= local_code.template_options({ nested_directory_name: "PULL_REQUEST_TEMPLATE", non_nested_file_name: "pull_request_template" }) end private def pr_id @pr_id ||= cli.pull_request_id end private def merge_method @merge_method ||= cli.merge_method end private def new_pr_title @new_pr_title ||= if cli.accept_autogenerated_title?(autogenerated_title) autogenerated_title else cli.title end end private def base_branch @base_branch ||= if cli.base_branch_default?(default_branch) default_branch else cli.base_branch end end private def new_pr_body @new_pr_body ||= template_name_to_apply ? local_code.read_template(template_name_to_apply) : '' end private def template_name_to_apply return @template_name_to_apply if @template_name_to_apply @template_name_to_apply = nil unless pr_template_options.empty? if pr_template_options.count == 1 apply_single_template = cli.apply_template?(pr_template_options.first) @template_name_to_apply = pr_template_options.first if apply_single_template else response = cli.template_to_apply(pr_template_options, 'pull') @template_name_to_apply = response unless response == "None" end end @template_name_to_apply end private def existing_pr_title @existing_pr_title ||= octokit_client.pull_request(local_repo, pr_id).title end private def octokit_client @octokit_client ||= GitHelper::OctokitClient.new.client end private def cli @cli ||= GitHelper::HighlineCli.new end private def local_code @local_code ||= GitHelper::LocalCode.new end end end