class GitHelper::GitHubPullRequest

Attributes

base_branch[RW]
highline[RW]
local_branch[RW]
local_code[RW]
local_repo[RW]
new_pr_title[RW]

Public Class Methods

new(options) click to toggle source
# File lib/git_helper/pull_request.rb, line 7
def initialize(options)
  @local_repo = options[:local_project]
  @local_branch = options[:local_branch]
  @local_code = options[:local_code]
  @highline = options[:highline]
end

Public Instance Methods

create(options) click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/git_helper/pull_request.rb, line 16
def create(options)
  @base_branch = options[:base_branch]
  @new_pr_title = options[:new_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 StandardError => e
  puts 'Could not create pull request:'
  puts e.message
end
merge() click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/git_helper/pull_request.rb, line 49
def merge
  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::NotFound
  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 StandardError => e
  puts 'Could not merge pull request:'
  puts e.message
end

Private Instance Methods

determine_template() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/git_helper/pull_request.rb, line 103
        def determine_template
  if pr_template_options.count == 1
    apply_single_template = highline.ask_yes_no(
      "Apply the pull request template from #{pr_template_options.first}? (y/n)"
    )
    @template_name_to_apply = pr_template_options.first if apply_single_template
  else
    response = highline.ask_options(
      'Which pull request template should be applied?', pr_template_options << 'None'
    )
    @template_name_to_apply = response unless response == 'None'
  end
end
existing_pr() click to toggle source
# File lib/git_helper/pull_request.rb, line 155
        def existing_pr
  @existing_pr ||= octokit_client.pull_request(local_repo, pr_id)
end
existing_project() click to toggle source
# File lib/git_helper/pull_request.rb, line 151
        def existing_project
  @existing_project ||= octokit_client.repository(local_repo)
end
merge_method() click to toggle source
# File lib/git_helper/pull_request.rb, line 132
        def merge_method
  @merge_method ||=
    if merge_options.length == 1
      merge_options.first
    else
      highline.ask_options('Merge method?', merge_options)
    end
end
merge_options() click to toggle source
# File lib/git_helper/pull_request.rb, line 141
        def merge_options
  return @merge_options if @merge_options

  merge_options = []
  merge_options << 'merge' if existing_project.allow_merge_commit
  merge_options << 'squash' if existing_project.allow_squash_merge
  merge_options << 'rebase' if existing_project.allow_rebase_merge
  @merge_options = merge_options
end
new_pr_body() click to toggle source

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength

# File lib/git_helper/pull_request.rb, line 88
        def new_pr_body
  @new_pr_body ||= template_name_to_apply ? local_code.read_template(template_name_to_apply) : ''
end
octokit_client() click to toggle source
# File lib/git_helper/pull_request.rb, line 159
        def octokit_client
  @octokit_client ||= GitHelper::OctokitClient.new.client
end
pr_id() click to toggle source
# File lib/git_helper/pull_request.rb, line 128
        def pr_id
  @pr_id ||= highline.ask('Pull Request ID?')
end
pr_template_options() click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/git_helper/pull_request.rb, line 118
        def pr_template_options
  @pr_template_options ||= local_code.template_options(
    {
      template_directory: '.github',
      nested_directory_name: 'PULL_REQUEST_TEMPLATE',
      non_nested_file_name: 'pull_request_template'
    }
  )
end
template_name_to_apply() click to toggle source
# File lib/git_helper/pull_request.rb, line 92
        def template_name_to_apply
  return @template_name_to_apply if @template_name_to_apply

  @template_name_to_apply = nil

  determine_template unless pr_template_options.empty?

  @template_name_to_apply
end