Class: GitHelper::GitLabMergeRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/git_helper/merge_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GitLabMergeRequest

Returns a new instance of GitLabMergeRequest.



7
8
9
10
11
12
# 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

Instance Attribute Details

#base_branchObject

Returns the value of attribute base_branch.



5
6
7
# File 'lib/git_helper/merge_request.rb', line 5

def base_branch
  @base_branch
end

#highlineObject

Returns the value of attribute highline.



5
6
7
# File 'lib/git_helper/merge_request.rb', line 5

def highline
  @highline
end

#local_branchObject

Returns the value of attribute local_branch.



5
6
7
# File 'lib/git_helper/merge_request.rb', line 5

def local_branch
  @local_branch
end

#local_codeObject

Returns the value of attribute local_code.



5
6
7
# File 'lib/git_helper/merge_request.rb', line 5

def local_code
  @local_code
end

#local_projectObject

Returns the value of attribute local_project.



5
6
7
# File 'lib/git_helper/merge_request.rb', line 5

def local_project
  @local_project
end

#new_mr_titleObject

Returns the value of attribute new_mr_title.



5
6
7
# File 'lib/git_helper/merge_request.rb', line 5

def new_mr_title
  @new_mr_title
end

Instance Method Details

#create(options) ⇒ Object

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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,
    title: new_mr_title
  }

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

  if mr.web_url && mr.diff_refs && (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}"
  elsif mr.web_url
    puts "Merge request successfully created: #{mr.web_url}"
  else
    raise StandardError, (mr.message.instance_of?(Array) ? mr.message.first : mr.message)
  end
rescue StandardError => e
  puts 'Could not create merge request:'
  puts "  #{e.message}"
end

#mergeObject

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

  raise StandardError, merge.message if merge.merge_commit_sha.nil?

  puts "Merge request successfully merged: #{merge.merge_commit_sha}"
rescue StandardError => e
  puts 'Could not merge merge request:'

  if e.message.include?('404 Not found')
    puts '  Could not a locate a merge request to merge with given ID'
  elsif e.message.include?('405 Method Not Allowed')
    puts '  The merge request is not mergeable'
  else
    puts "  #{e.message}"
  end
end