class GitHelper::GitLabMergeRequest

Attributes

base_branch[RW]
highline[RW]
local_branch[RW]
local_code[RW]
local_project[RW]
new_mr_title[RW]

Public Class Methods

new(options) click to toggle source
# File lib/git_helper/merge_request.rb, line 7
def initialize(options)
  @local_project = 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/merge_request.rb, line 16
def create(options)
  @base_branch = options[:base_branch]
  @new_mr_title = options[:new_title]

  options = {
    source_branch: local_branch,
    target_branch: base_branch,
    squash: squash_merge_request,
    remove_source_branch: remove_source_branch,
    description: new_mr_body
  }

  puts "Creating merge request: #{new_mr_title}"
  mr = gitlab_client.create_merge_request(local_project, new_mr_title, options)

  if mr.diff_refs.base_sha == mr.diff_refs.head_sha
    puts "Merge request was created, but no commits have been pushed to GitLab: #{mr.web_url}"
  else
    puts "Merge request successfully created: #{mr.web_url}"
  end
rescue Gitlab::Error::Conflict
  puts 'Could not create merge request:'
  puts '  A merge request already exists for this branch'
rescue StandardError => e
  puts 'Could not create merge request:'
  puts e.message
end
merge() click to toggle source

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

# File lib/git_helper/merge_request.rb, line 48
def merge
  mr_id
  options = {
    should_remove_source_branch: existing_mr.should_remove_source_branch || existing_mr.force_remove_source_branch,
    squash: existing_mr.squash,
    squash_commit_message: existing_mr.title
  }

  puts "Merging merge request: #{mr_id}"
  merge = gitlab_client.accept_merge_request(local_project, mr_id, options)

  if merge.merge_commit_sha.nil?
    options[:squash] = true
    merge = gitlab_client.accept_merge_request(local_project, mr_id, options)
  end

  if merge.merge_commit_sha.nil?
    puts 'Could not merge merge request:'
    puts "  #{merge.merge_error}"
  else
    puts "Merge request successfully merged: #{merge.merge_commit_sha}"
  end
rescue Gitlab::Error::MethodNotAllowed
  puts 'Could not merge merge request:'
  puts '  The merge request is not mergeable'
rescue Gitlab::Error::NotFound
  puts 'Could not merge merge request:'
  puts "  Could not a locate a merge request to merge with ID #{mr_id}"
rescue StandardError => e
  puts 'Could not merge merge request:'
  puts e.message
end

Private Instance Methods

determine_template() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/git_helper/merge_request.rb, line 98
        def determine_template
  if mr_template_options.count == 1
    apply_single_template = highline.ask_yes_no(
      "Apply the merge request template from #{mr_template_options.first}? (y/n)"
    )
    @template_name_to_apply = mr_template_options.first if apply_single_template
  else
    response = highline.ask_options(
      'Which merge request template should be applied?', mr_template_options << 'None'
    )
    @template_name_to_apply = response unless response == 'None'
  end
end
existing_mr() click to toggle source
# File lib/git_helper/merge_request.rb, line 152
        def existing_mr
  @existing_mr ||= gitlab_client.merge_request(local_project, mr_id)
end
existing_project() click to toggle source
# File lib/git_helper/merge_request.rb, line 148
        def existing_project
  @existing_project ||= gitlab_client.project(local_project)
end
gitlab_client() click to toggle source
# File lib/git_helper/merge_request.rb, line 156
        def gitlab_client
  @gitlab_client ||= GitHelper::GitLabClient.new.client
end
mr_id() click to toggle source
# File lib/git_helper/merge_request.rb, line 123
        def mr_id
  @mr_id ||= highline.ask('Merge Request ID?')
end
mr_template_options() click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/git_helper/merge_request.rb, line 113
        def mr_template_options
  @mr_template_options ||= local_code.template_options(
    {
      template_directory: '.gitlab',
      nested_directory_name: 'merge_request_templates',
      non_nested_file_name: 'merge_request_template'
    }
  )
end
new_mr_body() click to toggle source

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

# File lib/git_helper/merge_request.rb, line 83
        def new_mr_body
  @new_mr_body ||= template_name_to_apply ? local_code.read_template(template_name_to_apply) : ''
end
remove_source_branch() click to toggle source
# File lib/git_helper/merge_request.rb, line 141
        def remove_source_branch
  @remove_source_branch ||=
    existing_project.remove_source_branch_after_merge || highline.ask_yes_no(
      'Remove source branch after merging? (y/n)'
    )
end
squash_merge_request() click to toggle source
# File lib/git_helper/merge_request.rb, line 127
        def squash_merge_request
  return @squash_merge_request if @squash_merge_request

  @squash_merge_request =
    case existing_project.squash_option
    when 'always', 'default_on'
      true
    when 'never'
      false
    else # 'default_off' or anything else
      highline.ask_yes_no('Squash merge request? (y/n)')
    end
end
template_name_to_apply() click to toggle source
# File lib/git_helper/merge_request.rb, line 87
        def template_name_to_apply
  return @template_name_to_apply if @template_name_to_apply

  @template_name_to_apply = nil

  determine_template unless mr_template_options.empty?

  @template_name_to_apply
end