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.



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

def initialize(options)
  @local_project = options[:local_project]
  @local_branch = options[:local_branch]
  @local_code = options[:local_code]
  @cli = options[:cli]
end

Instance Attribute Details

#base_branchObject

Returns the value of attribute base_branch.



3
4
5
# File 'lib/git_helper/merge_request.rb', line 3

def base_branch
  @base_branch
end

#cliObject

Returns the value of attribute cli.



3
4
5
# File 'lib/git_helper/merge_request.rb', line 3

def cli
  @cli
end

#local_branchObject

Returns the value of attribute local_branch.



3
4
5
# File 'lib/git_helper/merge_request.rb', line 3

def local_branch
  @local_branch
end

#local_codeObject

Returns the value of attribute local_code.



3
4
5
# File 'lib/git_helper/merge_request.rb', line 3

def local_code
  @local_code
end

#local_projectObject

Returns the value of attribute local_project.



3
4
5
# File 'lib/git_helper/merge_request.rb', line 3

def local_project
  @local_project
end

#new_mr_titleObject

Returns the value of attribute new_mr_title.



3
4
5
# File 'lib/git_helper/merge_request.rb', line 3

def new_mr_title
  @new_mr_title
end

Instance Method Details

#create(options) ⇒ Object



12
13
14
15
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
# File 'lib/git_helper/merge_request.rb', line 12

def create(options)
  @base_branch = options[:base_branch]
  @new_mr_title = options[:new_title]

  begin
    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 => e
    puts 'Could not create merge request:'
    puts '  A merge request already exists for this branch'
  rescue Exception => e
    puts 'Could not create merge request:'
    puts e.message
  end
end

#mergeObject



42
43
44
45
46
47
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
# File 'lib/git_helper/merge_request.rb', line 42

def merge
  begin
    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 => e
    puts 'Could not merge merge request:'
    puts '  The merge request is not mergeable'
  rescue Gitlab::Error::NotFound => e
    puts 'Could not merge merge request:'
    puts "  Could not a locate a merge request to merge with ID #{mr_id}"
  rescue Exception => e
    puts 'Could not merge merge request:'
    puts e.message
  end
end